2017-09-04 09:19:29 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
$:.insert(0, 'lib')
|
|
|
|
|
|
|
|
require 'thor'
|
2017-09-07 15:05:06 +00:00
|
|
|
require 'rly'
|
|
|
|
require 'pry'
|
2019-12-21 16:15:14 +00:00
|
|
|
require 'namarara'
|
2017-09-12 14:23:44 +00:00
|
|
|
require 'pp'
|
2017-09-20 15:49:54 +00:00
|
|
|
require 'logger'
|
2017-09-04 09:19:29 +00:00
|
|
|
|
2019-12-21 16:15:14 +00:00
|
|
|
module Namarara
|
|
|
|
class ParseCli < Thor
|
|
|
|
desc 'parse OPTIONS INFILE VARS', 'Parse INFILE into tokens and evaluate VARS'
|
|
|
|
method_option :logfile, :type => :string, :aliases => '-l', :default => '-', :desc => "Logger with logfile"
|
|
|
|
def parse(infile, *vars)
|
|
|
|
unless options[:logfile].eql? '-'
|
|
|
|
logger = Logger.new("#{options[:logfile]}.log")
|
|
|
|
else
|
|
|
|
logger = Logger.new(STDOUT)
|
|
|
|
end
|
|
|
|
line = File.read(infile).gsub(/\n/,'')
|
|
|
|
lexer = Lexer.new(logger)
|
|
|
|
parser = Parser.new(lexer)
|
|
|
|
tab_vars = {}
|
|
|
|
vars.each do |var|
|
|
|
|
tab_vars[var.split('=')[0]] = var.split('=')[1]
|
2017-09-04 09:19:29 +00:00
|
|
|
end
|
2019-12-21 16:15:14 +00:00
|
|
|
|
|
|
|
# Give vars name and value from shell command to parser
|
|
|
|
parser.names=tab_vars
|
|
|
|
|
|
|
|
token = parser.parse(line.chomp, true)
|
|
|
|
pp token
|
|
|
|
puts "RAW : #{line}"
|
|
|
|
puts "EVAL: #{token.to_s}"
|
|
|
|
parser.check_grammar line, token
|
|
|
|
exit 1 unless !token.nil? && token.errors.empty?
|
|
|
|
|
|
|
|
puts "RESULT: #{token.compute}"
|
|
|
|
|
2017-09-04 09:19:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-21 16:15:14 +00:00
|
|
|
Namarara::ParseCli.start(ARGV)
|