Peek multiple tokens ahead

This commit is contained in:
greg 2019-06-14 01:30:53 -07:00
parent 3a207cf7a7
commit ccdc02bbd0

View File

@ -205,9 +205,16 @@ impl TokenHandler {
fn peek_kind(&mut self) -> TokenKind {
self.peek().kind
}
fn peek_kind_n(&mut self, n: usize) -> TokenKind {
self.peek_n(n).kind
}
fn peek(&mut self) -> Token {
self.tokens.get(self.idx).map(|t: &Token| { t.clone()}).unwrap_or(Token { kind: TokenKind::EOF, line_num: self.end_of_file.0, char_num: self.end_of_file.1})
}
fn peek_n(&mut self, n: usize) -> Token {
self.tokens.get(self.idx + n).map(|t: &Token| { t.clone()}).unwrap_or(Token { kind: TokenKind::EOF, line_num: self.end_of_file.0, char_num: self.end_of_file.1})
}
fn next(&mut self) -> Token {
self.idx += 1;
self.tokens.get(self.idx - 1).map(|t: &Token| { t.clone() }).unwrap_or(Token { kind: TokenKind::EOF, line_num: self.end_of_file.0, char_num: self.end_of_file.1})