Skip to content

Commit 025301a

Browse files
committed
make parser yield trees only at the end in one step
1 parent 3e5eb3a commit 025301a

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

src/fandango/language/grammar/parser/forest.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,22 +163,22 @@ def open_frame(target: ParseState) -> None:
163163
self._children_keepalive.append(frame.state)
164164
return resolve(state)
165165

166-
def all_children_of(self, state: ParseState) -> Iterator[DerivationTree]:
166+
def derivations_of(self, state: ParseState) -> Iterator[DerivationTree]:
167167
"""
168-
The children of `state` in every derivation, the first derivation first.
168+
The trees for all derivations of `state` on-demand.
169169
"""
170-
choices = _Choices([])
171-
yield from self.children_of(state, choices)
172-
picks = [0] * len(choices.edge_counts)
170+
picks: list[int] = []
173171
while True:
172+
choices = _Choices(picks)
173+
# Returns a list with exactly one element. We take it.
174+
(tree,) = self.children_of(state, choices)
175+
yield tree
176+
picks += [0] * (len(choices.edge_counts) - len(picks))
174177
while picks and picks[-1] + 1 == choices.edge_counts[len(picks) - 1]:
175178
picks.pop()
176179
if not picks:
177180
return
178181
picks[-1] += 1
179-
choices = _Choices(picks)
180-
yield from self.children_of(state, choices)
181-
picks += [0] * (len(choices.edge_counts) - len(picks))
182182

183183
def _is_ambiguous(self, state: ParseState) -> bool:
184184
"""

src/fandango/language/grammar/parser/iterative_parser.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,8 @@ def tree_at(self, offset: int) -> Generator[DerivationTree, None, None]:
499499
state = self._completed.get(offset)
500500
if state is None:
501501
return
502-
for child in self._forest.all_children_of(state):
503-
yield self.to_derivation_tree(child)
502+
for tree in self._forest.derivations_of(state):
503+
yield self.to_derivation_tree(tree)
504504

505505
def _consume(
506506
self, char: str | bytes | int, *, build_trees: bool = True
@@ -594,12 +594,12 @@ def _consume(
594594

595595
if build_trees:
596596
for state in starts_to_yield:
597-
for child in self._forest.all_children_of(state):
597+
for tree in self._forest.derivations_of(state):
598598
if self._parsing_mode == ParsingMode.INCOMPLETE:
599-
if child in self._yielded_incomplete:
599+
if tree in self._yielded_incomplete:
600600
continue
601-
self._yielded_incomplete.add(child)
602-
yield child, state.is_finished
601+
self._yielded_incomplete.add(tree)
602+
yield tree, state.is_finished
603603

604604
column_index += 1
605605
if column_index % columns_per_byte == 0:

0 commit comments

Comments
 (0)