Skip to content

Commit

Permalink
Fix AST to_str
Browse files Browse the repository at this point in the history
  • Loading branch information
igordejanovic committed Feb 24, 2023
1 parent a1cbd35 commit 6a99f96
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ backward incompatible changes will start to apply when the projects goes 1.0

## [Unreleased]

### Fixed
- Fix AST `to_str` in the context of syntactic sugar (BNF extensions).


## [0.16.0] (released: 2022-07-16)

### Added
Expand Down
14 changes: 12 additions & 2 deletions parglare/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1694,8 +1694,14 @@ def visit(n, subresults, depth):
n._pg_children_names[i],
subresult)
for (i, subresult) in enumerate(subresults)]))
elif isinstance(n, list):
s = '{}[\n{}\n{}]'.format(
indent,
'\n'.join(['{}{}'.format(indent, el)
for el in subresults]),
indent)
else:
s = str(n)
s = repr(n)
return s

return visitor(self, ast_tree_iterator, visit)
Expand Down Expand Up @@ -1966,4 +1972,8 @@ def __repr__(cls):


def ast_tree_iterator(root):
return iter(root._pg_children) if hasattr(root, '_pg_children') else iter([])
if hasattr(root, '_pg_children'):
return iter(root._pg_children)
if isinstance(root, list):
return iter(root)
return iter([])
45 changes: 41 additions & 4 deletions tests/func/parsing/test_to_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,48 @@ def test_ast_to_str():

result = parser.parse('1 2 3 3 3 4 2')
print(result.to_str())
assert result.to_str().strip() == '''
assert result.to_str().strip() == """
S [0->13]
second=Second [2->3]
val=2
third=['3', '3', '3']
val='2'
third= [
'3'
'3'
'3'
]
fourth=Fourth [10->13]
val=Second [12->13]
val=2'''.strip()
val='2'""".strip()


def test_ast_to_str_with_bnf_extensions():
"""
Tests `to_str` with lists returned by BNF extensions.
"""
grammar = r"""
S: "1" second=Second third=Third+ fourth=Fourth;
Second: val="2";
Third: val="3";
Fourth: "4" val=Second;
"""

g = Grammar.from_string(grammar)
parser = Parser(g)

result = parser.parse('1 2 3 3 3 4 2')
print(result.to_str())
assert result.to_str().strip() == """
S [0->13]
second=Second [2->3]
val='2'
third= [
Third [4->5]
val='3'
Third [6->7]
val='3'
Third [8->9]
val='3'
]
fourth=Fourth [10->13]
val=Second [12->13]
val='2'""".strip()

0 comments on commit 6a99f96

Please sign in to comment.