A simple crate for reducing the boilerplate when writing parsers with [syn
].
```rust
struct ExampleStruct {
#[parse(Attribute::parseouter)]
#[totokens(|tokens, val| tokens.append_all(val))]
attrs: Vec
path: Path,
#[syn(parenthesized)]
paren_token: Paren,
#[syn(in = brace_token)]
#[parse(Punctuated::parse_terminated)]
args: Punctuated<Box<Expr>, Token![,]>,
semi_token: Token![;],
} ```
#[syn([
,
parenthesized
])]#[syn([
,
braced
])]#[syn([
:
Corresponds to the isonymous macros in bracketed
])]syn
.
Must be attached to [struct@Paren
], [struct@Brace
], and [struct@Bracket
] fields, respectively.
#[syn(in = [
:
The field is read from inside the named delimiter pair.struct@Ident
])]
#[parse(fn([
:
A function used to parse the field,
often used with [ParseStream
]) -> [syn::Result
]\Punctuated::parse_terminated
]
or [Attribute::parse_outer
].
#[to_tokens(fn(&mut [
:
A function used to tokenize the field.
Often used with [TokenStream
], &T)]TokenStreamExt::append_all
],
though for type resolution reasons this needs to be indirected through a closure expression.
```rust
enum ExampleEnum { #[parse(peek = Token![struct])] Struct(ItemStruct), #[parse(peek = Token![enum])] Enum(ItemEnum),
Other {
path: Path,
semi_token: Token![;],
}
} ```
#[parse(peek = [
:
Checks whether the variant should be parsed.
Even if multiple peeks succeed, only the first successful variant is attempted.Token
])]
#[parse(peek_func = fn([
:
More powerful than ParseStream
]) -> [bool
])]peek
(such as allowing peek2
), but gives worse error messages on failure.
peek
should be preferred when possible.
derive-syn-parse
does not handle [ToTokens
].
It also seems to encourage throwing tokens away with its prefix
and postfix
attributes.parsel
uses its own types for parentheses, meaning the AST types have different API from [syn
]'s own.