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§
Sourcefn parser(
tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>,
) -> Result<Self, Error>
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.
impl Parser for bool
Parse a boolean value from the input stream.
Only true
and false
are valid boolean values.
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<bool, Error>
Source§impl Parser for char
impl Parser for char
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<char, Error>
Source§impl Parser for i8
Parse i8 may have a positive or negative sign but no suffix
impl Parser for i8
Parse i8 may have a positive or negative sign but no suffix
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<i8, Error>
Source§impl Parser for i16
Parse i16 may have a positive or negative sign but no suffix
impl Parser for i16
Parse i16 may have a positive or negative sign but no suffix
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<i16, Error>
Source§impl Parser for i32
Parse i32 may have a positive or negative sign but no suffix
impl Parser for i32
Parse i32 may have a positive or negative sign but no suffix
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<i32, Error>
Source§impl Parser for i64
Parse i64 may have a positive or negative sign but no suffix
impl Parser for i64
Parse i64 may have a positive or negative sign but no suffix
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<i64, Error>
Source§impl Parser for i128
Parse i128 may have a positive or negative sign but no suffix
impl Parser for i128
Parse i128 may have a positive or negative sign but no suffix
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<i128, Error>
Source§impl Parser for isize
Parse isize may have a positive or negative sign but no suffix
impl Parser for isize
Parse isize may have a positive or negative sign but no suffix
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<isize, Error>
Source§impl Parser for u8
Parse u8 may have a positive sign but no suffix
impl Parser for u8
Parse u8 may have a positive sign but no suffix
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<u8, Error>
Source§impl Parser for u16
Parse u16 may have a positive sign but no suffix
impl Parser for u16
Parse u16 may have a positive sign but no suffix
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<u16, Error>
Source§impl Parser for u32
Parse u32 may have a positive sign but no suffix
impl Parser for u32
Parse u32 may have a positive sign but no suffix
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<u32, Error>
Source§impl Parser for u64
Parse u64 may have a positive sign but no suffix
impl Parser for u64
Parse u64 may have a positive sign but no suffix
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<u64, Error>
Source§impl Parser for u128
Parse u128 may have a positive sign but no suffix
impl Parser for u128
Parse u128 may have a positive sign but no suffix
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<u128, Error>
Source§impl Parser for usize
Parse usize may have a positive sign but no suffix
impl Parser for usize
Parse usize may have a positive sign but no suffix
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<usize, Error>
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.
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<String, Error>
Source§impl<T> Parser for Option<T>where
T: Parse,
Zero or One of T.
impl<T> Parser for Option<T>where
T: Parse,
Zero or One of T.
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<Option<T>, Error>
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.
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<Box<T>, Error>
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.
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<Rc<T>, Error>
Source§impl<T> Parser for Vec<T>where
T: Parse,
Any number of T
impl<T> Parser for Vec<T>where
T: Parse,
Any number of T
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<Vec<T>, Error>
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.
fn parser( tokens: &mut ShadowCountedIter<'_, <TokenStream as IntoIterator>::IntoIter>, ) -> Result<RefCell<T>, Error>
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 DocInner
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 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 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 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 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.