pub trait Parserwhere
Self: Sized,{
// Required method
fn parser(tokens: &mut TokenIter) -> Result<Self>;
}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>
fn parser(tokens: &mut TokenIter) -> Result<Self>
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".
Implementations on Foreign Types§
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 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<A: Parse, B: Parse, C: Parse, D: Parse> Parser for (A, B, C, D)
Parse a 4-element tuple by parsing each element in sequence.
impl<A: Parse, B: Parse, C: Parse, D: Parse> 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<A: Parse, B: Parse, C: Parse> Parser for (A, B, C)
Parse a 3-element tuple by parsing each element in sequence.
impl<A: Parse, B: Parse, C: Parse> 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: Parse, B: Parse> Parser for (A, B)
Parse a 2-element tuple by parsing each element in sequence.
impl<A: Parse, B: Parse> Parser for (A, B)
Parse a 2-element tuple by parsing each element in sequence.
Source§impl<T: Parse> Parser for Box<T>
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: Parse> Parser for Box<T>
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: Parse> Parser for Rc<T>
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: Parse> Parser for Rc<T>
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: Parse> Parser for RefCell<T>
Put any parseable entity in a RefCell. In case one wants to mutate the a parse tree on the
fly.
impl<T: Parse> Parser for RefCell<T>
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 BraceGroup
impl Parser for BracketGroup
impl Parser for Disable
impl Parser for Enable
impl Parser for EndOfStream
impl Parser for Group
impl Parser for Ident
impl Parser for Invalid
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 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 TokenTree
impl Parser for TokensRemain
impl<A, B, C, D> Parser for Either<A, B, C, D>
impl<A: Parse, B: Parse, C: Parse, D: Parse> Parser for Cons<A, B, C, D>
impl<A: Parse, B: Parse> Parser for Swap<A, B>
impl<A: PredicateOp, B: PredicateOp, C: PredicateOp, D: PredicateOp> Parser for AllOf<A, B, C, D>
impl<A: PredicateOp, B: PredicateOp, C: PredicateOp, D: PredicateOp> Parser for AnyOf<A, B, C, D>
impl<A: PredicateOp, B: PredicateOp, C: PredicateOp, D: PredicateOp> Parser for OneOf<A, B, C, D>
impl<A: PredicateOp, B: PredicateOp, Same: PredicateOp, Different: PredicateOp> Parser for PredicateCmp<A, B, Same, Different>
impl<C: Parse> Parser for BraceGroupContaining<C>
impl<C: Parse> Parser for BracketGroupContaining<C>
impl<C: Parse> Parser for GroupContaining<C>
impl<C: Parse> Parser for NoneGroupContaining<C>
impl<C: Parse> Parser for ParenthesisGroupContaining<C>
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: Default> Parser for HiddenState<T>
impl<T: Default> Parser for Insert<T>
impl<T: Parse + ToTokens, D: Parse, const MIN: usize, const MAX: usize> Parser for DelimitedVec<T, D, Forbidden, MIN, MAX>
impl<T: Parse + ToTokens, D: Parse, const MIN: usize, const MAX: usize> Parser for DelimitedVec<T, D, Mandatory, MIN, MAX>
impl<T: Parse + ToTokens> Parser for Cached<T>
impl<T: Parse + ToTokens> Parser for IntoIdent<T>
proc_macro2 only.