Trait Peek

Source
pub trait Peek<'a>: Sized {
    const PEEK_KINDSET: KindSet = KindSet::ANY;

    // Provided method
    fn peek<I>(_: &Parser<'a, I>, c: Cursor) -> bool
       where I: Iterator<Item = Cursor> + Clone { ... }
}
Expand description

This trait allows AST nodes to indicate whether the Parser is in the right position to potentially Parse the node. Returning true from Peek is not a guarantee that a node will successfully parse, instead it offers an indication that the node can successfully parse the first node. This is useful for cheaply comparing a set of Nodes to see which one might viably parse, rather than calling Parser::try_parse() on each.

Nodes that implement this trait are entitled to peek any number of Cursors ahead from the Parser, to determine if those Cursors are viable to begin parsing, however there is a cost involved in peeking, so it is worth being conservative; peek the minimum amount ahead to determine this. Most implementations can peek just 1 Cursor ahead - this is provided as the second argument. To peek further, use the Parser::peek_n() method. Calling peek_n(2) will return the Cursor after the provided one peek_n(3) will return the second Cursor after, and so on.

For simple implementations it may be sufficient to just check the Kind of the given Cursor. Rather than implementing Peek::peek(), supplying Peek::PEEK_KINDSET and relying on the provided Peek::peek() method will work well.

However it is likely that more complex checks will be needed. In order to reason about the given Cursor (or other cursors ahead) an implementation might want to extract an Atom from the Cursor (using Parser::to_atom) and compare it against an AtomSet.

When peeking child nodes, implementations should not call Peek::peek() directly. Instead - call Parser::peek<T>(). Parser::parse_if_peek<T>() also exists to conveniently parse a Node if it passes the peek test.

If a Node can construct itself from a single Cursor it should also implement Parse.

Provided Associated Constants§

Source

const PEEK_KINDSET: KindSet = KindSet::ANY

Provided Methods§

Source

