Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance debug option handling to include 'parse.trace' definition #528

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 true' do
parser = Lrama::Parser.new("", "", true, {'api.pure' => nil})
yydebug = parser.instance_variable_get(:@yydebug)
expect(yydebug).to be_truthy
ydah marked this conversation as resolved.
Show resolved Hide resolved
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