diff --git a/lib/lrama.rb b/lib/lrama.rb index 08c73dec..501e65d5 100644 --- a/lib/lrama.rb +++ b/lib/lrama.rb @@ -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" diff --git a/lib/lrama/command.rb b/lib/lrama/command.rb index a96b653d..a09f95e6 100644 --- a/lib/lrama/command.rb +++ b/lib/lrama/command.rb @@ -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( diff --git a/lib/lrama/trace_reporter.rb b/lib/lrama/trace_reporter.rb new file mode 100644 index 00000000..87c01a4c --- /dev/null +++ b/lib/lrama/trace_reporter.rb @@ -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