Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support attribute tags with customisable : delimiter

  • Loading branch information...
commit a90c5c279dbce3b19d7a7baa44a5d47051ae96f2 1 parent a1091e6
Clifford Heath cjheath authored
2  .gitignore
View
@@ -2,3 +2,5 @@ pkg
.bundle
Gemfile.lock
.DS_Store
+.*.swp
+.*.swo
43 lib/textquery/textquery.rb
View
@@ -66,12 +66,39 @@ def accept(&block)
class TextQueryError < RuntimeError
end
+class TextQueryGrammarParser
+ attr_reader :options
+
+ def initialize options = {}
+ @options = options
+ super()
+ end
+
+ def update_options(options)
+ @options = {:delim => ' '}.merge(options)
+ @options[:delim] = "(#{[@options[:delim]].flatten.map { |opt| opt.is_a?(Regexp) ? opt : RegExp.escape(opt) }.join("|")})"
+ end
+
+ def _nt_attribute_delimiter
+ attribute_delimiter = options.has_key?(:attribute_delimiter) ? options[:attribute_delimiter] : ':'
+ if attribute_delimiter && has_terminal?(attribute_delimiter, false, index)
+ attribute_delimiter_size = attribute_delimiter.size
+ r0 = instantiate_node(SyntaxNode,input, index...(index + attribute_delimiter_size))
+ @index += attribute_delimiter_size
+ else
+ terminal_parse_failure(attribute_delimiter)
+ r0 = nil
+ end
+ r0
+ end
+end
+
class TextQuery
def initialize(query = '', options = {})
- @parser = TextQueryGrammarParser.new
+ @parser = TextQueryGrammarParser.new options
@query = nil
- update_options(options)
+ @parser.update_options(options)
parse(query) if not query.empty?
end
@@ -85,10 +112,10 @@ def parse(query)
end
def eval(input, options = {})
- update_options(options) if not options.empty?
+ @parser.update_options(options) if not options.empty?
if @query
- @query.eval(input, @options)
+ @query.eval(input, @parser.options)
else
raise TextQueryError, 'no query specified'
end
@@ -96,7 +123,7 @@ def eval(input, options = {})
alias :match? :eval
def accept(options = {}, &block)
- update_options(options) if not options.empty?
+ @parser.update_options(options) if not options.empty?
if @query
@query.accept(&block)
@@ -109,10 +136,4 @@ def terminal_failures
@parser.terminal_failures
end
- private
-
- def update_options(options)
- @options = {:delim => ' '}.merge(options)
- @options[:delim] = "(#{[@options[:delim]].flatten.map { |opt| opt.is_a?(Regexp) ? opt : RegExp.escape(opt) }.join("|")})"
- end
end
5 lib/textquery/textquery_grammar.treetop
View
@@ -101,8 +101,7 @@ grammar TextQueryGrammar
end
rule alpha
- # Any character except USASCII non-alphas. Pretty dodgey, but Treetop makes anything else rather hard
- [^\x00-\x40\x5B-\x5E\x60\x7B-\x7F]
+ [[:alpha:]]
end
rule alphanumeric
@@ -114,7 +113,7 @@ grammar TextQueryGrammar
end
rule term
- attribute ':' value
+ attribute attribute_delimiter value
# If you wish to restrict the allowed attributes, add a sempred here:
# &{|s| is_valid_attribute(s[0].text_value) }
{
8 spec/textquery_spec.rb
View
@@ -295,6 +295,14 @@ def parse(input)
TextQuery.new("a -tag:'b c'").accept { |*a| a }.should == [ :and, [ :value, 'a' ], [ :not, [ :attribute, 'tag', [ :value, 'b c' ] ] ] ]
end
+ it 'should allow query with attribute using custom delimiter' do
+ TextQuery.new("tag#b", :attribute_delimiter => '#').accept { |*a| a }.should == [ :attribute, 'tag', [ :value, 'b' ] ]
+ end
+
+ it 'should allow query disabling the attribute delimiter' do
+ TextQuery.new("tag:b", :attribute_delimiter => nil).accept { |*a| a }.should == [ :value, 'tag:b' ]
+ end
+
it 'should allow query with syntax similar to attributes' do
TextQuery.new("notatag;b").accept { |*a| a }.should == [ :value, 'notatag;b' ]
end
Please sign in to comment.
Something went wrong with that request. Please try again.