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.

Panics

Panics if another thread panicked while peeking through this value.

Examples found in repository?
examples/calc.rs (line 83)
81
82
83
84
85
86
87
88
89
90
91
92
93
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 380)
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
414
415
416
    fn primary(input: ParseStream) -> Result<Self> {
        let lookahead = input.lookahead();
        if lookahead.peek(kw::kw_false) {
            Ok(Expr::Literal(Literal::False(input.parse()?)))
        } else if lookahead.peek(kw::kw_true) {
            Ok(Expr::Literal(Literal::True(input.parse()?)))
        } else if lookahead.peek(kw::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::kw_super) {
            Ok(Expr::Super {
                keyword: input.parse()?,
                distance: Cell::new(None),
                dot: input.parse()?,
                method: kw::ident(input)?,
            })
        } else if input.peek(kw::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 content;
            let _: Parentheses = group!(content in input);
            Ok(Expr::Group(Box::new(content.parse()?)))
        } else {
            Err(lookahead.error())
        }
    }
source

pub fn error(self) -> Error

Generates an error based on the peek attempts.

Panics

Panics if another thread panicked while peeking through this value.

Examples found in repository?
examples/calc.rs (line 91)
81
82
83
84
85
86
87
88
89
90
91
92
93
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 414)
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
414
415
416
    fn primary(input: ParseStream) -> Result<Self> {
        let lookahead = input.lookahead();
        if lookahead.peek(kw::kw_false) {
            Ok(Expr::Literal(Literal::False(input.parse()?)))
        } else if lookahead.peek(kw::kw_true) {
            Ok(Expr::Literal(Literal::True(input.parse()?)))
        } else if lookahead.peek(kw::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::kw_super) {
            Ok(Expr::Super {
                keyword: input.parse()?,
                distance: Cell::new(None),
                dot: input.parse()?,
                method: kw::ident(input)?,
            })
        } else if input.peek(kw::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 content;
            let _: Parentheses = group!(content in input);
            Ok(Expr::Group(Box::new(content.parse()?)))
        } 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 T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where 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 T
where 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.