Rename namarara executable to namarara-cli

This commit is contained in:
Glenn Y. Rolland 2019-12-21 20:30:31 +01:00
parent d6a347e961
commit e3459991bc
2 changed files with 72 additions and 46 deletions

View file

@ -1,46 +0,0 @@
#!/usr/bin/env ruby
$:.insert(0, 'lib')
require 'thor'
require 'rly'
require 'pry'
require 'namarara'
require 'pp'
require 'logger'
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]
end
# 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}"
end
end
end
Namarara::ParseCli.start(ARGV)

72
exe/namarara-cli Executable file
View file

@ -0,0 +1,72 @@
#!/usr/bin/env ruby
$:.insert(0, 'lib')
require 'thor'
require 'rly'
require 'namarara'
require 'json'
module Namarara
class ParseCli < Thor
class_option :debug,
type: :boolean,
aliases: '-d',
default: false,
desc: "Enable debugging output"
class_option :format,
type: :string,
aliases: '-f',
default: 'text',
enum: ['text','json'],
desc: "Output format"
desc 'file FILE VARS',
'Parse FILE into tokens then compute with VARS'
def file(infile, *vars)
line = File.read(infile).gsub(/\n/,'')
vars_hash = get_vars_hash(vars)
res = parse_string(line, vars_hash, options[:debug])
puts format(res, options[:format])
end
desc 'string STRING VARS',
'Parse STRING into tokens then compute with VARS'
def string(line, *vars)
vars_hash = get_vars_hash(vars)
res = parse_string(line, vars_hash, options[:debug])
puts format(res, options[:format])
end
private
# Convert VAR=value list into hash
def get_vars_hash(vars)
tab_vars = {}
vars.each do |var|
tab_vars[var.split('=')[0]] = var.split('=')[1]
end
tab_vars
end
def format(result, format)
case format
when 'text' then
txt = []
txt << "EXPR: #{result[:expr]}"
txt << "TREE: #{result[:tree]}"
result[:errors].each do |error|
txt << "ERROR: #{error}"
end
txt << "RESULT: #{result[:result]}"
txt.join("\n")
when 'json'
JSON.generate(result)
else
raise 'Unknown output format'
end
end
end
end
Namarara::ParseCli.start(ARGV)