about summary refs log tree commit diff
path: root/users
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2020-11-28T15·52+0100
committertazjin <mail@tazj.in>2020-11-28T16·33+0000
commit9f61579dde53959486c80f1d2a5a7ab660ca93cd (patch)
treef0bc886db48b4b3918393aa6eb99ffc22747a052 /users
parent516c046ed984ba74da9a36f21e356dae9e227ca3 (diff)
feat(tazjin/rlox): Scan single-line comments r/1950
Change-Id: I2efafdb1374ac2863a1f8d8a9310123a7dec0900
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2187
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users')
-rw-r--r--users/tazjin/rlox/src/scanner.rs31
1 files changed, 29 insertions, 2 deletions
diff --git a/users/tazjin/rlox/src/scanner.rs b/users/tazjin/rlox/src/scanner.rs
index edd85496a73e..6b34288d1082 100644
--- a/users/tazjin/rlox/src/scanner.rs
+++ b/users/tazjin/rlox/src/scanner.rs
@@ -108,6 +108,17 @@ impl<'a> Scanner<'a> {
             '<' => self.add_if_next('=', TokenKind::LessEqual, TokenKind::Less),
             '>' => self.add_if_next('=', TokenKind::GreaterEqual, TokenKind::Greater),
 
+            '/' => {
+                // support comments until EOL by discarding characters
+                if self.match_next('/') {
+                    while self.peek() != '\n' && !self.is_at_end() {
+                        self.advance();
+                    }
+                } else {
+                    self.add_token(TokenKind::Slash);
+                }
+            },
+
             unexpected => self.errors.push(Error {
                 line: self.line,
                 kind: ErrorKind::UnexpectedChar(unexpected),
@@ -115,12 +126,28 @@ impl<'a> Scanner<'a> {
         };
     }
 
-    fn add_if_next(&mut self, expected:char, then: TokenKind, or: TokenKind) {
+    fn match_next(&mut self, expected: char) -> bool {
         if self.is_at_end() || self.source[self.current] != expected {
-            self.add_token(or);
+            false
         } else {
             self.current += 1;
+            true
+        }
+    }
+
+    fn add_if_next(&mut self, expected: char, then: TokenKind, or: TokenKind) {
+        if self.match_next(expected) {
             self.add_token(then);
+        } else {
+            self.add_token(or);
+        }
+    }
+
+    fn peek(&self) -> char {
+        if self.is_at_end() {
+            return '\0';
+        } else {
+            return self.source[self.current];
         }
     }