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

Address Aqua type piracy errors #31

Merged
merged 7 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
12 changes: 5 additions & 7 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
name = "HerbCore"
uuid = "2b23ba43-8213-43cb-b5ea-38c12b45bd45"
authors = ["Jaap de Jong <[email protected]>", "Nicolae Filat <[email protected]>", "Tilman Hinnerichs <[email protected]>", "Sebastijan Dumancic <[email protected]>"]
version = "0.3.2"
version = "0.3.3"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

[compat]
AbstractTrees = "0.4.5"
julia = "^1.8"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
8 changes: 7 additions & 1 deletion src/HerbCore.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module HerbCore

using AbstractTrees

include("grammar.jl")
include("rulenode.jl")
include("constraint.jl")
include("grammar.jl")

export
AbstractRuleNode,
Expand Down Expand Up @@ -30,4 +32,8 @@ export
have_same_shape, AbstractConstraint,
AbstractGrammar

# AbstractTrees interface
children,
nodevalue

end # module HerbCore
8 changes: 8 additions & 0 deletions src/grammar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ If the grammar is non-probabilistic, the list can be `nothing`.
For concrete types, see `ContextSensitiveGrammar` within the `HerbGrammar` module.
"""
abstract type AbstractGrammar end

function Base.show(io::IO, grammar::AbstractGrammar)
for i in eachindex(grammar.rules)
println(io, i, ": ", grammar.types[i], " = ", grammar.rules[i])
end
end

Base.getindex(grammar::AbstractGrammar, typ::Symbol) = grammar.bytype[typ]
4 changes: 4 additions & 0 deletions src/rulenode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Expression trees consist of [`RuleNode`](@ref)s and [`AbstractHole`](@ref)s.
"""
abstract type AbstractRuleNode end

# Interface to AbstractTrees.jl
AbstractTrees.children(node::AbstractRuleNode) = get_children(node)
AbstractTrees.nodevalue(node::AbstractRuleNode) = get_rule(node)

"""
RuleNode <: AbstractRuleNode

Expand Down
3 changes: 3 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
32 changes: 32 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
using AbstractTrees: children, nodevalue, treeheight
using HerbCore
using Test

@testset "HerbCore.jl" verbose=true begin
@testset "AbstractTrees Interface" begin
@test nodevalue(RuleNode(1)) == 1
@test isempty(children(RuleNode(1)))
@test length(children(RuleNode(1, [RuleNode(2), RuleNode(2)]))) == 2
@test treeheight(RuleNode(1)) == 0
@test treeheight(RuleNode(1, [RuleNode(2), RuleNode(2)])) == 1
end

@testset "RuleNode tests" begin
@testset "Equality tests" begin
@test RuleNode(1) == RuleNode(1)
Expand Down Expand Up @@ -335,4 +344,27 @@ using Test
"12{14,2{4{hole[Bool[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}},2{4{6}}}"
end
end

@testset "Grammar" begin
struct ExGrammar <: AbstractGrammar
rules::Vector{Any}
types::Vector{Symbol}
bytype::Dict{Symbol, Vector{Int}}
# ...
# only partially implementing the AbstractGrammar interface
# to test the Base.show
end

g = ExGrammar([1], [:A], Dict([:A => [1]]))

@testset "show" begin
io = IOBuffer()
Base.show(io, g)
@test String(take!(io)) == "1: A = 1\n"
end

@testset "get_index" begin
@test g[:A] == [1]
end
end
end