Skip to content

Commit 2291d29

Browse files
committed
made _populate_sources and _rec_remove_sources in grammar.py iterative
1 parent e12d60b commit 2291d29

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

src/fandango/language/grammar/grammar.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,22 @@ def populate_sources(self, tree: DerivationTree) -> None:
166166
self._populate_sources(tree)
167167

168168
def _populate_sources(self, tree: DerivationTree) -> None:
169-
if self.is_use_generator(tree):
170-
tree.sources = self.derive_sources(tree)
171-
for child in tree.children:
172-
child.set_all_read_only(True)
173-
return
174-
for child in tree.children:
175-
self._populate_sources(child)
169+
stack = [tree]
170+
while stack:
171+
node = stack.pop()
172+
if self.is_use_generator(node):
173+
node.sources = self.derive_sources(node)
174+
for child in node.children:
175+
child.set_all_read_only(True)
176+
continue
177+
stack.extend(reversed(node.children))
176178

177179
def _rec_remove_sources(self, tree: DerivationTree) -> None:
178-
tree.sources = []
179-
for child in tree.children:
180-
self._rec_remove_sources(child)
180+
stack = [tree]
181+
while stack:
182+
node = stack.pop()
183+
node.sources = []
184+
stack.extend(node.children)
181185

182186
def generate_string(
183187
self,

0 commit comments

Comments
 (0)