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_rule!(...) type coercion bug #49

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 8 additions & 6 deletions src/grammar_base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,15 @@ function add_rule!(g::Grammar, e::Expr)
rvec = Any[]
parse_rule!(rvec, rule)
for r ∈ rvec
if r ∈ g.rules
continue
# Only add a rule if it does not exist yet. Check for existance
# with strict equality so that true and 1 are not considered
# equal. that means we can't use `in` or `∈` for equality checking.
if !any(r === rule for rule ∈ g.rules)
push!(g.rules, r)
push!(g.iseval, iseval(rule))
push!(g.types, s)
g.bytype[s] = push!(get(g.bytype, s, Int[]), length(g.rules))
end
push!(g.rules, r)
push!(g.iseval, iseval(rule))
push!(g.types, s)
g.bytype[s] = push!(get(g.bytype, s, Int[]), length(g.rules))
end
end
alltypes = collect(keys(g.bytype))
Expand Down
24 changes: 24 additions & 0 deletions test/test_cfg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,28 @@
rm("toy_pcfg.grammar")
end

@testset "Test that strict equality is used during rule creation" begin
g₁ = @csgrammar begin
R = x
R = R + R
end

add_rule!(g₁, :(R = 1 | 2))

add_rule!(g₁,:(Bool = true))

@test all(g₁.rules .== [:x, :(R + R), 1, 2, true])

g₁ = @csgrammar begin
R = x
R = R + R
end

add_rule!(g₁,:(Bool = true))

add_rule!(g₁, :(R = 1 | 2))

@test all(g₁.rules .== [:x, :(R + R), true, 1, 2])
end

end