From 063e517a7cf5426dda35d419312519caf92a815a Mon Sep 17 00:00:00 2001 From: rocky Date: Sun, 17 Jul 2016 16:27:17 -0400 Subject: [PATCH] Better parse error formatting Start to move away for compiler-oriented terminology: Favor "instructions" over "tokens". Syntax error -> Parse error. --- uncompyle6/parser.py | 16 ++++++++-------- uncompyle6/semantics/pysource.py | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/uncompyle6/parser.py b/uncompyle6/parser.py index a2881e61..0c6ea93e 100644 --- a/uncompyle6/parser.py +++ b/uncompyle6/parser.py @@ -21,7 +21,7 @@ class ParserError(Exception): self.offset = offset def __str__(self): - return "Syntax error at or near `%r' token at offset %s\n" % \ + return "Parse error at or near `%r' instruction at offset %s\n" % \ (self.token, self.offset) nop_func = lambda self, args: None @@ -50,19 +50,19 @@ class PythonParser(GenericASTBuilder): for i in dir(self): setattr(self, i, None) - def error(self, tokens, index): + def error(self, instructions, index): # Find the last line boundary for start in range(index, -1, -1): - if tokens[start].linestart: break + if instructions[start].linestart: break pass - for finish in range(index+1, len(tokens)): - if tokens[finish].linestart: break + for finish in range(index+1, len(instructions)): + if instructions[finish].linestart: break pass - err_token = tokens[index] - print("Token context:") + err_token = instructions[index] + print("Instruction context:") for i in range(start, finish): indent = ' ' if i != index else '-> ' - print("%s%s" % (indent, tokens[i])) + print("%s%s" % (indent, instructions[i].format())) raise ParserError(err_token, err_token.offset) def typestring(self, token): diff --git a/uncompyle6/semantics/pysource.py b/uncompyle6/semantics/pysource.py index 30c6af41..38f7a2d0 100644 --- a/uncompyle6/semantics/pysource.py +++ b/uncompyle6/semantics/pysource.py @@ -482,7 +482,7 @@ class ParserError(python_parser.ParserError): def __str__(self): lines = ['--- This code section failed: ---'] - lines.extend( list(map(str, self.tokens)) ) + lines.extend([i.format() for i in self.tokens]) lines.extend( ['', str(self.error)] ) return '\n'.join(lines)