Skip to content

Commit

Permalink
Remove category and only support ON/OFF
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Jun 26, 2024
1 parent 89e2b2e commit 33e6440
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 87 deletions.
2 changes: 1 addition & 1 deletion lib/lrama/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 8 additions & 6 deletions lib/lrama/diagnostics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 1 addition & 31 deletions lib/lrama/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -90,14 +89,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 }
Expand Down Expand Up @@ -159,27 +151,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
4 changes: 2 additions & 2 deletions lib/lrama/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sig/lrama/options.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/lrama/diagnostics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
56 changes: 12 additions & 44 deletions spec/lrama/option_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -247,57 +240,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
context "when --warnings is passed" do
it "returns true" do
option_parser = Lrama::OptionParser.new
option_parser.send(:parse, ["--warnings=all", 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({all: true})
expect(options.diagnostic).to be true
end
end

context "when conflicts-sr 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-sr", 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 conflicts-rr is passed" do
it "returns { conflicts_sr: true, conflicts_rr: true }" do
option_parser = Lrama::OptionParser.new
option_parser.send(:parse, ["--warnings=conflicts-rr", 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
Expand Down

0 comments on commit 33e6440

Please sign in to comment.