Skip to content

Commit

Permalink
Merge pull request #77 from Herb-AI/bug/cant-insert-holes
Browse files Browse the repository at this point in the history
[v0.2.x] Allow `insert!`ion of `AbstractRuleNode`s
  • Loading branch information
THinnerichs authored Jun 3, 2024
2 parents c57f5e7 + 5a6c906 commit 5599ddd
Show file tree
Hide file tree
Showing 4 changed files with 53 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 = "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

2 comments on commit 5599ddd

@ReubenJ
Copy link
Member

@ReubenJ ReubenJ commented on 5599ddd Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/108143

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.2 -m "<description of version>" 5599ddde2370a5dc328fb09ab4b0ce566bb0957e
git push origin v0.2.2

Please sign in to comment.