Skip to content
Browse files

Provide a mechanism to traverse the parsed query.

  • Loading branch information...
1 parent d6f85cd commit 6560101c49de5b6f3dbd1ae77c6833053a957be3 @eric eric committed Nov 5, 2010
Showing with 54 additions and 0 deletions.
  1. +13 −0 lib/textquery/textquery.rb
  2. +36 −0 lib/textquery/textquery_grammar.treetop
  3. +5 −0 spec/textquery_spec.rb
View
13 lib/textquery/textquery.rb
@@ -52,6 +52,9 @@ def eval(text, opt)
end
end
+ def accept(&block)
+ block.call(:value, text_value)
+ end
end
Treetop.load File.dirname(__FILE__) + "/textquery_grammar"
@@ -88,6 +91,16 @@ def eval(input, options = {})
end
alias :match? :eval
+ def accept(options = {}, &block)
+ update_options(options) if not options.empty?
+
+ if @query
+ @query.accept(&block)
+ else
+ raise TextQueryError, 'no query specified'
+ end
+ end
+
def terminal_failures
@parser.terminal_failures
end
View
36 lib/textquery/textquery_grammar.treetop
@@ -9,12 +9,20 @@ grammar TextQueryGrammar
def eval(text, opt)
operator.eval(op1.eval(text, opt), op2.eval(text, opt))
end
+
+ def accept(&block)
+ operator.accept(op1.accept(&block), op2.accept(&block), &block)
+ end
}
/
op1:value [\s]+ op2:expression {
def eval(text, opt)
op1.eval(text, opt) && op2.eval(text, opt)
end
+
+ def accept(&block)
+ block.call(:and, op1.accept(&block), op2.accept(&block))
+ end
}
end
@@ -23,12 +31,20 @@ grammar TextQueryGrammar
def eval(a,b)
a && b
end
+
+ def accept(a, b, &block)
+ block.call(:and, a, b)
+ end
}
/
'OR' {
def eval(a,b)
a || b
end
+
+ def accept(a, b, &block)
+ block.call(:or, a, b)
+ end
}
end
@@ -37,6 +53,10 @@ grammar TextQueryGrammar
def eval(a)
not a
end
+
+ def accept(a, &block)
+ block.call(:not, a)
+ end
}
end
@@ -69,24 +89,40 @@ grammar TextQueryGrammar
def eval(text, opt)
expression.eval(text, opt)
end
+
+ def accept(&block)
+ expression.accept(&block)
+ end
}
/
operator:unary space value {
def eval(text, opt)
operator.eval(value.eval(text, opt))
end
+
+ def accept(&block)
+ operator.accept(value.accept(&block), &block)
+ end
}
/
double_quote space double_quote_words space double_quote {
def eval(text, opt)
double_quote_words.eval(text, opt)
end
+
+ def accept(&block)
+ double_quote_words.accept(&block)
+ end
}
/
single_quote space single_quote_words space single_quote {
def eval(text, opt)
single_quote_words.eval(text, opt)
end
+
+ def accept(&block)
+ single_quote_words.accept(&block)
+ end
}
/
word
View
5 spec/textquery_spec.rb
@@ -254,5 +254,10 @@ def parse(input)
TextQuery.new("a", :delim => [%r{a{2,3}}, 'b']).match?("aab").should be_false
TextQuery.new("b", :delim => [%r{a{2,3}}, 'a']).match?("aab").should be_true
end
+
+ it 'should allow query to be traversed' do
+ TextQuery.new("a b").accept { |*a| a }.should == [ :and, [ :value, 'a' ], [ :value, 'b' ] ]
+ TextQuery.new("a OR b").accept { |*a| a }.should == [ :or, [ :value, 'a' ], [ :value, 'b' ] ]
+ end
end
end

0 comments on commit 6560101

Please sign in to comment.
Something went wrong with that request. Please try again.