diff --git a/src/lex/mod.rs b/src/lex/mod.rs index 054d3e1..9a44a88 100644 --- a/src/lex/mod.rs +++ b/src/lex/mod.rs @@ -52,6 +52,8 @@ impl Iterator for Lexer { c if c.is_ascii_whitespace() => { self.cursor.skip_whitespace(); self.cursor.chop(); + self.token_line = self.cursor.line(); + self.token_col = self.cursor.col(); self.next()? } ',' => self.token_ok(token::Kind::Comma), @@ -114,9 +116,6 @@ impl Iterator for Lexer { c => self.syntax_error(format!("Unexpected character '{}'", c)), }; - if let Ok(token) = &result { - self.history.push(token.clone()); - } Some(result) } } @@ -148,9 +147,13 @@ impl Lexer { } pub fn prev(&mut self) -> Option<&Token> { - self.offset += 1; - let prev = &self.history[self.history.len() - self.offset]; - Some(prev) + if self.offset < self.history.len() - 1 { + self.offset += 1; + let prev = &self.history[self.history.len() - self.offset]; + Some(prev) + } else { + None + } } pub fn expect_kind(&mut self, kind: token::Kind) -> Result { @@ -261,6 +264,7 @@ impl Lexer { }; self.token_line = self.cursor.line(); self.token_col = self.cursor.col(); + self.history.push(t.clone()); t }