forked from ruby/lrama
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not include mid-rule actions when tracing rules
This PR, midrule actions should not be included when tracing rules. Before: ``` ❯ exe/lrama --trace=rules ./spec/fixtures/common/basic.y Grammar rules: $accept -> program EOI program -> class program -> '+' strings_1 program -> '-' strings_2 class -> keyword_class tSTRING keyword_end $@1 -> ε $@2 -> ε class -> keyword_class $@1 tSTRING '!' keyword_end $@2 $@3 -> ε $@4 -> ε class -> keyword_class $@3 tSTRING '?' keyword_end $@4 strings_1 -> string_1 strings_2 -> string_1 strings_2 -> string_2 string_1 -> string string_2 -> string '+' string -> tSTRING unused -> tNUMBER ``` After: ``` ❯ exe/lrama --trace=rules ./spec/fixtures/common/basic.y Grammar rules: $accept -> program EOI program -> class program -> '+' strings_1 program -> '-' strings_2 class -> keyword_class tSTRING keyword_end class -> keyword_class tSTRING '!' keyword_end class -> keyword_class tSTRING '?' keyword_end strings_1 -> string_1 strings_2 -> string_1 strings_2 -> string_2 string_1 -> string string_2 -> string '+' string -> tSTRING unused -> tNUMBER ```
- Loading branch information
Showing
3 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Lrama::TraceReporter do | ||
describe "#report" do | ||
let(:path) { "common/basic.y" } | ||
let(:y) { File.read(fixture_path(path)) } | ||
let(:grammar) do | ||
grammar = Lrama::Parser.new(y, path).parse | ||
grammar.prepare | ||
grammar.validate! | ||
grammar | ||
end | ||
|
||
context "when rules: true" do | ||
it "prints the rules" do | ||
expect do | ||
described_class.new(grammar).report(rules: true) | ||
end.to output(<<~RULES).to_stdout | ||
Grammar rules: | ||
$accept -> program EOI | ||
program -> class | ||
program -> '+' strings_1 | ||
program -> '-' strings_2 | ||
class -> keyword_class tSTRING keyword_end | ||
class -> keyword_class tSTRING '!' keyword_end | ||
class -> keyword_class tSTRING '?' keyword_end | ||
strings_1 -> string_1 | ||
strings_2 -> string_1 | ||
strings_2 -> string_2 | ||
string_1 -> string | ||
string_2 -> string '+' | ||
string -> tSTRING | ||
unused -> tNUMBER | ||
RULES | ||
end | ||
end | ||
|
||
context "when actions: true" do | ||
it "prints the actions" do | ||
expect do | ||
described_class.new(grammar).report(actions: true) | ||
end.to output(<<~RULES).to_stdout | ||
Grammar rules with actions: | ||
$accept -> program EOI {} | ||
program -> class {} | ||
program -> '+' strings_1 {} | ||
program -> '-' strings_2 {} | ||
class -> keyword_class tSTRING keyword_end { code 1 } | ||
$@1 -> ε { code 2 } | ||
$@2 -> ε { code 3 } | ||
class -> keyword_class $@1 tSTRING '!' keyword_end $@2 {} | ||
$@3 -> ε { code 4 } | ||
$@4 -> ε { code 5 } | ||
class -> keyword_class $@3 tSTRING '?' keyword_end $@4 {} | ||
strings_1 -> string_1 {} | ||
strings_2 -> string_1 {} | ||
strings_2 -> string_2 {} | ||
string_1 -> string {} | ||
string_2 -> string '+' {} | ||
string -> tSTRING {} | ||
unused -> tNUMBER {} | ||
RULES | ||
end | ||
end | ||
|
||
context 'when empty options' do | ||
it 'does not print anything' do | ||
expect do | ||
described_class.new(grammar).report | ||
end.to_not output.to_stdout | ||
end | ||
end | ||
end | ||
end |