Class: Rattler::Util::ParserCLI

Inherits:
Object
  • Object
show all
Defined in:
lib/rattler/util/parser_cli.rb

Overview

ParserCLI defines a command line interface for generated parsers.

Direct Known Subclasses

GrammarCLI

Constant Summary collapse

@@graph_formats =

The supported graph file formats

%w{jpg png svg pdf ps ps2 eps}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser_class) ⇒ ParserCLI

Create a new command line interface for the given parser class

Parameters:

  • parser_class (Class)

    the parser class to run the command line interface for



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rattler/util/parser_cli.rb', line 23

def initialize(parser_class)
  @parser_class = parser_class

  OptionParser.new do |opts|

    opts.banner = "Usage: #{File.basename($0)} [filenames] [options]"

    opts.separator ''

    opts.on '-g', '--graphviz',
            'Display the parse tree using GraphViz dotty' do |g|
      @graphviz = {:dot => '|dotty'}
    end

    @@graph_formats.each do |format|
      a = 'aefhilmnorsx'.include?(format[0]) ? 'an' : 'a'
      desc = "#{format.upcase} file"
      opts.on "--#{format} FILENAME",
              "Output the parse tree as #{a} #{desc} using GraphViz" do |f|
        @graphviz = {format => f}
      end
    end

    opts.on '-f', '--file FILENAME',
            'Output the parse tree as a GraphViz DOT language file' do |file|
      @graphviz = {:dot => file}
    end

    opts.on '-d', '--dot',
            'Output the parse tree in the GraphViz DOT language' do |d|
      @graphviz = {:dot => nil}
    end

    opts.separator ''

    opts.on_tail '-h', '--help', 'Show this message' do
      abort "#{opts}\n"
    end
  end.parse!(ARGV)
end

Class Method Details

.run(parser_class) ⇒ Object

Create and run the command line interface for the given parser class

Parameters:

  • parser_class (Class)

    the parser class to run the command line interface for



15
16
17
# File 'lib/rattler/util/parser_cli.rb', line 15

def self.run(parser_class)
  self.new(parser_class).run
end

Instance Method Details

#runObject

Run the command line interface



65
66
67
68
69
# File 'lib/rattler/util/parser_cli.rb', line 65

def run
  show_result @parser_class.parse!(ARGF.read)
rescue Rattler::Runtime::SyntaxError => e
  puts e
end