Function syn::parse2

source ·
pub fn parse2<T: Parse>(tokens: TokenStream) -> Result<T>
Available on crate feature parsing only.
Expand description

Parse a proc-macro2 token stream into the chosen syntax tree node.

This function will check that the input is fully parsed. If there are any unparsed tokens at the end of the stream, an error is returned.

This function parses a proc_macro2::TokenStream which is commonly useful when the input comes from a node of the Syn syntax tree, for example the body tokens of a Macro node. When in a procedural macro parsing the proc_macro::TokenStream provided by the compiler, use syn::parse instead.

This function is available only if Syn is built with the "parsing" feature.

Examples found in repository?
src/item.rs (line 1458)
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
    fn pop_variadic(args: &mut Punctuated<FnArg, Token![,]>) -> Option<Variadic> {
        let trailing_punct = args.trailing_punct();

        let last = match args.last_mut()? {
            FnArg::Typed(last) => last,
            _ => return None,
        };

        let ty = match last.ty.as_ref() {
            Type::Verbatim(ty) => ty,
            _ => return None,
        };

        let mut variadic = Variadic {
            attrs: Vec::new(),
            dots: parse2(ty.clone()).ok()?,
        };

        if let Pat::Verbatim(pat) = last.pat.as_ref() {
            if pat.to_string() == "..." && !trailing_punct {
                variadic.attrs = mem::replace(&mut last.attrs, Vec::new());
                args.pop();
            }
        }

        Some(variadic)
    }