-
-
Notifications
You must be signed in to change notification settings - Fork 310
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
Publicly expose parsing functions which don't consume the entire string. #81
Comments
I have been thinking about how to support function-like macros and the best idea I have so far is to expose the underlying nom parsers for the various bits of syntax and let procedural macro author use nom macros to parse their entire input. If this approach is flexible enough to parse all Rust syntax in syn, it should be flexible enough to parse practically any syntax a macro author dreams up. In your case: use syn::parsing::*;
named!(pub foo -> Vec<(Ident, String)>,
separated_nonempty_list!(punct!(","), do_parse!(
x: ident >>
keyword!("as") >>
y: quoted_string >>
(x, y)
))
); This defines a function basically like the one you suggested. The IResult contains a &str of the remaining input and you can compose this function with other nom parsers as necessary. fn foo(i: &str) -> IResult<&str, Vec<(Ident, String)>> What do you think? |
I was looking into that but I ran into a simple problem: We could have some sort of We could also simply export our internal |
I think we just export the nom fork. The upstream IResult::Incomplete is really harmful for the procedural macro use case and it's very easy to end up with subtly broken parsers or weird failures unless you wrap everything in Another option is to delete our fork and use real nom with in syn. I would be happy with that as long as the test suite passes and compile time for the macros 1.1 feature set does not regress too much. This may require some new techniques so that working with Incomplete is less risky. Let's see whether any other nom users have come up with good approaches here. |
I'm fine with just exporting the nom fork if we're confident enough in its stability, and if we export that, and people decide they want the full The biggest reason why I would be weary about exporting the |
It is stable. Let's move the macros to their own crate. |
Currently if I am trying to parse, say, a macro body with custom syntax like:
So, I have the input string
'a as "a", b as "b"'
and I want to parse it, I callsyn::parse_ident(s)
to parse the first ident from the string, and get an error, as there are characters which follow that ident. It would be nice to also have access to methods (saysyn::partial_parse_ident(&str) -> Result<(Ident, &str), String>
) which return the remaining string slice in addition to the ident which was parsed.This could also be done by changing the signature of
syn::parse_ident
to something more like:Which would allow me to extract the parsed value and remaining string data from the error value.
Implementing this shouldn't be difficult, but I would like @dtolnay's input on the design before I do so.
The text was updated successfully, but these errors were encountered: