Skip to content

Commit

Permalink
Enhance debug option handling to include 'parse.trace' definition
Browse files Browse the repository at this point in the history
Following bison's behavior:
```command
$ bison -h
Usage: bison [OPTION]... FILE
Generate a deterministic LR or generalized LR (GLR) parser employing
LALR(1), IELR(1), or canonical LR(1) parser tables.
: (snip)
Tuning the Parser:
  -L, --language=LANGUAGE          specify the output programming language
  -S, --skeleton=FILE              specify the skeleton to use
  -t, --debug                      instrument the parser for tracing
                                   same as '-Dparse.trace'
                                   ^^^^^^^^^^^^^^^^^^^^^^^
```
  • Loading branch information
ydah committed Feb 5, 2025
1 parent 58b28b9 commit 934edc0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/lrama/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def parse_by_option_parser(argv)
o.separator 'Tuning the Parser:'
o.on('-S', '--skeleton=FILE', 'specify the skeleton to use') {|v| @options.skeleton = v }
o.on('-t', '--debug', 'display debugging outputs of internal parser') {|v| @options.debug = true }
o.separator " same as '-Dparse.trace'"
o.on('-D', '--define=NAME[=VALUE]', Array, "similar to '%define NAME VALUE'") do |v|
@options.define = v.each_with_object({}) do |item, hash|
key, value = item.split('=', 2)
Expand Down
2 changes: 1 addition & 1 deletion lib/lrama/parser.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ include Lrama::Report::Duration

def initialize(text, path, debug = false, define = {})
@grammar_file = Lrama::Lexer::GrammarFile.new(path, text)
@yydebug = debug
@yydebug = debug || define.key?('parse.trace')
@rule_counter = Lrama::Grammar::Counter.new(0)
@midrule_action_counter = Lrama::Grammar::Counter.new(1)
@define = define
Expand Down
1 change: 1 addition & 0 deletions spec/lrama/option_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
Tuning the Parser:
-S, --skeleton=FILE specify the skeleton to use
-t, --debug display debugging outputs of internal parser
same as '-Dparse.trace'
-D, --define=NAME[=VALUE] similar to '%define NAME VALUE'
Output:
Expand Down
58 changes: 58 additions & 0 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,64 @@
HEADER
end

describe "#initialize" do
context 'when debug is true and define is not include `parse.trace`' do
context 'with define is not specified' do
it '@yydebug is true' do
parser = Lrama::Parser.new("", "", true)
yydebug = parser.instance_variable_get(:@yydebug)
expect(yydebug).to be_truthy
end
end

context 'with define is specified' do
context 'with `api.pure`' do
it '@yydebug is false' do
parser = Lrama::Parser.new("", "", true, {'api.pure' => nil})
yydebug = parser.instance_variable_get(:@yydebug)
expect(yydebug).to be_truthy
end
end

context 'with `parse.trace`' do
it '@yydebug is true' do
parser = Lrama::Parser.new("", "", true, {'parse.trace' => nil})
yydebug = parser.instance_variable_get(:@yydebug)
expect(yydebug).to be_truthy
end
end
end
end

context 'when debug is false and define is not include `parse.trace`' do
context 'with define is not specified' do
it '@yydebug is false' do
parser = Lrama::Parser.new("", "", false)
yydebug = parser.instance_variable_get(:@yydebug)
expect(yydebug).to be_falsey
end
end

context 'with define is specified' do
context 'with `api.pure`' do
it '@yydebug is false' do
parser = Lrama::Parser.new("", "", false, {'api.pure' => nil})
yydebug = parser.instance_variable_get(:@yydebug)
expect(yydebug).to be_falsey
end
end

context 'with `parse.trace`' do
it '@yydebug is true' do
parser = Lrama::Parser.new("", "", false, {'parse.trace' => nil})
yydebug = parser.instance_variable_get(:@yydebug)
expect(yydebug).to be_truthy
end
end
end
end
end

describe '#parse' do
it "basic" do
path = "common/basic.y"
Expand Down

0 comments on commit 934edc0

Please sign in to comment.