From f71684bc3874ceba9ade3afbadb5f108c1fc3901 Mon Sep 17 00:00:00 2001 From: fef Date: Wed, 27 Jul 2022 02:26:56 +0200 Subject: [PATCH] lex: fix yet another token position bug When skipping whitespace, the token position must be updated accordingly. Additionally, the token position is now updated when creating a new one. It still doesn't work because something with the cursor is broken. --- src/lex/mod.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) 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 }