Skip to content

Commit

Permalink
refactor grammar & no need to reload the grammar everytime
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Dec 29, 2009
1 parent c611563 commit d386a93
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.rdoc
@@ -1,8 +1,8 @@
= TextQuery

Does it match? When regular expressions are not enough, textquery is the answer. For
example, regular expressions cannot evaluate recursive rules, and often result in
overly verbosed and complicated expressions.
example, regular expressions cannot evaluate recursive rules and often result in
overly verbose and complicated expressions.

Textquery is a simple PEG grammar with support for:
- AND (spaces are implicit AND's)
Expand Down
13 changes: 12 additions & 1 deletion lib/textquery/textquery.rb
@@ -1,8 +1,19 @@
require 'treetop'

class WordMatch < Treetop::Runtime::SyntaxNode
def eval(text)
not text.match(/^#{query}\s|\s#{query}\s|\s#{query}$|^#{query}$/).nil?
end

def query
Regexp.escape(text_value)
end
end

Treetop.load File.dirname(__FILE__) + "/textquery_grammar"

class TextQuery
def initialize(query = '')
Treetop.load File.dirname(__FILE__) + "/textquery_grammar"
@parser = TextQueryGrammarParser.new
@query = nil

Expand Down
20 changes: 2 additions & 18 deletions lib/textquery/textquery_grammar.treetop
Expand Up @@ -45,27 +45,11 @@ grammar TextQueryGrammar
end

rule word
[^\s\(\)]+ {
def eval(text)
not text.match(/^#{query}\s|\s#{query}\s|\s#{query}$|^#{query}$/).nil?
end

def query
Regexp.escape(text_value)
end
}
[^\s\(\)]+ <WordMatch>
end

rule words
[^\']+ {
def eval(text)
not text.match(/^#{query}\s|\s#{query}\s|\s#{query}$|^#{query}$/).nil?
end

def query
Regexp.escape(text_value)
end
}
[^\']+ <WordMatch>
end

rule value
Expand Down

0 comments on commit d386a93

Please sign in to comment.