From 934edc0bc7656e56a076d3502022c964617a2abb Mon Sep 17 00:00:00 2001 From: ydah Date: Wed, 5 Feb 2025 18:55:44 +0900 Subject: [PATCH 1/2] Enhance debug option handling to include 'parse.trace' definition 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' ^^^^^^^^^^^^^^^^^^^^^^^ ``` --- lib/lrama/option_parser.rb | 1 + lib/lrama/parser.rb | 2 +- parser.y | 2 +- spec/lrama/option_parser_spec.rb | 1 + spec/lrama/parser_spec.rb | 58 ++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 2 deletions(-) diff --git a/lib/lrama/option_parser.rb b/lib/lrama/option_parser.rb index f2589544..78474fc0 100644 --- a/lib/lrama/option_parser.rb +++ b/lib/lrama/option_parser.rb @@ -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) diff --git a/lib/lrama/parser.rb b/lib/lrama/parser.rb index 177e784e..c887609b 100644 --- a/lib/lrama/parser.rb +++ b/lib/lrama/parser.rb @@ -660,7 +660,7 @@ class Parser < Racc::Parser 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 diff --git a/parser.y b/parser.y index 840797d0..ce358bb3 100644 --- a/parser.y +++ b/parser.y @@ -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 diff --git a/spec/lrama/option_parser_spec.rb b/spec/lrama/option_parser_spec.rb index 3af371fa..4770bf00 100644 --- a/spec/lrama/option_parser_spec.rb +++ b/spec/lrama/option_parser_spec.rb @@ -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: diff --git a/spec/lrama/parser_spec.rb b/spec/lrama/parser_spec.rb index 14ef1728..aa4107f7 100644 --- a/spec/lrama/parser_spec.rb +++ b/spec/lrama/parser_spec.rb @@ -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" From d36f10ac1fc6aeba271ac88373e78c9483c4e46b Mon Sep 17 00:00:00 2001 From: ydah Date: Thu, 6 Feb 2025 09:44:00 +0900 Subject: [PATCH 2/2] Fix an incorrect description in parser_spec.rb --- spec/lrama/parser_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lrama/parser_spec.rb b/spec/lrama/parser_spec.rb index aa4107f7..72689a5f 100644 --- a/spec/lrama/parser_spec.rb +++ b/spec/lrama/parser_spec.rb @@ -54,7 +54,7 @@ context 'with define is specified' do context 'with `api.pure`' do - it '@yydebug is false' 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