mirror of
https://github.com/davidhalter/parso.git
synced 2026-02-26 03:27:32 +08:00
Add better reprs
This commit is contained in:
@@ -20,9 +20,12 @@ from parso.python import token
|
|||||||
|
|
||||||
|
|
||||||
class DFAPlan(object):
|
class DFAPlan(object):
|
||||||
def __init__(self, next_dfa, pushes=[]):
|
def __init__(self, next_dfa, dfa_pushes=[]):
|
||||||
self.next_dfa = next_dfa
|
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):
|
class Grammar(object):
|
||||||
@@ -130,7 +133,7 @@ class Grammar(object):
|
|||||||
for terminal_or_nonterminal, next_dfa in dfa_state.arcs.items():
|
for terminal_or_nonterminal, next_dfa in dfa_state.arcs.items():
|
||||||
if terminal_or_nonterminal in self.nonterminal2number:
|
if terminal_or_nonterminal in self.nonterminal2number:
|
||||||
for t, plan in self._first_plans[terminal_or_nonterminal].items():
|
for t, plan in self._first_plans[terminal_or_nonterminal].items():
|
||||||
plans[t] = plan
|
plans[self._make_label(t)] = plan
|
||||||
else:
|
else:
|
||||||
ilabel = self._make_label(terminal_or_nonterminal)
|
ilabel = self._make_label(terminal_or_nonterminal)
|
||||||
plans[ilabel] = DFAPlan(next_dfa)
|
plans[ilabel] = DFAPlan(next_dfa)
|
||||||
@@ -231,7 +234,7 @@ class Grammar(object):
|
|||||||
assert not self._first_plans[nonterminal].get(t)
|
assert not self._first_plans[nonterminal].get(t)
|
||||||
self._first_plans[nonterminal][t] = DFAPlan(
|
self._first_plans[nonterminal][t] = DFAPlan(
|
||||||
plan.next_dfa,
|
plan.next_dfa,
|
||||||
[nonterminal_or_string] + plan.pushes
|
[next_] + plan.dfa_pushes
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# It's a string. We have finally found a possible first token.
|
# It's a string. We have finally found a possible first token.
|
||||||
|
|||||||
@@ -63,6 +63,9 @@ class StackNode(object):
|
|||||||
def nonterminal(self):
|
def nonterminal(self):
|
||||||
return self.dfa.from_rule
|
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):
|
def token_to_ilabel(grammar, type_, value):
|
||||||
# Map from token to label
|
# Map from token to label
|
||||||
@@ -171,20 +174,20 @@ class PgenParser(object):
|
|||||||
grammar = self.grammar
|
grammar = self.grammar
|
||||||
|
|
||||||
try:
|
try:
|
||||||
plan = stack[-1].current_dfa.ilabel_to_plan[ilabel]
|
plan = stack[-1].dfa.ilabel_to_plan[ilabel]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
self.error_recovery(grammar, stack, type_,
|
self.error_recovery(grammar, stack, type_,
|
||||||
value, start_pos, prefix, self.add_token)
|
value, start_pos, prefix, self.add_token)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
stack[-1].current_dfa = plan.next_dfa
|
stack[-1].dfa = plan.next_dfa
|
||||||
for push in plan.pushes:
|
for push in plan.dfa_pushes:
|
||||||
stack.append(StackNode(push.dfa))
|
stack.append(StackNode(push))
|
||||||
|
|
||||||
leaf = self.convert_leaf(grammar, type_, value, prefix, start_pos)
|
leaf = self.convert_leaf(grammar, type_, value, prefix, start_pos)
|
||||||
stack[-1].nodes.append(leaf)
|
stack[-1].nodes.append(leaf)
|
||||||
|
|
||||||
while stack[-1].current_dfa.is_final:
|
while stack[-1].dfa.is_final:
|
||||||
tos = stack.pop()
|
tos = stack.pop()
|
||||||
# If there's exactly one child, return that child instead of
|
# If there's exactly one child, return that child instead of
|
||||||
# creating a new node. We still create expr_stmt and
|
# creating a new node. We still create expr_stmt and
|
||||||
|
|||||||
@@ -60,6 +60,11 @@ class DFAState(object):
|
|||||||
|
|
||||||
__hash__ = None # For Py3 compatibility.
|
__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):
|
def _simplify_dfas(dfas):
|
||||||
# This is not theoretically optimal, but works well enough.
|
# This is not theoretically optimal, but works well enough.
|
||||||
|
|||||||
Reference in New Issue
Block a user