Add better reprs

This commit is contained in:
Dave Halter
2018-06-21 22:12:26 +02:00
parent e6fc739670
commit 2a082d69df
3 changed files with 20 additions and 9 deletions

View File

@@ -20,9 +20,12 @@ from parso.python import token
class DFAPlan(object):
def __init__(self, next_dfa, pushes=[]):
def __init__(self, next_dfa, dfa_pushes=[]):
self.next_dfa = next_dfa
self.pushes = pushes
self.dfa_pushes = dfa_pushes
def __repr__(self):
return '%s(%s, %s)' % (self.__class__.__name__, self.next_dfa, self.dfa_pushes)
class Grammar(object):
@@ -130,7 +133,7 @@ class Grammar(object):
for terminal_or_nonterminal, next_dfa in dfa_state.arcs.items():
if terminal_or_nonterminal in self.nonterminal2number:
for t, plan in self._first_plans[terminal_or_nonterminal].items():
plans[t] = plan
plans[self._make_label(t)] = plan
else:
ilabel = self._make_label(terminal_or_nonterminal)
plans[ilabel] = DFAPlan(next_dfa)
@@ -231,7 +234,7 @@ class Grammar(object):
assert not self._first_plans[nonterminal].get(t)
self._first_plans[nonterminal][t] = DFAPlan(
plan.next_dfa,
[nonterminal_or_string] + plan.pushes
[next_] + plan.dfa_pushes
)
else:
# It's a string. We have finally found a possible first token.

View File

@@ -63,6 +63,9 @@ class StackNode(object):
def nonterminal(self):
return self.dfa.from_rule
def __repr__(self):
return '%s(%s, %s)' % (self.__class__.__name__, self.dfa, self.nodes)
def token_to_ilabel(grammar, type_, value):
# Map from token to label
@@ -171,20 +174,20 @@ class PgenParser(object):
grammar = self.grammar
try:
plan = stack[-1].current_dfa.ilabel_to_plan[ilabel]
plan = stack[-1].dfa.ilabel_to_plan[ilabel]
except KeyError:
self.error_recovery(grammar, stack, type_,
value, start_pos, prefix, self.add_token)
return False
stack[-1].current_dfa = plan.next_dfa
for push in plan.pushes:
stack.append(StackNode(push.dfa))
stack[-1].dfa = plan.next_dfa
for push in plan.dfa_pushes:
stack.append(StackNode(push))
leaf = self.convert_leaf(grammar, type_, value, prefix, start_pos)
stack[-1].nodes.append(leaf)
while stack[-1].current_dfa.is_final:
while stack[-1].dfa.is_final:
tos = stack.pop()
# If there's exactly one child, return that child instead of
# creating a new node. We still create expr_stmt and

View File

@@ -60,6 +60,11 @@ class DFAState(object):
__hash__ = None # For Py3 compatibility.
def __repr__(self):
return '<%s: %s is_final=%s>' % (
self.__class__.__name__, self.from_rule, self.is_final
)
def _simplify_dfas(dfas):
# This is not theoretically optimal, but works well enough.