Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v0.2.x] Allow insert!ion of AbstractRuleNodes #77

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "HerbGrammar"
uuid = "4ef9e186-2fe5-4b24-8de7-9f7291f24af7"
authors = ["Sebastijan Dumancic <[email protected]>", "Jaap de Jong <[email protected]>", "Nicolae Filat <[email protected]>", "Piotr Cichoń <[email protected]>"]
version = "0.2.1"
version = "0.2.2"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
19 changes: 19 additions & 0 deletions src/nodelocation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 32 additions & 0 deletions test/test_nodelocation.jl
Original file line number Diff line number Diff line change
@@ -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
Loading