Class: SearchQueryParser::Grammar

Inherits:
Object
  • Object
show all
Defined in:
lib/search_query_parser/grammar.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(e) ⇒ Grammar

Returns a new instance of Grammar.



19
20
21
22
23
24
25
26
27
# File 'lib/search_query_parser/grammar.rb', line 19

def initialize(e)
  if e.is_a?(String)
    @expression = [:term, e]
  elsif e.is_a?(Array)
    @expression = e
  else
    raise RuntimeError.new("Unknown expression #{e.inspect} of #{e.class.name}")
  end
end

Instance Attribute Details

#expressionObject (readonly)

Returns the value of attribute expression.



17
18
19
# File 'lib/search_query_parser/grammar.rb', line 17

def expression
  @expression
end

Class Method Details

.prepare_text(text) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/search_query_parser/grammar.rb', line 3

def self.prepare_text(text)
  text.
    gsub(/[&]+/, '&').
    gsub(/[|]+/, '|').
    gsub(/[^[:alnum:]\-()&|!]+/, ' ').
    gsub(/([^[:alnum:]]|^)\-/, '\1').
    gsub(/\-([^[:alnum:]]|$)/, '\1').
    gsub(/ ?([|&]) ?/, '\1').
    gsub(/(!) /, '\1').
    gsub(/(\() /, '\1').
    gsub(/ (\))/, '\1').
    strip
end

Instance Method Details

#!Object



53
54
55
# File 'lib/search_query_parser/grammar.rb', line 53

def !
  self.not
end

#&(other) ⇒ Object



39
40
41
# File 'lib/search_query_parser/grammar.rb', line 39

def &(other)
  self.and(other)
end

#>>(other) ⇒ Object



32
33
34
# File 'lib/search_query_parser/grammar.rb', line 32

def >>(other)
  join(other)
end

#and(other) ⇒ Object



36
37
38
# File 'lib/search_query_parser/grammar.rb', line 36

def and(other)
  self.class.new([:and, self, other])
end

#join(other) ⇒ Object



29
30
31
# File 'lib/search_query_parser/grammar.rb', line 29

def join(other)
  self.class.new([:join, self, other])
end

#notObject



50
51
52
# File 'lib/search_query_parser/grammar.rb', line 50

def not
  self.class.new([:not, self])
end

#or(other) ⇒ Object



43
44
45
# File 'lib/search_query_parser/grammar.rb', line 43

def or(other)
  self.class.new([:or, self, other])
end

#reduce(&block) ⇒ Object

Block to reduce must be given and accept (op, x, y), (term, x), (not, x) and return the result of applying op to (x, y) or x (for ‘term’ and ‘not’)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/search_query_parser/grammar.rb', line 60

def reduce(&block)
  op, x, y = @expression
  case op
  when :term
    yield(:term, x, nil)
  when :not
    yield(:not, x.reduce(&block), nil)
  when :and
    yield(:and, x.reduce(&block), y.reduce(&block))
  when :or
    yield(:or, x.reduce(&block), y.reduce(&block))
  when :join
    yield(:join, x.reduce(&block), y.reduce(&block))
  else
    raise RuntimeError.new("Unknown op #{op.inspect}")
  end
end

#to_sObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/search_query_parser/grammar.rb', line 78

def to_s
  reduce do |op, x, y|
    case op
    when :term
      x
    when :and
      "(#{x} & #{y})"
    when :or
      "(#{x} | #{y})"
    when :join
      "(#{x} <-> #{y})"
    when :not
      "(! #{x})"
    end
  end
end

#|(other) ⇒ Object



46
47
48
# File 'lib/search_query_parser/grammar.rb', line 46

def |(other)
  self.or(other)
end