You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-03 00:45:53 +08:00
Better parse error formatting
Start to move away for compiler-oriented terminology: Favor "instructions" over "tokens". Syntax error -> Parse error.
This commit is contained in:
@@ -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):
|
||||
|
@@ -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)
|
||||
|
||||
|
Reference in New Issue
Block a user