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

Automatic parsing of Vec<SomeSynType> collections #6

Open
sam0x17 opened this issue Apr 7, 2023 · 2 comments
Open

Automatic parsing of Vec<SomeSynType> collections #6

sam0x17 opened this issue Apr 7, 2023 · 2 comments

Comments

@sam0x17
Copy link

sam0x17 commented Apr 7, 2023

I think the built-in support for bracket/brace/paren delimited things is great. I have been thinking about seeing if I could submit a PR that would make the following "just work":

#[derive(derive_syn_parse::Parse))
struct MyArgs {
    #[brace]
    _brace: Brace,
    #[inside(_brace)]
    contents: Vec<Stmt>,
}

In other words, I want to extend derive_syn_parse so that any Vec containing a type that implements Parse is automatically digestible by derive_syn_parse.

The way this would work under the hood is in the expansion, we would automatically output the following for such fields within the Parse implementation:

let mut #field_name = Vec::new();
while (some condition relating to the subsequent field) {
    #field_name.push(input.parse()?);
}

Would you be interested in such a PR and/or do you think something like this would be feasible @sharnoff?

@sharnoff
Copy link
Owner

Late reply here as well, as I'm going through everything now.

Any automatic handling of Vec<T> would have to be syntax-based, because derive-syn-parse, as a proc macro crate, cannot expose any other public items (unless things have changed since I first wrote this).

If we try, we get:

error: `proc-macro` crate types currently cannot export any items other than functions tagged with `#[proc_macro]`, `#[proc_macro_derive]`, or `#[proc_macro_attribute]`

(Incidentally, this is also why we don't have automated handling of Delimited<P, T>)

That said, I think it'd make sense to provide this behavior built-in. I'd be happy to have it under #[repeat] or something similar (bikeshedding welcome! you probably use this more than I do 😅).

What do you think? (Is this still relevant?)

@sam0x17
Copy link
Author

sam0x17 commented Mar 16, 2024

Yeah I mean I think something around this would definitely be helpful. I've run into this same problem making my own parsing library quoth recently, and I don't have a perfect solution there either

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

2 participants