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>
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.
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
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())
}
}
sourcepub fn error(self) -> Error
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
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> 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