-
Notifications
You must be signed in to change notification settings - Fork 653
/
sparql_10-final.jj
1158 lines (972 loc) · 26.4 KB
/
sparql_10-final.jj
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
// ARQ/SPARQL Grammar - native syntax for the query engine
// #if !defined(ARQ) && !defined(SPARQL_10) && !defined(SPARQL_11)
// #error Please define one of ARQ, SPARQL_10 or SPARQL11
// #endif
//
// #if defined(SPARQL_10)
// #define SPARQL
// #endif
// Constraint expression is derived from Java :
// example java1.2-a.jj grammer in JavaCC distribution
// Much modifed over time.
options
{
JAVA_UNICODE_ESCAPE = true ;
UNICODE_INPUT = false ;
STATIC = false ;
// DEBUG_PARSER = true ;
// DEBUG_TOKEN_MANAGER = true ;
}
PARSER_BEGIN(SPARQLParser10)
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package com.hp.hpl.jena.sparql.lang.sparql_10 ;
import com.hp.hpl.jena.graph.* ;
import com.hp.hpl.jena.query.* ;
import com.hp.hpl.jena.sparql.core.Var ;
import com.hp.hpl.jena.sparql.syntax.* ;
import com.hp.hpl.jena.sparql.expr.* ;
import com.hp.hpl.jena.sparql.path.* ;
public class SPARQLParser10 extends SPARQLParser10Base
{
boolean allowAggregatesInExpressions = false ;
}
PARSER_END(SPARQLParser10)
void QueryUnit(): { }
{
Query() <EOF>
}
void Query() : { }
{
Prologue()
( SelectQuery() | ConstructQuery() | DescribeQuery() | AskQuery() )
}
void Prologue() : {}
{
( BaseDecl() ) ?
( PrefixDecl() )*
}
void BaseDecl() : { String iri ; }
{
<BASE> iri = IRI_REF()
{ getPrologue().setBaseURI(iri) ; }
}
void PrefixDecl() : { Token t ; String iri ; }
{
<PREFIX> t = <PNAME_NS> iri = IRI_REF()
{ String s = fixupPrefix(t.image, t.beginLine, t.beginColumn) ;
getPrologue().setPrefix(s, iri) ; }
}
// ---- Query type clauses
void SelectQuery() : { }
{
Project()
( DatasetClause() )*
WhereClause()
SolutionModifier()
}
void SubSelect() :{ }
{
Project()
WhereClause()
SolutionModifier()
}
void Project() : { Var v ; Expr expr ; Node n ; }
{
<SELECT>
{ getQuery().setQuerySelectType() ; }
( <DISTINCT> { getQuery().setDistinct(true);}
| <REDUCED> { getQuery().setReduced(true); }
)?
{ allowAggregatesInExpressions = true ; }
(
(
( v = Var() { getQuery().addResultVar(v) ; } )
{ getQuery().setQueryResultStar(false) ; }
)+
|
<STAR> { getQuery().setQueryResultStar(true) ; }
)
{ allowAggregatesInExpressions = false ; }
}
void ConstructQuery() : { Template t ; }
{
<CONSTRUCT>
{ getQuery().setQueryConstructType() ; }
t = ConstructTemplate()
{ getQuery().setConstructTemplate(t) ; }
( DatasetClause() )*
WhereClause()
SolutionModifier()
}
void DescribeQuery() : { Node n ; }
{
<DESCRIBE>
{ getQuery().setQueryDescribeType() ; }
(
( n = VarOrIRIref() { getQuery().addDescribeNode(n) ; } )+
{ getQuery().setQueryResultStar(false) ; }
|
<STAR>
{ getQuery().setQueryResultStar(true) ; }
)
( DatasetClause() )*
( WhereClause() )?
SolutionModifier()
}
void AskQuery() : {}
{
<ASK> { getQuery().setQueryAskType() ; }
( DatasetClause() )*
WhereClause()
}
// ----
void DatasetClause() : {}
{
<FROM>
( DefaultGraphClause() | NamedGraphClause() )
}
void DefaultGraphClause() : { String iri ; }
{
iri = SourceSelector()
{
// This checks for duplicates
getQuery().addGraphURI(iri) ;
}
}
void NamedGraphClause() : { String iri ; }
{
<NAMED>
iri = SourceSelector()
{
// This checks for duplicates
getQuery().addNamedGraphURI(iri) ;
}
}
String SourceSelector() : { String iri ; }
{
iri = IRIref() { return iri ; }
}
void WhereClause() : { Element el ; }
{
(<WHERE>)? el = GroupGraphPattern() { getQuery().setQueryPattern(el) ; }
}
void SolutionModifier() : { }
{
( OrderClause() )?
( LimitOffsetClauses() )?
}
void OrderClause() : { }
{
<ORDER> <BY> ( OrderCondition() )+
}
void OrderCondition() :
{ int direction = 0 ; Expr expr = null ; Node v = null ; }
{
{ direction = Query.ORDER_DEFAULT ; }
(
( // These are for clarity in the HTML
( <ASC> { direction = Query.ORDER_ASCENDING ; }
| <DESC> { direction = Query.ORDER_DESCENDING ; } )
expr = BrackettedExpression()
)
|
( expr = Constraint()
| v = Var() //{ expr = asExpr(v) ; }
)
)
{ if ( v == null )
getQuery().addOrderBy(expr, direction) ;
else
getQuery().addOrderBy(v, direction) ; }
}
void LimitOffsetClauses() : { }
{
// SPARQL does not care about the order here.
// SQL (where implemented) does (it's LIMIT then OFFSET generally)
// But that is counter intuitive as it's applied the other way round
(
LimitClause() (OffsetClause())?
|
OffsetClause() (LimitClause())?
)
}
void LimitClause() : { Token t ; }
{
<LIMIT> t = <INTEGER>
{ getQuery().setLimit(integerValue(t.image)) ; }
}
void OffsetClause() : { Token t ; }
{
<OFFSET> t = <INTEGER>
{ getQuery().setOffset(integerValue(t.image)) ; }
}
// ---- SPARQL/Update (submission)
// SPARQL 1.1. Update
// ---- General Graph Pattern
Element GroupGraphPattern() : { Element el = null ; Token t ; }
{
t = <LBRACE>
el = GroupGraphPatternSub()
<RBRACE>
{ return el ; }
}
Element GroupGraphPatternSub() : { Element el = null ; }
{
{ ElementGroup elg = new ElementGroup() ; }
{ startGroup(elg) ; }
// Ensure two BGP's can't be next to each other
// Done by seeing if there is a non-BGP and recursing
// if there is an intermediate
(
{ startTriplesBlock() ; }
el = TriplesBlock(null)
{ endTriplesBlock() ;
elg.addElement(el) ; }
)?
(
( el = GraphPatternNotTriples() | el = Filter() )
{ elg.addElement(el) ; }
(<DOT>)?
(
{ startTriplesBlock() ; }
el = TriplesBlock(null)
{ endTriplesBlock() ;
elg.addElement(el) ; }
)?
)*
{ endGroup(elg) ; }
{ return elg ; }
}
// -- TriplesBlock
// Two versions - for SPARQL 1.0 and SPARQL 1.1 (with paths)
Element TriplesBlock(ElementTriplesBlock acc) : { }
{
{ if ( acc == null )
acc = new ElementTriplesBlock() ;
}
TriplesSameSubject(acc)
( <DOT> (TriplesBlock(acc))? )?
{ return acc ; }
}
// -----
Element GraphPatternNotTriples() : { Element el = null ; }
{
(
el = OptionalGraphPattern()
|
// "GroupPattern" or "GroupPattern" union "GroupPattern"
el = GroupOrUnionGraphPattern()
|
el = GraphGraphPattern()
)
{ return el ; }
}
// ---- Definitions of each pattern element
Element OptionalGraphPattern() : { Element el ; }
{ <OPTIONAL> el = GroupGraphPattern()
{ return new ElementOptional(el) ; }
}
Element GraphGraphPattern() : { Element el ; Node n ;}
{
<GRAPH> n = VarOrIRIref() el = GroupGraphPattern()
{ return new ElementNamedGraph(n, el) ; }
}
// G (union G)* can be a single group pattern
// or a group pattern as part of an union.
Element GroupOrUnionGraphPattern() :
{ Element el = null ; ElementUnion el2 = null ; }
{
el = GroupGraphPattern()
( <UNION>
{ if ( el2 == null )
{
el2 = new ElementUnion() ;
el2.addElement(el) ;
}
}
el = GroupGraphPattern()
{ el2.addElement(el) ; }
)*
{ return (el2==null)? el : el2 ; }
}
Element Filter() : { Expr c ; }
{
<FILTER> c = Constraint()
{ return new ElementFilter(c) ; }
}
Expr Constraint() : { Expr c ; }
{
( c = BrackettedExpression()
| c = BuiltInCall()
| c = FunctionCall()
)
{ return c ; }
}
Expr FunctionCall() : { String fname ; ExprList a ;
ExprList aParam = null ;
boolean distinct = false ;}
{
fname = IRIref()
a = ArgList()
{ return new E_Function(fname, a) ; }
}
ExprList ArgList() : { Expr expr ; ExprList args = new ExprList() ; }
{
(
<NIL>
|
<LPAREN>
expr = Expression() { args.add(expr) ; }
(<COMMA> expr = Expression() { args.add(expr) ; } )*
<RPAREN>
)
{ return args ; }
}
ExprList ParamList() : { Expr expr ; ExprList args = new ExprList() ; }
{
// "[ expr, expr ]"
(
<LBRACKET>
expr = Expression() { args.add(expr) ; }
(<COMMA> expr = Expression() { args.add(expr) ; } )*
<RBRACKET>
)
{ return args ; }
}
// -------- Construct patterns
Template ConstructTemplate() : { TripleCollectorBGP acc = new TripleCollectorBGP();
Template t = new Template(acc.getBGP()) ; }
{
{ setInConstructTemplate(true) ; }
<LBRACE>
(ConstructTriples(acc))?
<RBRACE>
{ setInConstructTemplate(false) ;
return t ; }
}
void ConstructTriples(TripleCollector acc) : { }
{
// SPARQL - recursion - does not scale for SPARQL/Update
TriplesSameSubject(acc)
(<DOT> (ConstructTriples(acc))? )?
}
// -------- Triple lists with property and object lists
// -------- Without paths: entry: TriplesSameSubject
void TriplesSameSubject(TripleCollector acc) : { Node s ; }
{
s = VarOrTerm()
PropertyListNotEmpty(s, acc)
|
// Any of the triple generating syntax elements
s = TriplesNode(acc)
PropertyList(s, acc)
}
void PropertyListNotEmpty(Node s, TripleCollector acc) :
{ Node p = null ; }
{
p = Verb()
ObjectList(s, p, null, acc)
( <SEMICOLON>
(
p = Verb()
ObjectList(s, p, null, acc)
)?
)*
}
void PropertyList(Node s, TripleCollector acc) : { }
{
( PropertyListNotEmpty(s, acc) ) ?
}
void ObjectList(Node s, Node p, Path path, TripleCollector acc): { Node o ; }
{
Object(s, p, path, acc)
( <COMMA> Object(s, p, path, acc) )*
}
void Object(Node s, Node p, Path path, TripleCollector acc): { Node o ; }
{
{ int mark = acc.mark() ; }
o = GraphNode(acc)
{ insert(acc, mark, s, p, path, o) ; }
}
Node Verb() : { Node p ;}
{
// Blank nodes as predicates
// ( p = VarOrBlankNodeOrIRIref() | <KW_A> { p = nRDFtype ; } )
( p = VarOrIRIref() | <KW_A> { p = nRDFtype ; } )
{ return p ; }
}
// -------- BGPs with paths.
// -------- Entry point: TriplesSameSubjectPath
// -------- Paths
// -------- Triple expansions
// Anything that can stand in a node slot and which is
// a number of triples
Node TriplesNode(TripleCollector acc) : { Node n ; }
{
n = Collection(acc) { return n ; }
|
n = BlankNodePropertyList(acc) { return n ; }
}
Node BlankNodePropertyList(TripleCollector acc) : { Token t ; }
{
t = <LBRACKET>
{ Node n = createBNode(t.beginLine, t.beginColumn) ; }
PropertyListNotEmpty(n, acc)
<RBRACKET>
{ return n ; }
}
// ------- RDF collections
Node Collection(TripleCollector acc) :
{ Node listHead = nRDFnil ; Node lastCell = null ; int mark ; Node n ; Token t ; }
{
t = <LPAREN>
(
{ Node cell = createListNode(t.beginLine, t.beginColumn) ;
if ( listHead == nRDFnil )
listHead = cell ;
if ( lastCell != null )
insert(acc, lastCell, nRDFrest, cell) ;
mark = acc.mark() ;
}
n = GraphNode(acc)
{
insert(acc, mark, cell, nRDFfirst, n) ;
lastCell = cell ;
}
) +
// Not * here - "()" is handled separately.
<RPAREN>
{ if ( lastCell != null )
insert(acc, lastCell, nRDFrest, nRDFnil) ;
return listHead ; }
}
// -------- Nodes in a graph pattern or template
Node GraphNode(TripleCollector acc) : { Node n ; }
{
n = VarOrTerm() { return n ; }
|
n = TriplesNode(acc) { return n ; }
}
Node VarOrTerm() : {Node n = null ; }
{
( n = Var() | n = GraphTerm() )
{ return n ; }
}
// Property (if no bNodes) + DESCRIBE
Node VarOrIRIref() : {Node n = null ; String iri ; }
{
( n = Var() | iri = IRIref() { n = createNode(iri) ; } )
{ return n ; }
}
Var Var() : { Token t ;}
{
( t = <VAR1> | t = <VAR2> )
{ return createVariable(t.image, t.beginLine, t.beginColumn) ; }
}
Node GraphTerm() : { Node n ; String iri ; }
{
iri = IRIref() { return createNode(iri) ; }
| n = RDFLiteral() { return n ; }
| n = NumericLiteral() { return n ; }
| n = BooleanLiteral() { return n ; }
| n = BlankNode() { return n ; }
// <LPAREN> <RPAREN> { return nRDFnil ; }
| <NIL> { return nRDFnil ; }
}
// -------- Constraint syntax
Expr Expression() : { Expr expr ; }
{
expr = ConditionalOrExpression()
{ return expr ; }
}
Expr ConditionalOrExpression() : { Expr expr1, expr2 ; }
{
expr1 = ConditionalAndExpression()
( <SC_OR> expr2 = ConditionalAndExpression()
{ expr1 = new E_LogicalOr(expr1, expr2) ; }
)*
{ return expr1 ; }
}
Expr ConditionalAndExpression() : { Expr expr1, expr2 ;}
{
expr1 = ValueLogical()
( <SC_AND> expr2 = ValueLogical()
{ expr1 = new E_LogicalAnd(expr1, expr2) ; }
)*
{ return expr1 ; }
}
Expr ValueLogical() : { Expr expr ; }
{
expr = RelationalExpression()
{ return expr ; }
}
Expr RelationalExpression() : { Expr expr1, expr2 ; ExprList a ; }
{
expr1 = NumericExpression()
(
<EQ> expr2 = NumericExpression()
{ expr1 = new E_Equals(expr1, expr2) ; }
| <NE> expr2 = NumericExpression()
{ expr1 = new E_NotEquals(expr1, expr2) ; }
| <LT> expr2 = NumericExpression()
{ expr1 = new E_LessThan(expr1, expr2) ; }
| <GT> expr2 = NumericExpression()
{ expr1 = new E_GreaterThan(expr1, expr2) ; }
| <LE> expr2 = NumericExpression()
{ expr1 = new E_LessThanOrEqual(expr1, expr2) ; }
| <GE> expr2 = NumericExpression()
{ expr1 = new E_GreaterThanOrEqual(expr1, expr2) ; }
)?
{ return expr1 ; }
}
Expr NumericExpression () : { Expr expr ; }
{
expr = AdditiveExpression()
{ return expr ; }
}
Expr AdditiveExpression() : { Expr expr1, expr2, expr3 ; boolean addition ; Node n ; }
{
expr1 = MultiplicativeExpression()
( <PLUS> expr2 = MultiplicativeExpression()
{ expr1 = new E_Add(expr1, expr2) ; }
| <MINUS> expr2 = MultiplicativeExpression()
{ expr1 = new E_Subtract(expr1, expr2) ; }
// SPARQL Bug.
// | n = NumericLiteralPositive()
// {
// n = stripSign(n) ;
// expr2 = asExpr(n) ;
// expr1 = new E_Add(expr1, expr2) ;
// }
// |
// n = NumericLiteralNegative()
// {
// n = stripSign(n) ;
// expr2 = asExpr(n) ;
// expr1 = new E_Subtract(expr1, expr2) ;
// }
// )*
// Expression - no + or -
|
(
n = NumericLiteralPositive()
{
n = stripSign(n) ;
expr2 = asExpr(n) ;
addition = true ;
}
|
n = NumericLiteralNegative()
{
n = stripSign(n) ;
expr2 = asExpr(n) ;
addition = false ;
}
)
(
( <STAR> expr3 = UnaryExpression() { expr2 = new E_Multiply(expr2, expr3) ; } )
|
( <SLASH> expr3 = UnaryExpression() { expr2 = new E_Divide(expr2, expr3) ; } )
)?
{ if ( addition )
expr1 = new E_Add(expr1, expr2) ;
else
expr1 = new E_Subtract(expr1, expr2) ;
}
)*
{ return expr1 ; }
}
Expr MultiplicativeExpression() : { Expr expr1, expr2 ; }
{
expr1 = UnaryExpression()
( <STAR> expr2 = UnaryExpression()
{ expr1 = new E_Multiply(expr1, expr2) ; }
| <SLASH> expr2 = UnaryExpression()
{ expr1 = new E_Divide(expr1, expr2) ; }
// | <REM> expr2 = UnaryExpression()
// { expr1 = new E_Modulus(expr1, expr2) ; }
)*
{ return expr1 ; }
}
Expr UnaryExpression() : { Expr expr ; }
{
<BANG> expr = PrimaryExpression()
{ return new E_LogicalNot(expr) ; }
| <PLUS> expr = PrimaryExpression() { return new E_UnaryPlus(expr) ; }
| <MINUS> expr = PrimaryExpression() { return new E_UnaryMinus(expr) ; }
| expr = PrimaryExpression() { return expr ; }
}
Expr PrimaryExpression() : { Expr expr ; Node gn ; }
{
( expr = BrackettedExpression() { return expr ; }
| expr = BuiltInCall() { return expr ; }
| expr = IRIrefOrFunction() { return expr ; }
// NOT | gn = VarOrTerm() { return asExpr(gn) ; }
// Because of IRIrefOrFunction vs <NIL> and blank nodes
| gn = RDFLiteral() { return asExpr(gn) ; }
| gn = NumericLiteral() { return asExpr(gn) ; }
| gn = BooleanLiteral() { return asExpr(gn) ; }
| gn = Var() { return asExpr(gn) ; }
)
}
Expr BrackettedExpression() : { Expr expr ; }
{
<LPAREN> expr = Expression() <RPAREN> { return expr ; }
}
Expr BuiltInCall() : { Expr expr ; Expr expr1 = null ; Expr expr2 = null ;
Node gn ; Token t ; ExprList a ; }
{
<STR> <LPAREN> expr = Expression() <RPAREN>
{ return new E_Str(expr) ; }
| <LANG> <LPAREN> expr = Expression() <RPAREN>
{ return new E_Lang(expr) ; }
| <LANGMATCHES>
<LPAREN> expr1 = Expression() <COMMA> expr2 = Expression() <RPAREN>
{ return new E_LangMatches(expr1, expr2) ; }
| <DTYPE> <LPAREN> expr = Expression() <RPAREN>
{ return new E_Datatype(expr) ; }
| <BOUND> <LPAREN> gn = Var() <RPAREN>
{ return new E_Bound(new ExprVar(gn)) ; }
| <SAME_TERM> <LPAREN> expr1 = Expression() <COMMA> expr2 = Expression() <RPAREN>
{ return new E_SameTerm(expr1, expr2) ; }
| t = <IS_IRI> <LPAREN> expr = Expression() <RPAREN>
{ return new E_IsIRI(expr) ; }
| t = <IS_URI> <LPAREN> expr = Expression() <RPAREN>
{ return new E_IsURI(expr) ; }
| <IS_BLANK> <LPAREN> expr = Expression() <RPAREN>
{ return new E_IsBlank(expr) ; }
| <IS_LITERAL> <LPAREN> expr = Expression() <RPAREN>
{ return new E_IsLiteral(expr) ; }
| // Regular expression matcher
expr = RegexExpression() { return expr ; }
}
Expr RegexExpression() :
{ Expr expr ; Expr patExpr = null ; Expr flagsExpr = null ; }
{
<REGEX> <LPAREN>
expr = Expression()
<COMMA>
patExpr = Expression()
( <COMMA> flagsExpr = Expression() ) ?
<RPAREN>
{ return new E_Regex(expr, patExpr, flagsExpr) ; }
}
// See also FunctionCall.
// The case of "q:name()" or "q:agg[]()" or just "q:name"
// by expanding out FunctionCall()
Expr IRIrefOrFunction() : { String iri ; ExprList a = null ;
ExprList aParam = null ;
boolean distinct = false ; }
{
iri = IRIref()
( a = ArgList() )?
{ if ( a == null ) return asExpr(createNode(iri)) ;
return new E_Function(iri, a) ;
}
}
Node RDFLiteral() : { Token t ; String lex = null ; }
{
lex = String()
// Optional lang tag and datatype.
{ String lang = null ; String uri = null ; }
(
( t = <LANGTAG> { lang = stripChars(t.image, 1) ; } )
|
( <DATATYPE> uri = IRIref() )
)?
{ return createLiteral(lex, lang, uri) ; }
}
Node NumericLiteral() : { Node n ; }
{
(
n = NumericLiteralUnsigned()
| n = NumericLiteralPositive()
| n = NumericLiteralNegative()
)
{ return n ; }
}
Node NumericLiteralUnsigned() : { Token t ; }
{
t = <INTEGER> { return createLiteralInteger(t.image) ; }
| t = <DECIMAL> { return createLiteralDecimal(t.image) ; }
| t = <DOUBLE> { return createLiteralDouble(t.image) ; }
}
Node NumericLiteralPositive() : { Token t ; }
{
t = <INTEGER_POSITIVE> { return createLiteralInteger(t.image) ; }
| t = <DECIMAL_POSITIVE> { return createLiteralDecimal(t.image) ; }
| t = <DOUBLE_POSITIVE> { return createLiteralDouble(t.image) ; }
}
Node NumericLiteralNegative() : { Token t ; }
{
t = <INTEGER_NEGATIVE> { return createLiteralInteger(t.image) ; }
| t = <DECIMAL_NEGATIVE> { return createLiteralDecimal(t.image) ; }
| t = <DOUBLE_NEGATIVE> { return createLiteralDouble(t.image) ; }
}
Node BooleanLiteral() : {}
{
<TRUE> { return XSD_TRUE ; }
|
<FALSE> { return XSD_FALSE ; }
}
String String() : { Token t ; String lex ; }
{
( t = <STRING_LITERAL1> { lex = stripQuotes(t.image) ; }
| t = <STRING_LITERAL2> { lex = stripQuotes(t.image) ; }
| t = <STRING_LITERAL_LONG1> { lex = stripQuotes3(t.image) ; }
| t = <STRING_LITERAL_LONG2> { lex = stripQuotes3(t.image) ; }
)
{
lex = unescapeStr(lex, t.beginLine, t.beginColumn) ;
return lex ;
}
}
String IRIref() : { String iri ; }
{
iri = IRI_REF() { return iri ; }
|
iri = PrefixedName() { return iri ; }
}
String PrefixedName() : { Token t ; }
{
( t = <PNAME_LN>
{ return resolvePName(t.image, t.beginLine, t.beginColumn) ; }
|
t = <PNAME_NS>
{ return resolvePName(t.image, t.beginLine, t.beginColumn) ; }
)
}
Node BlankNode() : { Token t = null ; }
{
t = <BLANK_NODE_LABEL>
{ return createBNode(t.image, t.beginLine, t.beginColumn) ; }
|
// <LBRACKET> <RBRACKET> { return createBNode() ; }
t = <ANON> { return createBNode( t.beginLine, t.beginColumn) ; }
}
String IRI_REF() : { Token t ; }
{
t = <IRIref>
{ return resolveQuotedIRI(t.image, t.beginLine, t.beginColumn) ; }
}
// ------------------------------------------
// Tokens
// Comments and whitespace
SKIP : { " " | "\t" | "\n" | "\r" | "\f" }
TOKEN: { <#WS: " " | "\t" | "\n" | "\r" | "\f"> }
SPECIAL_TOKEN :
{ <SINGLE_LINE_COMMENT: "#" (~["\n","\r"])* ("\n"|"\r"|"\r\n")? > }
// Main tokens */
TOKEN:
{
// Includes # for relative URIs
<IRIref: "<" (~[ ">","<", "\"", "{", "}", "^", "\\", "|", "`",
"\u0000"-"\u0020"])* ">" >
| <PNAME_NS: (<PN_PREFIX>)? ":" >
| <PNAME_LN: <PNAME_NS> <PN_LOCAL> >
| <BLANK_NODE_LABEL: "_:" <PN_LOCAL> >
| <VAR1: "?" <VARNAME> >
| <VAR2: "$" <VARNAME> >
| <LANGTAG: <AT> (<A2Z>)+("-" (<A2ZN>)+)* >
| <#A2Z: ["a"-"z","A"-"Z"]>
| <#A2ZN: ["a"-"z","A"-"Z","0"-"9"]>
}
// -------------------------------------------------
// Keyworks : includes operators that are words and should be
// before general things like IDENTIFIER which swallow almost
// anything
TOKEN : { <KW_A: "a" > }
TOKEN [IGNORE_CASE] :
{
// Prologue
< BASE: "base" >
| < PREFIX: "prefix" >
// Result forms
| < SELECT: "select" >
| < DISTINCT: "distinct" >
| < REDUCED: "reduced" >
| < DESCRIBE: "describe" >
| < CONSTRUCT: "construct" >
| < ASK: "ask" >
| < LIMIT: "limit" >
| < OFFSET: "offset" >
| < ORDER: "order" >
| < BY: "by" >
| < ASC: "asc" >
| < DESC: "desc" >
// Dataset
| < NAMED: "named" >
| < FROM: "from" >
// Graph pattern operators
| < WHERE: "where" >
| < AND: "and" >