Show token number in parser error listing...

But only if -g is given
This commit is contained in:
rocky
2020-03-31 10:12:52 -04:00
parent 2b2e7d3242
commit 5fde4f2e05
8 changed files with 48 additions and 42 deletions

View File

@@ -23,7 +23,7 @@
# Things that change more often go here. # Things that change more often go here.
copyright = """ copyright = """
Copyright (C) 2015-2019 Rocky Bernstein <rb@dustyfeet.com>. Copyright (C) 2015-2020 Rocky Bernstein <rb@dustyfeet.com>.
""" """
classifiers = ["Development Status :: 5 - Production/Stable", classifiers = ["Development Status :: 5 - Production/Stable",

View File

@@ -1,10 +1,10 @@
# Python 2.7 # Python 2.7
# Embedded file name: simple_source/branching/05_if.py # Embedded file name: simple_source/branching/05_if.py
6 0 LOAD_NAME 0 'True' L. 6 0 LOAD_NAME 0 'True'
3 POP_JUMP_IF_FALSE 15 'to 15' 3 POP_JUMP_IF_FALSE 15 'to 15'
7 6 LOAD_NAME 1 'False' L. 7 6 LOAD_NAME 1 'False'
9 STORE_NAME 2 'b' 9 STORE_NAME 2 'b'
12 JUMP_FORWARD 0 'to 15' 12 JUMP_FORWARD 0 'to 15'
15_0 COME_FROM 12 '12' 15_0 COME_FROM 12 '12'

View File

@@ -1,14 +1,14 @@
# Python 2.7 # Python 2.7
# Embedded file name: simple_source/branching/05_ifelse.py # Embedded file name: simple_source/branching/05_ifelse.py
3 0 LOAD_NAME 0 'True' L. 3 0 LOAD_NAME 0 'True'
3 POP_JUMP_IF_FALSE 15 'to 15' 3 POP_JUMP_IF_FALSE 15 'to 15'
4 6 LOAD_CONST 1 L. 4 6 LOAD_CONST 1
9 STORE_NAME 1 'b' 9 STORE_NAME 1 'b'
12 JUMP_FORWARD 6 'to 21' 12 JUMP_FORWARD 6 'to 21'
6 15 LOAD_CONST 2 L. 6 15 LOAD_CONST 2
18 STORE_NAME 2 'd' 18 STORE_NAME 2 'd'
21_0 COME_FROM 12 '12' 21_0 COME_FROM 12 '12'
21 LOAD_CONST None 21 LOAD_CONST None

View File

@@ -120,11 +120,18 @@ class Token:
def __str__(self): def __str__(self):
return self.format(line_prefix="") return self.format(line_prefix="")
def format(self, line_prefix=""): def format(self, line_prefix="", token_num=None):
if token_num is not None:
prefix = ( prefix = (
"\n%s%4d " % (line_prefix, self.linestart) "\n(%03d)%s L.%4d " % (token_num, line_prefix, self.linestart)
if self.linestart if self.linestart
else (" " * (6 + len(line_prefix))) else ("(%03d)%s" % (token_num, " " * (9 + len(line_prefix))))
)
else:
prefix = (
"\n%s L.%4d " % (line_prefix, self.linestart)
if self.linestart
else (" " * (9 + len(line_prefix)))
) )
offset_opname = "%8s %-17s" % (self.offset, self.kind) offset_opname = "%8s %-17s" % (self.offset, self.kind)

View File

@@ -1,7 +0,0 @@
# Whatever it is you want to do, it should be forwarded to the
# to top-level irectories
PHONY=check all
all: check
%:
$(MAKE) -C ../.. $@

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2018 by Rocky Bernstein # Copyright (c) 2018, 2020 by Rocky Bernstein
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@@ -13,13 +13,19 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import uncompyle6.parser as python_parser import uncompyle6.parser as python_parser
class ParserError(python_parser.ParserError): class ParserError(python_parser.ParserError):
def __init__(self, error, tokens): def __init__(self, error, tokens, debug):
self.error = error # previous exception self.error = error # previous exception
self.tokens = tokens self.tokens = tokens
self.debug = debug
def __str__(self): def __str__(self):
lines = ['--- This code section failed: ---'] lines = ["--- This code section failed: ---"]
lines.extend([str(i) for i in self.tokens]) if self.debug:
lines.extend( ['', str(self.error)] ) lines.extend([t.format(token_num=i + 1) for i, t in enumerate(self.tokens)])
return '\n'.join(lines) else:
lines.extend([t.format() for t in self.tokens])
lines.extend(["", str(self.error)])
return "\n".join(lines)

View File

@@ -2480,7 +2480,7 @@ class SourceWalker(GenericASTTraversal, object):
ast = python_parser.parse(self.p, tokens, customize) ast = python_parser.parse(self.p, tokens, customize)
self.p.insts = p_insts self.p.insts = p_insts
except (python_parser.ParserError, AssertionError) as e: except (python_parser.ParserError, AssertionError) as e:
raise ParserError(e, tokens) raise ParserError(e, tokens, self.p.debug['reduce'])
checker(ast, False, self.ast_errors) checker(ast, False, self.ast_errors)