diff --git a/lib/lrama/option_parser.rb b/lib/lrama/option_parser.rb index fc245f76..c0c3f813 100644 --- a/lib/lrama/option_parser.rb +++ b/lib/lrama/option_parser.rb @@ -65,11 +65,19 @@ def parse_by_option_parser(argv) o.separator 'Output:' o.on('-H', '--header=[FILE]', 'also produce a header file named FILE') {|v| @options.header = true; @options.header_file = v } o.on('-d', 'also produce a header file') { @options.header = true } - o.on('-r', '--report=THINGS', Array, 'also produce details on the automaton') {|v| @report = v } + o.on('-r', '--report=REPORTS', Array, 'also produce details on the automaton') {|v| @report = v } o.on_tail '' - o.on_tail 'Valid Reports:' - o.on_tail " #{VALID_REPORTS.join(' ')}" - + o.on_tail 'REPORTS is a list of comma-separated words that can include:' + o.on_tail ' states describe the states' + o.on_tail ' itemsets complete the core item sets with their closure' + o.on_tail ' lookaheads explicitly associate lookahead tokens to items' + o.on_tail ' solved describe shift/reduce conflicts solving' + o.on_tail ' counterexamples, cex generate conflict counterexamples' + o.on_tail ' rules list unused rules' + o.on_tail ' terms list unused terminals' + o.on_tail ' verbose report detailed internal state and analysis results' + o.on_tail ' all include all the above reports' + o.on_tail ' none disable all reports' o.on('--report-file=FILE', 'also produce details on the automaton output to a file named FILE') {|v| @options.report_file = v } o.on('-o', '--output=FILE', 'leave output to FILE') {|v| @options.outfile = v } @@ -91,34 +99,34 @@ def parse_by_option_parser(argv) end end - BISON_REPORTS = %w[states itemsets lookaheads solved counterexamples cex all none] - OTHER_REPORTS = %w[rules terms verbose] - NOT_SUPPORTED_REPORTS = %w[cex none] - VALID_REPORTS = BISON_REPORTS + OTHER_REPORTS - NOT_SUPPORTED_REPORTS + ALIASED_REPORTS = { cex: :counterexamples } + VALID_REPORTS = %i[states itemsets lookaheads solved counterexamples rules terms verbose] def validate_report(report) - list = VALID_REPORTS h = { grammar: true } + return h if report.empty? + return {} if report == ['none'] + if report == ['all'] + VALID_REPORTS.each { |r| h[r] = true } + return h + end report.each do |r| - if list.include?(r) - h[r.to_sym] = true + aliased = aliased_report_option(r) + if VALID_REPORTS.include?(aliased) + h[aliased] = true else raise "Invalid report option \"#{r}\"." end end - if h[:all] - (BISON_REPORTS - NOT_SUPPORTED_REPORTS).each do |r| - h[r.to_sym] = true - end - - h.delete(:all) - end - return h end + def aliased_report_option(opt) + (ALIASED_REPORTS[opt.to_sym] || opt).to_sym + end + VALID_TRACES = %w[ none locations scan parse automaton bitsets closure grammar rules actions resource diff --git a/spec/lrama/option_parser_spec.rb b/spec/lrama/option_parser_spec.rb index 9fb294a7..e4d7ed98 100644 --- a/spec/lrama/option_parser_spec.rb +++ b/spec/lrama/option_parser_spec.rb @@ -54,7 +54,7 @@ Output: -H, --header=[FILE] also produce a header file named FILE -d also produce a header file - -r, --report=THINGS also produce details on the automaton + -r, --report=REPORTS also produce details on the automaton --report-file=FILE also produce details on the automaton output to a file named FILE -o, --output=FILE leave output to FILE --trace=THINGS also output trace logs at runtime @@ -67,8 +67,17 @@ -V, --version output version information and exit -h, --help display this help and exit - Valid Reports: - states itemsets lookaheads solved counterexamples all rules terms verbose + REPORTS is a list of comma-separated words that can include: + states describe the states + itemsets complete the core item sets with their closure + lookaheads explicitly associate lookahead tokens to items + solved describe shift/reduce conflicts solving + counterexamples, cex generate conflict counterexamples + rules list unused rules + terms list unused terminals + verbose report detailed internal state and analysis results + all include all the above reports + none disable all reports Valid Traces: none locations scan parse automaton bitsets closure grammar rules actions resource sets muscles tools m4-early m4 skeleton time ielr cex all @@ -81,21 +90,43 @@ describe "#validate_report" do let(:option_parser) { Lrama::OptionParser.new } - describe "valid options are passed" do + context "when no options are passed" do + it "returns option hash with grammar flag enabled" do + opts = option_parser.send(:validate_report, []) + expect(opts).to eq({grammar: true}) + end + end + + context "when valid options are passed" do it "returns option hash" do opts = option_parser.send(:validate_report, ["states", "itemsets"]) expect(opts).to eq({grammar: true, states: true, itemsets: true}) end - describe "all is passed" do + context "when cex is passed" do + it "returns option hash counterexamples flag enabled" do + opts = option_parser.send(:validate_report, ["cex"]) + expect(opts).to eq({grammar: true, counterexamples: true}) + end + end + + context "when all is passed" do it "returns option hash all flags enabled" do opts = option_parser.send(:validate_report, ["all"]) expect(opts).to eq({ grammar: true, states: true, itemsets: true, lookaheads: true, solved: true, counterexamples: true, + rules: true, terms: true, verbose: true }) end end + + context "when none is passed" do + it "returns empty option hash" do + opts = option_parser.send(:validate_report, ["none"]) + expect(opts).to eq({}) + end + end end describe "invalid options are passed" do