forked from infinispan/infinispan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IckleParser.g
754 lines (610 loc) · 19.4 KB
/
IckleParser.g
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
* Copyright 2016, Red Hat Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
parser grammar IckleParser;
options {
tokenVocab=IckleLexer;
output=AST;
superClass=ParserBase;
}
@parser::header {
package org.infinispan.objectfilter.impl.ql.parse;
import java.util.ArrayList;
import java.util.List;
}
statement
@init { if (state.backtracking == 0) pushEnableParameterUsage(true); }
@after { popEnableParameterUsage(); }
: (selectStatement | deleteStatement) EOF!
;
selectStatement
: querySpec orderByClause? -> ^(QUERY querySpec orderByClause?)
;
deleteStatement
: deleteClause^ whereClause?
;
deleteClause
: delete_key^ fromClause
;
querySpec
: selectFrom whereClause? ( groupByClause havingClause? )? -> ^(QUERY_SPEC selectFrom whereClause? groupByClause? havingClause?)
;
groupByClause
@init { if (state.backtracking == 0) pushEnableParameterUsage(false); }
@after { popEnableParameterUsage(); }
: group_by_key^ groupingSpecification
;
havingClause
: having_key^ logicalExpression
;
groupingSpecification
: groupingValue ( COMMA! groupingValue )*
;
groupingValue
: additiveExpression collationSpecification? -> ^(GROUPING_VALUE additiveExpression collationSpecification?)
;
whereClause
: where_key^ logicalExpression
;
selectFrom
: sc=selectClause? fc=fromClause -> { generateImplicitSelectFrom((Tree) $sc.tree, (Tree) $fc.tree, $fc.aliasList) }
;
fromClause returns [List aliasList]
scope {
List<String> aliases;
}
@init { $fromClause::aliases = new ArrayList<>(); }
@after { $aliasList = $fromClause::aliases; }
: from_key^ persisterSpaces
;
persisterSpaces
: ps+=persisterSpace ( COMMA ps+=persisterSpace )* -> { generatePersisterSpacesTree($ps) }
;
persisterSpace
: persisterSpaceRoot ( qualifiedJoin | crossJoin )*
;
crossJoin
: cross_key join_key mainEntityPersisterReference
-> ^(PERSISTER_JOIN[$join_key.start, "persister-join"] cross_key mainEntityPersisterReference)
;
qualifiedJoin
@init { boolean isEntityReference = false; boolean hasFetch = false; }
@after { if (!hasFetch) $fromClause::aliases.add(((Tree)$ac.tree).getText()); }
: nonCrossJoinType join_key (fetch_key {hasFetch = true;})? path ac=aliasClause[true]
(on_key { isEntityReference = true; } logicalExpression
| propertyFetch? withClause?)
-> {isEntityReference}? ^(PERSISTER_JOIN[$join_key.start, "persister-join"] nonCrossJoinType ^(ENTITY_PERSISTER_REF ENTITY_NAME<EntityNameTree>[$path.start, $path.text, (Tree) $path.tree] aliasClause?) ^(on_key logicalExpression))
-> ^(PROPERTY_JOIN[$join_key.start, "property-join"] nonCrossJoinType fetch_key? aliasClause? propertyFetch? ^(PATH path) withClause?)
;
withClause
: with_key^ logicalExpression
;
nonCrossJoinType
: inner_key
| outerJoinType outer_key?
| -> INNER
;
outerJoinType
: left_key
| right_key
| full_key
;
persisterSpaceRoot
options {
backtrack=true;
} : hibernateLegacySyntax
| jpaCollectionReference
| mainEntityPersisterReference
;
mainEntityPersisterReference
@after { $fromClause::aliases.add(((Tree)$ac.tree).getText()); }
: entityName ac=aliasClause[true] propertyFetch?
-> ^(ENTITY_PERSISTER_REF entityName aliasClause? propertyFetch?)
;
propertyFetch
: fetch_key all_key properties_key -> PROP_FETCH[$fetch_key.start, "property-fetch"]
;
hibernateLegacySyntax returns [boolean isPropertyJoin]
@init {$isPropertyJoin = false;}
@after { $fromClause::aliases.add(((Tree)$ad.tree).getText()); }
: ad=aliasDeclaration in_key
(collectionExpression {$isPropertyJoin = true;} -> ^(PROPERTY_JOIN INNER[$in_key.start, "inner legacy"] aliasDeclaration collectionExpression))
;
jpaCollectionReference
@after { $fromClause::aliases.add(((Tree)$ac.tree).getText()); }
: in_key LPAREN propertyReference RPAREN ac=aliasClause[true]
-> ^(PROPERTY_JOIN INNER[$in_key.start, "inner"] aliasClause? propertyReference)
;
selectClause
: select_key^ distinct_key? rootSelectExpression
;
rootSelectExpression
: jpaSelectObjectSyntax
| explicitSelectList
;
explicitSelectList
: explicitSelectItem ( COMMA explicitSelectItem )* -> ^(SELECT_LIST explicitSelectItem+)
;
explicitSelectItem
: selectExpression
;
selectExpression
@init { if (state.backtracking == 0) pushEnableParameterUsage(false); }
@after { popEnableParameterUsage(); }
//PARAMETERS CAN'T BE USED -> This verification should be scoped
: expression aliasClause[false]
-> ^(SELECT_ITEM expression aliasClause?)
;
aliasClause[boolean generateAlias]
options {
k=2;
} : -> {$generateAlias}? ALIAS_NAME[buildUniqueImplicitAlias()]
->
| aliasDeclaration
| as_key! aliasDeclaration
;
aliasDeclaration
: IDENTIFIER -> ALIAS_NAME[$IDENTIFIER]
;
aliasReference
: IDENTIFIER -> ALIAS_REF[$IDENTIFIER]
;
jpaSelectObjectSyntax
: object_key LPAREN aliasReference RPAREN -> ^(SELECT_ITEM aliasReference)
;
orderByClause
: order_by_key^ sortSpecification ( COMMA! sortSpecification )*
;
sortSpecification
: sortKey collationSpecification? orderingSpecification -> ^(SORT_SPEC sortKey collationSpecification? orderingSpecification)
;
orderingSpecification
: ascending_key -> ORDER_SPEC[$ascending_key.start, "asc"]
| descending_key -> ORDER_SPEC[$descending_key.start, "desc"]
| -> ORDER_SPEC["asc"]
;
sortKey
@init { if (state.backtracking == 0) pushEnableParameterUsage(false); }
@after { popEnableParameterUsage(); }
//PARAMETERS CAN'T BE USED -> This verification should be scoped
: additiveExpression
;
collationSpecification
: collate_key collateName -> COLLATE[$collateName.start, $collateName.text]
;
collateName
: dotIdentifierPath
;
logicalExpression
: expression
;
expression
: logicalOrExpression
;
logicalOrExpression
: logicalAndExpression ( or_key^ logicalAndExpression )*
;
logicalAndExpression
: negatedExpression ( and_key^ negatedExpression )*
;
negatedExpression
: not_key^ negatedExpression
| equalityExpression
;
equalityExpression
@init { boolean isNull = false; boolean isNegated = false; }
: (ftOccurrence? ftFieldPath COLON)=> fullTextExpression
| (relationalExpression -> relationalExpression)
(is_key (not_key {isNegated = true;})? (NULL {isNull = true;} | empty_key)
-> {isNull && isNegated}? ^(IS_NOT_NULL[$not_key.start, "is not null"] $equalityExpression)
-> {isNull && !isNegated}? ^(IS_NULL[$NULL, "is null"] $equalityExpression)
-> {!isNull && isNegated}? ^(IS_NOT_EMPTY $equalityExpression)
-> ^(IS_EMPTY $equalityExpression)
| (op=EQUALS | op=NOT_EQUAL) relationalExpression -> ^($op $equalityExpression relationalExpression)
)*
;
relationalExpression
@init {boolean isNegated = false;}
: (additiveExpression -> additiveExpression)
(
( ( op=LESS | op=GREATER | op=LESS_EQUAL | op=GREATER_EQUAL ) additiveExpression
-> ^($op $relationalExpression additiveExpression)
)+
| (not_key {isNegated = true;} )?
( in_key inList
-> {isNegated}? ^(NOT_IN[$not_key.start, "not in"] $relationalExpression inList)
-> ^(in_key $relationalExpression inList)
| between_key betweenList
-> {isNegated}? ^(NOT_BETWEEN[$not_key.start, "not between"] $relationalExpression betweenList)
-> ^(between_key $relationalExpression betweenList)
| like_key additiveExpression likeEscape?
-> {isNegated}? ^(NOT_LIKE[$not_key.start, "not like"] $relationalExpression additiveExpression likeEscape?)
-> ^(like_key $relationalExpression additiveExpression likeEscape?)
| member_of_key path
-> {isNegated}? ^(NOT_MEMBER_OF[$not_key.start, "not member of"] $relationalExpression ^(PATH path))
-> ^(member_of_key $relationalExpression ^(PATH path))
)
)?
;
likeEscape
: escape_key^ additiveExpression
;
inList
: collectionExpression -> ^(IN_LIST collectionExpression)
| LPAREN additiveExpression (COMMA additiveExpression)* RPAREN -> ^(IN_LIST additiveExpression+)
;
betweenList
: lower=additiveExpression and_key upper=additiveExpression -> ^(BETWEEN_LIST $lower $upper)
;
additiveExpression
: quantifiedExpression
| standardFunction
| setFunction
| versionFunction
| collectionExpression
| atom
;
quantifiedExpression
: (some_key^ | exists_key^ | all_key^ | any_key^) (collectionExpression | aliasReference)
;
standardFunction
@init { if (state.backtracking == 0) pushEnableParameterUsage(true); }
@after { popEnableParameterUsage(); }
: sizeFunction
| indexFunction
;
sizeFunction
: size_key^ LPAREN! propertyReference RPAREN!
;
indexFunction
: index_key^ LPAREN! aliasReference RPAREN!
;
versionFunction
: version_key^ LPAREN! aliasReference RPAREN!
;
setFunction
@init { boolean generateOmittedElement = true; if (state.backtracking == 0) pushEnableParameterUsage(true); }
@after { popEnableParameterUsage(); }
: ( sum_key^ | avg_key^ | max_key^ | min_key^ ) LPAREN! additiveExpression RPAREN!
| count_key LPAREN ( ASTERISK {generateOmittedElement = false;} | ( ( (distinct_key | all_key) {generateOmittedElement = false;} )? countFunctionArguments ) ) RPAREN
-> {generateOmittedElement}? ^(count_key ASTERISK? ALL countFunctionArguments?)
-> ^(count_key ASTERISK? distinct_key? all_key? countFunctionArguments?)
;
countFunctionArguments
: propertyReference
| collectionExpression
| signedNumericLiteral
;
collectionExpression
: (elements_key^ | indices_key^) LPAREN! propertyReference RPAREN!
;
atom
: identPrimary -> ^(PATH identPrimary)
//TODO if ends with:
// .class -> class type
// if contains "()" it is a function call
// if it is constantReference (using context)
// otherwise it will be a generic element to be resolved on the next phase (1st tree walker)
| constant
| parameterSpecification { if (!isParameterUsageEnabled()) throw new RecognitionException(input); }
//validate using Scopes if it is enabled or not to use parameterSpecification.. if not generate an exception
| LPAREN! expressionOrVector RPAREN!
;
parameterSpecification
: COLON IDENTIFIER -> NAMED_PARAM[$IDENTIFIER]
| PARAM INTEGER_LITERAL -> POSITIONAL_PARAM[$INTEGER_LITERAL]
| PARAM
;
expressionOrVector
@init {boolean isVectorExp = false;}
: expression (vectorExpr {isVectorExp = true;})?
-> {isVectorExp}? ^(VECTOR_EXPR expression vectorExpr)
-> expression
;
vectorExpr
@init { if (state.backtracking == 0) pushEnableParameterUsage(true); }
@after { popEnableParameterUsage(); }
: COMMA! expression (COMMA! expression)*
;
identPrimary
: IDENTIFIER
( DOT^ IDENTIFIER
| LSQUARE^ expression RSQUARE!
| LSQUARE^ RSQUARE!
| LPAREN^ exprList RPAREN!
)*
;
exprList
@init { if (state.backtracking == 0) pushEnableParameterUsage(true); }
@after { popEnableParameterUsage(); }
: expression (COMMA! expression)*
|
;
constant
: booleanLiteral
| stringLiteral
| signedNumericLiteral
| NULL
;
stringLiteral
: CHARACTER_LITERAL -> ^(CONST_STRING_VALUE CHARACTER_LITERAL)
| STRING_LITERAL -> ^(CONST_STRING_VALUE STRING_LITERAL)
;
booleanLiteral
: TRUE
| FALSE
;
numericLiteral
: INTEGER_LITERAL
| DECIMAL_LITERAL
| FLOATING_POINT_LITERAL
| HEX_LITERAL
| OCTAL_LITERAL
;
signedNumericLiteral
: (MINUS | PLUS)^ numericLiteral
| numericLiteral
;
entityName
: dotIdentifierPath -> ENTITY_NAME<EntityNameTree>[$dotIdentifierPath.start, $dotIdentifierPath.text, (Tree) $dotIdentifierPath.tree]
//here the polymorphic entities should be resolved... to:
// 1. to place inside the ENTITY_NAME Token all its possible values, otherwise it would be much difficult to return to the place that should explit the sentence
;
propertyReference
: path -> ^(PROPERTY_REFERENCE path)
;
dotIdentifierPath
: IDENTIFIER (DOT^ IDENTIFIER)*
;
path
: IDENTIFIER
( DOT^ IDENTIFIER
| LSQUARE^ expression RSQUARE!
| LSQUARE^ RSQUARE!
)*
;
object_key
: {validateSoftKeyword("object")}?=> IDENTIFIER -> OBJECT[$IDENTIFIER]
;
sum_key
: {validateSoftKeyword("sum")}?=> IDENTIFIER -> SUM[$IDENTIFIER]
;
avg_key
: {validateSoftKeyword("avg")}?=> IDENTIFIER -> AVG[$IDENTIFIER]
;
max_key
: {validateSoftKeyword("max")}?=> IDENTIFIER -> MAX[$IDENTIFIER]
;
min_key
: {validateSoftKeyword("min")}?=> IDENTIFIER -> MIN[$IDENTIFIER]
;
count_key
: {validateSoftKeyword("count")}?=> IDENTIFIER -> COUNT[$IDENTIFIER]
;
version_key
: {validateSoftKeyword("version")}?=> IDENTIFIER -> VERSION[$IDENTIFIER]
;
size_key
: {validateSoftKeyword("size")}?=> IDENTIFIER -> SIZE[$IDENTIFIER]
;
index_key
: {validateSoftKeyword("index")}?=> IDENTIFIER -> INDEX[$IDENTIFIER]
;
any_key
: {validateSoftKeyword("any")}?=> IDENTIFIER -> ANY[$IDENTIFIER]
;
exists_key
: {validateSoftKeyword("exists")}?=> IDENTIFIER -> EXISTS[$IDENTIFIER]
;
some_key
: {validateSoftKeyword("some")}?=> IDENTIFIER -> SOME[$IDENTIFIER]
;
escape_key
: {validateSoftKeyword("escape")}?=> IDENTIFIER -> ESCAPE[$IDENTIFIER]
;
like_key
: {validateSoftKeyword("like")}?=> IDENTIFIER -> LIKE[$IDENTIFIER]
;
between_key
: {validateSoftKeyword("between")}?=> IDENTIFIER -> BETWEEN[$IDENTIFIER]
;
member_of_key
: {validateSoftKeyword("member") && validateSoftKeyword(2, "of")}?=> id=IDENTIFIER IDENTIFIER -> MEMBER_OF[$id]
;
empty_key
: {validateSoftKeyword("empty")}?=> IDENTIFIER -> EMPTY[$IDENTIFIER]
;
is_key
: {validateSoftKeyword("is")}?=> IDENTIFIER -> IS[$IDENTIFIER]
;
or_key
: OR
| {validateSoftKeyword("or")}?=> IDENTIFIER -> OR[$IDENTIFIER]
;
and_key
: AND
| {validateSoftKeyword("and")}?=> IDENTIFIER -> AND[$IDENTIFIER]
;
not_key
: EXCLAMATION
| {validateSoftKeyword("not")}?=> IDENTIFIER -> NOT[$IDENTIFIER]
;
to_key
: {validateSoftKeyword("to")}?=> IDENTIFIER -> TO[$IDENTIFIER]
;
having_key
: {validateSoftKeyword("having")}?=> IDENTIFIER -> HAVING[$IDENTIFIER]
;
with_key
: {validateSoftKeyword("with")}?=> IDENTIFIER -> WITH[$IDENTIFIER]
;
on_key
: {validateSoftKeyword("on")}?=> IDENTIFIER -> ON[$IDENTIFIER]
;
indices_key
: {validateSoftKeyword("indices")}?=> IDENTIFIER -> INDICES[$IDENTIFIER]
;
cross_key
: {validateSoftKeyword("cross")}?=> IDENTIFIER -> CROSS[$IDENTIFIER]
;
join_key
: {validateSoftKeyword("join")}?=> IDENTIFIER -> JOIN[$IDENTIFIER]
;
inner_key
: {validateSoftKeyword("inner")}?=> IDENTIFIER -> INNER[$IDENTIFIER]
;
outer_key
: {validateSoftKeyword("outer")}?=> IDENTIFIER -> OUTER[$IDENTIFIER]
;
left_key
: {validateSoftKeyword("left")}?=> IDENTIFIER -> LEFT[$IDENTIFIER]
;
right_key
: {validateSoftKeyword("right")}?=> IDENTIFIER -> RIGHT[$IDENTIFIER]
;
full_key
: {validateSoftKeyword("full")}?=> IDENTIFIER -> FULL[$IDENTIFIER]
;
elements_key
: {validateSoftKeyword("elements")}?=> IDENTIFIER -> ELEMENTS[$IDENTIFIER]
;
properties_key
: {validateSoftKeyword("properties")}?=> IDENTIFIER -> PROPERTIES[$IDENTIFIER]
;
fetch_key
: {validateSoftKeyword("fetch")}?=> IDENTIFIER -> FETCH[$IDENTIFIER]
;
in_key
: {validateSoftKeyword("in")}?=> IDENTIFIER -> IN[$IDENTIFIER]
;
as_key
: {validateSoftKeyword("as")}?=> IDENTIFIER -> AS[$IDENTIFIER]
;
where_key
: {validateSoftKeyword("where")}?=> IDENTIFIER -> WHERE[$IDENTIFIER]
;
select_key
: {validateSoftKeyword("select")}?=> IDENTIFIER -> SELECT[$IDENTIFIER]
;
delete_key
: {validateSoftKeyword("delete")}?=> IDENTIFIER -> DELETE[$IDENTIFIER]
;
distinct_key
: {validateSoftKeyword("distinct")}?=> IDENTIFIER -> DISTINCT[$IDENTIFIER]
;
all_key
: {validateSoftKeyword("all")}?=> IDENTIFIER -> ALL[$IDENTIFIER]
;
ascending_key
: {validateSoftKeyword("asc")}?=> IDENTIFIER
;
descending_key
: {validateSoftKeyword("desc")}?=> IDENTIFIER
;
collate_key
: {validateSoftKeyword("collate")}?=> IDENTIFIER
;
order_by_key
: {validateSoftKeyword("order") && validateSoftKeyword(2, "by")}?=> id=IDENTIFIER IDENTIFIER -> ORDER_BY[$id]
;
group_by_key
: {validateSoftKeyword("group") && validateSoftKeyword(2, "by")}?=> id=IDENTIFIER IDENTIFIER -> GROUP_BY[$id]
;
from_key
: {validateSoftKeyword("from")}?=> IDENTIFIER -> FROM[$IDENTIFIER]
;
/* TODO [anistor]
DIFFERENCES TO LUCENE SYNTAX:
- whitespace is not significant.
- no wildcard (*) for field names, so *:* is not a valid query.
- a field name/path is always specified. we do not have a default field like lucene has.
- = operator is not accepted instead of : as in lucene StandardSyntaxParser
- &&, || are accepted alternatives for AND/OR, in both full-text and jpa predicates
- ! can be used instead of NOT
- AND,OR,NOT are capitalised in lucene but in infinispan they are case insensitive
- when and/or is missing, OR will be assumed
- string terms must be enclosed in single or double quotes. lucene allows single-word terms to be unquoted
- fuzziness and boosting are not accepted in arbitrary order as in lucene's parser. fuzziness must always come first.
- we accept != instead of <> from jpa
- >,>=,<,<= operators work as in JPA not as in lucene's StandardSyntaxParser so boosting cannot be applied. use ranges to achieve that.
*/
fullTextExpression
: ftOccurrence ftFieldPath COLON ftBoostedQuery -> ^(COLON ftFieldPath ^(ftOccurrence ftBoostedQuery))
| ftFieldPath COLON^ ftBoostedQuery
;
ftOccurrence
: PLUS -> FT_OCCUR_MUST[$PLUS, $PLUS.text]
| MINUS -> FT_OCCUR_MUST_NOT[$MINUS, $MINUS.text]
| EXCLAMATION -> FT_OCCUR_MUST_NOT[$EXCLAMATION, $EXCLAMATION.text]
| HASH -> FT_OCCUR_FILTER[$HASH, $HASH.text]
| not_key -> FT_OCCUR_MUST_NOT[$not_key.start, $not_key.text]
;
ftFieldPath
: dotIdentifierPath -> ^(PATH dotIdentifierPath)
;
ftBoostedQuery
: ftTermOrQuery ftBoost^?
;
// TODO [anistor] maybe we should not accept space between CARAT and following number/param to get rid of syntactic ambiguity
ftBoost
: CARAT ftNumericLiteralOrParameter -> CARAT[$CARAT, $ftNumericLiteralOrParameter.text]
;
ftTermOrQuery
: ftTerm
| ftRange
| LPAREN! ftConjunction (ftOr^ ftConjunction)* RPAREN!
;
ftTerm
: ftLiteralOrParameter ftFuzzySlop? -> ^(FT_TERM ftLiteralOrParameter ftFuzzySlop?)
| REGEXP_LITERAL -> ^(FT_REGEXP REGEXP_LITERAL)
;
// TODO [anistor] maybe we should not accept space between TILDE and following number/param to get rid of syntactic ambiguity
ftFuzzySlop
: TILDE (options { greedy=true; } : ftNumericLiteralOrParameter)? -> TILDE[$TILDE, $ftNumericLiteralOrParameter.text]
;
ftRange
: ftRangeBegin lower=ftRangeBound to_key? upper=ftRangeBound ftRangeEnd -> ^(FT_RANGE ftRangeBegin $lower $upper ftRangeEnd)
;
ftRangeBegin
: LSQUARE
| LCURLY
;
ftRangeEnd
: RSQUARE
| RCURLY
;
ftRangeBound
: ASTERISK
| ftLiteralOrParameter
;
ftOr
: or_key
| -> OR
;
ftConjunction
: ftClause (and_key^ ftClause)*
;
ftClause
: (options { greedy=true; } : ftOccurrence^)? ftBoostedQuery
;
ftLiteralOrParameter
: parameterSpecification
| stringLiteral
| signedNumericLiteral
;
ftNumericLiteralOrParameter
: parameterSpecification
| numericLiteral
;