Skip to content

Commit

Permalink
Merge pull request #86 from kwasielewski/allow-rules-with-same-rhs
Browse files Browse the repository at this point in the history
Fix skipping of new repeated rules
  • Loading branch information
ReubenJ authored Aug 27, 2024
2 parents ce15a3a + 1330d9c commit 97b0e62
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/grammar_base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ function add_rule!(g::AbstractGrammar, e::Expr)
# 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 || typeof(r)==Expr && r == rule for rule g.rules)
if !any(s == type && (r === rule || typeof(r)==Expr && r == rule) for (type, rule) zip(g.types, g.rules))
push!(g.rules, r)
push!(g.iseval, iseval(rule))
push!(g.types, s)
Expand Down
20 changes: 20 additions & 0 deletions test/test_csg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,24 @@
1.0 : A = 1
end) == Expr
end

@testset "Test adding duplicated rules" begin
g = @cfgrammar begin
S = 1 + A
S = 2 * B
A = 1
B = 1
B = 2
end
# All rules should be present in grammar
@test g.rules == [:(1 + A), :(2 * B), 1, 1, 2]

# Adding duplicated rule
add_rule!(g, :(A = 1))
@test g.rules == [:(1 + A), :(2 * B), 1, 1, 2]

# Adding new rule with already existing rhs
add_rule!(g, :(A = 2))
@test g.rules == [:(1 + A), :(2 * B), 1, 1, 2, 2]
end
end

0 comments on commit 97b0e62

Please sign in to comment.