fn peek<I>(_: &Parser<'a, I>, c: Cursor) -> bool
where I: Iterator<Item = Cursor> + Clone,

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<'a, A, B> Peek<'a> for (A, B)
where A: Peek<'a>, B: Peek<'a>,

Source§

fn peek<Iter>(p: &Parser<'a, Iter>, c: Cursor) -> bool
where Iter: Iterator<Item = Cursor> + Clone,

Source§

impl<'a, A, B, C> Peek<'a> for (A, B, C)
where A: Peek<'a>, B: Peek<'a>, C: Peek<'a>,

Source§

fn peek<Iter>(p: &Parser<'a, Iter>, c: Cursor) -> bool
where Iter: Iterator<Item = Cursor> + Clone,

Source§

impl<'a, A, B, C, D> Peek<'a> for (A, B, C, D)
where A: Peek<'a>, B: Peek<'a>, C: Peek<'a>, D: Peek<'a>,

Source§

fn peek<Iter>(p: &Parser<'a, Iter>, c: Cursor) -> bool
where Iter: Iterator<Item = Cursor> + Clone,

Source§

impl<'a, A, B, C, D, E> Peek<'a> for (A, B, C, D, E)
where A: Peek<'a>, B: Peek<'a>, C: Peek<'a>, D: Peek<'a>, E: Peek<'a>,

Source§

fn peek<Iter>(p: &Parser<'a, Iter>, c: Cursor) -> bool
where Iter: Iterator<Item = Cursor> + Clone,

Source§

impl<'a, A, B, C, D, E, F> Peek<'a> for (A, B, C, D, E, F)
where A: Peek<'a>, B: Peek<'a>, C: Peek<'a>, D: Peek<'a>, E: Peek<'a>, F: Peek<'a>,

Source§

fn peek<Iter>(p: &Parser<'a, Iter>, c: Cursor) -> bool
where Iter: Iterator<Item = Cursor> + Clone,

Source§

impl<'a, A, B, C, D, E, F, G> Peek<'a> for (A, B, C, D, E, F, G)
where A: Peek<'a>, B: Peek<'a>, C: Peek<'a>, D: Peek<'a>, E: Peek<'a>, F: Peek<'a>, G: Peek<'a>,

Source§

fn peek<Iter>(p: &Parser<'a, Iter>, c: Cursor) -> bool
where Iter: Iterator<Item = Cursor> + Clone,

Source§

impl<'a, A, B, C, D, E, F, G, H> Peek<'a> for (A, B, C, D, E, F, G, H)
where A: Peek<'a>, B: Peek<'a>, C: Peek<'a>, D: Peek<'a>, E: Peek<'a>, F: Peek<'a>, G: Peek<'a>, H: Peek<'a>,

Source§

fn peek<Iter>(p: &Parser<'a, Iter>, c: Cursor) -> bool
where Iter: Iterator<Item = Cursor> + Clone,

Source§

impl<'a, A, B, C, D, E, F, G, H, I> Peek<'a> for (A, B, C, D, E, F, G, H, I)
where A: Peek<'a>, B: Peek<'a>, C: Peek<'a>, D: Peek<'a>, E: Peek<'a>, F: Peek<'a>, G: Peek<'a>, H: Peek<'a>, I: Peek<'a>,

Source§

fn peek<Iter>(p: &Parser<'a, Iter>, c: Cursor) -> bool
where Iter: Iterator<Item = Cursor> + Clone,

Source§

impl<'a, A, B, C, D, E, F, G, H, I, J> Peek<'a> for (A, B, C, D, E, F, G, H, I, J)
where A: Peek<'a>, B: Peek<'a>, C: Peek<'a>, D: Peek<'a>, E: Peek<'a>, F: Peek<'a>, G: Peek<'a>, H: Peek<'a>, I: Peek<'a>, J: Peek<'a>,

Source§

fn peek<Iter>(p: &Parser<'a, Iter>, c: Cursor) -> bool
where Iter: Iterator<Item = Cursor> + Clone,

Source§

impl<'a, A, B, C, D, E, F, G, H, I, J, K> Peek<'a> for (A, B, C, D, E, F, G, H, I, J, K)
where A: Peek<'a>, B: Peek<'a>, C: Peek<'a>, D: Peek<'a>, E: Peek<'a>, F: Peek<'a>, G: Peek<'a>, H: Peek<'a>, I: Peek<'a>, J: Peek<'a>, K: Peek<'a>,

Source§

fn peek<Iter>(p: &Parser<'a, Iter>, c: Cursor) -> bool
where Iter: Iterator<Item = Cursor> + Clone,

Source§

impl<'a, A, B, C, D, E, F, G, H, I, J, K, L> Peek<'a> for (A, B, C, D, E, F, G, H, I, J, K, L)
where A: Peek<'a>, B: Peek<'a>, C: Peek<'a>, D: Peek<'a>, E: Peek<'a>, F: Peek<'a>, G: Peek<'a>, H: Peek<'a>, I: Peek<'a>, J: Peek<'a>, K: Peek<'a>, L: Peek<'a>,

Source§

fn peek<Iter>(p: &Parser<'a, Iter>, c: Cursor) -> bool
where Iter: Iterator<Item = Cursor> + Clone,

Source§

impl<'a, T: Peek<'a>> Peek<'a> for Option<T>

Source§

fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
where I: Iterator<Item = Cursor> + Clone,

Source§

impl<'a, T: Peek<'a>> Peek<'a> for Vec<'a, T>

Source§

const PEEK_KINDSET: KindSet = T::PEEK_KINDSET

Source§

fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
where I: Iterator<Item = Cursor> + Clone,

Implementors§

Source§

impl<'a> Peek<'a> for ComponentValue<'a>

Source§

impl<'a> Peek<'a> for BangImportant

Source§

impl<'a> Peek<'a> for ComponentValues<'a>

Source§

impl<'a> Peek<'a> for NoBlockAllowed

Source§

impl<'a> Peek<'a> for And

Source§

impl<'a> Peek<'a> for At

Source§

impl<'a> Peek<'a> for Backtick

Source§

impl<'a> Peek<'a> for Bang

Source§

impl<'a> Peek<'a> for Caret

Source§

impl<'a> Peek<'a> for Dash

Source§

impl<'a> Peek<'a> for Dollar

Source§

impl<'a> Peek<'a> for Dot

Source§

impl<'a> Peek<'a> for Eq

Source§

impl<'a> Peek<'a> for Gt

Source§

impl<'a> Peek<'a> for css_parse::token_macros::delim::Hash

Source§

impl<'a> Peek<'a> for Lt

Source§

impl<'a> Peek<'a> for Or

Source§

impl<'a> Peek<'a> for Percent

Source§

impl<'a> Peek<'a> for Plus

Source§

impl<'a> Peek<'a> for Question

Source§

impl<'a> Peek<'a> for Slash

Source§

impl<'a> Peek<'a> for Star

Source§

impl<'a> Peek<'a> for Tilde

Source§

impl<'a> Peek<'a> for Underscore

Source§

impl<'a> Peek<'a> for CaretEqual

Source§

impl<'a> Peek<'a> for ColonColon

Source§

impl<'a> Peek<'a> for DollarEqual

Source§

impl<'a> Peek<'a> for EqualEqual

Source§

impl<'a> Peek<'a> for GreaterThanEqual

Source§

impl<'a> Peek<'a> for LessThanEqual

Source§

impl<'a> Peek<'a> for PipeEqual

Source§

impl<'a> Peek<'a> for PipePipe

Source§

impl<'a> Peek<'a> for StarEqual

Source§

impl<'a> Peek<'a> for StarPipe

Source§

impl<'a> Peek<'a> for TildeEqual

Source§

impl<'a> Peek<'a> for Any

Source§

impl<'a> Peek<'a> for AtKeyword

Source§

impl<'a> Peek<'a> for BadString

Source§

impl<'a> Peek<'a> for BadUrl

Source§

impl<'a> Peek<'a> for CdcOrCdo

Source§

impl<'a> Peek<'a> for Colon

Source§

impl<'a> Peek<'a> for Comma

Source§

impl<'a> Peek<'a> for Comment

Source§

impl<'a> Peek<'a> for DashedIdent

Source§

impl<'a> Peek<'a> for Delim

Source§

impl<'a> Peek<'a> for Dimension

Source§

impl<'a> Peek<'a> for Eof

Source§

impl<'a> Peek<'a> for Function

Source§

impl<'a> Peek<'a> for css_parse::token_macros::Hash

Source§

impl<'a> Peek<'a> for Ident

Source§

impl<'a> Peek<'a> for LeftCurly

Source§

impl<'a> Peek<'a> for LeftParen

Source§

impl<'a> Peek<'a> for LeftSquare

Source§

impl<'a> Peek<'a> for Number

Source§

impl<'a> Peek<'a> for PairWiseEnd

Source§

impl<'a> Peek<'a> for PairWiseStart

Source§

impl<'a> Peek<'a> for RightCurly

Source§

impl<'a> Peek<'a> for RightParen

Source§

impl<'a> Peek<'a> for RightSquare

Source§

impl<'a> Peek<'a> for Semicolon

Source§

impl<'a> Peek<'a> for String

Source§

impl<'a> Peek<'a> for Url

Source§

impl<'a> Peek<'a> for Whitespace

Source§

impl<'a, A, B> Peek<'a> for Optionals2<A, B>
where A: Parse<'a> + Peek<'a>, B: Parse<'a> + Peek<'a>,

Source§

impl<'a, A, B, C> Peek<'a> for Optionals3<A, B, C>
where A: Parse<'a> + Peek<'a>, B: Parse<'a> + Peek<'a>, C: Parse<'a> + Peek<'a>,

Source§

impl<'a, A, B, C, D> Peek<'a> for Optionals4<A, B, C, D>
where A: Parse<'a> + Peek<'a>, B: Parse<'a> + Peek<'a>, C: Parse<'a> + Peek<'a>, D: Parse<'a> + Peek<'a>,

Source§

impl<'a, A, B, C, D, E> Peek<'a> for Optionals5<A, B, C, D, E>
where A: Parse<'a> + Peek<'a>, B: Parse<'a> + Peek<'a>, C: Parse<'a> + Peek<'a>, D: Parse<'a> + Peek<'a>, E: Parse<'a> + Peek<'a>,

Source§

impl<'a, D, R, M> Peek<'a> for Block<'a, D, R, M>
where D: DeclarationValue<'a, M>, M: NodeMetadata,

Source§

impl<'a, D, R, M> Peek<'a> for DeclarationRuleList<'a, D, R, M>
where D: DeclarationValue<'a, M>, R: NodeWithMetadata<M>, M: NodeMetadata,

Source§

impl<'a, P, D, R, M> Peek<'a> for QualifiedRule<'a, P, D, R, M>
where P: Peek<'a>, D: DeclarationValue<'a, M>, M: NodeMetadata,

Source§

impl<'a, R, M> Peek<'a> for RuleList<'a, R, M>

Source§

impl<'a, T: Peek<'a>, const MIN: usize> Peek<'a> for CommaSeparated<'a, T, MIN>

Source§

const PEEK_KINDSET: KindSet = T::PEEK_KINDSET

Source§

impl<'a, V, M> Peek<'a> for Declaration<'a, V, M>
where V: DeclarationValue<'a, M>, M: NodeMetadata,

Source§

impl<'a, V, M> Peek<'a> for DeclarationList<'a, V, M>
where V: DeclarationValue<'a, M>, M: NodeMetadata,