Skip to content

Commit 0e06cfd

Browse files
feat(tree): add vis method for printing tree in address order and update ParseResult to utilise it
1 parent 6edad69 commit 0e06cfd

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/dylan/tree/tree.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ def clone(self) -> Tree:
7575
"""Deep copy nodes, pointer, and variable pools (Java ``Tree.clone`` sketch)."""
7676
return Tree(self)
7777

78+
def vis(self) -> None:
79+
"""Print this tree in address order (same plain-text view as :meth:`~dynamicsyntax.parse_result.ParseResult.vis`)."""
80+
from dylan.gui.formatting import format_ds_tree
81+
82+
print(format_ds_tree(self))
83+
7884
def set_pointer(self, addr: NodeAddress) -> None:
7985
"""Set the pointer address (Java ``setPointer``)."""
8086
self.pointer = addr

src/dynamicsyntax/parse_result.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def vis(self) -> None:
4848
if self.tree is None:
4949
print("(no parse tree)")
5050
return
51-
print(format_ds_tree(self.tree))
51+
self.tree.vis()
5252

5353
def get_vocab(
5454
self,

tests/test_dynamicsyntax_parse.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ def test_vis_prints_address_order(capsys: pytest.CaptureFixture[str]) -> None:
176176
assert "00" in out and "man" in out.lower()
177177

178178

179+
def test_tree_vis_matches_parse_result_vis(capsys: pytest.CaptureFixture[str]) -> None:
180+
"""``Tree.vis`` prints the same address-order text as ``ParseResult.vis`` (e.g. after ``complete_tree``)."""
181+
p = ds.parse("a man arrives", "ttr")
182+
assert p.tree is not None
183+
p.vis()
184+
from_parse_result = capsys.readouterr().out
185+
p.tree.vis()
186+
from_tree = capsys.readouterr().out
187+
assert from_parse_result == from_tree
188+
189+
179190
def test_parse_trace_snapshots() -> None:
180191
"""``trace=True`` records one tree per word plus the initial state."""
181192
p = ds.parse("a man arrives", "ttr", trace=True)

0 commit comments

Comments
 (0)