From 1bfb61b913128bbec5d477032a1aba997ce86e99 Mon Sep 17 00:00:00 2001 From: ahmedsamyh Date: Sat, 16 Nov 2024 22:21:31 +0500 Subject: [PATCH] Fix bug in parsing identifiers. - Update tests. - [test.py] Bail out if any test fails (and didn't update the output). --- main.py | 9 +++------ test.py | 2 ++ tests/04-identifier.momo.test | 2 +- tests/05-multiple-identifiers.momo.test | 6 +++--- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 9820eb2..e7d6c0c 100644 --- 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 b5c67f8..3582442 100644 --- 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!") diff --git a/tests/04-identifier.momo.test b/tests/04-identifier.momo.test index 17c5818..0436921 100644 --- a/tests/04-identifier.momo.test +++ b/tests/04-identifier.momo.test @@ -1,2 +1,2 @@ -"Token (Ident, 'foo', ./tests/04-identifier.momo:1:3)" +"Token (Ident, 'foo', ./tests/04-identifier.momo:1:4)" 'None' diff --git a/tests/05-multiple-identifiers.momo.test b/tests/05-multiple-identifiers.momo.test index d2de25f..665fb25 100644 --- a/tests/05-multiple-identifiers.momo.test +++ b/tests/05-multiple-identifiers.momo.test @@ -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' -- 2.39.5