diff --git a/lib/lrama/command.rb b/lib/lrama/command.rb index 4530f27e..0095c1a1 100644 --- a/lib/lrama/command.rb +++ b/lib/lrama/command.rb @@ -61,7 +61,7 @@ def run(argv) logger = Lrama::Logger.new exit false unless Lrama::GrammarValidator.new(grammar, states, logger).valid? - Lrama::Diagnostics.new(grammar, states, logger).run(**options.diagnostic_opts) + Lrama::Diagnostics.new(grammar, states, logger).run(options.diagnostic) end end end diff --git a/lib/lrama/diagnostics.rb b/lib/lrama/diagnostics.rb index 1f6f9ed9..e9da398c 100644 --- a/lib/lrama/diagnostics.rb +++ b/lib/lrama/diagnostics.rb @@ -8,19 +8,21 @@ def initialize(grammar, states, logger) @logger = logger end - def run(conflicts_sr: false, conflicts_rr: false, parameterizing_redefined: false) - diagnose_conflict(conflicts_sr, conflicts_rr) - diagnose_parameterizing_redefined if parameterizing_redefined + def run(diagnostic) + if diagnostic + diagnose_conflict + diagnose_parameterizing_redefined + end end private - def diagnose_conflict(conflicts_sr, conflicts_rr) - if conflicts_sr && @states.sr_conflicts_count != 0 + def diagnose_conflict + if @states.sr_conflicts_count != 0 @logger.warn("shift/reduce conflicts: #{@states.sr_conflicts_count} found") end - if conflicts_rr && @states.rr_conflicts_count != 0 + if @states.rr_conflicts_count != 0 @logger.warn("reduce/reduce conflicts: #{@states.rr_conflicts_count} found") end end diff --git a/lib/lrama/option_parser.rb b/lib/lrama/option_parser.rb index 8459e818..517693ad 100644 --- a/lib/lrama/option_parser.rb +++ b/lib/lrama/option_parser.rb @@ -16,7 +16,6 @@ def parse(argv) @options.trace_opts = validate_trace(@trace) @options.report_opts = validate_report(@report) - @options.diagnostic_opts = validate_diagnostic(@diagnostic) @options.grammar_file = argv.shift if !@options.grammar_file @@ -82,14 +81,7 @@ def parse_by_option_parser(argv) o.on('-v', 'reserved, do nothing') { } o.separator '' o.separator 'Diagnostics:' - o.on('-W', '--warnings=CATEGORY', Array, 'report the warnings falling in category') {|v| @diagnostic = v } - o.separator '' - o.separator 'Warning categories include:' - o.separator ' conflicts-sr Shift/Reduce conflicts (enabled by default)' - o.separator ' conflicts-rr Reduce/Reduce conflicts (enabled by default)' - o.separator ' parameterizing-redefined redefinition of parameterizing rule' - o.separator ' all all warnings' - o.separator ' none turn off all warnings' + o.on('-W', '--warnings', 'report the warnings') {|v| @options.diagnostic = true } o.separator '' o.separator 'Error Recovery:' o.on('-e', 'enable error recovery') {|v| @options.error_recovery = true } @@ -151,27 +143,5 @@ def validate_trace(trace) return h end - - DIAGNOSTICS = %w[] - HYPHENATED_DIAGNOSTICS = %w[conflicts-sr conflicts-rr parameterizing-redefined] - - def validate_diagnostic(diagnostic) - h = { conflicts_sr: true, conflicts_rr: true } - return h if diagnostic.nil? - return {} if diagnostic.any? { |d| d == 'none' } - return { all: true } if diagnostic.any? { |d| d == 'all' } - - diagnostic.each do |d| - if DIAGNOSTICS.include?(d) - h[d.to_sym] = true - elsif HYPHENATED_DIAGNOSTICS.include?(d) - h[d.gsub('-', '_').to_sym] = true - else - raise "Invalid diagnostic option \"#{d}\"." - end - end - - return h - end end end diff --git a/lib/lrama/options.rb b/lib/lrama/options.rb index 9e2929ac..ccd76803 100644 --- a/lib/lrama/options.rb +++ b/lib/lrama/options.rb @@ -7,7 +7,7 @@ class Options :report_file, :outfile, :error_recovery, :grammar_file, :trace_opts, :report_opts, - :diagnostic_opts, :y, :debug + :diagnostic, :y, :debug def initialize @skeleton = "bison/yacc.c" @@ -19,7 +19,7 @@ def initialize @grammar_file = nil @trace_opts = nil @report_opts = nil - @diagnostic_opts = nil + @diagnostic = false @y = STDIN @debug = false end diff --git a/sig/lrama/options.rbs b/sig/lrama/options.rbs index 2fddb15d..d32dd675 100644 --- a/sig/lrama/options.rbs +++ b/sig/lrama/options.rbs @@ -9,7 +9,7 @@ module Lrama attr_accessor grammar_file: String? attr_accessor trace_opts: Hash[Symbol, bool]? attr_accessor report_opts: Hash[Symbol, bool]? - attr_accessor diagnostic_opts: Hash[Symbol, bool]? + attr_accessor diagnostic: bool attr_accessor y: IO attr_accessor debug: bool diff --git a/spec/lrama/diagnostics_spec.rb b/spec/lrama/diagnostics_spec.rb index e2d765e1..87185662 100644 --- a/spec/lrama/diagnostics_spec.rb +++ b/spec/lrama/diagnostics_spec.rb @@ -75,7 +75,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS states.compute logger = Lrama::Logger.new allow(logger).to receive(:warn) - Lrama::Diagnostics.new(grammar, states, logger).run(conflicts_sr: true, conflicts_rr: true) + Lrama::Diagnostics.new(grammar, states, logger).run(true) expect(logger).to have_received(:warn).with("shift/reduce conflicts: 2 found") expect(logger).to have_received(:warn).with("reduce/reduce conflicts: 1 found") end @@ -109,7 +109,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS states.compute logger = Lrama::Logger.new allow(logger).to receive(:warn) - Lrama::Diagnostics.new(grammar, states, logger).run(parameterizing_redefined: true) + Lrama::Diagnostics.new(grammar, states, logger).run(true) expect(logger).to have_received(:warn).with("parameterizing rule redefined: foo(X)") expect(logger).to have_received(:warn).with("parameterizing rule redefined: foo(Y)") end diff --git a/spec/lrama/option_parser_spec.rb b/spec/lrama/option_parser_spec.rb index 270efa06..9c977e1c 100644 --- a/spec/lrama/option_parser_spec.rb +++ b/spec/lrama/option_parser_spec.rb @@ -61,14 +61,7 @@ -v reserved, do nothing Diagnostics: - -W, --warnings=CATEGORY report the warnings falling in category - - Warning categories include: - conflicts-sr Shift/Reduce conflicts (enabled by default) - conflicts-rr Reduce/Reduce conflicts (enabled by default) - parameterizing-redefined redefinition of parameterizing rule - all all warnings - none turn off all warnings + -W, --warnings report the warnings Error Recovery: -e enable error recovery @@ -216,57 +209,32 @@ end end - describe "@diagnostic_opts" do + describe "@diagnostic" do context "when diagnostic option is not passed" do - it "returns { conflicts_sr: true, conflicts_rr: true }" do + it "returns false" do option_parser = Lrama::OptionParser.new option_parser.send(:parse, [fixture_path("command/basic.y")]) options = option_parser.instance_variable_get(:@options) - expect(options.diagnostic_opts).to eq({ conflicts_sr: true, conflicts_rr: true }) + expect(options.diagnostic).to be false end end context "when diagnostic option is passed" do - context "when all is passed" do - it "returns { all: true }" do - option_parser = Lrama::OptionParser.new - option_parser.send(:parse, ["--warnings=all", fixture_path("command/basic.y")]) - options = option_parser.instance_variable_get(:@options) - expect(options.diagnostic_opts).to eq({all: true}) - end - end - - context "when conflicts-sr is passed" do - it "returns { conflicts_sr: true, conflicts_rr: true }" do + context "when --warnings is passed" do + it "returns true" do option_parser = Lrama::OptionParser.new - option_parser.send(:parse, ["--warnings=conflicts-sr", fixture_path("command/basic.y")]) + option_parser.send(:parse, ["--warnings", fixture_path("command/basic.y")]) options = option_parser.instance_variable_get(:@options) - expect(options.diagnostic_opts).to eq({ conflicts_sr: true, conflicts_rr: true }) + expect(options.diagnostic).to be true end end - context "when conflicts-rr is passed" do - it "returns { conflicts_sr: true, conflicts_rr: true }" do + context "when -W is passed" do + it "returns true" do option_parser = Lrama::OptionParser.new - option_parser.send(:parse, ["--warnings=conflicts-rr", fixture_path("command/basic.y")]) + option_parser.send(:parse, ["-W", fixture_path("command/basic.y")]) options = option_parser.instance_variable_get(:@options) - expect(options.diagnostic_opts).to eq({ conflicts_sr: true, conflicts_rr: true }) - end - end - - context "when none is passed" do - it "returns empty hash" do - option_parser = Lrama::OptionParser.new - option_parser.send(:parse, ["--warnings=none", fixture_path("command/basic.y")]) - options = option_parser.instance_variable_get(:@options) - expect(options.diagnostic_opts).to eq({}) - end - end - - context "when invalid option is passed" do - it "raise error" do - option_parser = Lrama::OptionParser.new - expect { option_parser.send(:parse, ["--warnings=invalid", fixture_path("command/basic.y")]) }.to raise_error(/Invalid diagnostic option/) + expect(options.diagnostic).to be true end end end