From cc435be6593d3be03a89bcefd8eb7f0e0934b63b Mon Sep 17 00:00:00 2001 From: Roguelearg Date: Thu, 14 Sep 2017 16:52:54 +0200 Subject: [PATCH] Add exit before evaluate grammar if there are errors --- exe/mm2ep-depend | 3 +- lib/mm2ep_depend/parser.rb | 62 +++++++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/exe/mm2ep-depend b/exe/mm2ep-depend index 1a0fa74..94c6eed 100755 --- a/exe/mm2ep-depend +++ b/exe/mm2ep-depend @@ -20,7 +20,7 @@ module Mm2ep vars.each do |var| tab_vars[var.split('=')[0]] = var.split('=')[1] end - binding.pry + # Give vars name and value from shell command to parser parser.names=tab_vars @@ -28,6 +28,7 @@ module Mm2ep pp token puts "RAW : #{line}" puts "EVAL: #{token.to_s}" + exit 1 unless token.errors.empty? puts "RESULT: #{token.compute}" end end diff --git a/lib/mm2ep_depend/parser.rb b/lib/mm2ep_depend/parser.rb index 943b50b..aab4c80 100755 --- a/lib/mm2ep_depend/parser.rb +++ b/lib/mm2ep_depend/parser.rb @@ -1,14 +1,15 @@ module Mm2ep module Depend class TreeExpr + def compute raise NotImplementedError end end class TreeValue + def value - raise "No value for #{@name}" if @value.nil? @value end @@ -18,13 +19,22 @@ module Mm2ep end class VarValue < TreeValue + attr_reader :errors + def initialize(str, value) + @errors = [] @name = str @value = case value when /true/i then true when /false/i then false else value end + if @value.nil? + @errors << VarNotDefined.new( + message:"No value for #{@name}", + var: @name + ) + end end def compute @@ -37,7 +47,10 @@ module Mm2ep end class NumberValue < TreeValue + attr_reader :errors + def initialize(str) + @errors = [] @value = str end @@ -51,7 +64,10 @@ module Mm2ep end class StringValue < TreeValue + attr_reader :errors + def initialize(str) + @errors = [] @value = str end @@ -65,7 +81,10 @@ module Mm2ep end class BoolValue < TreeValue + attr_reader :errors + def initialize(str) + @errors = [] @value = case str when /true/i then true when /false/i then false @@ -82,37 +101,51 @@ module Mm2ep end class AndOp < TreeExpr - def initialize(expr1, expr2) - @expr1 = expr1 - @expr2 = expr2 + attr_reader :errors + + def initialize(lval, rval) + @errors = [] + @errors.concat lval.errors + @errors.concat rval.errors + @lval = lval + @rval = rval end def compute - @expr1.compute && @expr2.compute + @lval.compute && @rval.compute end def to_s - "( #{@expr1} ) AND ( #{@expr2} )" + "( #{@lval} ) AND ( #{@rval} )" end end class OrOp - def initialize(expr1, expr2) - @expr1 = expr1 - @expr2 = expr2 + attr_reader :errors + + def initialize(lval, rval) + @errors = [] + @errors.concat lval.errors + @errors.concat rval.errors + @lval = lval + @rval = rval end def compute - @expr1.compute || @expr2.compute + @lval.compute || @rval.compute end def to_s - "( #{@expr1} ) OR ( #{@expr2} )" + "( #{@lval} ) OR ( #{@rval} )" end end class NotOp + attr_reader :errors + def initialize(expr) + @errors = [] + @errors.concat expr.errors @expr = expr end @@ -126,9 +159,15 @@ module Mm2ep end class EqOp + attr_reader :errors + def initialize(lval, rval) + @errors = [] + @errors.concat lval.errors + @errors.concat rval.errors @lval = lval @rval = rval + end def compute @@ -142,6 +181,7 @@ module Mm2ep class Parser < Rly::Yacc attr_writer :names + attr_reader :errors # def names(tab) # @names = tab # end