Skip to content

Commit

Permalink
Test case for %printer
Browse files Browse the repository at this point in the history
  • Loading branch information
yui-knk committed Oct 21, 2023
1 parent 99ddcc7 commit 0a6c80e
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions spec/lrama/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <val1> NUM
%type <val2> add
%type <val3> expr
%left '+' '-'
%left '*' '/'
%printer {
printf("val1: %d\\n", $$);
} <val1> // printer for TAG
%printer {
printf("val2: %d\\n", $$);
} <val2>
%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
Expand Down

0 comments on commit 0a6c80e

Please sign in to comment.