From 7f54baf2668a58f2908f8242b2fbafc65a7f684a Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Mon, 29 Aug 2022 20:40:35 +0800 Subject: hackc: exit on invalid token --- projects/hackc/parser.py | 7 +++++++ projects/hackc/utils.py | 9 +++++++++ 2 files changed, 16 insertions(+) create mode 100644 projects/hackc/utils.py (limited to 'projects') diff --git a/projects/hackc/parser.py b/projects/hackc/parser.py index c3056d7..400b096 100644 --- a/projects/hackc/parser.py +++ b/projects/hackc/parser.py @@ -1,4 +1,5 @@ from .tokens import Token +from .utils import * KEYWORDS = [ "class", @@ -83,3 +84,9 @@ class Parser: if token is not None: self.tokens.append(token) pos += token.length() + else: + # invalid token + print_err(f"{self._fp}:{line_no + 1}") + print_err(line) + print_err(" " * pos + f"^ Invalid token") + exit(EXIT_CODE_INVALID_TOKEN) diff --git a/projects/hackc/utils.py b/projects/hackc/utils.py new file mode 100644 index 0000000..d1ea3ca --- /dev/null +++ b/projects/hackc/utils.py @@ -0,0 +1,9 @@ +from sys import stderr + +EXIT_CODE_FILE_ERROR = 1 +EXIT_CODE_INVALID_TOKEN = 2 +EXIT_CODE_SYNTAX_ERROR = 4 + + +def print_err(msg): + print(msg, file=stderr) -- cgit v1.2.3