Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce the Lrama::TraceReporter class to organize the command.rb #444

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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