From: Jacob Lifshay Date: Tue, 9 May 2023 07:10:27 +0000 (-0700) Subject: bypass ply's eating SyntaxErrors X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=6ea34a955a9ba4d281a19470dea71f262c6e0d0d;p=openpower-isa.git bypass ply's eating SyntaxErrors --- diff --git a/src/openpower/decoder/pseudo/lexer.py b/src/openpower/decoder/pseudo/lexer.py index 876ec87d..dfda1976 100644 --- a/src/openpower/decoder/pseudo/lexer.py +++ b/src/openpower/decoder/pseudo/lexer.py @@ -13,12 +13,27 @@ from ply import lex from openpower.decoder.selectable_int import SelectableInt +class SyntaxError2(Exception): + """ class used to raise a syntax error but get ply to stop eating errors + since it catches and discards SyntaxError after setting a flag. + """ + + def __repr__(self): + return repr(SyntaxError(*self.args)) + + def __str__(self): + return str(SyntaxError(*self.args)) + + def raise_syntax_error(self): + raise SyntaxError(*self.args) from self + + def raise_syntax_error(msg, filename, lineno, lexpos, input_text): line_start = input_text.rfind('\n', 0, lexpos) + 1 line_end = input_text.find('\n', line_start) col = (lexpos - line_start) + 1 - raise SyntaxError(str(msg), (filename, lineno, col, - input_text[line_start:line_end])) + raise SyntaxError2(str(msg), (filename, lineno, col, + input_text[line_start:line_end])) # I implemented INDENT / DEDENT generation as a post-processing filter diff --git a/src/openpower/decoder/pseudo/parser.py b/src/openpower/decoder/pseudo/parser.py index de3eda73..ef825fb0 100644 --- a/src/openpower/decoder/pseudo/parser.py +++ b/src/openpower/decoder/pseudo/parser.py @@ -14,7 +14,8 @@ import astor from copy import deepcopy from openpower.decoder.power_decoder import create_pdecode -from openpower.decoder.pseudo.lexer import IndentLexer, raise_syntax_error +from openpower.decoder.pseudo.lexer import ( + IndentLexer, raise_syntax_error, SyntaxError2) from openpower.decoder.orderedset import OrderedSet # I use the Python AST @@ -1002,7 +1003,10 @@ class GardenSnakeCompiler(object): raise ValueError("missing filename") self.parser.filename = filename self.parser.input_text = code - tree = self.parser.parse(code) + try: + tree = self.parser.parse(code) + except SyntaxError2 as e: + e.raise_syntax_error() print("snake") pprint(tree) return tree