Add logger for illegal characters

This commit is contained in:
Brendon 2017-09-20 17:49:54 +02:00
parent 40be4077fe
commit 960b3637c9
2 changed files with 19 additions and 5 deletions

View file

@ -7,20 +7,26 @@ require 'rly'
require 'pry' require 'pry'
require 'mm2ep_depend' require 'mm2ep_depend'
require 'pp' require 'pp'
require 'logger'
module Mm2ep module Mm2ep
module Depend module Depend
class ParseCli < Thor class ParseCli < Thor
desc 'parse INFILE VARS', 'Parse INFILE into tokens and evaluate vars' desc 'parse OPTIONS INFILE VARS', 'Parse INFILE into tokens and evaluate VARS'
method_option :logfile, :aliases => "-l", :desc => "Logger with logfile"
method_option :stdout, :aliases => "-f", :desc => "Logger with STDOUT"
def parse(infile, *vars) def parse(infile, *vars)
logger = Logger.new('illegal_character.log') if options[:logfile]
logger = Logger.new(STDOUT) if options[:stdout]
line = File.read(infile).gsub(/\n/,'') line = File.read(infile).gsub(/\n/,'')
parser = Parser.new(Lexer.new) lexer = Lexer.new(logger)
parser = Parser.new(lexer)
tab_vars = {} tab_vars = {}
vars.each do |var| vars.each do |var|
tab_vars[var.split('=')[0]] = var.split('=')[1] tab_vars[var.split('=')[0]] = var.split('=')[1]
end end
binding.pry
# Give vars name and value from shell command to parser # Give vars name and value from shell command to parser
parser.names=tab_vars parser.names=tab_vars
@ -28,7 +34,6 @@ module Mm2ep
pp token pp token
puts "RAW : #{line}" puts "RAW : #{line}"
puts "EVAL: #{token.to_s}" puts "EVAL: #{token.to_s}"
parser.check_grammar line, token parser.check_grammar line, token
exit 1 unless !token.nil? && token.errors.empty? exit 1 unless !token.nil? && token.errors.empty?

View file

@ -1,6 +1,8 @@
module Mm2ep module Mm2ep
module Depend module Depend
class Lexer < Rly::Lex class Lexer < Rly::Lex
attr_reader :logger
ignore "\t\n " ignore "\t\n "
# token :SPACE, /\s+/ # token :SPACE, /\s+/
@ -20,8 +22,15 @@ module Mm2ep
token :OR_OP, /OR/ token :OR_OP, /OR/
token :NOT_OP, /NOT/ token :NOT_OP, /NOT/
def initialize(logger = nil)
@logger = logger
super()
end
on_error do |t| on_error do |t|
puts "Illegal character #{t.value}" unless t.lexer.logger.nil?
t.lexer.logger.error "Illegal character #{t.value}"
end
t.lexer.pos += 1 t.lexer.pos += 1
nil nil
end end