mirror of
https://github.com/davidhalter/parso.git
synced 2026-01-09 04:52:42 +08:00
Make nonterminal_to_dfas public
This commit is contained in:
@@ -36,8 +36,8 @@ class Grammar(object):
|
|||||||
do this (see the conv and pgen modules).
|
do this (see the conv and pgen modules).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, bnf_grammar, start_nonterminal, rule_to_dfas, reserved_syntax_strings):
|
def __init__(self, start_nonterminal, rule_to_dfas, reserved_syntax_strings):
|
||||||
self._nonterminal_to_dfas = rule_to_dfas
|
self.nonterminal_to_dfas = rule_to_dfas
|
||||||
|
|
||||||
self.reserved_syntax_strings = reserved_syntax_strings
|
self.reserved_syntax_strings = reserved_syntax_strings
|
||||||
self.start_nonterminal = start_nonterminal
|
self.start_nonterminal = start_nonterminal
|
||||||
@@ -48,7 +48,7 @@ class Grammar(object):
|
|||||||
# Map from grammar rule (nonterminal) name to a set of tokens.
|
# Map from grammar rule (nonterminal) name to a set of tokens.
|
||||||
self._first_plans = {}
|
self._first_plans = {}
|
||||||
|
|
||||||
nonterminals = list(self._nonterminal_to_dfas.keys())
|
nonterminals = list(self.nonterminal_to_dfas.keys())
|
||||||
nonterminals.sort()
|
nonterminals.sort()
|
||||||
for nonterminal in nonterminals:
|
for nonterminal in nonterminals:
|
||||||
if nonterminal not in self._first_plans:
|
if nonterminal not in self._first_plans:
|
||||||
@@ -57,14 +57,14 @@ class Grammar(object):
|
|||||||
# Now that we have calculated the first terminals, we are sure that
|
# Now that we have calculated the first terminals, we are sure that
|
||||||
# there is no left recursion or ambiguities.
|
# there is no left recursion or ambiguities.
|
||||||
|
|
||||||
for dfas in self._nonterminal_to_dfas.values():
|
for dfas in self.nonterminal_to_dfas.values():
|
||||||
for dfa_state in dfas:
|
for dfa_state in dfas:
|
||||||
for nonterminal, next_dfa in dfa_state.nonterminal_arcs.items():
|
for nonterminal, next_dfa in dfa_state.nonterminal_arcs.items():
|
||||||
for transition, pushes in self._first_plans[nonterminal].items():
|
for transition, pushes in self._first_plans[nonterminal].items():
|
||||||
dfa_state.ilabel_to_plan[transition] = DFAPlan(next_dfa, pushes)
|
dfa_state.ilabel_to_plan[transition] = DFAPlan(next_dfa, pushes)
|
||||||
|
|
||||||
def _calculate_first_terminals(self, nonterminal):
|
def _calculate_first_terminals(self, nonterminal):
|
||||||
dfas = self._nonterminal_to_dfas[nonterminal]
|
dfas = self.nonterminal_to_dfas[nonterminal]
|
||||||
new_first_plans = {}
|
new_first_plans = {}
|
||||||
self._first_plans[nonterminal] = None # dummy to detect left recursion
|
self._first_plans[nonterminal] = None # dummy to detect left recursion
|
||||||
# We only need to check the first dfa. All the following ones are not
|
# We only need to check the first dfa. All the following ones are not
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ class PgenParser(object):
|
|||||||
self.convert_node = convert_node
|
self.convert_node = convert_node
|
||||||
self.convert_leaf = convert_leaf
|
self.convert_leaf = convert_leaf
|
||||||
|
|
||||||
self.stack = Stack([StackNode(grammar._nonterminal_to_dfas[start_nonterminal][0])])
|
self.stack = Stack([StackNode(grammar.nonterminal_to_dfas[start_nonterminal][0])])
|
||||||
self.error_recovery = error_recovery
|
self.error_recovery = error_recovery
|
||||||
|
|
||||||
def parse(self, tokens):
|
def parse(self, tokens):
|
||||||
|
|||||||
@@ -209,7 +209,7 @@ def generate_grammar(bnf_grammar, token_namespace):
|
|||||||
)
|
)
|
||||||
dfa_state.ilabel_to_plan[transition] = DFAPlan(next_dfa)
|
dfa_state.ilabel_to_plan[transition] = DFAPlan(next_dfa)
|
||||||
|
|
||||||
return Grammar(bnf_grammar, start_nonterminal, rule_to_dfas, reserved_strings)
|
return Grammar(start_nonterminal, rule_to_dfas, reserved_strings)
|
||||||
|
|
||||||
|
|
||||||
def _make_transition(token_namespace, reserved_syntax_strings, label):
|
def _make_transition(token_namespace, reserved_syntax_strings, label):
|
||||||
|
|||||||
Reference in New Issue
Block a user