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
- #generate(input, options) ⇒ Object
- #output_from_input(input) ⇒ String
- #parse(argv) ⇒ Array
- #run(argv = ARGV) ⇒ Object
Instance Method Details
#generate(input, options) ⇒ Object
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 |
#output_from_input(input) ⇒ 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 |
#parse(argv) ⇒ 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) = { :requires => true, :output => nil } parser = OptionParser.new do |opt| opt.summary_indent = ' ' opt. = "Usage: ruby-ll [INPUT-GRAMMAR] [OPTIONS]\n\nAbout:\n\n Generates a Ruby LL(1) parser from a ruby-ll compatible grammar file.\n\nExamples:\n\n ruby-ll lib/ll/parser.rll # output goes to lib/ll/parser.rl\n ruby-ll lib/ll/parser.rll -o /tmp/parser.rb # output goes to /tmp/parser.rb\n EOF\n\n opt.separator \"\\nOptions:\\n\\n\"\n\n opt.on '-h', '--help', 'Shows this help message' do\n abort parser.to_s\n end\n\n opt.on '--no-requires', 'Disables adding of require calls' do\n options[:requires] = false\n end\n\n opt.on '-o [PATH]', '--output [PATH]', 'Writes output to PATH' do |val|\n options[:output] = val\n end\n\n opt.on '-v', '--version', 'Shows the current version' do\n puts \"ruby-ll \#{VERSION} on \#{RUBY_DESCRIPTION}\"\n exit\n end\n end\n\n leftovers = parser.parse(argv)\n\n return options, leftovers\nend\n".strip |
#run(argv = ARGV) ⇒ Object
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 "Error: you must specify a grammar input file'\n\n\#{parser}\n".strip end input = File.(leftovers[0]) unless [:output] [:output] = output_from_input(input) end generate(input, ) end |