Skip to content

Commit

Permalink
Merge pull request #100 from Herb-AI/fix/expr2rulenode-symbols-numbers
Browse files Browse the repository at this point in the history
Fix `expr2rulenode` for lone `Symbol`s and `Number`s
  • Loading branch information
ReubenJ authored Dec 12, 2024
2 parents ab79408 + d01aac7 commit 96f8fb3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
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 @@ end
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)
(s, rn) = expr2rulenode(expr, grammar, tags)
while s != startSymbol

Expand All @@ -375,9 +375,9 @@ end
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

2 comments on commit 96f8fb3

@ReubenJ
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/121258

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.2 -m "<description of version>" 96f8fb3b75c70748912c6f484226e20911f88662
git push origin v0.4.2

Please sign in to comment.