Herb-AI / HerbCore.jl

Core structures for Herb.jl
https://herb-ai.github.io/
MIT License
0 stars 1 forks source link

Implement the `AbstractTrees` interface for `RuleNode`s #23

Open ReubenJ opened 5 months ago

ReubenJ commented 5 months ago

Implementing the interface (described here) would give us access to useful functions, especially print_tree(...). The most basic implementation would only consist of defining

AbstractTrees.children(node::AbstractRuleNode) = get_children(node)

Out of the box, this would allow for

r = RuleNode(1, [RuleNode(2), RuleNode(2), RuleNode(3, [RuleNode(5)])])

to be displayed by

julia> AbstractTrees.print_tree(r)
1{2,2,3{5}}
├─ 2,
├─ 2,
└─ 3{5}
   └─ 5,

and with a little extra tweaking

julia> AbstractTrees.print_tree((io, x) -> print(io, get_rule(x)), stdout, r)
1
├─ 2
├─ 2
└─ 3
   └─ 5

The current, default, output (Base.show(...)) looks like this.

1{2,2,3{5}}

We could add the tree pretty-printing as an alternative or replace Base.show(...) for rule nodes with this.

I don't know whether printing the actual expressions using the grammar (instead of the rule number seen above) is straightforward with this API, but it would be nice to have that as well.

ReubenJ commented 5 months ago

Just found that this is somewhat addressed in HerbGrammar here, but it seems like this functionality should be here in HerbCore as it's only concerned with RuleNodes.

We should probably also expand this to AbstractRuleNodes as well.