-
-
Notifications
You must be signed in to change notification settings - Fork 316
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
Implement spans #41
Comments
How would you imagine these spans being implemented? My immediate thought was that they could be implemented by adding a span: &'a str field to each of the AST structs, which holds a string slice into the input string. This wouldn't include line and column number info, which would then be at minimum O(n) time to compute, but would act as a sort-of-byte-index tracker. The other simple option would be to do literally that, which is record the starting and ending byte indexes of each of the AST nodes in the source, like: span: (usize, usize) Actually tracking the occurrence of newline characters when parsing seems unpleasant, and like it would require non-insignificant changes to |
I should also add that one of the nice things about doing byte offsets for this is that it makes it very easy to retrieve the original source text for an AST node from the source string, which is nice for error reporting when using |
Once we get procedural macros I would like to take advantage of the spans contained in those rather than implementing our own separate system. The parser will be able to parse a TokenStream rather than a string and it can keep track of the span of each syntax tree node. Then the user's procedural macro logic will be able to trigger errors on particular syntax tree nodes that rustc is able to display in the right place. I haven't been keeping track of how far we are from a usable API for iterating through a TokenStream but once we have that, we can implement TokenStream parsing behind a cfg in syn. |
I would also like spans for string inputs as well, for situations where I am parsing full .rs files with syn. Do you think whatever solution we end up using will support both? |
I think nom handles this with their |
@mystor have you looked into possibly using In general it looks like that library is designed for more advanced use cases and it may serve us better to direct people who need spans to use strata_rs and keep syn focused on the proc macro use case. |
This is superseded by #142 which will use real spans from the compiler. |
This is a requirement for implementing something like rustfmt against syn.
The text was updated successfully, but these errors were encountered: