Class: Lrama::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/lrama/command.rb

Instance Method Summary collapse

Instance Method Details

#run(argv) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lrama/command.rb', line 3

def run(argv)
  options = OptionParser.new.parse(argv)

  Report::Duration.enable if options.trace_opts[:time]

  warning = Lrama::Warning.new
  grammar = Lrama::Parser.new(options.y.read).parse
  options.y.close if options.y != STDIN
  states = Lrama::States.new(grammar, warning, trace_state: (options.trace_opts[:automaton] || options.trace_opts[:closure]))
  states.compute
  context = Lrama::Context.new(states)

  if options.report_file
    reporter = Lrama::StatesReporter.new(states)
    File.open(options.report_file, "w+") do |f|
      reporter.report(f, **options.report_opts)
    end
  end

  File.open(options.outfile, "w+") do |f|
    Lrama::Output.new(
      out: f,
      output_file_path: options.outfile,
      template_name: options.skeleton,
      grammar_file_path: options.grammar_file,
      header_file_path: options.header_file,
      context: context,
      grammar: grammar,
      error_recovery: options.error_recovery,
    ).render
  end

  if warning.has_error?
    exit 1
  end
end