From 09bf493a8434ff81368862a83243c3c3bfa3e50c Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Thu, 12 Dec 2024 10:15:40 +0300 Subject: [PATCH 1/3] Add failing tests for `expr2rulenode` Fails when the expr is just a symbol or a number. Neither of these are actually expressions in Julia, so `expr2rulenode` hasn't been defined for them yet-- it's only defined over `Expr`s, currently. --- test/test_expr2rulenode.jl | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/test_expr2rulenode.jl b/test/test_expr2rulenode.jl index 664313e..836320f 100644 --- a/test/test_expr2rulenode.jl +++ b/test/test_expr2rulenode.jl @@ -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 From 742d01c49d2fc02540aa964d27044a119ce2ddd7 Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Thu, 12 Dec 2024 10:16:41 +0300 Subject: [PATCH 2/3] Fix `expr2rulenode` for `Symbols` and `Numbers` --- src/rulenode_operators.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/rulenode_operators.jl b/src/rulenode_operators.jl index 2774cf0..73fc82e 100644 --- a/src/rulenode_operators.jl +++ b/src/rulenode_operators.jl @@ -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 @@ -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 From d01aac74778e274ec87c067deb267618d6f6256a Mon Sep 17 00:00:00 2001 From: Reuben Gardos Reid <5456207+ReubenJ@users.noreply.github.com> Date: Thu, 12 Dec 2024 10:21:31 +0300 Subject: [PATCH 3/3] Bump patch version number --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index d5112dd..b473c9c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "HerbGrammar" uuid = "4ef9e186-2fe5-4b24-8de7-9f7291f24af7" authors = ["Sebastijan Dumancic ", "Jaap de Jong ", "Nicolae Filat ", "Piotr CichoĊ„ "] -version = "0.4.1" +version = "0.4.2" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"