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

Auto Trait Implementations§

§

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

§

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>,

Source§

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>,

Source§

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.