diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-08-31 17:08:38 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-08-31 17:08:38 +0800 |
commit | 1328887bb78ba034fe3d496ad8029382c1ec678e (patch) | |
tree | f3b47fc3abaf9094184deacee3cae47317a2f0bd | |
parent | 482eed0ae339e977a618c4ab60525596426cf967 (diff) |
hackc: Parser handles IndexError
-rw-r--r-- | projects/hackc/classes.py | 2 | ||||
-rw-r--r-- | projects/hackc/parser.py | 6 | ||||
-rw-r--r-- | projects/hackc/utils.py | 1 |
3 files changed, 7 insertions, 2 deletions
diff --git a/projects/hackc/classes.py b/projects/hackc/classes.py index a8d446a..b300d98 100644 --- a/projects/hackc/classes.py +++ b/projects/hackc/classes.py @@ -61,8 +61,6 @@ class Class: subroutines.append(subroutine) t += dt - if t == tokens_total: - raise JackSyntaxError(f"Class {self.name} ends unexpectedly", tokens[-1]) end = tokens[t] if end != RIGHT_BRACE: raise JackSyntaxError( diff --git a/projects/hackc/parser.py b/projects/hackc/parser.py index 8375d52..188bddc 100644 --- a/projects/hackc/parser.py +++ b/projects/hackc/parser.py @@ -102,3 +102,9 @@ class Parser: print_err(self.lines[err.token.line_no]) print_err(" " * err.token.column + "^ " + err.message) exit(EXIT_CODE_SYNTAX_ERROR) + except IndexError: + last_line = self.lines[-1] + print_err(f"{self._fp}:{len(self.lines)}") + print_err(last_line) + print_err(" " * len(last_line) + "^ Unexpected EOF") + exit(EXIT_CODE_EOF) diff --git a/projects/hackc/utils.py b/projects/hackc/utils.py index 4b9c9f0..58888ac 100644 --- a/projects/hackc/utils.py +++ b/projects/hackc/utils.py @@ -3,6 +3,7 @@ from sys import stderr EXIT_CODE_FILE_ERROR = 1 EXIT_CODE_INVALID_TOKEN = 2 EXIT_CODE_SYNTAX_ERROR = 4 +EXIT_CODE_EOF = 7 # vim autoindent misbehaves if I type these verbatim in strings LEFT_BRACE = "{" |