Skip to content

Commit

Permalink
Add more peek
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Nov 26, 2024
1 parent 62001ba commit 5a7aca0
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3278,6 +3278,12 @@ impl<'a> Parser<'a> {
self.peek_nth_token(0)
}

/// Return the first non-whitespace token that has not yet been processed
/// (or None if reached end-of-file)
pub fn peek_token_ref(&self) -> &TokenWithLocation {
self.peek_nth_token_ref(0)
}

/// Returns the `N` next non-whitespace tokens that have not yet been
/// processed.
///
Expand Down Expand Up @@ -3305,6 +3311,11 @@ impl<'a> Parser<'a> {
.map(|with_loc| with_loc.token)
}

pub fn peek_tokens_ref<const N: usize>(&self) -> [&Token; N] {
self.peek_tokens_with_location_ref()
.map(|with_loc| &with_loc.token)
}

/// Returns the `N` next non-whitespace tokens with locations that have not
/// yet been processed.
///
Expand All @@ -3328,6 +3339,26 @@ impl<'a> Parser<'a> {
})
}

pub fn peek_tokens_with_location_ref<const N: usize>(&self) -> [&TokenWithLocation; N] {
let mut index = self.index;
core::array::from_fn(|_| loop {
let token = self.tokens.get(index);
index += 1;
if let Some(TokenWithLocation {
token: Token::Whitespace(_),
span: _,
}) = token
{
continue;
}
if let Some(tok) = token {
return tok;
} else {
return eof_token();
};
})
}

/// Return nth non-whitespace token that has not yet been processed
pub fn peek_nth_token(&self, n: usize) -> TokenWithLocation {
self.peek_nth_token_ref(n).clone()
Expand Down Expand Up @@ -3458,6 +3489,13 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_keyword_token_ref(&mut self, expected: Keyword) -> Option<&TokenWithLocation> {
match &self.peek_token_ref().token {
Token::Word(w) if expected == w.keyword => Some(self.next_token_ref()),
_ => None,
}
}

#[must_use]
pub fn peek_keyword(&mut self, expected: Keyword) -> bool {
matches!(self.peek_token().token, Token::Word(w) if expected == w.keyword)
Expand Down

0 comments on commit 5a7aca0

Please sign in to comment.