I'm trying to parse some Rust source code and found the syn crate for this. This works great and the quick compile time is nice!
However, I don't seem to able to find the offset or line number for the things I parse. Am I missing something, or has this functionality been left out of syn? I believe syn is primarily meant for procedural macros, and there line numbers might not be needed.
If someone has a suggestion for a different parser crate, then I would be very happy to hear about it!
Well, there will be GitHub - matklad/fall, once it is ready, but at the moment it is slow, unfinished, does not have a propper API yet, does not support all rust syntax and is not published to crates.io
But the syntax tree it produces already preserves 100% of information about offsets, whitespace and all that other "irrelevant" information
My Fuzzy Pickles crate is a parser that parses all of the Rust code I've thrown at it* (some 30K files from crates dating back to Rust 1.0), returning an AST of extents — the byte offsets of the beginning and end of each element.
You can then transform that to lines / columns (the error implementation does this). It currently uses impl trait, so it requires nightly, but theoretically it should be able to work with boxed trait objects.
* It does have one known issue - nested block comments aren't supported. I'm also pretty sure that the expression precedence is wrong, but that's "just" tweaking some numbers in a function.
Do you mean that fall would be a replacement for peresil?
I've got a number of crates related to my overall project (Strata Rust)... and it looks like I forgot to even change the README for Fuzzy Pickles when I extracted it from Strata Rust...
Okey, I've definitely got confused, because I've seen Strata Rust (and that's what I was thinking about) and I have not actually seen fuzzy-picklers in it's extracted form, only as an "untitled rust parser" So yeah, fall could be a replacement for persil, and for manually written AST/visitors. And lang_rust, which is implemented with fall and lives in its directory, could become a replacement for fuzzy-pickers.
A short elevator pitch for fall is that it is ("will be" would be more correct though) a parser/AST generator which is lossless (has comments, whitespace, and smartly attaches them to proper nodes), generates conveniet to use AST and agressively recovers from the errors.
The goal of preserving all information is great -- I think there need to be such a parser somewhere in the ecosystem for tools like linters and formatters, as you say.
Awesome, I've just updated my little "survey" above I'll be happy to give it a try for my little project -- I switched to syntex_syntax now and that compiles rather slowly (which is why I tried the newer syn crate in the first place).