From 1328887bb78ba034fe3d496ad8029382c1ec678e Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Wed, 31 Aug 2022 17:08:38 +0800 Subject: hackc: Parser handles IndexError --- projects/hackc/classes.py | 2 -- projects/hackc/parser.py | 6 ++++++ 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 = "{" -- cgit v1.2.3