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.
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",

View File

@@ -5,7 +5,7 @@ from uncompyle6.scanners.tok import Token
def test_token():
# Test token formatting of: LOAD_CONST None
t = Token("LOAD_CONST", offset=0, attr=None, pattr=None, has_arg=True)
expect = " 0 LOAD_CONST None"
expect = " 0 LOAD_CONST None"
# print(t.format())
assert t
assert t.format() == expect
@@ -17,7 +17,7 @@ def test_token():
# Make sure formatting of: LOAD_CONST False. We assume False is the 0th index
# of co_consts.
t = Token("LOAD_CONST", offset=1, attr=False, pattr=False, has_arg=True)
expect = " 1 LOAD_CONST False"
expect = " 1 LOAD_CONST False"
assert t.format() == expect

View File

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

View File

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

View File

@@ -120,12 +120,19 @@ class Token:
def __str__(self):
return self.format(line_prefix="")
def format(self, line_prefix=""):
prefix = (
"\n%s%4d " % (line_prefix, self.linestart)
if self.linestart
else (" " * (6 + len(line_prefix)))
)
def format(self, line_prefix="", token_num=None):
if token_num is not None:
prefix = (
"\n(%03d)%s L.%4d " % (token_num, line_prefix, self.linestart)
if self.linestart
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)
if not self.has_arg:

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
# 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
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import uncompyle6.parser as python_parser
class ParserError(python_parser.ParserError):
def __init__(self, error, tokens):
self.error = error # previous exception
def __init__(self, error, tokens, debug):
self.error = error # previous exception
self.tokens = tokens
self.debug = debug
def __str__(self):
lines = ['--- This code section failed: ---']
lines.extend([str(i) for i in self.tokens])
lines.extend( ['', str(self.error)] )
return '\n'.join(lines)
lines = ["--- This code section failed: ---"]
if self.debug:
lines.extend([t.format(token_num=i + 1) for i, t in enumerate(self.tokens)])
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)
self.p.insts = p_insts
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)