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:
rocky
2016-07-17 16:27:17 -04:00
parent 4a3a62d01b
commit 063e517a7c
2 changed files with 9 additions and 9 deletions

View File

@@ -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):

View File

@@ -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)