diff --git a/exe/mm2ep-depend b/exe/mm2ep-depend index d59d18e..b2a5dab 100755 --- a/exe/mm2ep-depend +++ b/exe/mm2ep-depend @@ -7,6 +7,8 @@ require 'rly' require 'pry' require 'parser.rb' require 'lexer.rb' +require 'lexer2.rb' +require 'parser2.rb' module Mm2ep module Depend @@ -16,9 +18,14 @@ module Mm2ep def parse(infile) File.open(infile).each_line do |line| - parser = Parser.new(Lexer.new) - token = parser.parse(line.chomp) - puts token + # parser = Parser.new(Lexer.new) + # puts line.inspect + # token = parser.parse(line.chomp, true) + # puts token + # binding.pry + parser = Parser2.new + lexer = TestLexer.new(parser.parse(line)) + puts lexer.testlexe end end end diff --git a/lib/lexer2.rb b/lib/lexer2.rb new file mode 100644 index 0000000..502a390 --- /dev/null +++ b/lib/lexer2.rb @@ -0,0 +1,110 @@ +module Mm2ep + module Depend + class TestLexer + attr_reader :truc, :tokens, :fait, :non_traites, :i, :atteint + + BOOL_EXPR = ['T_BOOL', 'F_BOOL'].freeze + + EXPR = { + 'EXPR_T1' => ['VAR'], + 'EXPR_T2' => ['BOOL_EXPR'], + 'EXPR_T3' => ['VAR', 'SPACE', 'EQ_OP', 'SPACE', 'NUMBER'], + 'EXPR_T4' => ['VAR', 'SPACE', 'EQ_OP', 'SPACE', 'STRING'], + 'EXPR_T5' => ['VAR', 'SPACE', 'EQ_OP', 'SPACE', 'T_BOOL'], + 'EXPR_T6' => ['VAR', 'SPACE', 'EQ_OP', 'SPACE', 'F_BOOL'], + 'EXPR_NT1' => ['EXPR', 'SPACE', 'OR_OP', 'SPACE', 'EXPR'], + 'EXPR_NT2' => ['EXPR', 'SPACE', 'AND_OP', 'SPACE', 'EXPR'], + 'EXPR_NT3' => ['L_PAR', 'SPACE', 'EXPR', 'SPACE', 'R_PAR'], + 'EXPR_NT4' => ['NOT_OP', 'SPACE', 'EXPR'] + }.freeze + + def initialize tokens + @non_traites = [] + @fait = false + @tokens= tokens + @i = 0 + @atteint = false + end + + def try_expr + @truc = Hash[@truc.sort_by { |key, value| value.length }.reverse!] + @truc.each do |_, value| + if @tokens[@i...@i+value.length].eql? value + @tokens.slice!(@i...@i+value.length) + @tokens.insert(@i, 'EXPR') + unless @non_traites.empty? + @i -= 2 + @non_traites.pop(2) + @fait = true + break + end + end + end + end + + def move_forward + @non_traites << @tokens[@i] + @non_traites << @tokens[@i+1] + @i += 2 + end + + def move_backwards + @i -= 2 + @non_traites.pop(2) + end + + def expr_nt + @fait = false + try_expr + # binding.pry + if !fait && @i < @tokens.size-1 + if !@atteint || (!@tokens[@i].eql?('OR_OP') && !@tokens[@i].eql?('AND_OP')) + move_forward + else + move_backwards + end + elsif !fait + move_backwards + end + end + + def expr + @truc = EXPR.select { |key, value| value.include? @tokens[@i] } + if(!@truc.keys[0].nil? && @truc.keys[0].include?('EXPR_NT')) + expr_nt + elsif(!@truc.keys[0].nil? && @truc.keys[0].include?('EXPR_T')) + @truc = Hash[@truc.sort_by { |key, value| value.length }.reverse!] + try_expr + elsif !@non_traites.empty? + move_backwards + else + raise 'BUG 2 !!!!!' + end + end + + def lexe + if @i == @tokens.size-1 + @atteint = true + end + if !EXPR.select{|key, value| value.include? @tokens[@i]}.nil? + expr + elsif !@non_traites.empty? + move_backwards + else + raise 'BUG !!!!' + end + end + + def testlexe + cle, valeur = nil + compteur = 0 + while @tokens.size != 1 && compteur < 1000 + lexe + compteur += 1 + end + return false unless @tokens. size == 1 && @tokens[0].eql?('EXPR') + return true + end + end + end +end diff --git a/lib/parser2.rb b/lib/parser2.rb new file mode 100644 index 0000000..3c7af34 --- /dev/null +++ b/lib/parser2.rb @@ -0,0 +1,42 @@ +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