mirror of
https://github.com/davidhalter/parso.git
synced 2026-02-20 16:48:54 +08:00
Some cleanups and documentation
This commit is contained in:
@@ -175,7 +175,7 @@ class _GrammarParser():
|
|||||||
a, z = self._parse_rhs()
|
a, z = self._parse_rhs()
|
||||||
self._expect(token.NEWLINE)
|
self._expect(token.NEWLINE)
|
||||||
|
|
||||||
#_dump_nfa(a, z)
|
_dump_nfa(a, z)
|
||||||
dfa = _make_dfa(a, z)
|
dfa = _make_dfa(a, z)
|
||||||
#_dump_dfa(self._current_rule_name, dfa)
|
#_dump_dfa(self._current_rule_name, dfa)
|
||||||
# oldlen = len(dfa)
|
# oldlen = len(dfa)
|
||||||
@@ -197,21 +197,24 @@ class _GrammarParser():
|
|||||||
else:
|
else:
|
||||||
aa = NFAState(self._current_rule_name)
|
aa = NFAState(self._current_rule_name)
|
||||||
zz = NFAState(self._current_rule_name)
|
zz = NFAState(self._current_rule_name)
|
||||||
aa.add_arc(a)
|
while True:
|
||||||
z.add_arc(zz)
|
# Add the possibility to go into the state of a and come back
|
||||||
while self.value == "|":
|
# to finish.
|
||||||
self._gettoken()
|
|
||||||
a, z = self._parse_alt()
|
|
||||||
aa.add_arc(a)
|
aa.add_arc(a)
|
||||||
z.add_arc(zz)
|
z.add_arc(zz)
|
||||||
|
if self.value != "|":
|
||||||
|
break
|
||||||
|
|
||||||
|
self._gettoken()
|
||||||
|
a, z = self._parse_alt()
|
||||||
return aa, zz
|
return aa, zz
|
||||||
|
|
||||||
def _parse_alt(self):
|
def _parse_alt(self):
|
||||||
# items: item+
|
# items: item+
|
||||||
a, b = self._parse_item()
|
a, b = self._parse_item()
|
||||||
while (self.value in ("(", "[") or
|
while self.type in (token.NAME, token.STRING, token.LPAR, token.LSQB):
|
||||||
self.type in (token.NAME, token.STRING)):
|
|
||||||
c, d = self._parse_item()
|
c, d = self._parse_item()
|
||||||
|
# Need to end on the next item.
|
||||||
b.add_arc(c)
|
b.add_arc(c)
|
||||||
b = d
|
b = d
|
||||||
return a, b
|
return a, b
|
||||||
@@ -222,6 +225,8 @@ class _GrammarParser():
|
|||||||
self._gettoken()
|
self._gettoken()
|
||||||
a, z = self._parse_rhs()
|
a, z = self._parse_rhs()
|
||||||
self._expect(token.RSQB)
|
self._expect(token.RSQB)
|
||||||
|
# Make it also possible that there is no token and change the
|
||||||
|
# state.
|
||||||
a.add_arc(z)
|
a.add_arc(z)
|
||||||
return a, z
|
return a, z
|
||||||
else:
|
else:
|
||||||
@@ -230,10 +235,13 @@ class _GrammarParser():
|
|||||||
if value not in ("+", "*"):
|
if value not in ("+", "*"):
|
||||||
return a, z
|
return a, z
|
||||||
self._gettoken()
|
self._gettoken()
|
||||||
|
# Make it clear that we can go back to the old state and repeat.
|
||||||
z.add_arc(a)
|
z.add_arc(a)
|
||||||
if value == "+":
|
if value == "+":
|
||||||
return a, z
|
return a, z
|
||||||
else:
|
else:
|
||||||
|
# The end state is the same as the beginning, nothing must
|
||||||
|
# change.
|
||||||
return a, a
|
return a, a
|
||||||
|
|
||||||
def _parse_atom(self):
|
def _parse_atom(self):
|
||||||
@@ -246,6 +254,7 @@ class _GrammarParser():
|
|||||||
elif self.type in (token.NAME, token.STRING):
|
elif self.type in (token.NAME, token.STRING):
|
||||||
a = NFAState(self._current_rule_name)
|
a = NFAState(self._current_rule_name)
|
||||||
z = NFAState(self._current_rule_name)
|
z = NFAState(self._current_rule_name)
|
||||||
|
# Make it clear that the state transition requires that value.
|
||||||
a.add_arc(z, self.value)
|
a.add_arc(z, self.value)
|
||||||
self._gettoken()
|
self._gettoken()
|
||||||
return a, z
|
return a, z
|
||||||
@@ -390,6 +399,7 @@ def _make_dfa(start, finish):
|
|||||||
state.add_arc(st, label)
|
state.add_arc(st, label)
|
||||||
return states # List of DFAState instances; first one is start
|
return states # List of DFAState instances; first one is start
|
||||||
|
|
||||||
|
|
||||||
def _dump_nfa(start, finish):
|
def _dump_nfa(start, finish):
|
||||||
print("Dump of NFA for", start.from_rule)
|
print("Dump of NFA for", start.from_rule)
|
||||||
todo = [start]
|
todo = [start]
|
||||||
@@ -406,6 +416,7 @@ def _dump_nfa(start, finish):
|
|||||||
else:
|
else:
|
||||||
print(" %s -> %d" % (label, j))
|
print(" %s -> %d" % (label, j))
|
||||||
|
|
||||||
|
|
||||||
def _dump_dfa(name, dfa):
|
def _dump_dfa(name, dfa):
|
||||||
print("Dump of DFA for", name)
|
print("Dump of DFA for", name)
|
||||||
for i, state in enumerate(dfa):
|
for i, state in enumerate(dfa):
|
||||||
|
|||||||
Reference in New Issue
Block a user