Parser

Trait Parser 

Source
pub trait Parser: Sized {
    // Required method
    fn parser(
        tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>,
    ) -> Result<Self, Error>;
}
Expand description

The Parser trait that must be implemented by anything we want to parse. We are parsing over a TokenIter (proc_macro2::TokenStream iterator).

Required Methods§

Source

fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> 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.

Source§

impl Parser for char

Source§

impl Parser for i8

Parse i8 may have a positive or negative sign but no suffix

Source§

impl Parser for i16

Parse i16 may have a positive or negative sign but no suffix

Source§

impl Parser for i32

Parse i32 may have a positive or negative sign but no suffix

Source§

impl Parser for i64

Parse i64 may have a positive or negative sign but no suffix

Source§

impl Parser for i128

Parse i128 may have a positive or negative sign but no suffix

Source§

impl Parser for isize

Parse isize may have a positive or negative sign but no suffix

Source§

impl Parser for u8

Parse u8 may have a positive sign but no suffix

Source§

impl Parser for u16

Parse u16 may have a positive sign but no suffix

Source§

impl Parser for u32

Parse u32 may have a positive sign but no suffix

Source§

impl Parser for u64

Parse u64 may have a positive sign but no suffix

Source§

impl Parser for u128

Parse u128 may have a positive sign but no suffix

Source§

impl Parser for usize

Parse usize may have a positive sign but no suffix

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.

Source§

impl<T> Parser for Option<T>
where T: Parse,

Zero or One of T.

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.

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.

Source§

impl<T> Parser for Vec<T>
where T: Parse,

Any number of T

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.

Implementors§

Source§

impl Parser for AdtDecl

Source§

impl Parser for AttributeInner

Source§

impl Parser for ConstOrMut

Source§

impl Parser for EnumVariantData

Source§

impl Parser for Expr

Source§

impl Parser for FacetInner

Source§

impl Parser for GenericParam

Source§

impl Parser for LifetimeOrTt

Source§

impl Parser for StructKind

Source§

impl Parser for TokenTree

Source§

impl Parser for Vis

Source§

impl Parser for AngleTokenTree

Source§

impl Parser for Attribute

Source§

impl Parser for BraceGroup

Source§

impl Parser for BracketGroup

Source§

impl Parser for ChildInner

Source§

impl Parser for DefaultEqualsInner

Source§

impl Parser for DocInner

Source§

impl Parser for EndOfStream

Source§

impl Parser for Enum

Source§

impl Parser for EnumVariantLike

Source§

impl Parser for FacetAttr

Source§

impl Parser for FlattenInner

Source§

impl Parser for GenericParams

Source§

impl Parser for Group

Source§

impl Parser for Ident

Source§

impl Parser for Invalid

Source§

impl Parser for InvariantInner

Source§

impl Parser for KChild

Source§

impl Parser for KConst

Source§

impl Parser for KCrate

Source§

impl Parser for KDefault

Source§

impl Parser for KDenyUnknownFields

Source§

impl Parser for KDoc

Source§

impl Parser for KEnum

Source§

impl Parser for KFacet

Source§

impl Parser for KFlatten

Source§

impl Parser for KIn

Source§

impl Parser for KInvariants

Source§

impl Parser for KMut

Source§

impl Parser for KOpaque

Source§

impl Parser for KPub

Source§

impl Parser for KRename

Source§

impl Parser for KRenameAll

Source§

impl Parser for KRepr

Source§

impl Parser for KSensitive

Source§

impl Parser for KSkipSerializing

Source§

impl Parser for KSkipSerializingIf

Source§

impl Parser for KStruct

Source§

impl Parser for KTransparent

Source§

impl Parser for KTypeTag

Source§

impl Parser for KWhere

Source§

impl Parser for Lifetime

Source§

impl Parser for Literal

Source§

impl Parser for LiteralCharacter

Source§

impl Parser for LiteralInteger

Source§

impl Parser for LiteralString

Source§

impl Parser for NonEmptyTokenStream

Source§

impl Parser for NoneGroup

Source§

impl Parser for Nothing

Source§

impl Parser for ParenthesisGroup

Source§

impl Parser for Punct

Source§

impl Parser for RenameAllInner

Source§

impl Parser for RenameInner

Source§

impl Parser for ReprInner

Source§

impl Parser for SkipSerializingIfInner

Source§

impl Parser for SkipSerializingInner

Source§

impl Parser for Struct

Source§

impl Parser for StructEnumVariant

Source§

impl Parser for StructField

Source§

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.

Source§

impl Parser for TupleField

Source§

impl Parser for TupleVariant

Source§

impl Parser for TypeTagInner

Source§

impl Parser for UnitVariant

Source§

impl Parser for WhereClause

Source§

impl Parser for WhereClauses

Source§

impl<A, B, C, D> Parser for Either<A, B, C, D>
where A: Parse, B: Parse, C: Parse, D: Parse,

Source§

impl<A, B, C, D> Parser for Cons<A, B, C, D>
where A: Parse, B: Parse, C: Parse, D: Parse,

Source§

impl<C> Parser for BraceGroupContaining<C>
where C: Parse,

Source§

impl<C> Parser for BracketGroupContaining<C>
where C: Parse,

Source§

impl<C> Parser for GroupContaining<C>
where C: Parse,

Source§

impl<C> Parser for NoneGroupContaining<C>
where C: Parse,

Source§

impl<C> Parser for ParenthesisGroupContaining<C>
where C: Parse,

Source§

impl<T> Parser for Cached<T>
where T: Parse + ToTokens,

Source§

impl<T> Parser for Discard<T>
where T: Parse,

Source§

impl<T> Parser for Except<T>
where T: Parse,

Source§

impl<T> Parser for Expect<T>
where T: Parse,

Source§

impl<T> Parser for HiddenState<T>
where T: Default,

Source§

impl<T> Parser for Skip<T>
where T: Parse,

Source§

impl<T, D> Parser for Delimited<T, D>
where T: Parse, D: Parse,

Source§

impl<T, D> Parser for DelimitedVec<T, D>
where T: Parse, D: Parse,

Source§

impl<T, S> Parser for LazyVec<T, S>
where T: Parse, S: Parse,

Source§

impl<const C1: char, const C2: char, const C3: char, const C4: char> Parser for Operator<C1, C2, C3, C4>

Source§

impl<const C: char> Parser for PunctAlone<C>

Source§

impl<const C: char> Parser for PunctAny<C>

Source§

impl<const C: char> Parser for PunctJoint<C>

Source§

impl<const MIN: usize, const MAX: usize, T, D> Parser for Repeats<MIN, MAX, T, D>
where T: Parse, D: Parse,