From 0a6c80e036f285af77bd2fa51f7ae69aa4369833 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sat, 21 Oct 2023 09:39:32 +0900 Subject: [PATCH] Test case for %printer --- spec/lrama/integration_spec.rb | 82 ++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/spec/lrama/integration_spec.rb b/spec/lrama/integration_spec.rb index 2c0863af..d23bfa0d 100644 --- a/spec/lrama/integration_spec.rb +++ b/spec/lrama/integration_spec.rb @@ -320,6 +320,88 @@ def test_rules(rules, input, expected, command_args: [], debug: false) end end + describe "%printer" do + it "prints messages" do + # (1+) #=> 101 + # '100' is complemented + input = [ + %w[NUM val1 1], + %w['+'], + %w[NUM val1 2], + %w['*'], + %w[NUM val1 3] + ] + + expected = <<~STR.chomp + val1: 1 + val1: 1 + val1: 1 + expr: 1 + val1: 2 + val1: 2 + val1: 2 + expr: 2 + val1: 3 + val1: 3 + val1: 3 + expr: 3 + expr: 2 + expr: 3 + expr: 6 + expr: 1 + expr: 6 + val2: 7 + val2: 7 + expr: 7 + expr: 7 + => 7 + STR + + test_rules(<<~Rules, input, expected, debug: true) + %union { + int val1; + int val2; + int val3; + } + %token NUM + %type add + %type expr + %left '+' '-' + %left '*' '/' + + %printer { + printf("val1: %d\\n", $$); + } // printer for TAG + + %printer { + printf("val2: %d\\n", $$); + } + + %printer { + printf("expr: %d\\n", $$); + } expr // printer for symbol + + %% + + program : { (void)yynerrs; } + | expr { printf("=> %d", $1); } + ; + + add : expr '+' expr { $$ = $1 + $3; } + + expr : NUM + | add + | expr '-' expr { $$ = $1 - $3; } + | expr '*' expr { $$ = $1 * $3; } + | expr '/' expr { $$ = $1 / $3; } + | '(' expr ')' { $$ = $2; } + ; + + %% + Rules + end + end + # TODO: Add test case for "(1+2" describe "error_recovery" do it "returns 6 for '(1+)'" do