/
ExprAnalyzer.java
1488 lines (1353 loc) · 42.1 KB
/
ExprAnalyzer.java
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
/*
* [New BSD License]
* Copyright (c) 2011-2012, Brackit Project Team <info@brackit.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Brackit Project Team nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.brackit.xquery.compiler.analyzer;
import org.brackit.xquery.ErrorCode;
import org.brackit.xquery.QueryException;
import org.brackit.xquery.XQuery;
import org.brackit.xquery.atomic.AnyURI;
import org.brackit.xquery.atomic.Bool;
import org.brackit.xquery.atomic.Numeric;
import org.brackit.xquery.atomic.QNm;
import org.brackit.xquery.atomic.Str;
import org.brackit.xquery.compiler.AST;
import org.brackit.xquery.compiler.Bits;
import org.brackit.xquery.compiler.XQ;
import org.brackit.xquery.expr.Variable;
import org.brackit.xquery.module.Functions;
import org.brackit.xquery.module.Module;
import org.brackit.xquery.module.Namespaces;
import org.brackit.xquery.module.StaticContext;
import org.brackit.xquery.util.Whitespace;
import org.brackit.xquery.xdm.Function;
import org.brackit.xquery.xdm.Kind;
import org.brackit.xquery.xdm.type.AnyItemType;
import org.brackit.xquery.xdm.type.AtomicType;
import org.brackit.xquery.xdm.type.AttributeType;
import org.brackit.xquery.xdm.type.Cardinality;
import org.brackit.xquery.xdm.type.ElementType;
import org.brackit.xquery.xdm.type.ItemType;
import org.brackit.xquery.xdm.type.NSNameWildcardTest;
import org.brackit.xquery.xdm.type.NSWildcardNameTest;
import org.brackit.xquery.xdm.type.SequenceType;
/**
*
* @author Sebastian Baechle
*
*/
public class ExprAnalyzer extends AbstractAnalyzer {
protected final VarScopes variables;
protected final Module module;
public ExprAnalyzer(Module module) {
this.variables = new VarScopes();
this.module = module;
this.sctx = module.getStaticContext();
}
public Module getModule() {
return module;
}
void functionBody(AST body) throws QueryException {
enclosedExpr(body);
}
protected boolean queryBody(AST body) throws QueryException {
if (body.getType() != XQ.QueryBody) {
return false;
}
expr(body.getChild(0));
return true;
}
boolean expr(AST expr) throws QueryException {
if (expr.getType() != XQ.SequenceExpr) {
exprSingle(expr);
} else {
for (int i = 0; i < expr.getChildCount(); i++) {
exprSingle(expr.getChild(i));
}
}
return true;
}
boolean exprSingle(AST expr) throws QueryException {
return (flowrExpr(expr) || quantifiedExpr(expr) || switchExpr(expr)
|| typeswitchExpr(expr) || ifExpr(expr) || tryCatchExpr(expr) ||
/* Begin XQuery Update Facility 1.0 */
insertExpr(expr) || deleteExpr(expr) || renameExpr(expr)
|| replaceExpr(expr) || transformExpr(expr) ||
/* End XQuery Update Facility 1.0 */
orExpr(expr));
}
// Begin XQuery Update Facility 1.0
protected boolean insertExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.InsertExpr) {
return false;
}
AST src = expr.getChild(1);
exprSingle(src);
AST target = expr.getChild(2);
exprSingle(target);
return true;
}
protected boolean deleteExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.DeleteExpr) {
return false;
}
AST target = expr.getChild(0);
exprSingle(target);
return true;
}
protected boolean renameExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.RenameExpr) {
return false;
}
AST target = expr.getChild(0);
exprSingle(target);
AST newName = expr.getChild(1);
exprSingle(newName);
return true;
}
protected boolean replaceExpr(AST expr) throws QueryException {
if (expr.getType() == XQ.ReplaceValueExpr) {
AST target = expr.getChild(0);
exprSingle(target);
AST replacement = expr.getChild(1);
exprSingle(replacement);
} else if (expr.getType() == XQ.ReplaceNodeExpr) {
AST target = expr.getChild(0);
exprSingle(target);
AST replacement = expr.getChild(1);
exprSingle(replacement);
} else {
return false;
}
return true;
}
protected boolean transformExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.TransformExpr) {
return false;
}
int scopeCount = scopeCount();
int pos = 0;
while (pos < expr.getChildCount() - 2) {
openScope();
AST binding = expr.getChild(pos++);
QNm name = (QNm) binding.getChild(0).getValue();
// expand, bind and update AST
name = expand(name, DefaultNS.EMPTY);
name = bind(name);
binding.getChild(0).setValue(name);
exprSingle(binding.getChild(1));
offerScope();
}
AST modify = expr.getChild(pos++);
exprSingle(modify);
AST ret = expr.getChild(pos);
exprSingle(ret);
for (int i = scopeCount(); i > scopeCount; i--) {
closeScope();
}
return true;
}
// End XQuery Update Facility 1.0
protected boolean flowrExpr(AST flwor) throws QueryException {
if (flwor.getType() != XQ.FlowrExpr) {
return false;
}
int scopeCount = scopeCount();
initialClause(flwor.getChild(0));
for (int i = 1; i < flwor.getChildCount() - 1; i++) {
intermediateClause(flwor.getChild(i));
}
exprSingle(flwor.getChild(flwor.getChildCount() - 1).getChild(0));
for (int i = scopeCount(); i > scopeCount; i--) {
closeScope();
}
return true;
}
protected boolean initialClause(AST clause) throws QueryException {
return (forClause(clause) || letClause(clause) || windowClause(clause));
}
protected boolean forClause(AST clause) throws QueryException {
if (clause.getType() != XQ.ForClause) {
return false;
}
forBinding(clause);
return true;
}
protected boolean forBinding(AST clause) throws QueryException {
openScope();
int pos = 0;
AST child = clause.getChild(pos++);
QNm forVar = typedVarBinding(child);
child = clause.getChild(pos++);
if (child.getType() == XQ.AllowingEmpty) {
child = clause.getChild(pos++);
}
if (child.getType() == XQ.TypedVariableBinding) {
try {
positionalVar(child);
} catch (QueryException e) {
if (e.getCode().equals(ErrorCode.ERR_DUPLICATE_VARIABLE_DECL)) {
throw new QueryException(
ErrorCode.ERR_FOR_VAR_AND_POS_VAR_EQUAL,
"Bound variable '%s' and its associated positional variable have the same name",
forVar);
}
throw e;
}
child = clause.getChild(pos++);
}
exprSingle(child);
offerScope();
return true;
}
protected QNm typedVarBinding(AST binding) throws QueryException {
QNm name = (QNm) binding.getChild(0).getValue();
// expand, bind and update AST
QNm expanded = expand(name, DefaultNS.EMPTY);
name = bind(expanded);
binding.getChild(0).setValue(name);
SequenceType stype;
if (binding.getChildCount() >= 2) {
stype = typeDeclaration(binding.getChild(1));
} else {
stype = new SequenceType(AnyItemType.ANY, Cardinality.ZeroOrMany);
}
return expanded;
}
SequenceType typeDeclaration(AST decl) throws QueryException {
return sequenceType(decl);
}
protected QNm positionalVar(AST binding) throws QueryException {
QNm name = (QNm) binding.getChild(0).getValue();
// expand, bind and update AST
QNm expanded = expand(name, DefaultNS.EMPTY);
name = bind(expanded);
binding.getChild(0).setValue(name);
SequenceType stype = new SequenceType(AtomicType.INR, Cardinality.One);
return expanded;
}
protected boolean letClause(AST clause) throws QueryException {
if (clause.getType() != XQ.LetClause) {
return false;
}
letBinding(clause);
return true;
}
protected boolean letBinding(AST clause) throws QueryException {
openScope();
typedVarBinding(clause.getChild(0));
exprSingle(clause.getChild(1));
offerScope();
return true;
}
protected boolean windowClause(AST clause) throws QueryException {
return (tumblingWindowClause(clause) || slidingWindowClause(clause));
}
protected boolean tumblingWindowClause(AST clause) throws QueryException {
if (clause.getType() != XQ.TumblingWindowClause) {
return false;
}
openScope();
typedVarBinding(clause.getChild(0));
exprSingle(clause.getChild(1));
windowStartCondition(clause.getChild(2));
if (clause.getChildCount() >= 4) {
windowEndCondition(clause.getChild(3));
}
// TODO check all variable names are distinct
offerScope();
return true;
}
protected boolean windowStartCondition(AST cond) throws QueryException {
openScope();
windowVars(cond.getChild(0));
offerScope();
exprSingle(cond.getChild(1));
closeScope();
return true;
}
protected boolean windowEndCondition(AST cond) throws QueryException {
openScope();
windowVars(cond.getChild(0));
offerScope();
exprSingle(cond.getChild(1));
closeScope();
return true;
}
protected void windowVars(AST windowVars) throws QueryException {
for (int i = 0; i < windowVars.getChildCount(); i++) {
QNm name = (QNm) windowVars.getChild(i).getChild(0).getValue();
// expand, bind and update AST
name = expand(name, DefaultNS.EMPTY);
name = bind(name);
windowVars.getChild(i).getChild(0).setValue(name);
}
}
protected boolean slidingWindowClause(AST clause) throws QueryException {
if (clause.getType() != XQ.SlidingWindowClause) {
return false;
}
openScope();
typedVarBinding(clause.getChild(0));
exprSingle(clause.getChild(1));
windowStartCondition(clause.getChild(2));
windowEndCondition(clause.getChild(3));
// TODO check all variable names are distinct
offerScope();
return true;
}
protected boolean intermediateClause(AST clause) throws QueryException {
return (initialClause(clause) || whereClause(clause)
|| groupByClause(clause) || orderByClause(clause) || countClause(clause));
}
protected boolean whereClause(AST clause) throws QueryException {
if (clause.getType() != XQ.WhereClause) {
return false;
}
exprSingle(clause.getChild(0));
return true;
}
protected boolean groupByClause(AST clause) throws QueryException {
if (clause.getType() != XQ.GroupByClause) {
return false;
}
for (int i = 0; i < clause.getChildCount() - 1; i++) {
AST groupBySpec = clause.getChild(i);
QNm name = (QNm) groupBySpec.getChild(0).getValue();
// expand resolve and update AST
name = expand(name, DefaultNS.EMPTY);
name = resolve(name);
groupBySpec.getChild(0).setValue(name);
// TODO check for err:XQST0094
if (groupBySpec.getChildCount() >= 2) {
String col = groupBySpec.getChild(1).getStringValue();
if (!col.equals(StaticContext.UNICODE_COLLATION)) {
throw new QueryException(
ErrorCode.ERR_UNKNOWN_COLLATION_IN_FLWOR_CLAUSE,
"Unknown collation in group-by clause: %s", col);
}
}
}
return true;
}
protected boolean orderByClause(AST clause) throws QueryException {
if (clause.getType() != XQ.OrderByClause) {
return false;
}
for (int i = 0; i < clause.getChildCount(); i++) {
AST orderBySpec = clause.getChild(i);
exprSingle(orderBySpec.getChild(0));
for (int j = 1; j < orderBySpec.getChildCount(); j++) {
if (orderBySpec.getChild(j).getType() == XQ.Collation) {
String col = orderBySpec.getChild(j).getStringValue();
if (!col.equals(StaticContext.UNICODE_COLLATION)) {
throw new QueryException(
ErrorCode.ERR_UNKNOWN_COLLATION_IN_FLWOR_CLAUSE,
"Unknown collation in order-by clause: %s", col);
}
}
}
}
return true;
}
protected boolean countClause(AST clause) throws QueryException {
if (clause.getType() != XQ.CountClause) {
return false;
}
QNm name = (QNm) clause.getChild(0).getChild(0).getValue();
// expand, bind and update AST
name = expand(name, DefaultNS.EMPTY);
name = bind(name);
clause.getChild(0).getChild(0).setValue(name);
return true;
}
protected boolean quantifiedExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.QuantifiedExpr) {
return false;
}
int scopeCount = scopeCount();
// child 0 is quantifier type
for (int i = 1; i < expr.getChildCount() - 1; i++) {
openScope();
typedVarBinding(expr.getChild(i).getChild(0));
exprSingle(expr.getChild(i).getChild(1));
offerScope();
}
exprSingle(expr.getChild(expr.getChildCount() - 1));
for (int i = scopeCount(); i > scopeCount; i--) {
closeScope();
}
return true;
}
protected boolean switchExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.SwitchExpr) {
return false;
}
expr(expr.getChild(0));
for (int i = 1; i < expr.getChildCount() - 1; i++) {
switchClause(expr.getChild(i));
}
exprSingle(expr.getChild(expr.getChildCount() - 1));
return true;
}
protected boolean switchClause(AST clause) throws QueryException {
for (int i = 0; i < clause.getChildCount() - 1; i++) {
exprSingle(clause.getChild(i));
}
exprSingle(clause.getChild(clause.getChildCount() - 1));
return true;
}
protected boolean typeswitchExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.TypeSwitch) {
return false;
}
openScope();
expr(expr.getChild(0));
for (int i = 1; i < expr.getChildCount(); i++) {
// handle default case as case clause
caseClause(expr.getChild(i));
}
closeScope();
return true;
}
protected boolean caseClause(AST clause) throws QueryException {
openScope();
int pos = 0;
AST varOrType = clause.getChild(pos);
if (varOrType.getType() == XQ.Variable) {
QNm name = (QNm) varOrType.getValue();
// expand, bind and update AST
name = expand(name, DefaultNS.EMPTY);
name = bind(name);
varOrType.setValue(name);
pos++;
}
while (pos < clause.getChildCount() - 1) {
sequenceType(clause.getChild(pos++));
}
offerScope();
exprSingle(clause.getChild(clause.getChildCount() - 1));
closeScope();
return true;
}
protected boolean ifExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.IfExpr) {
return false;
}
exprSingle(expr.getChild(0));
exprSingle(expr.getChild(1));
exprSingle(expr.getChild(2));
return true;
}
protected boolean tryCatchExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.TryCatchExpr) {
return false;
}
expr(expr.getChild(0));
openScope();
QNm error = bind(Namespaces.ERR_CODE);
AST errorBinding = errorInfoBinding(error, "xs:QName",
XQ.CardinalityOne);
expr.insertChild(1, errorBinding);
QNm desc = bind(Namespaces.ERR_DESCRIPTION);
AST descBinding = errorInfoBinding(desc, "xs:string",
XQ.CardinalityZeroOrOne);
expr.insertChild(2, descBinding);
QNm value = bind(Namespaces.ERR_VALUE);
AST valueBinding = errorInfoBinding(value, "xs:item",
XQ.CardinalityZeroOrMany);
expr.insertChild(3, valueBinding);
QNm module = bind(Namespaces.ERR_MODULE);
AST moduleBinding = errorInfoBinding(module, "xs:string",
XQ.CardinalityZeroOrOne);
expr.insertChild(4, moduleBinding);
QNm lineNo = bind(Namespaces.ERR_LINE_NUMBER);
AST lineNoBinding = errorInfoBinding(lineNo, "xs:integer",
XQ.CardinalityZeroOrOne);
expr.insertChild(5, lineNoBinding);
QNm colNo = bind(Namespaces.ERR_COLUMN_NUMBER);
AST colNoBinding = errorInfoBinding(colNo, "xs:integer",
XQ.CardinalityZeroOrOne);
expr.insertChild(6, colNoBinding);
offerScope();
for (int i = 7; i < expr.getChildCount(); i++) {
tryClause(expr.getChild(i));
}
closeScope();
return true;
}
private AST errorInfoBinding(QNm error, String type, int card) {
AST errorBinding = new AST(XQ.TypedVariableBinding);
errorBinding.addChild(new AST(XQ.QNm, error));
AST sType = new AST(XQ.SequenceType);
AST aType = new AST(XQ.AtomicOrUnionType);
aType.addChild(new AST(XQ.QNm, type));
sType.addChild(aType);
sType.addChild(new AST(card));
errorBinding.addChild(sType);
return errorBinding;
}
protected boolean tryClause(AST clause) throws QueryException {
catchErrorList(clause.getChild(0));
expr(clause.getChild(1));
return true;
}
protected void catchErrorList(AST errorList) throws QueryException {
for (int i = 0; i < errorList.getChildCount(); i++) {
nameTest(errorList.getChild(i), false);
}
}
protected void catchVars(AST catchVar) throws QueryException {
for (int i = 0; i < catchVar.getChildCount(); i++) {
QNm name = (QNm) catchVar.getChild(i).getValue();
// expand and update AST
name = expand(name, DefaultNS.EMPTY);
catchVar.getChild(i).setValue(name);
}
}
protected boolean orExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.OrExpr) {
return andExpr(expr);
}
orExpr(expr.getChild(0));
andExpr(expr.getChild(1));
return true;
}
protected boolean andExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.AndExpr) {
return comparisonExpr(expr);
}
andExpr(expr.getChild(0));
comparisonExpr(expr.getChild(1));
return true;
}
protected boolean comparisonExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.ComparisonExpr) {
return rangeExpr(expr);
}
comparisonExpr(expr.getChild(1));
rangeExpr(expr.getChild(2));
return true;
}
protected boolean rangeExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.RangeExpr) {
return additiveExpr(expr);
}
rangeExpr(expr.getChild(0));
additiveExpr(expr.getChild(1));
return true;
}
protected boolean additiveExpr(AST expr) throws QueryException {
if ((expr.getType() != XQ.ArithmeticExpr)
|| ((expr.getChild(0).getType() != XQ.AddOp) && (expr.getChild(
0).getType() != XQ.SubtractOp))) {
return multiplicativeExpr(expr);
}
additiveExpr(expr.getChild(1));
multiplicativeExpr(expr.getChild(2));
return true;
}
protected boolean multiplicativeExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.ArithmeticExpr) {
return unionExpr(expr);
}
multiplicativeExpr(expr.getChild(1));
unionExpr(expr.getChild(2));
return true;
}
protected boolean unionExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.UnionExpr) {
return intersectExpr(expr);
}
unionExpr(expr.getChild(0));
intersectExpr(expr.getChild(1));
return true;
}
protected boolean intersectExpr(AST expr) throws QueryException {
if ((expr.getType() != XQ.IntersectExpr)
&& (expr.getType() != XQ.ExceptExpr)) {
return instanceOfExpr(expr);
}
intersectExpr(expr.getChild(0));
instanceOfExpr(expr.getChild(1));
return true;
}
protected boolean instanceOfExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.InstanceofExpr) {
return treatExpr(expr);
}
treatExpr(expr.getChild(0));
sequenceType(expr.getChild(1));
return true;
}
protected boolean treatExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.TreatExpr) {
return castableExpr(expr);
}
castableExpr(expr.getChild(0));
sequenceType(expr.getChild(1));
return true;
}
protected boolean castableExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.CastableExpr) {
return castExpr(expr);
}
castExpr(expr.getChild(0));
singleType(expr.getChild(1));
return true;
}
protected boolean castExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.CastExpr) {
return unaryExpr(expr);
}
unaryExpr(expr.getChild(0));
singleType(expr.getChild(1));
return true;
}
protected boolean unaryExpr(AST expr) throws QueryException {
return valueExpr(expr);
}
protected boolean valueExpr(AST expr) throws QueryException {
return (validateExpr(expr) || pathExpr(expr) || extensionExpr(expr));
}
protected boolean extensionExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.ExtensionExpr) {
return false;
}
for (int i = 0; i < expr.getChildCount(); i++) {
AST pragmaOrExpr = expr.getChild(i);
if (pragmaOrExpr.getType() == XQ.Pragma) {
QNm name = (QNm) pragmaOrExpr.getChild(0).getValue();
// expand and update AST
name = expand(name, DefaultNS.PRAGMA);
pragmaOrExpr.getChild(0).setValue(name);
} else {
expr(pragmaOrExpr);
}
}
return true;
}
protected boolean pathExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.PathExpr) {
return stepExpr(expr);
}
for (int i = 0; i < expr.getChildCount(); i++) {
stepExpr(expr.getChild(i));
openContextItemScope();
}
for (int i = 0; i < expr.getChildCount(); i++) {
closeContextItemScope();
}
return true;
}
// BEGIN Custom record syntax extension
protected boolean stepExpr(AST expr) throws QueryException {
return (origStepExpr(expr) || derefExpr(expr));
}
// vanilla XQuery 3.0 step expression
protected boolean origStepExpr(AST expr) throws QueryException {
return (postFixExpr(expr) || axisStep(expr));
}
// END Custom record syntax extension
protected boolean axisStep(AST expr) throws QueryException {
if (expr.getType() != XQ.StepExpr) {
return false;
}
// child 0 is the axis
nodeTest(expr.getChild(1),
expr.getChild(0).getChild(0).getType() == XQ.ATTRIBUTE);
referContextItem();
openContextItemScope();
for (int i = 2; i < expr.getChildCount(); i++) {
predicate(expr.getChild(i));
}
closeContextItemScope();
return true;
}
protected ItemType nodeTest(AST nodeTest, boolean attributeAxis)
throws QueryException {
ItemType test = kindTest(nodeTest);
test = (test != null) ? test : nameTest(nodeTest, !attributeAxis);
return test;
}
protected ItemType nameTest(AST test, boolean element)
throws QueryException {
if (test.getType() != XQ.NameTest) {
return null;
}
ItemType type = wildcard(test.getChild(0), element);
if (type != null) {
return type;
}
if (element) {
QNm name = (QNm) test.getChild(0).getValue();
// expand and update AST
name = expand(name, DefaultNS.ELEMENT_OR_TYPE);
test.getChild(0).setValue(name);
return new ElementType(name);
} else {
QNm name = (QNm) test.getChild(0).getValue();
// expand and update AST
name = expand(name, DefaultNS.EMPTY);
test.getChild(0).setValue(name);
return new AttributeType(name);
}
}
protected ItemType wildcard(AST test, boolean element)
throws QueryException {
if (test.getType() == XQ.Wildcard) {
return new ElementType();
} else if (test.getType() == XQ.NSWildcardNameTest) {
Kind kind = (element) ? Kind.ELEMENT : Kind.ATTRIBUTE;
return new NSWildcardNameTest(kind, test.getStringValue());
} else if (test.getType() == XQ.NSNameWildcardTest) {
Kind kind = (element) ? Kind.ELEMENT : Kind.ATTRIBUTE;
return new NSNameWildcardTest(kind,
resolvePrefix(test.getStringValue()));
} else {
return null;
}
}
protected void predicate(AST predicate) throws QueryException {
expr(predicate.getChild(0));
}
protected boolean validateExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.ValidateExpr) {
return false;
}
if (expr.getChild(0).getType() == XQ.QNm) {
QNm name = (QNm) expr.getChild(0).getValue();
// expand and update AST
name = expand(name, DefaultNS.ELEMENT_OR_TYPE);
expr.getChild(0).setValue(name);
sctx.getTypes().resolveType(name);
}
expr(expr.getChild(1));
return true;
}
protected boolean postFixExpr(AST expr) throws QueryException {
// BEGIN Custom array syntax extension
if (expr.getType() == XQ.ArrayAccess) {
expr(expr.getChild(0));
exprSingle(expr.getChild(1));
return true;
}
// END Custom array syntax extension
// BEGIN Custom array syntax extension
if (expr.getType() == XQ.RecordProjection) {
expr(expr.getChild(0));
for (int i = 1; i < expr.getChildCount(); i++) {
exprSingle(expr.getChild(i));
}
return true;
}
// END Custom array syntax extension
if (expr.getType() == XQ.FilterExpr) {
expr(expr.getChild(0));
openContextItemScope();
for (int i = 1; i < expr.getChildCount(); i++) {
predicate(expr.getChild(i));
}
closeContextItemScope();
} else if (expr.getType() == XQ.DynamicFunctionCallExpr) {
expr(expr.getChild(0));
for (int i = 1; i < expr.getChildCount(); i++) {
argument(expr.getChild(i));
}
} else {
return primaryExpr(expr);
}
return true;
}
protected boolean primaryExpr(AST expr) throws QueryException {
return (literal(expr) || varRef(expr) || parenthesizedExpr(expr)
|| contextItemExpr(expr) || functionCall(expr)
|| orderedExpr(expr) || unorderedExpr(expr)
|| constructor(expr) || functionItemExpr(expr));
}
protected boolean literal(AST expr) throws QueryException {
return (numericLiteral(expr) || (expr.getType() == XQ.Str));
}
protected boolean varRef(AST expr) throws QueryException {
if (expr.getType() != XQ.VariableRef) {
return false;
}
QNm name = (QNm) expr.getValue();
// expand, resolve and update AST
name = expand(name, DefaultNS.EMPTY);
name = resolve(name);
expr.setValue(name);
return true;
}
protected boolean parenthesizedExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.ParenthesizedExpr) {
return false;
}
if (expr.getChildCount() != 0) {
expr(expr.getChild(0));
}
return true;
}
protected boolean contextItemExpr(AST expr) throws QueryException {
if (expr.getType() != XQ.ContextItemExpr) {
return false;
}
referContextItem();
return true;
}
protected boolean functionCall(AST expr) throws QueryException {
if (expr.getType() != XQ.FunctionCall) {
return false;
}
QNm name = (QNm) expr.getValue();
// expand and update AST
name = expand(name, DefaultNS.FUNCTION);
expr.setValue(name);
if (replaceFunctionCall(expr, name)) {
return true;
}
int noOfParams = expr.getChildCount();
for (int i = 0; i < noOfParams; i++) {
argument(expr.getChild(i));
}
Function fun = sctx.getFunctions().resolve(name, noOfParams);
if (fun == null) {
unknownFunction(name, noOfParams);
}
if ((noOfParams == 0)
&& (fun.getSignature().defaultCtxItemType() != null)) {
referContextItem();
}
if (fun == null) {
unknownFunction(name, noOfParams);
}
return true;
}
private void unknownFunction(QNm name, int noOfParams)
throws QueryException {
String argp = (noOfParams > 0) ? "?" : "";
for (int i = 1; i < noOfParams; i++) {
argp += ", ?";
}
throw new QueryException(ErrorCode.ERR_UNDEFINED_FUNCTION,
"Unknown function: %s(%s)", name, argp);
}
protected boolean replaceFunctionCall(AST expr, QNm name)
throws QueryException {
int argc = expr.getChildCount();
if ((name.equals(Functions.FN_POSITION))
|| (name.equals(Functions.FN_LAST))) {
if (argc != 0) {
throw new QueryException(ErrorCode.ERR_UNDEFINED_FUNCTION,
"Illegal number of parameters for function %s() : %s'",
name, argc);
}
// change expr to variable reference
expr.setType(XQ.VariableRef);
QNm newName = name.equals(Functions.FN_POSITION) ? Bits.FS_POSITION
: Bits.FS_LAST;
expr.setValue(newName);
return true;
}
if ((name.equals(Functions.FN_TRUE) || name.equals(Functions.FN_FALSE))) {
if (argc != 0) {
throw new QueryException(ErrorCode.ERR_UNDEFINED_FUNCTION,
"Illegal number of parameters for function %s() : %s'",
name, argc);
}
// change expr to boolean literal
expr.setType(XQ.Bool);
Bool val = (name.equals(Functions.FN_TRUE)) ? Bool.TRUE
: Bool.FALSE;
expr.setValue(val);
return true;
}
if (name.equals(Functions.FN_DEFAULT_COLLATION)) {
if (argc != 0) {
throw new QueryException(ErrorCode.ERR_UNDEFINED_FUNCTION,
"Illegal number of parameters for function %s() : %s'",
name, argc);
}
// change expr to string literal
expr.setType(XQ.Str);
expr.setValue(new Str(sctx.getDefaultCollation()));
return true;
}
if (name.equals(Functions.FN_STATIC_BASE_URI)) {
if (argc != 0) {
throw new QueryException(ErrorCode.ERR_UNDEFINED_FUNCTION,
"Illegal number of parameters for function %s() : %s'",
name, argc);
}
AnyURI baseURI = sctx.getBaseURI();
if (baseURI != null) {
// change expr to uri literal
expr.setType(XQ.AnyURI);
expr.setValue(baseURI);
} else {
// change expr to empty sequence
expr.setType(XQ.SequenceExpr);
expr.setValue(XQ.NAMES[XQ.SequenceExpr]);
}
return true;
}
return false;
}