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

Fix expr2rulenode for lone Symbols and Numbers #100

Merged
merged 3 commits into from
Dec 12, 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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.1"
version = "0.4.2"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
8 changes: 4 additions & 4 deletions src/rulenode_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
Converts an expression into a [`AbstractRuleNode`](@ref) corresponding to the rule definitions in the grammar.
"""
function expr2rulenode(expr::Symbol, grammar::AbstractGrammar, startSymbol::Symbol)
tags = get_tags(grammar)
tags = grammar_map_right_to_left(grammar)

Check warning on line 357 in src/rulenode_operators.jl

View check run for this annotation

Codecov / codecov/patch

src/rulenode_operators.jl#L357

Added line #L357 was not covered by tests
(s, rn) = expr2rulenode(expr, grammar, tags)
while s != startSymbol

Expand All @@ -375,9 +375,9 @@

Converts an expression into a [`AbstractRuleNode`](@ref) corresponding to the rule definitions in the grammar.
"""
function expr2rulenode(expr::Symbol, grammar::AbstractGrammar)
tags = get_tags(grammar)
(s, rn) = expr2rulenode(expr, grammar, tags)
function expr2rulenode(expr::Union{Symbol,Number}, grammar::AbstractGrammar)
tags = grammar_map_right_to_left(grammar)
(s, rn) = _expr2rulenode(expr, grammar, tags)
return rn
end

Expand Down
24 changes: 23 additions & 1 deletion test/test_expr2rulenode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,27 @@
@test expr2rulenode(expr5, g2) == RuleNode(12, [RuleNode(14, []), RuleNode(3, [RuleNode(4, [RuleNode(9, [])]) , RuleNode(2, [RuleNode(4, [RuleNode(6, [])])])]), RuleNode(2, [RuleNode(4, [RuleNode(6, [])])])])
@test expr2rulenode(expr5, g2, :Start) == RuleNode(1, [RuleNode(2, [RuleNode(5, [RuleNode(12, [RuleNode(14, []), RuleNode(3, [RuleNode(4, [RuleNode(9, [])]) , RuleNode(2, [RuleNode(4, [RuleNode(6, [])])])]), RuleNode(2, [RuleNode(4, [RuleNode(6, [])])])])])])])

end
int_grammar = @csgrammar begin
Val = 0 | 1 | 2
end

int_expr = :(2)
@test expr2rulenode(int_expr, int_grammar) == RuleNode(3)
@test rulenode2expr(expr2rulenode(int_expr, int_grammar), int_grammar) == int_expr

float_grammar = @csgrammar begin
Val = 0.0 | 1.0 | 2.0 | 3.14
end

float_expr = :(3.14)
@test expr2rulenode(float_expr, float_grammar) == RuleNode(4)
@test rulenode2expr(expr2rulenode(float_expr, float_grammar), float_grammar) == float_expr

sym_grammar = @csgrammar begin
Val = x
end

sym = :(x)
@test expr2rulenode(sym, sym_grammar) == RuleNode(1)
@test rulenode2expr(expr2rulenode(sym, sym_grammar), sym_grammar) == sym
end
Loading