diff --git a/Project.toml b/Project.toml index b473c9c..64d65da 100644 --- a/Project.toml +++ b/Project.toml @@ -1,24 +1,13 @@ name = "HerbGrammar" uuid = "4ef9e186-2fe5-4b24-8de7-9f7291f24af7" authors = ["Sebastijan Dumancic ", "Jaap de Jong ", "Nicolae Filat ", "Piotr Cichoń "] -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"] diff --git a/src/HerbGrammar.jl b/src/HerbGrammar.jl index 7914c0d..32b27d3 100644 --- a/src/HerbGrammar.jl +++ b/src/HerbGrammar.jl @@ -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, @@ -58,8 +53,8 @@ export expr2pcsgrammar, SymbolTable, + grammar2symboltable, - change_expr, rulenode2expr, rulenode_log_probability, diff --git a/src/grammar_base.jl b/src/grammar_base.jl index 8258b15..95c8f32 100644 --- a/src/grammar_base.jl +++ b/src/grammar_base.jl @@ -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} @@ -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) diff --git a/src/rulenode_operators.jl b/src/rulenode_operators.jl index 73fc82e..e28863b 100644 --- a/src/rulenode_operators.jl +++ b/src/rulenode_operators.jl @@ -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}() """ @@ -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 diff --git a/src/utils.jl b/src/utils.jl index 3eaf4b2..7200c9b 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -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) @@ -43,17 +38,18 @@ 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) @@ -61,6 +57,9 @@ function HerbGrammar.SymbolTable(grammar::AbstractGrammar, mod::Module=Main) 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 diff --git a/test/Project.toml b/test/Project.toml new file mode 100644 index 0000000..b90b5b4 --- /dev/null +++ b/test/Project.toml @@ -0,0 +1,4 @@ +[deps] +Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" +HerbCore = "2b23ba43-8213-43cb-b5ea-38c12b45bd45" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/runtests.jl b/test/runtests.jl index 1a7221f..52b5a4c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 diff --git a/test/test_utils.jl b/test/test_utils.jl new file mode 100644 index 0000000..b909287 --- /dev/null +++ b/test/test_utils.jl @@ -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