Skip to content

Commit

Permalink
Merge pull request #444 from ydah/refactor-trace-reporter
Browse files Browse the repository at this point in the history
Introduce the `Lrama::TraceReporter` class to organize the command.rb
  • Loading branch information
ydah authored Jun 12, 2024
2 parents 9a75458 + a1ef27a commit 0ea8965
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/lrama.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
require_relative "lrama/state"
require_relative "lrama/states"
require_relative "lrama/states_reporter"
require_relative "lrama/trace_reporter"
require_relative "lrama/version"
require_relative "lrama/warning"
11 changes: 2 additions & 9 deletions lib/lrama/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,8 @@ def run(argv)
end
end

if options.trace_opts && options.trace_opts[:rules]
puts "Grammar rules:"
grammar.rules.each { |rule| puts rule.display_name }
end

if options.trace_opts && options.trace_opts[:actions]
puts "Grammar rules with actions:"
grammar.rules.each { |rule| puts rule.with_actions }
end
reporter = Lrama::TraceReporter.new(grammar)
reporter.report(**options.trace_opts)

File.open(options.outfile, "w+") do |f|
Lrama::Output.new(
Expand Down
30 changes: 30 additions & 0 deletions lib/lrama/trace_reporter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Lrama
class TraceReporter
def initialize(grammar)
@grammar = grammar
end

def report(**options)
_report(**options)
end

private

def _report(rules: false, actions: false, **_)
report_rules if rules
report_actions if actions
end

def report_rules
puts "Grammar rules:"
@grammar.rules.each { |rule| puts rule.display_name }
end

def report_actions
puts "Grammar rules with actions:"
@grammar.rules.each { |rule| puts rule.with_actions }
end
end
end

0 comments on commit 0ea8965

Please sign in to comment.