- def __init__(self, filename, stream=None):
+ filename: Path
+ stream: IO[Text]
+ generator: Iterator[GoodTokenInfo]
+ first: Dict[Text, Optional[Dict[Text, int]]]
+
+ def __init__(self, filename: Path, stream: Optional[IO[Text]] = None) -> None:
self.dfas, self.startsymbol = self.parse()
if close_stream is not None:
close_stream()
self.dfas, self.startsymbol = self.parse()
if close_stream is not None:
close_stream()
- def make_first(self, c, name):
+ def make_first(self, c: PgenGrammar, name: Text) -> Dict[int, int]:
first = {}
for label in sorted(rawfirst):
ilabel = self.make_label(c, label)
first = {}
for label in sorted(rawfirst):
ilabel = self.make_label(c, label)
- raise ValueError("rule %s is ambiguous; %s is in the"
- " first sets of %s as well as %s" %
- (name, symbol, label, inverse[symbol]))
+ raise ValueError(
+ "rule %s is ambiguous; %s is in the first sets of %s as well"
+ " as %s"
+ % (name, symbol, label, inverse[symbol])
+ )
# MSTART: (NEWLINE | RULE)* ENDMARKER
while self.type != token.ENDMARKER:
while self.type == token.NEWLINE:
# MSTART: (NEWLINE | RULE)* ENDMARKER
while self.type != token.ENDMARKER:
while self.type == token.NEWLINE:
self.expect(token.OP, ":")
a, z = self.parse_rhs()
self.expect(token.NEWLINE)
self.expect(token.OP, ":")
a, z = self.parse_rhs()
self.expect(token.NEWLINE)
# To turn an NFA into a DFA, we define the states of the DFA
# to correspond to *sets* of states of the NFA. Then do some
# state reduction. Let's represent sets as dicts with 1 for
# values.
assert isinstance(start, NFAState)
assert isinstance(finish, NFAState)
# To turn an NFA into a DFA, we define the states of the DFA
# to correspond to *sets* of states of the NFA. Then do some
# state reduction. Let's represent sets as dicts with 1 for
# values.
assert isinstance(start, NFAState)
assert isinstance(finish, NFAState)
for nfastate in state.nfaset:
for label, next in nfastate.arcs:
if label is not None:
for nfastate in state.nfaset:
for label, next in nfastate.arcs:
if label is not None:
- def dump_nfa(self, name, start, finish):
+ def dump_nfa(self, name: Text, start: "NFAState", finish: "NFAState") -> None:
print("Dump of NFA for", name)
todo = [start]
for i, state in enumerate(todo):
print("Dump of NFA for", name)
todo = [start]
for i, state in enumerate(todo):
print("Dump of DFA for", name)
for i, state in enumerate(dfa):
print(" State", i, state.isfinal and "(final)" or "")
for label, next in sorted(state.arcs.items()):
print(" %s -> %d" % (label, dfa.index(next)))
print("Dump of DFA for", name)
for i, state in enumerate(dfa):
print(" State", i, state.isfinal and "(final)" or "")
for label, next in sorted(state.arcs.items()):
print(" %s -> %d" % (label, dfa.index(next)))
# This is not theoretically optimal, but works well enough.
# Algorithm: repeatedly look for two states that have the same
# set of arcs (same labels pointing to the same nodes) and
# This is not theoretically optimal, but works well enough.
# Algorithm: repeatedly look for two states that have the same
# set of arcs (same labels pointing to the same nodes) and
- while (self.value in ("(", "[") or
- self.type in (token.NAME, token.STRING)):
+ while self.value in ("(", "[") or self.type in (token.NAME, token.STRING):
- self.raise_error("expected (...) or NAME or STRING, got %s/%s",
- self.type, self.value)
+ self.raise_error(
+ "expected (...) or NAME or STRING, got %s/%s", self.type, self.value
+ )
+ assert False
- self.raise_error("expected %s/%s, got %s/%s",
- type, value, self.type, self.value)
+ self.raise_error(
+ "expected %s/%s, got %s/%s", type, value, self.type, self.value
+ )
tup = next(self.generator)
while tup[0] in (tokenize.COMMENT, tokenize.NL):
tup = next(self.generator)
self.type, self.value, self.begin, self.end, self.line = tup
tup = next(self.generator)
while tup[0] in (tokenize.COMMENT, tokenize.NL):
tup = next(self.generator)
self.type, self.value, self.begin, self.end, self.line = tup
- raise SyntaxError(msg, (self.filename, self.end[0],
- self.end[1], self.line))
+ raise SyntaxError(msg, (self.filename, self.end[0], self.end[1], self.line))
+
assert label is None or isinstance(label, str)
assert isinstance(next, NFAState)
self.arcs.append((label, next))
assert label is None or isinstance(label, str)
assert isinstance(next, NFAState)
self.arcs.append((label, next))
- def __init__(self, nfaset, final):
+ def __init__(self, nfaset: Dict[NFAState, Any], final: NFAState) -> None:
assert isinstance(nfaset, dict)
assert isinstance(next(iter(nfaset)), NFAState)
assert isinstance(final, NFAState)
self.nfaset = nfaset
self.isfinal = final in nfaset
assert isinstance(nfaset, dict)
assert isinstance(next(iter(nfaset)), NFAState)
assert isinstance(final, NFAState)
self.nfaset = nfaset
self.isfinal = final in nfaset
assert isinstance(label, str)
assert label not in self.arcs
assert isinstance(next, DFAState)
self.arcs[label] = next
assert isinstance(label, str)
assert label not in self.arcs
assert isinstance(next, DFAState)
self.arcs[label] = next
# Equality test -- ignore the nfaset instance variable
assert isinstance(other, DFAState)
if self.isfinal != other.isfinal:
# Equality test -- ignore the nfaset instance variable
assert isinstance(other, DFAState)
if self.isfinal != other.isfinal: