pub trait Parser: Sized {
// Required method
fn parser(tokens: &mut TokenIter) -> Result<Self, Error>;
}Expand description
The Parser trait that must be implemented by anything we want to parse. We are parsing
over a TokenIter (TokenStream iterator).
Required Methods§
Sourcefn parser(tokens: &mut TokenIter) -> Result<Self, Error>
fn parser(tokens: &mut TokenIter) -> Result<Self, Error>
The actual parsing function that must be implemented. This mutates the tokens
iterator directly. It should not be called from user code except for implementing
parsers itself and then only when the rules below are followed.
§Implementing Parsers
The parsers for TokenStream, TokenTree, Group, Ident, Punct,
Literal, Except and Nothing (and few more) are the fundamental parsers.
Any other parser is composed from those.
Calling another T::parser() implementation is only valid when this is a conjunctive
operation and a failure is returned immediately by the ? operator. This can be used
as performance optimization. Any other call to a parser must be done within a transaction.
Otherwise the iterator will be left in a consumed state which breaks further parsing.
Transactions can be done by calling Parse::parse() or with the
Transaction::transaction() method on the iterator.
§Errors
The parser() implementation must return an error when it cannot parse the
input. This error must be a Error. User code will parse a grammar by calling
Parse::parse_all(), Parse::parse() or Parse::parse_with() which will call
this method within a transaction and roll back on error.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl Parser for bool
Parse a boolean value from the input stream.
Only true and false are valid boolean values.
impl Parser for bool
Parse a boolean value from the input stream.
Only true and false are valid boolean values.
Source§impl Parser for String
Parse a String from the input stream. Parsing into a string is special as it parses any
kind of TokenTree and converts it .to_string(). Thus it looses its relationship to the
type of the underlying token/syntactic entity. This is only useful when one wants to parse
string like parameters in a macro that are not emitted later. This limits the use of this
parser significantly.
impl Parser for String
Parse a String from the input stream. Parsing into a string is special as it parses any
kind of TokenTree and converts it .to_string(). Thus it looses its relationship to the
type of the underlying token/syntactic entity. This is only useful when one wants to parse
string like parameters in a macro that are not emitted later. This limits the use of this
parser significantly.
Source§impl<A, B> Parser for (A, B)
Parse a 2-element tuple by parsing each element in sequence.
impl<A, B> Parser for (A, B)
Parse a 2-element tuple by parsing each element in sequence.
Source§impl<A, B, C> Parser for (A, B, C)
Parse a 3-element tuple by parsing each element in sequence.
impl<A, B, C> Parser for (A, B, C)
Parse a 3-element tuple by parsing each element in sequence.
§Example
let mut tokens = "foo + 42".to_token_iter();
let (id, op, num): (Ident, Punct, u8) = Parse::parse(&mut tokens).unwrap();
assert_eq!(id.to_string(), "foo");
assert_eq!(op.to_string(), "+");
assert_eq!(num, 42);Source§impl<A, B, C, D> Parser for (A, B, C, D)
Parse a 4-element tuple by parsing each element in sequence.
impl<A, B, C, D> Parser for (A, B, C, D)
Parse a 4-element tuple by parsing each element in sequence.
§Example
let mut tokens = "5 true 'a' 255".to_token_iter();
let tuple: (u8, bool, char, u8) = Parse::parse(&mut tokens).unwrap();
assert_eq!(tuple, (5, true, 'a', 255));Source§impl<T> Parser for Box<T>where
T: Parse,
Box a parseable entity. In a enum it may happen that most variants are rather small while
few variants are large. In this case it may be beneficial to box the large variants to
keep the enum lean. Box or Rc are required for parsing recursive grammars.
impl<T> Parser for Box<T>where
T: Parse,
Box a parseable entity. In a enum it may happen that most variants are rather small while
few variants are large. In this case it may be beneficial to box the large variants to
keep the enum lean. Box or Rc are required for parsing recursive grammars.
Source§impl<T> Parser for Rc<T>where
T: Parse,
Rc a parseable entity. Just because we can. Sometimes when a value is shared between
multiple entities it may be beneficial to use Rc. Box or Rc are required for parsing recursive grammars.
impl<T> Parser for Rc<T>where
T: Parse,
Rc a parseable entity. Just because we can. Sometimes when a value is shared between
multiple entities it may be beneficial to use Rc. Box or Rc are required for parsing recursive grammars.
Source§impl<T> Parser for RefCell<T>where
T: Parse,
Put any parseable entity in a RefCell. In case one wants to mutate the a parse tree on the
fly.
impl<T> Parser for RefCell<T>where
T: Parse,
Put any parseable entity in a RefCell. In case one wants to mutate the a parse tree on the
fly.
Source§impl<T> Parser for PhantomData<T>
PhantomData behaves like Nothing it doesn’t parse anything and doesnt emit tokens.
impl<T> Parser for PhantomData<T>
PhantomData behaves like Nothing it doesn’t parse anything and doesnt emit tokens.
Implementors§
impl Parser for AdtDecl
impl Parser for AttributeInner
impl Parser for ConstOrMut
impl Parser for EnumVariantData
impl Parser for Expr
impl Parser for FacetInner
impl Parser for GenericParam
impl Parser for LifetimeOrTt
impl Parser for StructKind
impl Parser for TokenTree
impl Parser for Vis
impl Parser for AngleTokenTree
impl Parser for Attribute
impl Parser for BraceGroup
impl Parser for BracketGroup
impl Parser for ChildInner
impl Parser for DefaultEqualsInner
impl Parser for DeserializeWithInner
impl Parser for Disable
impl Parser for DocInner
impl Parser for Enable
impl Parser for EndOfStream
impl Parser for Enum
impl Parser for EnumVariantLike
impl Parser for FacetAttr
impl Parser for FlattenInner
impl Parser for GenericParams
impl Parser for Group
impl Parser for Ident
impl Parser for Invalid
impl Parser for InvariantInner
impl Parser for KChild
impl Parser for KConst
impl Parser for KCrate
impl Parser for KDefault
impl Parser for KDenyUnknownFields
impl Parser for KDeserializeWith
impl Parser for KDoc
impl Parser for KEnum
impl Parser for KFacet
impl Parser for KFlatten
impl Parser for KIn
impl Parser for KInvariants
impl Parser for KMut
impl Parser for KOpaque
impl Parser for KPub
impl Parser for KRename
impl Parser for KRenameAll
impl Parser for KRepr
impl Parser for KSensitive
impl Parser for KSerializeWith
impl Parser for KSkipSerializing
impl Parser for KSkipSerializingIf
impl Parser for KStruct
impl Parser for KTransparent
impl Parser for KTypeTag
impl Parser for KWhere
impl Parser for Lifetime
impl Parser for Literal
impl Parser for LiteralCharacter
impl Parser for LiteralInteger
impl Parser for LiteralString
impl Parser for NonEmptyTokenStream
impl Parser for NonParseable
nonparseable only.impl Parser for NoneGroup
impl Parser for Nothing
impl Parser for ParenthesisGroup
impl Parser for Punct
impl Parser for RenameAllInner
impl Parser for RenameInner
impl Parser for ReprInner
impl Parser for SerializeWithInner
impl Parser for SkipSerializingIfInner
impl Parser for SkipSerializingInner
impl Parser for Struct
impl Parser for StructEnumVariant
impl Parser for StructField
impl Parser for TokenStream
Parses a TokenStream from the input tokens. This is the primary entity to parse when
dealing with opaque entities where internal details are left out.
Note that this matches a empty stream (see EndOfStream) as well.
impl Parser for TokensRemain
impl Parser for TupleField
impl Parser for TupleVariant
impl Parser for TypeTagInner
impl Parser for UnitVariant
impl Parser for WhereClause
impl Parser for WhereClauses
impl<A, B> Parser for Swap<A, B>
impl<A, B, C, D> Parser for Either<A, B, C, D>
impl<A, B, C, D> Parser for AllOf<A, B, C, D>
impl<A, B, C, D> Parser for AnyOf<A, B, C, D>
impl<A, B, C, D> Parser for Cons<A, B, C, D>
impl<A, B, C, D> Parser for OneOf<A, B, C, D>
impl<A, B, Same, Different> Parser for PredicateCmp<A, B, Same, Different>
impl<C> Parser for BraceGroupContaining<C>where
C: Parse,
impl<C> Parser for BracketGroupContaining<C>where
C: Parse,
impl<C> Parser for GroupContaining<C>where
C: Parse,
impl<C> Parser for NoneGroupContaining<C>where
C: Parse,
impl<C> Parser for ParenthesisGroupContaining<C>where
C: Parse,
impl<Operand, Operator, const MAX: usize> Parser for LeftAssocExpr<Operand, Operator, MAX>
impl<Operand, Operator, const MAX: usize> Parser for PostfixExpr<Operand, Operator, MAX>
impl<Operand, Operator, const MAX: usize> Parser for RightAssocExpr<Operand, Operator, MAX>
impl<Operator, Operand, const MAX: usize> Parser for PrefixExpr<Operator, Operand, MAX>
impl<T> Parser for Cached<T>
impl<T> Parser for Discard<T>where
T: Parse,
impl<T> Parser for DynNode<T>where
T: Parse + DynamicTokens,
impl<T> Parser for Except<T>where
T: Parse,
impl<T> Parser for Expect<T>where
T: Parse,
impl<T> Parser for HiddenState<T>where
T: Default,
impl<T> Parser for Insert<T>where
T: Default,
impl<T> Parser for IntoIdent<T>
proc_macro2 only.