]> www.git.momoyon.org Git - lang.git/commitdiff
Fix bug in parsing identifiers.
authorahmedsamyh <ahmedsamyh10@gmail.com>
Sat, 16 Nov 2024 17:21:31 +0000 (22:21 +0500)
committerahmedsamyh <ahmedsamyh10@gmail.com>
Sat, 16 Nov 2024 17:21:31 +0000 (22:21 +0500)
- Update tests.
- [test.py] Bail out if any test fails (and didn't update the output).

main.py
test.py
tests/04-identifier.momo.test
tests/05-multiple-identifiers.momo.test

diff --git a/main.py b/main.py
index 9820eb2d9cd75db7d623445b2b7244e5915f821e..e7d6c0c467521523fac28e5ab95365f133564e26 100644 (file)
--- a/main.py
+++ b/main.py
@@ -108,14 +108,11 @@ class Parser:
     def consume_identifier(self) -> (str, Loc):
         # Identifiers can start with [a-z][A-Z]_ and contain [0-9] after the first char
         assert self.current_char().isalpha() or self.current_char() == '_', "Called consume_identifier() at the wrong character!"
-        ident_loc: Loc = Loc(self.filename, self.line, self.row())
         ident: str = self.consume_char()
+        ident_loc: Loc = Loc(self.filename, self.line, self.row())
 
-        c = self.consume_char()
-
-        while (c.isalpha() or c == '_' or c.isdigit()) and not self.eof():
-            ident += c
-            c = self.consume_char()
+        while (self.current_char().isalpha() or self.current_char() == '_' or self.current_char().isdigit()) and not self.eof():
+            ident += self.consume_char()
 
         return (ident, ident_loc)
 
diff --git a/test.py b/test.py
index b5c67f883f74e40ee591f7834f0e768207902171..3582442c2dae2eaeef11df31c1312a5749141d44 100644 (file)
--- a/test.py
+++ b/test.py
@@ -58,6 +58,8 @@ def test_source_file(filename: str):
             info(f"Updating test file {testfilename}")
             with open(testfilename, 'w') as f:
                 f.write(output)
+        else:
+            exit(1)
     else:
         print("Success!")
 
index 17c5818f3797aeb94c3bdf1b9e16a0be5f097b0c..0436921f0257898090b7e69015fa7c8dc1b6a320 100644 (file)
@@ -1,2 +1,2 @@
-"Token (Ident, 'foo', ./tests/04-identifier.momo:1:3)"
+"Token (Ident, 'foo', ./tests/04-identifier.momo:1:4)"
 'None'
index d2de25f44d9f87dd7f7ce9a942ae273f48f52f7c..665fb2524d7deda949eeb90110d883cd4469d923 100644 (file)
@@ -1,4 +1,4 @@
-"Token (Ident, 'foo', ./tests/05-multiple-identifiers.momo:1:2)"
-"Token (Ident, 'bar', ./tests/05-multiple-identifiers.momo:1:6)"
-"Token (Ident, 'baz', ./tests/05-multiple-identifiers.momo:1:10)"
+"Token (Ident, 'foo', ./tests/05-multiple-identifiers.momo:1:3)"
+"Token (Ident, 'bar', ./tests/05-multiple-identifiers.momo:2:1)"
+"Token (Ident, 'baz', ./tests/05-multiple-identifiers.momo:2:5)"
 'None'