Skip to content

Commit

Permalink
Merge pull request #456 from ydah/adjust-trace-command
Browse files Browse the repository at this point in the history
Adjust the output of the trace help command
  • Loading branch information
ydah committed Jul 2, 2024
2 parents c8bae2e + efb7858 commit fd28ccc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 14 deletions.
34 changes: 23 additions & 11 deletions lib/lrama/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,16 @@ def parse_by_option_parser(argv)
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 }

o.on('--trace=THINGS', Array, 'also output trace logs at runtime') {|v| @trace = v }
o.on('--trace=TRACES', Array, 'also output trace logs at runtime') {|v| @trace = v }
o.on_tail ''
o.on_tail 'Valid Traces:'
o.on_tail " #{VALID_TRACES.join(' ')}"

o.on_tail 'TRACES is a list of comma-separated words that can include:'
o.on_tail ' automaton display states'
o.on_tail ' closure display states'
o.on_tail ' rules display grammar rules'
o.on_tail ' actions display grammar rules with actions'
o.on_tail ' time display generation time'
o.on_tail ' all include all the above traces'
o.on_tail ' none disable all traces'
o.on('-v', 'reserved, do nothing') { }
o.separator ''
o.separator 'Diagnostics:'
Expand Down Expand Up @@ -131,18 +135,26 @@ def aliased_report_option(opt)
end

VALID_TRACES = %w[
none locations scan parse automaton bitsets
closure grammar rules actions resource
sets muscles tools m4-early m4 skeleton time
ielr cex all
locations scan parse automaton bitsets closure
grammar rules actions resource sets muscles
tools m4-early m4 skeleton time ielr cex
]
NOT_SUPPORTED_TRACES = %w[
locations scan parse bitsets grammar resource
sets muscles tools m4-early m4 skeleton ielr cex
]

def validate_trace(trace)
list = VALID_TRACES
h = {}
return h if trace.empty? || trace == ['none']
supported = VALID_TRACES - NOT_SUPPORTED_TRACES
if trace == ['all']
supported.each { |t| h[t.to_sym] = true }
return h
end

trace.each do |t|
if list.include?(t)
if supported.include?(t)
h[t.to_sym] = true
else
raise "Invalid trace option \"#{t}\"."
Expand Down
52 changes: 49 additions & 3 deletions spec/lrama/option_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
-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
--trace=TRACES also output trace logs at runtime
-v reserved, do nothing
Diagnostics:
Expand All @@ -82,8 +82,14 @@
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
TRACES is a list of comma-separated words that can include:
automaton display states
closure display states
rules display grammar rules
actions display grammar rules with actions
time display generation time
all include all the above traces
none disable all traces
HELP
end
Expand Down Expand Up @@ -139,6 +145,46 @@
end
end

describe "#validate_trace" do
let(:option_parser) { Lrama::OptionParser.new }

context "when no options are passed" do
it "returns empty option hash" do
opts = option_parser.send(:validate_trace, [])
expect(opts).to eq({})
end
end

context "when valid options are passed" do
it "returns option hash" do
opts = option_parser.send(:validate_trace, ["automaton", "closure"])
expect(opts).to eq({automaton: true, closure: true})
end

context "when all is passed" do
it "returns option hash all flags enabled" do
opts = option_parser.send(:validate_trace, ["all"])
expect(opts).to eq({
automaton: true, closure: true, rules: true, actions: true, time: true
})
end
end

context "when none is passed" do
it "returns empty option hash" do
opts = option_parser.send(:validate_trace, ["none"])
expect(opts).to eq({})
end
end
end

describe "invalid options are passed" do
it "returns option hash" do
expect { option_parser.send(:validate_trace, ["invalid"]) }.to raise_error(/Invalid trace option/)
end
end
end

describe "@grammar_file" do
context "file is specified" do
it "@grammar_file is file name" do
Expand Down

0 comments on commit fd28ccc

Please sign in to comment.