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

Add aqua, remove pirated methods #96

Merged
merged 11 commits into from
Dec 12, 2024
15 changes: 2 additions & 13 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
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.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"
TreeView = "^0.5"
Serialization = "^1.8,^1.10,^1.11"
julia = "^1.8"

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

[targets]
test = ["Test"]
11 changes: 3 additions & 8 deletions src/HerbGrammar.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
module HerbGrammar

import TreeView: walk_tree
using AbstractTrees
using DataStructures # NodeRecycler
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
ContextFree,
ContextSensitive,

ContextSensitiveGrammar,
AbstractRuleNode,
RuleNode,
Expand Down Expand Up @@ -58,8 +53,8 @@ export
expr2pcsgrammar,

SymbolTable,
grammar2symboltable,

change_expr,
rulenode2expr,
rulenode_log_probability,

Expand Down
8 changes: 0 additions & 8 deletions src/grammar_base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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)

Expand Down
14 changes: 0 additions & 14 deletions src/rulenode_operators.jl
Original file line number Diff line number Diff line change
@@ -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}()

"""
Expand Down Expand Up @@ -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
15 changes: 7 additions & 8 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -43,24 +38,28 @@ 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}

"""
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)
end
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

Expand Down
4 changes: 4 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[deps]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
HerbCore = "2b23ba43-8213-43cb-b5ea-38c12b45bd45"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
6 changes: 4 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Aqua
using HerbCore
using HerbGrammar
using Test


@testset "HerbGrammar.jl" verbose=true begin
@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")
include("test_expr2rulenode.jl")
include("test_utils.jl")
end
15 changes: 15 additions & 0 deletions test/test_utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module DefiningAVariable
x = 1
end
@testset "SymbolTable Tests" begin
g = @cfgrammar begin
Real = |(1:9)
Real = x
end

st = grammar2symboltable(g, DefiningAVariable)
@test st[:x] == 1

@test_warn r"deprecated" st = SymbolTable(g, DefiningAVariable)
@test st[:x] == 1
end
Loading