lib/cel/parser.rb in cel-0.1.2 vs lib/cel/parser.rb in cel-0.2.0

- old
+ new

@@ -61,15 +61,24 @@ def parse(str) tokenize(str) do_parse rescue Racc::ParseError => err - ex = Cel::ParseError.new(err.message) - ex.set_backtrace(err.backtrace) - raise ex + raise parse_error(err) end +def parse_error(error) + parse_error = case error.message + when /parse error on value "([^"]+)" \(tRESERVED\)/ + Cel::ParseError.new("invalid usage of the reserved word \"#{$1}\"") + else + Cel::ParseError.new(error.message) + end + parse_error.set_backtrace(error.backtrace) + parse_error +end + def tokenize(str) str.force_encoding(Encoding::BINARY) unless str.valid_encoding? scanner = StringScanner.new(str) @@ -81,14 +90,10 @@ # skip whitespace when scanner.scan(/#(( *)|( ?(?<string>.*)))\n/) # skip comment lines when scanner.scan(NUMBER_REGEX) @q << convert_to_number(scanner) - when scanner.scan(/true|false/) - @q << [:tBOOL, scanner.matched == "true"] - when scanner.scan(/null/) - @q << [:tNULL, nil] when scanner.scan(/[bB]?[rR]?#{STRING_LIT_REGEX}/) # string # s = scanner.matched.yield_self {|s| s[1, s.length - 2] } # .gsub(DBL_QUOTE_STR_ESCAPE_SEQUENCES_RE) do |match| # case match # when '\\a' then "\a" @@ -113,11 +118,17 @@ else [:tSTRING, convert_to_string(str)] end when scanner.scan(IDENTIFIER_REGEX) word = scanner.matched - if RESERVED.include?(word) + if word == "null" + @q << [:tNULL, nil] + elsif word == "true" + @q << [:tBOOL, true] + elsif word == "false" + @q << [:tBOOL, false] + elsif RESERVED.include?(word) @q << [:tRESERVED, scanner.matched] elsif word == "in" @q << [OPERATORS[scanner.matched], scanner.matched] else @q << [:tIDENTIFIER, scanner.matched] @@ -468,11 +479,11 @@ 1, 47, :_reduce_none, 3, 49, :_reduce_41, 1, 49, :_reduce_42, 0, 45, :_reduce_43, 1, 45, :_reduce_none, - 5, 51, :_reduce_none, + 5, 51, :_reduce_45, 3, 51, :_reduce_46, 5, 50, :_reduce_47, 3, 50, :_reduce_48, 1, 48, :_reduce_49, 1, 48, :_reduce_50, @@ -736,11 +747,11 @@ end .,., module_eval(<<'.,.,', 'parser.ry', 55) def _reduce_30(val, _values, result) - result = Cel::Invoke.new(func: val[0], args: [val[2]].flatten(1)) + result = Cel::Invoke.new(func: val[0], args: [val[2]].flatten(1)) result end .,., module_eval(<<'.,.,', 'parser.ry', 56) @@ -772,11 +783,11 @@ # reduce 37 omitted module_eval(<<'.,.,', 'parser.ry', 65) def _reduce_38(val, _values, result) - result = nil + result = [] result end .,., module_eval(<<'.,.,', 'parser.ry', 67) @@ -809,10 +820,15 @@ end .,., # reduce 44 omitted -# reduce 45 omitted +module_eval(<<'.,.,', 'parser.ry', 76) + def _reduce_45(val, _values, result) + result = val[0].merge(Cel::Identifier.new(val[2]) => val[4]) + result + end +.,., module_eval(<<'.,.,', 'parser.ry', 77) def _reduce_46(val, _values, result) result = { Cel::Identifier.new(val[0]) => val[2] } result