From 1ba5e60e16d3d2a371e46063d70fed7ca21f0ccb Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:56:47 +0100 Subject: [PATCH 01/11] Add Aqua test --- Project.toml | 6 ------ test/Project.toml | 4 ++++ test/runtests.jl | 2 ++ 3 files changed, 6 insertions(+), 6 deletions(-) create mode 100644 test/Project.toml diff --git a/Project.toml b/Project.toml index b473c9c..d6d65cd 100644 --- a/Project.toml +++ b/Project.toml @@ -16,9 +16,3 @@ DataStructures = "0.17,0.18" HerbCore = "^0.3.0" TreeView = "^0.5" julia = "^1.8" - -[extras] -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[targets] -test = ["Test"] diff --git a/test/Project.toml b/test/Project.toml new file mode 100644 index 0000000..b90b5b4 --- /dev/null +++ b/test/Project.toml @@ -0,0 +1,4 @@ +[deps] +Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" +HerbCore = "2b23ba43-8213-43cb-b5ea-38c12b45bd45" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/runtests.jl b/test/runtests.jl index 1a7221f..e346f1e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,9 +1,11 @@ +using Aqua using HerbCore using HerbGrammar using Test @testset "HerbGrammar.jl" verbose=true begin + @testset "Aqua.jl Checks" Aqua.test_all(HerbGrammar) include("test_csg.jl") include("test_rulenode_operators.jl") include("test_rulenode2expr.jl") From fe2706fbd4f255bcf30d1b03b5f13cb1879329dc Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Thu, 14 Nov 2024 16:07:02 +0100 Subject: [PATCH 02/11] Remove non-existent symbols from export --- src/HerbGrammar.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/HerbGrammar.jl b/src/HerbGrammar.jl index 7914c0d..4f45df4 100644 --- a/src/HerbGrammar.jl +++ b/src/HerbGrammar.jl @@ -18,9 +18,6 @@ include("csg/probabilistic_csg.jl") include("grammar_io.jl") export - ContextFree, - ContextSensitive, - ContextSensitiveGrammar, AbstractRuleNode, RuleNode, @@ -59,7 +56,6 @@ export SymbolTable, - change_expr, rulenode2expr, rulenode_log_probability, From 3ecc44b67b2357479322281f70dc9933da123a09 Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:15:59 +0100 Subject: [PATCH 03/11] Move pirated functions reported by Aqua to `HerbCore` --- Project.toml | 5 ++--- src/HerbGrammar.jl | 1 - src/grammar_base.jl | 8 -------- src/rulenode_operators.jl | 14 -------------- src/utils.jl | 11 +++-------- 5 files changed, 5 insertions(+), 34 deletions(-) diff --git a/Project.toml b/Project.toml index d6d65cd..31d5844 100644 --- a/Project.toml +++ b/Project.toml @@ -1,18 +1,17 @@ name = "HerbGrammar" uuid = "4ef9e186-2fe5-4b24-8de7-9f7291f24af7" authors = ["Sebastijan Dumancic ", "Jaap de Jong ", "Nicolae Filat ", "Piotr Cichoń "] -version = "0.4.2" +version = "0.5.0" [deps] -AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" HerbCore = "2b23ba43-8213-43cb-b5ea-38c12b45bd45" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" TreeView = "39424ebd-4cf3-5550-a685-96706a953f40" [compat] -AbstractTrees = "^0.4" DataStructures = "0.17,0.18" HerbCore = "^0.3.0" +Serialization = "^1.8,^1.10,^1.11" TreeView = "^0.5" julia = "^1.8" diff --git a/src/HerbGrammar.jl b/src/HerbGrammar.jl index 4f45df4..844960a 100644 --- a/src/HerbGrammar.jl +++ b/src/HerbGrammar.jl @@ -1,7 +1,6 @@ module HerbGrammar import TreeView: walk_tree -using AbstractTrees using DataStructures # NodeRecycler using Serialization # grammar_io diff --git a/src/grammar_base.jl b/src/grammar_base.jl index 8258b15..95c8f32 100644 --- a/src/grammar_base.jl +++ b/src/grammar_base.jl @@ -45,7 +45,6 @@ function get_childtypes(rule::Any, types::AbstractVector{Symbol}) return retval end -Base.getindex(grammar::AbstractGrammar, typ::Symbol) = grammar.bytype[typ] """ nonterminals(grammar::AbstractGrammar)::Vector{Symbol} @@ -183,13 +182,6 @@ Returns the maximum arity (number of children) over all production rules in the max_arity(grammar::AbstractGrammar)::Int = maximum(length(cs) for cs in grammar.childtypes) -function Base.show(io::IO, grammar::AbstractGrammar) - for i in eachindex(grammar.rules) - println(io, i, ": ", grammar.types[i], " = ", grammar.rules[i]) - end -end - - """ add_rule!(g::AbstractGrammar, e::Expr) diff --git a/src/rulenode_operators.jl b/src/rulenode_operators.jl index 73fc82e..e28863b 100644 --- a/src/rulenode_operators.jl +++ b/src/rulenode_operators.jl @@ -1,8 +1,3 @@ -HerbCore.RuleNode(ind::Int, grammar::AbstractGrammar) = RuleNode(ind, nothing, [Hole(get_domain(grammar, type)) for type ∈ grammar.childtypes[ind]]) -HerbCore.RuleNode(ind::Int, _val::Any, grammar::AbstractGrammar) = RuleNode(ind, _val, [Hole(get_domain(grammar, type)) for type ∈ grammar.childtypes[ind]]) - -HerbCore.UniformHole(domain::BitVector, grammar::AbstractGrammar) = UniformHole(domain, [Hole(get_domain(grammar, type)) for type ∈ grammar.childtypes[findfirst(domain)]]) - rulesoftype(::Hole, ::Set{Int}) = Set{Int}() """ @@ -514,12 +509,3 @@ function contains_returntype(node::RuleNode, grammar::AbstractGrammar, sym::Symb end return false end - -function Base.display(rulenode::RuleNode, grammar::AbstractGrammar) - root = rulenode2expr(rulenode, grammar) - if isa(root, Expr) - walk_tree(root) - else - root - end -end diff --git a/src/utils.jl b/src/utils.jl index 3eaf4b2..be853ba 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1,8 +1,3 @@ -# Interface to AbstractTrees.jl -AbstractTrees.children(node::RuleNode) = node.children -AbstractTrees.printnode(io::IO, node::RuleNode) = print(io, node.ind) - - """ mindepth_map(grammar::AbstractGrammar) @@ -45,15 +40,15 @@ end Data structure for mapping terminal symbols in the [`AbstractGrammar`](@ref) to their Julia interpretation. """ -const SymbolTable = Dict{Symbol,Any} +const SymbolTable{I} = Dict{Symbol,I} """ - SymbolTable(grammar::AbstractGrammar, mod::Module=Main) + grammar2symboltable(grammar::AbstractGrammar, mod::Module=Main) Returns a [`SymbolTable`](@ref) populated with a mapping from symbols in the [`AbstractGrammar`](@ref) to symbols in module `mod` or `Main`, if defined. """ -function HerbGrammar.SymbolTable(grammar::AbstractGrammar, mod::Module=Main) +function grammar2symboltable(grammar::AbstractGrammar, mod::Module=Main) tab = SymbolTable() for rule in grammar.rules _add_to_symboltable!(tab, rule, mod) From 4e27d43dce639baa8c2e5ef8ea5baf058bb4d175 Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:39:55 +0100 Subject: [PATCH 04/11] Add `grammar2symboltable` test Currently broken --- src/HerbGrammar.jl | 1 + src/utils.jl | 2 +- test/runtests.jl | 1 + test/test_utils.jl | 10 ++++++++++ 4 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 test/test_utils.jl diff --git a/src/HerbGrammar.jl b/src/HerbGrammar.jl index 844960a..6f60903 100644 --- a/src/HerbGrammar.jl +++ b/src/HerbGrammar.jl @@ -54,6 +54,7 @@ export expr2pcsgrammar, SymbolTable, + grammar2symboltable, rulenode2expr, rulenode_log_probability, diff --git a/src/utils.jl b/src/utils.jl index be853ba..e4158fd 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -40,7 +40,7 @@ end Data structure for mapping terminal symbols in the [`AbstractGrammar`](@ref) to their Julia interpretation. """ -const SymbolTable{I} = Dict{Symbol,I} +const SymbolTable = Dict{Symbol,Any} """ grammar2symboltable(grammar::AbstractGrammar, mod::Module=Main) diff --git a/test/runtests.jl b/test/runtests.jl index e346f1e..81fd98d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -10,4 +10,5 @@ using Test include("test_rulenode_operators.jl") include("test_rulenode2expr.jl") include("test_expr2rulenode.jl") + include("test_utils.jl") end diff --git a/test/test_utils.jl b/test/test_utils.jl new file mode 100644 index 0000000..2d22f72 --- /dev/null +++ b/test/test_utils.jl @@ -0,0 +1,10 @@ +@testset "SymbolTable Tests" begin + x = 1 + g₁ = @cfgrammar begin + Real = |(1:9) + Real = x + end + + st = grammar2symboltable(g₁) + @test !isnothing(st[:x]) +end \ No newline at end of file From e0166b1806b5b8c23bdd2a21a42e7584833829de Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Tue, 26 Nov 2024 19:39:53 +0300 Subject: [PATCH 05/11] Explicitly deprecate `SymbolTable` constructor --- src/utils.jl | 6 +++++- test/runtests.jl | 5 ++--- test/test_utils.jl | 15 ++++++++++----- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index e4158fd..7200c9b 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -38,7 +38,8 @@ end """ SymbolTable -Data structure for mapping terminal symbols in the [`AbstractGrammar`](@ref) to their Julia interpretation. +Type alias for a `Dict` that maps terminal symbols in the [`AbstractGrammar`](@ref) +to their Julia interpretation. """ const SymbolTable = Dict{Symbol,Any} @@ -56,6 +57,9 @@ function grammar2symboltable(grammar::AbstractGrammar, mod::Module=Main) tab end +# When we eventually remove this deprecation, also remove `SymbolTables` from +# the `treat_as_own` option in `test/runtests.jl` +@deprecate SymbolTable(g::AbstractGrammar, m::Module) grammar2symboltable(g, m) _add_to_symboltable!(tab::SymbolTable, rule::Any, mod::Module) = true diff --git a/test/runtests.jl b/test/runtests.jl index 81fd98d..52b5a4c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,9 +3,8 @@ using HerbCore using HerbGrammar using Test - -@testset "HerbGrammar.jl" verbose=true begin - @testset "Aqua.jl Checks" Aqua.test_all(HerbGrammar) +@testset "HerbGrammar.jl" verbose = true begin + @testset "Aqua.jl Checks" Aqua.test_all(HerbGrammar; piracies=(treat_as_own=[SymbolTable],)) include("test_csg.jl") include("test_rulenode_operators.jl") include("test_rulenode2expr.jl") diff --git a/test/test_utils.jl b/test/test_utils.jl index 2d22f72..0ed9523 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -1,10 +1,15 @@ -@testset "SymbolTable Tests" begin +module DefiningAVariable x = 1 - g₁ = @cfgrammar begin +end +@testset "SymbolTable Tests" begin + g = @cfgrammar begin Real = |(1:9) Real = x end - st = grammar2symboltable(g₁) - @test !isnothing(st[:x]) -end \ No newline at end of file + st = grammar2symboltable(g, DefiningAVariable) + @test st[:x] == 1 + + st = SymbolTable(g, DefiningAVariable) + @test st[:x] == 1 +end From 82f620bc14609fe556825258c98e4d137dbdc58d Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Tue, 26 Nov 2024 19:41:38 +0300 Subject: [PATCH 06/11] Re-add empty-child RuleNode and UniformHole constructors --- src/HerbGrammar.jl | 8 +++-- src/rulenode_operators.jl | 69 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/HerbGrammar.jl b/src/HerbGrammar.jl index 6f60903..7dcae40 100644 --- a/src/HerbGrammar.jl +++ b/src/HerbGrammar.jl @@ -7,13 +7,14 @@ using Serialization # grammar_io using HerbCore include("grammar_base.jl") -include("rulenode_operators.jl") include("utils.jl") include("nodelocation.jl") include("csg/csg.jl") include("csg/probabilistic_csg.jl") +include("rulenode_operators.jl") + include("grammar_io.jl") export @@ -68,6 +69,9 @@ export read_pcsg, add_rule!, remove_rule!, - cleanup_removed_rules! + cleanup_removed_rules!, + holes_from_child_types, + rulenode_with_empty_children, + uniform_hole_with_empty_children end # module HerbGrammar diff --git a/src/rulenode_operators.jl b/src/rulenode_operators.jl index e28863b..9abdedf 100644 --- a/src/rulenode_operators.jl +++ b/src/rulenode_operators.jl @@ -1,3 +1,72 @@ +""" + holes_from_child_types(index::Integer, grammar::ContextSensitiveGrammar) + +Given the `index` of a rule in a `grammar`, create a vector of [`Hole`](@ref)s +corresponding to the children of the rule at `index`. +""" +function holes_from_child_types(index::Integer, grammar::ContextSensitiveGrammar) + return [Hole(get_domain(grammar, type)) for type in grammar.childtypes[index]] +end + +""" + RuleNode(ind::Int, grammar::ContextSensitiveGrammar) + +Create a [`RuleNode`](@ref) with [`Hole`](@ref)s as children. The holes +are initialized with the types of the children of the rule at `ind`. + +# Examples +```jldoctest +julia> g = @csgrammar begin + A = 1 | 2 | 3 + B = A + A + end +1: A = 1 +2: A = 2 +3: A = 3 +4: B = A + A + + +julia> rulenode_with_empty_children(4, g) +4{hole[Bool[1, 1, 1, 0]],hole[Bool[1, 1, 1, 0]]} +``` +""" +function rulenode_with_empty_children(ind::Int, _val::Union{Any,Nothing}, grammar::ContextSensitiveGrammar) + child_holes = holes_from_child_types(ind, grammar) + return RuleNode(ind, _val, child_holes) +end + +function rulenode_with_empty_children(ind::Int, grammar::ContextSensitiveGrammar) + return rulenode_with_empty_children(ind, nothing, grammar) +end + +""" + uniform_hole_with_empty_children(domain::BitVector, grammar::AbstractGrammar) + +Create a [`UniformHole`](@ref) with [`Hole`](@ref)s as children. The holes +are initialized with the types of the children of the rule at `ind`. + +# Examples +```jldoctest +julia> g = @csgrammar begin + A = 1 | 2 | 3 + B = (A + A) | (A - A) + end +1: A = 1 +2: A = 2 +3: A = 3 +4: B = A + A +5: B = A - A + + +julia> uniform_hole_with_empty_children(BitVector([0, 0, 0, 1, 1]), g) +fshole[Bool[0, 0, 0, 1, 1]]{hole[Bool[1, 1, 1, 0, 0]],hole[Bool[1, 1, 1, 0, 0]]} +``` +""" +function uniform_hole_with_empty_children(domain::BitVector, grammar::AbstractGrammar) + child_holes = holes_from_child_types(findfirst(domain), grammar) + return UniformHole(domain, child_holes) +end + rulesoftype(::Hole, ::Set{Int}) = Set{Int}() """ From 947656b2b5ad5cfb687c9feca36b9374bd3e6bc3 Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Tue, 26 Nov 2024 19:31:45 +0300 Subject: [PATCH 07/11] Add tests --- test/test_rulenode_operators.jl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/test_rulenode_operators.jl b/test/test_rulenode_operators.jl index dee0c14..fd52ea6 100644 --- a/test/test_rulenode_operators.jl +++ b/test/test_rulenode_operators.jl @@ -15,4 +15,27 @@ end @test !isvariable(g₁, RuleNode(7, g₁), SomeDefinitions) @test isvariable(g₁, RuleNode(7, g₁)) end + + @testset "Create `RuleNode`s with `Hole`s for children" begin + g = @csgrammar begin + A = 1 | 2 | 3 + B = A + A + end + + r = rulenode_with_empty_children(4, g) + + @test get_children(r)[1].domain == [1, 1, 1, 0] + @test get_children(r)[2].domain == [1, 1, 1, 0] + end + + @testset "Create `UniformHole`s with `Hole`s for children" begin + g = @csgrammar begin + A = 1 | 2 | 3 + B = (A + A) | (A - A) + end + h = uniform_hole_with_empty_children(BitVector([0, 0, 0, 1, 1]), g) + + @test get_children(h)[1].domain == [1, 1, 1, 0, 0] + @test get_children(h)[2].domain == [1, 1, 1, 0, 0] + end end From af095fb5bfd67749c0ccdfb6045b575710dd4656 Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Tue, 26 Nov 2024 19:50:47 +0300 Subject: [PATCH 08/11] Test deprecation --- test/test_utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_utils.jl b/test/test_utils.jl index 0ed9523..b909287 100644 --- a/test/test_utils.jl +++ b/test/test_utils.jl @@ -10,6 +10,6 @@ end st = grammar2symboltable(g, DefiningAVariable) @test st[:x] == 1 - st = SymbolTable(g, DefiningAVariable) + @test_warn r"deprecated" st = SymbolTable(g, DefiningAVariable) @test st[:x] == 1 end From 007504ad15e4e51182e6853031beb491bf2dd160 Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Mon, 2 Dec 2024 16:30:44 +0300 Subject: [PATCH 09/11] Clarify new `empty_children` method --- src/HerbGrammar.jl | 2 +- src/rulenode_operators.jl | 25 +++++++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/HerbGrammar.jl b/src/HerbGrammar.jl index 7dcae40..e84842d 100644 --- a/src/HerbGrammar.jl +++ b/src/HerbGrammar.jl @@ -70,7 +70,7 @@ export add_rule!, remove_rule!, cleanup_removed_rules!, - holes_from_child_types, + empty_children, rulenode_with_empty_children, uniform_hole_with_empty_children diff --git a/src/rulenode_operators.jl b/src/rulenode_operators.jl index 9abdedf..378ed57 100644 --- a/src/rulenode_operators.jl +++ b/src/rulenode_operators.jl @@ -1,10 +1,27 @@ """ - holes_from_child_types(index::Integer, grammar::ContextSensitiveGrammar) + empty_children(index::Integer, grammar::ContextSensitiveGrammar) Given the `index` of a rule in a `grammar`, create a vector of [`Hole`](@ref)s corresponding to the children of the rule at `index`. + +# Examples +```jldoctest +julia> g = @csgrammar begin + A = 1 | 2 | 3 + B = A + A + end +1: A = 1 +2: A = 2 +3: A = 3 +4: B = A + A + + +julia> empty_children(4, g) +2-element Vector{Hole}: + hole[Bool[1, 1, 1, 0]] + hole[Bool[1, 1, 1, 0]]``` """ -function holes_from_child_types(index::Integer, grammar::ContextSensitiveGrammar) +function empty_children(index::Integer, grammar::ContextSensitiveGrammar) return [Hole(get_domain(grammar, type)) for type in grammar.childtypes[index]] end @@ -31,7 +48,7 @@ julia> rulenode_with_empty_children(4, g) ``` """ function rulenode_with_empty_children(ind::Int, _val::Union{Any,Nothing}, grammar::ContextSensitiveGrammar) - child_holes = holes_from_child_types(ind, grammar) + child_holes = empty_children(ind, grammar) return RuleNode(ind, _val, child_holes) end @@ -63,7 +80,7 @@ fshole[Bool[0, 0, 0, 1, 1]]{hole[Bool[1, 1, 1, 0, 0]],hole[Bool[1, 1, 1, 0, 0]]} ``` """ function uniform_hole_with_empty_children(domain::BitVector, grammar::AbstractGrammar) - child_holes = holes_from_child_types(findfirst(domain), grammar) + child_holes = empty_children(findfirst(domain), grammar) return UniformHole(domain, child_holes) end From 57582d10136bc4bcd714531fe01df2ad1ee253d4 Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Tue, 3 Dec 2024 18:43:38 +0300 Subject: [PATCH 10/11] Remove extraneous dependencies --- Project.toml | 4 ---- src/HerbGrammar.jl | 2 -- 2 files changed, 6 deletions(-) diff --git a/Project.toml b/Project.toml index 31d5844..64d65da 100644 --- a/Project.toml +++ b/Project.toml @@ -4,14 +4,10 @@ authors = ["Sebastijan Dumancic ", "Jaap de Jong Date: Thu, 12 Dec 2024 16:55:52 +0300 Subject: [PATCH 11/11] Remove empty-child RuleNode and UniformHole constructors They don't seem to be used at the point in time, and their utility seems fairly vague anyway. --- src/HerbGrammar.jl | 5 +- src/rulenode_operators.jl | 86 --------------------------------- test/test_rulenode_operators.jl | 23 --------- 3 files changed, 1 insertion(+), 113 deletions(-) diff --git a/src/HerbGrammar.jl b/src/HerbGrammar.jl index 65d68d1..32b27d3 100644 --- a/src/HerbGrammar.jl +++ b/src/HerbGrammar.jl @@ -67,9 +67,6 @@ export read_pcsg, add_rule!, remove_rule!, - cleanup_removed_rules!, - empty_children, - rulenode_with_empty_children, - uniform_hole_with_empty_children + cleanup_removed_rules! end # module HerbGrammar diff --git a/src/rulenode_operators.jl b/src/rulenode_operators.jl index 378ed57..e28863b 100644 --- a/src/rulenode_operators.jl +++ b/src/rulenode_operators.jl @@ -1,89 +1,3 @@ -""" - empty_children(index::Integer, grammar::ContextSensitiveGrammar) - -Given the `index` of a rule in a `grammar`, create a vector of [`Hole`](@ref)s -corresponding to the children of the rule at `index`. - -# Examples -```jldoctest -julia> g = @csgrammar begin - A = 1 | 2 | 3 - B = A + A - end -1: A = 1 -2: A = 2 -3: A = 3 -4: B = A + A - - -julia> empty_children(4, g) -2-element Vector{Hole}: - hole[Bool[1, 1, 1, 0]] - hole[Bool[1, 1, 1, 0]]``` -""" -function empty_children(index::Integer, grammar::ContextSensitiveGrammar) - return [Hole(get_domain(grammar, type)) for type in grammar.childtypes[index]] -end - -""" - RuleNode(ind::Int, grammar::ContextSensitiveGrammar) - -Create a [`RuleNode`](@ref) with [`Hole`](@ref)s as children. The holes -are initialized with the types of the children of the rule at `ind`. - -# Examples -```jldoctest -julia> g = @csgrammar begin - A = 1 | 2 | 3 - B = A + A - end -1: A = 1 -2: A = 2 -3: A = 3 -4: B = A + A - - -julia> rulenode_with_empty_children(4, g) -4{hole[Bool[1, 1, 1, 0]],hole[Bool[1, 1, 1, 0]]} -``` -""" -function rulenode_with_empty_children(ind::Int, _val::Union{Any,Nothing}, grammar::ContextSensitiveGrammar) - child_holes = empty_children(ind, grammar) - return RuleNode(ind, _val, child_holes) -end - -function rulenode_with_empty_children(ind::Int, grammar::ContextSensitiveGrammar) - return rulenode_with_empty_children(ind, nothing, grammar) -end - -""" - uniform_hole_with_empty_children(domain::BitVector, grammar::AbstractGrammar) - -Create a [`UniformHole`](@ref) with [`Hole`](@ref)s as children. The holes -are initialized with the types of the children of the rule at `ind`. - -# Examples -```jldoctest -julia> g = @csgrammar begin - A = 1 | 2 | 3 - B = (A + A) | (A - A) - end -1: A = 1 -2: A = 2 -3: A = 3 -4: B = A + A -5: B = A - A - - -julia> uniform_hole_with_empty_children(BitVector([0, 0, 0, 1, 1]), g) -fshole[Bool[0, 0, 0, 1, 1]]{hole[Bool[1, 1, 1, 0, 0]],hole[Bool[1, 1, 1, 0, 0]]} -``` -""" -function uniform_hole_with_empty_children(domain::BitVector, grammar::AbstractGrammar) - child_holes = empty_children(findfirst(domain), grammar) - return UniformHole(domain, child_holes) -end - rulesoftype(::Hole, ::Set{Int}) = Set{Int}() """ diff --git a/test/test_rulenode_operators.jl b/test/test_rulenode_operators.jl index fd52ea6..dee0c14 100644 --- a/test/test_rulenode_operators.jl +++ b/test/test_rulenode_operators.jl @@ -15,27 +15,4 @@ end @test !isvariable(g₁, RuleNode(7, g₁), SomeDefinitions) @test isvariable(g₁, RuleNode(7, g₁)) end - - @testset "Create `RuleNode`s with `Hole`s for children" begin - g = @csgrammar begin - A = 1 | 2 | 3 - B = A + A - end - - r = rulenode_with_empty_children(4, g) - - @test get_children(r)[1].domain == [1, 1, 1, 0] - @test get_children(r)[2].domain == [1, 1, 1, 0] - end - - @testset "Create `UniformHole`s with `Hole`s for children" begin - g = @csgrammar begin - A = 1 | 2 | 3 - B = (A + A) | (A - A) - end - h = uniform_hole_with_empty_children(BitVector([0, 0, 0, 1, 1]), g) - - @test get_children(h)[1].domain == [1, 1, 1, 0, 0] - @test get_children(h)[2].domain == [1, 1, 1, 0, 0] - end end