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.
main
anna 2 years ago
parent 50754ed26d
commit f71684bc38
Signed by: fef
GPG Key ID: EC22E476DC2D3D84

@ -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<Token, Error> {
@ -261,6 +264,7 @@ impl Lexer {
};
self.token_line = self.cursor.line();
self.token_col = self.cursor.col();
self.history.push(t.clone());
t
}

Loading…
Cancel
Save