Skip to content

Commit

Permalink
Add third depth param to visitor funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
igordejanovic committed Dec 20, 2021
1 parent c7d14d0 commit 0771ca5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
5 changes: 3 additions & 2 deletions docs/parse_forest_trees.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@ Where:
callable should return an iterator yielding children nodes.
- `visit` is a function called when the node is visited. It results will be
passed into visitors higher in the hierarchy (thus enabling bottom-up
processing). `visit` function should accept a node and sub-results from
lower-level visitors.
processing). `visit` function should accept three parameters: current tree
node, sub-results from lower-level visitors and the depth of the current tree
node.
- `memoize` - Should results be cached. Handy for direct acyclic graphs if we
want to prevent multiple calculation of the same sub-graph.
- `check_cycle` - If set to `True` will prevent traversing of cyclic structure
Expand Down
4 changes: 2 additions & 2 deletions parglare/glr.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ def iter_non_visited(n, collection):
else:
return iter([])

def calculate(node, subresults):
def calculate(node, subresults, _):
amb = 0
if isinstance(node, Parent) and len(node.possibilities) > 1:
amb = 1
Expand All @@ -752,7 +752,7 @@ def iterator(node):
else:
return iter([])

def calculate(node, subresults):
def calculate(node, subresults, _):
if isinstance(node, Parent):
return sum(subresults)
else:
Expand Down
31 changes: 16 additions & 15 deletions parglare/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,23 @@ def _iter():
def to_str(root):
from parglare.glr import Parent

def visit(n, subresults):
def visit(n, subresults, depth):
indent = ' ' * depth
if isinstance(n, Parent):
s = f'{n.head.symbol} - ambiguity[{n.ambiguity}]'
s = f'{indent}{n.head.symbol} - ambiguity[{n.ambiguity}]'
for idx, p in enumerate(subresults):
s += f'\n{idx+1}:' + p
s += f'\n{indent}{idx+1}:{p}'
elif n.is_nonterm():
s = '{}[{}->{}]'.format(n.production.symbol,
n.start_position,
n.end_position)
s = '{}{}[{}->{}]'.format(indent, n.production.symbol,
n.start_position,
n.end_position)
if subresults:
s += '\n ' + '\n'.join(subresults).replace('\n', '\n ')
s = '{}\n{}'.format(s, '\n'.join(subresults))
else:
s = '{}[{}->{}, "{}"]'.format(n.symbol,
n.start_position,
n.end_position,
n.value)
s = '{}{}[{}->{}, "{}"]'.format(indent, n.symbol,
n.start_position,
n.end_position,
n.value)
return s
return visitor(root, tree_node_iterator, visit)

Expand All @@ -63,7 +64,7 @@ def to_dot(self, positions=True):
rendered = set()
terminals = []

def visit(n, subresults):
def visit(n, subresults, _):
sub_str = ''.join(s[1] for s in subresults if id(s[1]) not in rendered)
rendered.update((id(s[1]) for s in subresults))
pos = f'[{n.start_position}-{n.end_position}]' if positions else ''
Expand Down Expand Up @@ -307,7 +308,7 @@ def tree_iterator(n):
else:
return iter([])

def visit(n, subresults):
def visit(n, subresults, _):
if isinstance(n, Parent):
return subresults[0]
elif n.is_nonterm():
Expand Down Expand Up @@ -338,7 +339,7 @@ def disambiguate(self, disamfun):
def tree_iterator(n):
return iter(n)

def visit(n, _):
def visit(n, _, __):
if isinstance(n, Parent) and len(n.possibilities) > 1:
disamfun(n)

Expand Down Expand Up @@ -394,7 +395,7 @@ def visitor(root, iterator, visit, memoize=True, check_cycle=False):
stack.pop()
if check_cycle:
visiting.remove(id(node))
result = visit(node, results)
result = visit(node, results, len(stack))
if memoize:
cache[id(node)] = result
if stack:
Expand Down
2 changes: 1 addition & 1 deletion tests/func/grammar/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def layout_action(context, _):
tree = result[0]
content = set()

def collect(n, _):
def collect(n, _, __):
content.add(n.layout_content_ahead)

visitor(tree, lambda n: iter(n.children or []), collect)
Expand Down

0 comments on commit 0771ca5

Please sign in to comment.