feat(tazjin/rlox): Implement multi-character scanning

Change-Id: Ic5a246a7f5834477aeb97824fa30ba74a8422bc1
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2161
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2020-11-27 18:10:32 +01:00 committed by tazjin
parent 6363efbebf
commit 3f6b88bce2

View file

@ -90,6 +90,7 @@ impl<'a> Scanner<'a> {
fn scan_token(&mut self) {
match self.advance() {
// simple single-character tokens
'(' => self.add_token(TokenKind::LeftParen),
')' => self.add_token(TokenKind::RightParen),
'{' => self.add_token(TokenKind::LeftBrace),
@ -101,6 +102,12 @@ impl<'a> Scanner<'a> {
';' => self.add_token(TokenKind::Semicolon),
'*' => self.add_token(TokenKind::Star),
// possible multi-character tokens
'!' => self.add_if_next('=', TokenKind::BangEqual, TokenKind::Bang),
'=' => self.add_if_next('=', TokenKind::EqualEqual, TokenKind::Equal),
'<' => self.add_if_next('=', TokenKind::LessEqual, TokenKind::Less),
'>' => self.add_if_next('=', TokenKind::GreaterEqual, TokenKind::Greater),
unexpected => self.errors.push(Error {
line: self.line,
kind: ErrorKind::UnexpectedChar(unexpected),
@ -108,6 +115,15 @@ impl<'a> Scanner<'a> {
};
}
fn add_if_next(&mut self, expected:char, then: TokenKind, or: TokenKind) {
if self.is_at_end() || self.source[self.current] != expected {
self.add_token(or);
} else {
self.current += 1;
self.add_token(then);
}
}
fn scan_tokens(mut self) -> Vec<Token<'a>> {
while !self.is_at_end() {
self.start = self.current;