Skip to content

Commit

Permalink
Merge pull request #396 from ydah/new-option
Browse files Browse the repository at this point in the history
Introduce the `--trace=actions` option
  • Loading branch information
yui-knk authored Apr 17, 2024
2 parents 1d9f17b + 5978860 commit 3ee5723
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/lrama/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def run(argv)
puts grammar.rules
end

if options.trace_opts && options.trace_opts[:actions]
puts "Grammar rules with actions:"
grammar.rules.each { |rule| puts rule.with_actions }
end

File.open(options.outfile, "w+") do |f|
Lrama::Output.new(
out: f,
Expand Down
4 changes: 4 additions & 0 deletions lib/lrama/grammar/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def as_comment
"#{l}: #{r}"
end

def with_actions
"#{to_s} {#{token_code&.s_value}}"
end

# opt_nl: ε <-- empty_rule
# | '\n' <-- not empty_rule
def empty_rule?
Expand Down
5 changes: 3 additions & 2 deletions lib/lrama/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ def validate_report(report)

VALID_TRACES = %w[
none locations scan parse automaton bitsets
closure grammar rules resource sets muscles tools
m4-early m4 skeleton time ielr cex all
closure grammar rules actions resource
sets muscles tools m4-early m4 skeleton time
ielr cex all
]

def validate_trace(trace)
Expand Down
19 changes: 19 additions & 0 deletions spec/lrama/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@
end
end

context "when `--trace=actions` option specified" do
it "print grammar rules with actions" do
command = Lrama::Command.new
expect { command.run(o_option + [fixture_path("command/basic.y"), "--trace=actions"]) }.to output(<<~'OUTPUT').to_stdout
Grammar rules with actions:
$accept -> list, YYEOF {}
list -> ε {}
list -> list, LF {}
list -> list, expr, LF { printf("=> %d\n", $2); }
expr -> NUM {}
expr -> expr, '+', expr { $$ = $1 + $3; }
expr -> expr, '-', expr { $$ = $1 - $3; }
expr -> expr, '*', expr { $$ = $1 * $3; }
expr -> expr, '/', expr { $$ = $1 / $3; }
expr -> '(', expr, ')' { $$ = $2; }
OUTPUT
end
end

context "when `--report-file` option specified" do
it "create report file" do
allow(File).to receive(:open).and_call_original
Expand Down
2 changes: 1 addition & 1 deletion spec/lrama/option_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
states itemsets lookaheads solved counterexamples all verbose
Valid Traces:
none locations scan parse automaton bitsets closure grammar rules resource sets muscles tools m4-early m4 skeleton time ielr cex all
none locations scan parse automaton bitsets closure grammar rules actions resource sets muscles tools m4-early m4 skeleton time ielr cex all
HELP
end
Expand Down

0 comments on commit 3ee5723

Please sign in to comment.