Class: LL::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ll/cli.rb

Overview

CLI that can be used to generate ruby-ll parsers from a grammar file.

Instance Method Summary (collapse)

Instance Method Details

- (Object) generate(input, options)

Parameters:

  • input (String)
  • options (Hash)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ll/cli.rb', line 43

def generate(input, options)
  raw_grammar    = File.read(input)
  parser         = Parser.new(raw_grammar, input)
  gcompiler      = GrammarCompiler.new
  codegen        = CodeGenerator.new
  configcompiler = ConfigurationCompiler.new

  ast      = parser.parse
  cgrammar = gcompiler.compile(ast)

  cgrammar.display_messages

  if cgrammar.valid?
    config = configcompiler.generate(cgrammar)
    output = codegen.generate(config, options[:requires])

    File.open(options[:output], 'w') do |file|
      file.write(output)
    end
  else
    exit 1
  end
end

- (String) output_from_input(input)

Parameters:

  • input (String)

Returns:

  • (String)


33
34
35
36
37
# File 'lib/ll/cli.rb', line 33

def output_from_input(input)
  input_ext = File.extname(input)

  return input.gsub(/#{Regexp.compile(input_ext)}$/, '.rb')
end

- (Array) parse(argv)

Parameters:

  • argv (Array)

Returns:

  • (Array)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ll/cli.rb', line 71

def parse(argv)
  options = {
    :requires => true,
    :output   => nil
  }

  parser = OptionParser.new do |opt|
    opt.summary_indent = '  '

    opt.banner = <<-EOF.strip
Usage: ruby-ll [INPUT-GRAMMAR] [OPTIONS]

About:

  Generates a Ruby LL(1) parser from a ruby-ll compatible grammar file.

Examples:

  ruby-ll lib/ll/parser.rll                   # output goes to lib/ll/parser.rl
  ruby-ll lib/ll/parser.rll -o /tmp/parser.rb # output goes to /tmp/parser.rb
    EOF

    opt.separator "\nOptions:\n\n"

    opt.on '-h', '--help', 'Shows this help message' do
      abort parser.to_s
    end

    opt.on '--no-requires', 'Disables adding of require calls' do
      options[:requires] = false
    end

    opt.on '-o [PATH]', '--output [PATH]', 'Writes output to PATH' do |val|
      options[:output] = val
    end

    opt.on '-v', '--version', 'Shows the current version' do
      puts "ruby-ll #{VERSION} on #{RUBY_DESCRIPTION}"
      exit
    end
  end

  leftovers = parser.parse(argv)

  return options, leftovers
end

- (Object) run(argv = ARGV)

Parameters:

  • argv (Array) (defaults to: ARGV)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ll/cli.rb', line 9

def run(argv = ARGV)
  options, leftovers = parse(argv)

  if leftovers.empty?
    abort <<-EOF.strip
Error: you must specify a grammar input file'

#{parser}
EOF
  end

  input = File.expand_path(leftovers[0])

  unless options[:output]
    options[:output] = output_from_input(input)
  end

  generate(input, options)
end