Skip to content

Commit

Permalink
Add a new macro @rulenode
Browse files Browse the repository at this point in the history
Allows for convenient `RuleNode` construction
in the form `RuleNode`s are already printed in
(`1{2,3}` for `RuleNode(1, [RuleNode(2), RuleNode(3)]`)
  • Loading branch information
ReubenJ committed Dec 11, 2024
1 parent 2d5dcc6 commit 6998d08
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "HerbCore"
uuid = "2b23ba43-8213-43cb-b5ea-38c12b45bd45"
authors = ["Jaap de Jong <[email protected]>", "Nicolae Filat <[email protected]>", "Tilman Hinnerichs <[email protected]>", "Sebastijan Dumancic <[email protected]>"]
version = "0.3.2"
version = "0.3.3"

[compat]
julia = "^1.8"
Expand Down
1 change: 1 addition & 0 deletions src/HerbCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ include("grammar.jl")
export
AbstractRuleNode,
RuleNode,
@rulenode,
AbstractHole,
AbstractUniformHole,
UniformHole,
Expand Down
41 changes: 41 additions & 0 deletions src/rulenode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,47 @@ Create a [`RuleNode`](@ref) for the [`AbstractGrammar`](@ref) rule with index `i
"""
RuleNode(ind::Int, children::Vector{<:AbstractRuleNode}) = RuleNode(ind, nothing, children)

function _shorthand2rulenode(i::Integer)
:(RuleNode($i))
end

function _shorthand2rulenode(ex::Expr)
@assert ex.head==:curly "Input to the @rulenode macro should be in the form of: 1{2,3}"
:(RuleNode(
# Interpolate the _value_ of the first rule (1 from 1{2,3})
# into the first argument of the RuleNode constructor
$(ex.args[1]),
[
# Fill in the array of children recursively with the contents of the
# curly brackets (2 and 3 from 1{2,3})
$([_shorthand2rulenode(child) for child in ex.args[2:end]]...)
]
))
end

"""
@rulenode
Construct a [`RuleNode`](@ref) using the shorthand notation [`RuleNode`](@ref)s are printed
with using [`Base.show`](@ref). Does not support [`AbstractHole`](@ref)s.
# Examples
```jldoctest
julia> @rulenode 1{4{5,6},1{2,3}}
1{4{5,6},1{2,3}}
julia> @rulenode 1
1
julia> @rulenode 1{2, 3}
1{2,3}
```
"""
macro rulenode(ex::Union{Integer, Expr})
_shorthand2rulenode(ex)
end

"""
RuleNode(ind::Int, _val::Any)
Expand Down
17 changes: 17 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,23 @@ using Test
@test hasdynamicvalue(Hole(BitVector((1, 0)))) == false
end

@testset "@rulenode" begin
node = @rulenode 1{2, 3}
children = get_children(node)
@test get_rule(node) == 1
@test get_rule(children[1]) == 2
@test get_rule(children[2]) == 3

node = @rulenode 1
children = get_children(node)
@test get_rule(node) == 1
@test isempty(children)

node = @rulenode 1{4{5, 6}, 1{2, 3}}
@test get_rule(node) == 1
@test depth(node) == 3
end

@testset "show" begin
node = RuleNode(1, [RuleNode(1), RuleNode(2), RuleNode(3)])
io = IOBuffer()
Expand Down

0 comments on commit 6998d08

Please sign in to comment.