From 94d287a2a2746314edaf0a97c2257061aa025e67 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Tue, 9 May 2023 00:10:27 -0700 Subject: [PATCH] bypass ply's eating SyntaxErrors --- src/openpower/decoder/pseudo/lexer.py | 19 +++++++++++++++++-- src/openpower/decoder/pseudo/parser.py | 8 ++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) 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 -- 2.30.2