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>
impl<'a> Lookahead<'a>
Sourcepub fn peek<T: Peek>(&self, token: T) -> bool
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
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 }
Sourcepub fn error(self) -> Error
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
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more