From 6998d08b055ef9a3aaa43630c1260202c0867991 Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Wed, 11 Dec 2024 18:11:16 +0300 Subject: [PATCH] Add a new macro `@rulenode` Allows for convenient `RuleNode` construction in the form `RuleNode`s are already printed in (`1{2,3}` for `RuleNode(1, [RuleNode(2), RuleNode(3)]`) --- Project.toml | 2 +- src/HerbCore.jl | 1 + src/rulenode.jl | 41 +++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 17 +++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 4c4453f..6f8e1ed 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "HerbCore" uuid = "2b23ba43-8213-43cb-b5ea-38c12b45bd45" authors = ["Jaap de Jong ", "Nicolae Filat ", "Tilman Hinnerichs ", "Sebastijan Dumancic "] -version = "0.3.2" +version = "0.3.3" [compat] julia = "^1.8" diff --git a/src/HerbCore.jl b/src/HerbCore.jl index 844cac5..039c069 100644 --- a/src/HerbCore.jl +++ b/src/HerbCore.jl @@ -7,6 +7,7 @@ include("grammar.jl") export AbstractRuleNode, RuleNode, + @rulenode, AbstractHole, AbstractUniformHole, UniformHole, diff --git a/src/rulenode.jl b/src/rulenode.jl index 0f02447..08c46ad 100644 --- a/src/rulenode.jl +++ b/src/rulenode.jl @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index 06d0c01..245cde8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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()