Skip to content

Commit

Permalink
NOT operator
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Dec 28, 2009
1 parent a5bf8cc commit 3a82f4d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 13 additions & 1 deletion spec/textquery_spec.rb
Expand Up @@ -69,5 +69,17 @@ def parse(input)
parse("a AND b OR c").eval("c").should be_false
parse("a AND b OR c").eval("b").should be_false
end


it "should accept logical NOT" do
%w[- NOT].each do |operator|
parse("#{operator} a").eval("a").should be_false
parse("#{operator} #{operator} a").eval("a").should be_true

parse("#{operator} a OR a").eval("a").should be_true
parse("a OR #{operator} a").eval("a").should be_true

parse("b AND #{operator} a").eval("b").should be_true
parse("b AND #{operator} a").eval("a").should be_false
end
end
end
14 changes: 14 additions & 0 deletions textquery.treetop
Expand Up @@ -27,6 +27,14 @@ grammar TextQuery
}
end

rule unary
('-' / 'NOT') {
def eval(a)
not a
end
}
end

rule space
[\s]*
end
Expand All @@ -44,6 +52,12 @@ grammar TextQuery
end

rule value
operator:unary space value {
def eval(text)
operator.eval(value.eval(text))
end
}
/
word
end

Expand Down

0 comments on commit 3a82f4d

Please sign in to comment.