Struct flexi_parse::Lookahead

source ·
pub struct Lookahead<'a> { /* private fields */ }
Expand description

A type for peeking at the next token, and generating a helpful error if it isn’t an expected type.

Implementations§

source§

impl<'a> Lookahead<'a>

source

pub fn peek<T: Peek>(&self, token: T) -> bool

Returns true if the next token is the given type.

Examples found in repository?
examples/calc.rs (line 84)
82
83
84
85
86
87
88
89
90
91
92
93
94
fn primary(input: ParseStream<'_>) -> Result<Expr> {
    let lookahead = input.lookahead();
    if lookahead.peek(token::LitFloat) {
        Ok(Expr::Num(input.parse::<token::LitFloat>()?.value()))
    } else if lookahead.peek(token::LitInt) {
        Ok(Expr::Num(input.parse::<token::LitInt>()?.value() as f64))
    } else if lookahead.peek(token::LeftParen) {
        let group: Group<Parentheses> = input.parse()?;
        parse(group.into_token_stream())
    } else {
        Err(lookahead.error())
    }
}
More examples
Hide additional examples
examples/lox/main.rs (line 378)
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
    fn primary(input: ParseStream<'_>) -> Result<Self> {
        let lookahead = input.lookahead();
        if lookahead.peek(kw::keyword_false) {
            Ok(Expr::Literal(Literal::False(input.parse()?)))
        } else if lookahead.peek(kw::keyword_true) {
            Ok(Expr::Literal(Literal::True(input.parse()?)))
        } else if lookahead.peek(kw::keyword_nil) {
            Ok(Expr::Literal(Literal::Nil(input.parse()?)))
        } else if lookahead.peek(LitFloat) {
            Ok(Expr::Literal(Literal::Float(input.parse()?)))
        } else if lookahead.peek(LitInt) {
            Ok(Expr::Literal(Literal::Int(input.parse()?)))
        } else if lookahead.peek(LitStr) {
            Ok(Expr::Literal(Literal::String(input.parse()?)))
        } else if input.peek(kw::keyword_super) {
            Ok(Expr::Super {
                keyword: input.parse()?,
                distance: Cell::new(None),
                dot: input.parse()?,
                method: input.parse()?,
            })
        } else if input.peek(kw::keyword_this) {
            Ok(Expr::This {
                keyword: input.parse()?,
                distance: Cell::new(None),
            })
        } else if lookahead.peek(Ident) {
            Ok(Expr::Variable {
                name: kw::ident(input)?,
                distance: Cell::new(None),
            })
        } else if lookahead.peek(Punct!["("]) {
            let group: Group<Parentheses> = input.parse()?;
            Ok(Expr::Group(Box::new(parse(group.into_token_stream())?)))
        } else {
            Err(lookahead.error())
        }
    }
source

pub fn error(self) -> Error

Generates an error based on the peek attempts.

Examples found in repository?
examples/calc.rs (line 92)
82
83
84
85
86
87
88
89
90
91
92
93
94
fn primary(input: ParseStream<'_>) -> Result<Expr> {
    let lookahead = input.lookahead();
    if lookahead.peek(token::LitFloat) {
        Ok(Expr::Num(input.parse::<token::LitFloat>()?.value()))
    } else if lookahead.peek(token::LitInt) {
        Ok(Expr::Num(input.parse::<token::LitInt>()?.value() as f64))
    } else if lookahead.peek(token::LeftParen) {
        let group: Group<Parentheses> = input.parse()?;
        parse(group.into_token_stream())
    } else {
        Err(lookahead.error())
    }
}
More examples
Hide additional examples
examples/lox/main.rs (line 411)
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
    fn primary(input: ParseStream<'_>) -> Result<Self> {
        let lookahead = input.lookahead();
        if lookahead.peek(kw::keyword_false) {
            Ok(Expr::Literal(Literal::False(input.parse()?)))
        } else if lookahead.peek(kw::keyword_true) {
            Ok(Expr::Literal(Literal::True(input.parse()?)))
        } else if lookahead.peek(kw::keyword_nil) {
            Ok(Expr::Literal(Literal::Nil(input.parse()?)))
        } else if lookahead.peek(LitFloat) {
            Ok(Expr::Literal(Literal::Float(input.parse()?)))
        } else if lookahead.peek(LitInt) {
            Ok(Expr::Literal(Literal::Int(input.parse()?)))
        } else if lookahead.peek(LitStr) {
            Ok(Expr::Literal(Literal::String(input.parse()?)))
        } else if input.peek(kw::keyword_super) {
            Ok(Expr::Super {
                keyword: input.parse()?,
                distance: Cell::new(None),
                dot: input.parse()?,
                method: input.parse()?,
            })
        } else if input.peek(kw::keyword_this) {
            Ok(Expr::This {
                keyword: input.parse()?,
                distance: Cell::new(None),
            })
        } else if lookahead.peek(Ident) {
            Ok(Expr::Variable {
                name: kw::ident(input)?,
                distance: Cell::new(None),
            })
        } else if lookahead.peek(Punct!["("]) {
            let group: Group<Parentheses> = input.parse()?;
            Ok(Expr::Group(Box::new(parse(group.into_token_stream())?)))
        } else {
            Err(lookahead.error())
        }
    }

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Lookahead<'a>

§

impl<'a> !Send for Lookahead<'a>

§

impl<'a> !Sync for Lookahead<'a>

§

impl<'a> Unpin for Lookahead<'a>

§

impl<'a> UnwindSafe for Lookahead<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.