-
Notifications
You must be signed in to change notification settings - Fork 9
/
textquery_grammar.treetop
130 lines (110 loc) · 2.17 KB
/
textquery_grammar.treetop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
grammar TextQueryGrammar
rule expression
logical / value
end
rule logical
op1:value space operator:binary space op2:expression {
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
rule binary
('&' /'AND') {
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
rule unary
('-' / 'NOT') {
def eval(a)
not a
end
def accept(a, &block)
block.call(:not, a)
end
}
end
rule space
[\s]*
end
rule word
[^\s\(\)]+ <WordMatch>
end
rule single_quote
[']
end
rule single_quote_words
[^']+ <WordMatch>
end
rule double_quote
["]
end
rule double_quote_words
[^"]+ <WordMatch>
end
rule value
'(' space expression space ')' {
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 double_quote_words double_quote {
def eval(text, opt)
double_quote_words.eval(text, opt)
end
def accept(&block)
double_quote_words.accept(&block)
end
}
/
single_quote single_quote_words single_quote {
def eval(text, opt)
single_quote_words.eval(text, opt)
end
def accept(&block)
single_quote_words.accept(&block)
end
}
/
word
end
end