diff --git a/Project.toml b/Project.toml index db2e0c3..ffaa34a 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "HerbGrammar" uuid = "4ef9e186-2fe5-4b24-8de7-9f7291f24af7" authors = ["Sebastijan Dumancic ", "Jaap de Jong ", "Nicolae Filat ", "Piotr CichoĊ„ "] -version = "0.2.1" +version = "0.2.2" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" diff --git a/src/nodelocation.jl b/src/nodelocation.jl index 801c036..1f54294 100644 --- a/src/nodelocation.jl +++ b/src/nodelocation.jl @@ -44,3 +44,22 @@ function Base.insert!(root::RuleNode, loc::NodeLoc, rulenode::RuleNode) end return root end + +""" +insert!(root::RuleNode, loc::NodeLoc, hole::Hole) + +Inserts a hole at the location pointed to by loc. + +!!! warning +The user is responsible for ensuring that the hole's domain matches the domain of +the node it is replacing. This function does not currently check for this. +""" +function Base.insert!(root::RuleNode, loc::NodeLoc, hole::Hole) + parent, i = loc.parent, loc.i + if loc.i > 0 + parent.children[i] = hole + else + throw(ArgumentError("Inserting a hole at the root node is not supported.")) + end + return root +end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index f4a7f0a..36f6bef 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -4,4 +4,5 @@ using Test @testset "HerbGrammar.jl" verbose=true begin include("test_csg.jl") include("test_rulenode_operators.jl") + include("test_nodelocation.jl") end diff --git a/test/test_nodelocation.jl b/test/test_nodelocation.jl new file mode 100644 index 0000000..1a125d1 --- /dev/null +++ b/test/test_nodelocation.jl @@ -0,0 +1,32 @@ +using HerbCore + + +@testset verbose = true "NodeLoc" begin + + @testset "Replace root with a rulenode" begin + root = RuleNode(1, [ + RuleNode(2, []), + RuleNode(3, [ + RuleNode(4, []) + ]) + ]) + loc = NodeLoc(root, 0) + new_node = RuleNode(5, []) + insert!(root, loc, new_node) + @test get(root, loc) == new_node + end + + @testset "Replace subtree with hole" begin + root = RuleNode(1, [ + RuleNode(2, []), + RuleNode(3, [ + RuleNode(4, []) + ]) + ]) + loc = NodeLoc(root, 2) + new_node = Hole([0, 0, 0, 1]) + insert!(root, loc, new_node) + @test get(root, loc) isa Hole + @test get(root, loc).domain == [0, 0, 0, 1] + end +end \ No newline at end of file