-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathquery_string.y
233 lines (220 loc) · 5.41 KB
/
query_string.y
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
%{
package querystr
import(
"github.com/blugelabs/bluge"
)
%}
%union {
s string
n int
f float64
q bluge.Query
pf *float64}
%token tSTRING tPHRASE tPLUS tMINUS tCOLON tBOOST tNUMBER tSTRING tGREATER tLESS
tEQUAL tTILDE
%type <s> tSTRING
%type <s> tPHRASE
%type <s> tNUMBER
%type <s> posOrNegNumber
%type <s> tTILDE
%type <s> tBOOST
%type <q> searchBase
%type <pf> searchSuffix
%type <n> searchPrefix
%%
input:
searchParts {
yylex.(*lexerWrapper).logDebugGrammarf("INPUT")
};
searchParts:
searchPart searchParts {
yylex.(*lexerWrapper).logDebugGrammarf("SEARCH PARTS")
}
|
searchPart {
yylex.(*lexerWrapper).logDebugGrammarf("SEARCH PART")
};
searchPart:
searchPrefix searchBase searchSuffix {
q := $2
if $3 != nil {
var err error
q, err = queryStringSetBoost($2, *$3)
if err != nil {
yylex.(*lexerWrapper).lex.Error(err.Error())
}
}
switch($1) {
case queryShould:
yylex.(*lexerWrapper).query.AddShould(q)
case queryMust:
yylex.(*lexerWrapper).query.AddMust(q)
case queryMustNot:
yylex.(*lexerWrapper).query.AddMustNot(q)
}
};
searchPrefix:
/* empty */ {
$$ = queryShould
}
|
tPLUS {
yylex.(*lexerWrapper).logDebugGrammarf("PLUS")
$$ = queryMust
}
|
tMINUS {
yylex.(*lexerWrapper).logDebugGrammarf("MINUS")
$$ = queryMustNot
};
searchBase:
tSTRING {
yylex.(*lexerWrapper).logDebugGrammarf("STRING - %s", $1)
$$ = queryStringStringToken(yylex, "", $1)
}
|
tSTRING tTILDE {
yylex.(*lexerWrapper).logDebugGrammarf("FUZZY STRING - %s %s", $1, $2)
q, err := queryStringStringTokenFuzzy(yylex, "", $1, $2)
if err != nil {
yylex.(*lexerWrapper).lex.Error(err.Error())
}
$$ = q
}
|
tSTRING tCOLON tSTRING tTILDE {
yylex.(*lexerWrapper).logDebugGrammarf("FIELD - %s FUZZY STRING - %s %s", $1, $3, $4)
q, err := queryStringStringTokenFuzzy(yylex, $1, $3, $4)
if err != nil {
yylex.(*lexerWrapper).lex.Error(err.Error())
}
$$ = q
}
|
tNUMBER {
yylex.(*lexerWrapper).logDebugGrammarf("STRING - %s", $1)
q, err := queryStringNumberToken(yylex, "", $1)
if err != nil {
yylex.(*lexerWrapper).lex.Error(err.Error())
}
$$ = q
}
|
tPHRASE {
yylex.(*lexerWrapper).logDebugGrammarf("PHRASE - %s", $1)
$$ = queryStringPhraseToken("", $1)
}
|
tSTRING tCOLON tSTRING {
yylex.(*lexerWrapper).logDebugGrammarf("FIELD - %s STRING - %s", $1, $3)
$$ = queryStringStringToken(yylex, $1, $3)
}
|
tSTRING tCOLON posOrNegNumber {
yylex.(*lexerWrapper).logDebugGrammarf("FIELD - %s STRING - %s", $1, $3)
q, err := queryStringNumberToken(yylex, $1, $3)
if err != nil {
yylex.(*lexerWrapper).lex.Error(err.Error())
}
$$ = q
}
|
tSTRING tCOLON tPHRASE {
yylex.(*lexerWrapper).logDebugGrammarf("FIELD - %s PHRASE - %s", $1, $3)
$$ = queryStringPhraseToken($1, $3)
}
|
tSTRING tCOLON tGREATER posOrNegNumber {
yylex.(*lexerWrapper).logDebugGrammarf("FIELD - GREATER THAN %s", $4)
q, err := queryStringNumericRangeGreaterThanOrEqual($1, $4, false)
if err != nil {
yylex.(*lexerWrapper).lex.Error(err.Error())
}
$$ = q
}
|
tSTRING tCOLON tGREATER tEQUAL posOrNegNumber {
yylex.(*lexerWrapper).logDebugGrammarf("FIELD - GREATER THAN OR EQUAL %s", $5)
q, err := queryStringNumericRangeGreaterThanOrEqual($1, $5, true)
if err != nil {
yylex.(*lexerWrapper).lex.Error(err.Error())
}
$$ = q
}
|
tSTRING tCOLON tLESS posOrNegNumber {
yylex.(*lexerWrapper).logDebugGrammarf("FIELD - LESS THAN %s", $4)
q, err := queryStringNumericRangeLessThanOrEqual($1, $4, false)
if err != nil {
yylex.(*lexerWrapper).lex.Error(err.Error())
}
$$ = q
}
|
tSTRING tCOLON tLESS tEQUAL posOrNegNumber {
yylex.(*lexerWrapper).logDebugGrammarf("FIELD - LESS THAN OR EQUAL %s", $5)
q, err := queryStringNumericRangeLessThanOrEqual($1, $5, true)
if err != nil {
yylex.(*lexerWrapper).lex.Error(err.Error())
}
$$ = q
}
|
tSTRING tCOLON tGREATER tPHRASE {
yylex.(*lexerWrapper).logDebugGrammarf("FIELD - GREATER THAN DATE %s", $4)
q, err := queryStringDateRangeGreaterThanOrEqual(yylex, $1, $4, false)
if err != nil {
yylex.(*lexerWrapper).lex.Error(err.Error())
}
$$ = q
}
|
tSTRING tCOLON tGREATER tEQUAL tPHRASE {
yylex.(*lexerWrapper).logDebugGrammarf("FIELD - GREATER THAN OR EQUAL DATE %s", $5)
q, err := queryStringDateRangeGreaterThanOrEqual(yylex, $1, $5, true)
if err != nil {
yylex.(*lexerWrapper).lex.Error(err.Error())
}
$$ = q
}
|
tSTRING tCOLON tLESS tPHRASE {
yylex.(*lexerWrapper).logDebugGrammarf("FIELD - LESS THAN DATE %s", $4)
q, err := queryStringDateRangeLessThanOrEqual(yylex, $1, $4, false)
if err != nil {
yylex.(*lexerWrapper).lex.Error(err.Error())
}
$$ = q
}
|
tSTRING tCOLON tLESS tEQUAL tPHRASE {
yylex.(*lexerWrapper).logDebugGrammarf("FIELD - LESS THAN OR EQUAL DATE %s", $5)
q, err := queryStringDateRangeLessThanOrEqual(yylex, $1, $5, true)
if err != nil {
yylex.(*lexerWrapper).lex.Error(err.Error())
}
$$ = q
};
searchSuffix:
/* empty */ {
$$ = nil
}
|
tBOOST {
$$ = nil
yylex.(*lexerWrapper).logDebugGrammarf("BOOST %s", $1)
boost, err := queryStringParseBoost($1)
if err != nil {
yylex.(*lexerWrapper).lex.Error(err.Error())
} else {
$$ = &boost
}
};
posOrNegNumber:
tNUMBER {
$$ = $1
}
|
tMINUS tNUMBER {
$$ = "-" + $2
};