Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ability to use smart pointers for source #341

Open
InfiniteCoder01 opened this issue Oct 15, 2023 · 5 comments
Open

Add an ability to use smart pointers for source #341

InfiniteCoder01 opened this issue Oct 15, 2023 · 5 comments

Comments

@InfiniteCoder01
Copy link

This might be useful for #159, #324 and #340.
I don't know anything about proc-macros in rust, but I'll try to implement it.

@InfiniteCoder01
Copy link
Author

I did not realize that this feature is there, we need docs for proc macros. But it's expecting type to be something that we put behind a reference, which is not the case with Rc or Arc. Changing the title

@InfiniteCoder01 InfiniteCoder01 changed the title Add something like #[logos(source="Rc<str>")] #[logos(source="Rc<str>")] doesn expects a reference Oct 15, 2023
@InfiniteCoder01 InfiniteCoder01 changed the title #[logos(source="Rc<str>")] doesn expects a reference #[logos(source="Rc<str>")] doesn't work, puts it behind a reference Oct 15, 2023
@InfiniteCoder01 InfiniteCoder01 changed the title #[logos(source="Rc<str>")] doesn't work, puts it behind a reference Add an ability to use smart pointers for source Oct 15, 2023
@jeertmans
Copy link
Collaborator

Hello @InfiniteCoder01, could you provide an example code showcasing a basic use case?

@InfiniteCoder01
Copy link
Author

Example of this that I can think of is quite complex, but here it is:
When using logos in combination with codespan_reporting and writing a compiler for a language, that is like rust - uses mod somemodule; to mark somemodule.rs or somemodule/mod.rs as files to parse & link. Especially useful with multithreading.
You are parsing a file, lexer is a stream of tokens, you encounter mod somemodule;. (Normally you would put it into AST but in my case AST is more like LLVM IR). You need to search for module file and add it to codespan_reporting Files. If those files where borrowed by lexer, it would be impossible to add a file immediately. When using Arc, we can read this file and put it into this database immediately, perhaps even start a separate thread, parsing it.
This explanation is quite complex, but I hope it made it clearer.

@InfiniteCoder01
Copy link
Author

For now, we can use a workaround:

let source = file.source().clone(); // File is codespan_reporting::files::SimpleFile<String, Rc<str>>
let mut lexer = lexer::Token::lexer(&source);

@axelkar
Copy link

axelkar commented Jun 28, 2024

Can't you just deref Arc to &T? and use &T as the source?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants