namarara/lib/parser2.rb
2017-09-11 12:08:27 +02:00

42 lines
927 B
Ruby

module Mm2ep
module Depend
class Parser2
attr_reader :tokens
TOKEN = {
'L_PAR' => "(",
'NUMBER' => /[0-9]+(\.[0-9]+)?/,
'STRING' => /"[^"]*"/,
'EQ_OP' => "=",
'T_BOOL' => /[tT]rue/,
'F_BOOL' => /[fF]alse/,
'VAR' => /[a-z][a-zA-Z0-9_]+/,
'AND_OP' => "AND",
'OR_OP' => "OR",
'NOT_OP' => "NOT",
'SPACE' => /\s+/,
'R_PAR' => ")"
}.freeze
def initialize
@tokens = []
end
def parse s
s = s.split(' ')
s.each_with_index do |element, idx|
TOKEN.each do |token, regex|
next if element.sub!(regex, '').nil?
@tokens << token
break if element.nil?
end
@tokens <<'SPACE' unless idx == s.length-1
end
# puts @tokens
@tokens
end # def
end # class
end # module
end # module