From a1ef27a0f0b4444d14c52a17c86d90a6d3b63663 Mon Sep 17 00:00:00 2001 From: ydah Date: Wed, 12 Jun 2024 02:16:16 +0900 Subject: [PATCH] Introduce the `Lrama::TraceReporter` class to organize the command.rb This PR introduce the `Lrama::TraceReporter` class to organize the code. --- lib/lrama.rb | 1 + lib/lrama/command.rb | 11 ++--------- lib/lrama/trace_reporter.rb | 30 ++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 lib/lrama/trace_reporter.rb 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