Herb-AI / HerbGrammar.jl

Grammars for Herb.jl
https://herb-ai.github.io/
MIT License
0 stars 2 forks source link

Export grammar in precompilation step fails #62

Open nicolaefilat opened 7 months ago

nicolaefilat commented 7 months ago
ERROR: LoadError: Evaluation into the closed module `HerbGrammar` breaks incremental compilation because the side effects will not be permanent. This is likely due to some other module mutating `HerbGrammar` with `eval` during precompilation - don't do this.
Stacktrace:
 [1] eval
   @ ./boot.jl:368 [inlined]
 [2] eval
   @ ~/.julia/packages/HerbGrammar/x0E9w/src/HerbGrammar.jl:1 [inlined]
 [3] parse_rule!(v::Vector{Any}, ex::Expr)
   @ HerbGrammar ~/.julia/packages/HerbGrammar/x0E9w/src/cfg/cfg.jl:121
 [4] expr2cfgrammar(ex::Expr)
   @ HerbGrammar ~/.julia/packages/HerbGrammar/x0E9w/src/cfg/cfg.jl:64
 [5] expr2csgrammar(ex::Expr)
   @ HerbGrammar ~/.julia/packages/HerbGrammar/x0E9w/src/csg/csg.jl:59
 [6] var"@csgrammar"(__source__::LineNumberNode, __module__::Module, ex::Any)
   @ HerbGrammar ~/.julia/packages/HerbGrammar/x0E9w/src/csg/csg.jl:98
 [7] include
   @ ./Base.jl:419 [inlined]
 [8] include_package_for_output(pkg::Base.PkgId, input::String, depot_path::Vector{String}, dl_load_path::Vector{String}, load_path::Vector{String}, concrete_deps::Vector{Pair{Base.PkgId, UInt64}}, source::Nothing)
   @ Base ./loading.jl:1554
 [9] top-level scope
   @ stdin:1

The fix is available on mattermost, we just have to implement it.

function parse_rule!(v::Vector{Any}, ex::Expr)
    if ex.head == :call && ex.args[1] == :|
        terms = length(ex.args) == 2 ?
        collect(ex.args[2].args[2]:ex.args[2].args[3]) :    #|(a:c) case
        ex.args[2:end]                 #a|b|c case
        for t in terms
            parse_rule!(v, t)
        end
    else
        push!(v, ex)
    end
end