Class: LL::CLI
- Inherits:
-
Object
- Object
- LL::CLI
- 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)
-
- (Object) generate(input, options)
-
- (String) output_from_input(input)
-
- (Array) parse(argv)
-
- (Object) run(argv = ARGV)
Instance Method Details
- (Object) generate(input, options)
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, ) 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. if cgrammar.valid? config = configcompiler.generate(cgrammar) output = codegen.generate(config, [:requires]) File.open([:output], 'w') do |file| file.write(output) end else exit 1 end end |
- (String) output_from_input(input)
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)
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) = { :requires => true, :output => nil } parser = OptionParser.new do |opt| opt.summary_indent = ' ' opt. = <<-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 [:requires] = false end opt.on '-o [PATH]', '--output [PATH]', 'Writes output to PATH' do |val| [: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 , leftovers end |
- (Object) run(argv = 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) , leftovers = parse(argv) if leftovers.empty? abort <<-EOF.strip Error: you must specify a grammar input file' #{parser} EOF end input = File.(leftovers[0]) unless [:output] [:output] = output_from_input(input) end generate(input, ) end |