lib/ustate/query_string.treetop in ustate-client-0.0.6 vs lib/ustate/query_string.treetop in ustate-client-0.0.7

- old
+ new

@@ -1,10 +1,10 @@ module UState grammar QueryString # (state =~ "foo" or state == "bar") and service == "foo" # Binding order proceeds from loosest to tightest. - + rule or first:and rest:(space 'or' space and)* { def query rest.elements.map { |x| x.and }.inject(first.query) do |a, sub| Query::Or.new a, sub.query @@ -19,26 +19,38 @@ end } end rule and - first:primary rest:(space 'and' space primary)* { + first:(not / primary) rest:(space 'and' space p:(not / primary))* { def query - rest.elements.map { |x| x.primary }.inject(first.query) do |a, sub| + rest.elements.map { |x| x.p }.inject(first.query) do |a, sub| Query::And.new a, sub.query end end def sql - rest.elements.map { |x| x.primary }. + rest.elements.map { |x| x.p }. inject(first.sql) do |a, sub| a & sub.sql end end } end + rule not + 'not' space p:(not / primary) { + def query + Query::Not.new p.query + end + + def sql + ~ p.sql + end + } + end + rule primary '(' space? x:or space? ')' { def query x.query end @@ -50,11 +62,11 @@ / predicate end rule predicate - equals / not_equals / approximately + true / false / less_equal / less / greater_equal / greater / equals / not_equals / approximately end rule approximately field space? '=~' space? string { def query @@ -64,11 +76,59 @@ def sql Sequel::SQL::StringExpression.like field.sql, string.sql end } end + + rule less_equal + field space? '<=' space? value { + def query + Query::LessEqual.new field.sql, value.sql + end + def sql + Sequel::SQL::BooleanExpression.new(:<=, field.sql, value.sql) + end + } + end + + rule less + field space? '<' space? value { + def query + Query::Less.new field.sql, value.sql + end + + def sql + Sequel::SQL::BooleanExpression.new(:<, field.sql, value.sql) + end + } + end + + rule greater_equal + field space? '>=' space? value { + def query + Query::GreaterEqual.new field.sql, value.sql + end + + def sql + Sequel::SQL::BooleanExpression.new(:>=, field.sql, value.sql) + end + } + end + + rule greater + field space? '>' space? value { + def query + Query::Greater.new field.sql, value.sql + end + + def sql + Sequel::SQL::BooleanExpression.new(:>, field.sql, value.sql) + end + } + end + rule not_equals field space? '!=' space? value { def query Query::NotEquals.new field.sql, value.sql end @@ -90,13 +150,45 @@ end } end rule value - double_quoted_string / float / integer / null + true / false / double_quoted_string / float / integer / null end + rule true + 'true' { + def query + Query::True.new + end + + def ruby_value + true + end + + def sql + Sequel::TRUE + end + } + end + + rule false + 'false' { + def query + Query::False.new + end + + def ruby_value + false + end + + def sql + Sequel::FALSE + end + } + end + rule integer '-'? [0-9]+ { def ruby_value Integer(text_value) end @@ -114,10 +206,10 @@ } end rule field # [a-zA-Z] [a-zA-Z_0-9]* - ("state" / "host" / "service" / "description" / "metric_f") { + ("state" / "host" / "service" / "description" / "metric_f" / "time") { def sql text_value.to_sym end } end