Lines Matching full:parse

16 //! enums (not shown) and structs, then provide implementations of the [`Parse`]
17 //! trait to parse these syntax tree data structures from a token stream.
19 //! Once `Parse` impls have been defined, they can be called conveniently from a
23 //! pointing out the exact token that triggered the failure to parse.
32 //! use syn::parse::{Parse, ParseStream};
49 //! impl Parse for Item {
50 //! fn parse(input: ParseStream) -> Result<Self> {
53 //! input.parse().map(Item::Struct)
55 //! input.parse().map(Item::Enum)
62 //! impl Parse for ItemStruct {
63 //! fn parse(input: ParseStream) -> Result<Self> {
66 //! struct_token: input.parse()?,
67 //! ident: input.parse()?,
74 //! # impl Parse for ItemEnum {
75 //! # fn parse(input: ParseStream) -> Result<Self> {
91 //! # The `syn::parse*` functions
93 //! The [`syn::parse`], [`syn::parse2`], and [`syn::parse_str`] functions serve
96 //! implements the [`Parse`] trait, which includes most types in Syn.
98 //! [`syn::parse`]: crate::parse()
128 //! The `Parse` trait is not implemented in these cases because there is no good
139 //! // Can't parse `Punctuated` without knowing whether trailing punctuation
141 //! let path: Punctuated<PathSegment, Token![::]> = syn::parse(tokens)?;
148 //! single `Parse` implementation, and those parser functions can be invoked
156 //! use syn::parse::Parser;
161 //! // Parse a nonempty sequence of path segments separated by `::` punctuation
165 //! let _path = parser.parse(tokens)?;
167 //! // Parse a possibly empty sequence of expressions terminated by commas with
171 //! let _args = parser.parse(tokens)?;
173 //! // Parse zero or more outer attributes but not inner attributes.
176 //! let _attrs = parser.parse(tokens)?;
210 /// the `Parse` trait.
213 pub trait Parse: Sized {
214 fn parse(input: ParseStream) -> Result<Self>;
240 /// - One of [the `syn::parse*` functions][syn-parse]; or
244 /// [syn-parse]: self#the-synparse-functions
297 /// use syn::parse::ParseStream;
321 /// # input.parse()
324 /// # use syn::parse::Parser;
362 /// Triggers an error at the current position of the parse stream.
459 /// parse stream past it.
460 pub fn parse<T: Parse>(&self) -> Result<T> {
461 T::parse(self)
464 /// Calls the given parser function to parse a syntax tree node of type `T`
469 /// The parser below invokes [`Attribute::parse_outer`] to parse a vector of
476 /// use syn::parse::{Parse, ParseStream};
489 /// impl Parse for UnitStruct {
490 /// fn parse(input: ParseStream) -> Result<Self> {
493 /// struct_token: input.parse()?,
494 /// name: input.parse()?,
495 /// semi_token: input.parse()?,
504 /// Looks at the next token in the parse stream to determine whether it
507 /// Does not advance the position of the parse stream.
528 /// use syn::parse::{Parse, ParseStream};
543 /// impl Parse for MarkerTrait {
544 /// fn parse(input: ParseStream) -> Result<Self> {
545 /// let trait_token: Token![trait] = input.parse()?;
546 /// let ident: Ident = input.parse()?;
547 /// let mut generics: Generics = input.parse()?;
548 /// let colon_token: Option<Token![:]> = input.parse()?;
553 /// supertraits.push_value(input.parse()?);
557 /// supertraits.push_punct(input.parse()?);
561 /// generics.where_clause = input.parse()?;
581 /// Looks at the second-next token in the parse stream.
588 /// keyword in Rust. We can't use just `peek` and decide to parse a union if
595 /// use syn::parse::{Parse, ParseStream};
605 /// impl Parse for UnionOrMacro {
606 /// fn parse(input: ParseStream) -> Result<Self> {
608 /// input.parse().map(UnionOrMacro::Union)
610 /// input.parse().map(UnionOrMacro::Macro)
629 /// Looks at the third-next token in the parse stream.
651 /// Parsing continues until the end of this parse stream. The entire content
652 /// of this parse stream must consist of `T` and `P`.
660 /// use syn::parse::{Parse, ParseStream};
663 /// // Parse a simplified tuple struct syntax like:
674 /// impl Parse for TupleStruct {
675 /// fn parse(input: ParseStream) -> Result<Self> {
678 /// struct_token: input.parse()?,
679 /// ident: input.parse()?,
681 /// fields: content.parse_terminated(Type::parse, Token![,])?,
682 /// semi_token: input.parse()?,
705 /// use syn::parse::{Parse, ParseStream};
714 /// impl Parse for Fin {
715 /// fn parse(input: ParseStream) -> Result<Self> {
716 /// Ok(Self(input.parse()?, input.parse()?))
724 /// impl Parse for Thing {
725 /// fn parse(input: ParseStream) -> Result<Self> {
746 P::Token: Parse,
761 /// use syn::parse::{Parse, ParseStream};
771 /// impl Parse for Mod {
772 /// fn parse(input: ParseStream) -> Result<Self> {
775 /// mod_token: input.parse()?,
776 /// name: input.parse()?,
781 /// items.push(content.parse()?);
800 /// use syn::parse::{Parse, ParseStream};
819 /// impl Parse for GenericParam {
820 /// fn parse(input: ParseStream) -> Result<Self> {
823 /// input.parse().map(GenericParam::Type)
825 /// input.parse().map(GenericParam::Lifetime)
827 /// input.parse().map(GenericParam::Const)
838 /// Forks a parse stream so that parsing tokens out of either the original
843 /// Forking a parse stream is a cheap fixed amount of work and does not
850 /// # use syn::parse::ParseStream;
854 /// if input.fork().parse::<Expr>().is_ok() {
855 /// return input.parse::<Expr>();
862 /// parse stream. Only use a fork when the amount of work performed against
866 /// unavoidable, use [`parse::discouraged::Speculative`] to advance the
867 /// original stream once the fork's parse is determined to have been
873 /// [`parse::discouraged::Speculative`]: discouraged::Speculative
878 /// The parse implementation shown here parses possibly restricted `pub`
904 /// The parser uses a forked parse stream to check the first token inside of
906 /// work performed against the forked parse stream.
911 /// use syn::parse::{Parse, ParseStream};
924 /// impl Parse for PubVisibility {
925 /// fn parse(input: ParseStream) -> Result<Self> {
926 /// let pub_token: Token![pub] = input.parse()?;
950 /// in_token: Some(content.parse()?),
975 /// Triggers an error at the current position of the parse stream.
981 /// use syn::parse::{Parse, ParseStream};
988 /// impl Parse for Loop {
989 /// fn parse(input: ParseStream) -> Result<Self> {
995 /// expr: input.parse()?,
1007 /// Speculatively parses tokens from this parse stream, advancing the
1010 /// This is a powerful low-level API used for defining the `Parse` impls of
1019 /// use syn::parse::ParseStream;
1043 /// # input.parse()
1046 /// # use syn::parse::Parser;
1082 /// Returns the `Span` of the next token in the parse stream, or
1083 /// `Span::call_site()` if this parse stream has completely exhausted its
1095 /// parse stream.
1098 /// will affect the state of this parse stream.
1105 /// use syn::parse::{ParseStream, Result};
1136 /// use syn::parse::{Parse, Parser};
1139 /// // Parse syn::Type as a TokenStream, surrounded by angle brackets.
1141 /// let _langle: Token![<] = input.parse()?;
1142 /// let ty = recognize_token_stream(syn::Type::parse)(input)?;
1143 /// let _rangle: Token![>] = input.parse()?;
1164 impl<T: Parse> Parse for Box<T> {
1165 fn parse(input: ParseStream) -> Result<Self> {
1166 input.parse().map(Box::new)
1171 impl<T: Parse + Token> Parse for Option<T> {
1172 fn parse(input: ParseStream) -> Result<Self> {
1174 Ok(Some(input.parse()?))
1182 impl Parse for TokenStream {
1183 fn parse(input: ParseStream) -> Result<Self> {
1189 impl Parse for TokenTree {
1190 fn parse(input: ParseStream) -> Result<Self> {
1199 impl Parse for Group {
1200 fn parse(input: ParseStream) -> Result<Self> {
1213 impl Parse for Punct {
1214 fn parse(input: ParseStream) -> Result<Self> {
1223 impl Parse for Literal {
1224 fn parse(input: ParseStream) -> Result<Self> {
1232 /// Parser that can parse Rust tokens into a particular syntax tree node.
1240 /// Parse a proc-macro2 token stream into the chosen syntax tree node.
1246 /// Parse tokens of source code into the chosen syntax tree node.
1252 fn parse(self, tokens: proc_macro::TokenStream) -> Result<Self::Output> {
1256 /// Parse a string of Rust code into the chosen syntax tree node.
1334 /// use syn::parse::Nothing;
1356 impl Parse for Nothing {
1357 fn parse(_input: ParseStream) -> Result<Self> {