Debian Codegraph @

net/sf/saxon/expr/parser/Evaluator.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.expr.parser;
9
10 import net.sf.saxon.Controller;
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.event.SequenceOutputter;
13 import net.sf.saxon.expr.*;
14 import net.sf.saxon.expr.instruct.Block;
15 import net.sf.saxon.om.*;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.value.*;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 /**
23 * An Evaluator evaluates an expression to return a sequence
24 */
25 public abstract class Evaluator {
26
27
28 public static Evaluator evaluatorForCode(int code) {
29 switch (code) {
30 case ExpressionTool.EVALUATE_LITERAL:
31 return Evaluator.LITERAL;
32 case ExpressionTool.EVALUATE_VARIABLE:
33 return Evaluator.VARIABLE;
34 case ExpressionTool.MAKE_CLOSURE:
35 return Evaluator.LAZY_SEQUENCE;
36 case ExpressionTool.MAKE_MEMO_CLOSURE:
37 return Evaluator.MEMO_CLOSURE;
38 case ExpressionTool.RETURN_EMPTY_SEQUENCE:
39 return Evaluator.EMPTY_SEQUENCE;
40 case ExpressionTool.EVALUATE_AND_MATERIALIZE_VARIABLE:
41 return Evaluator.VARIABLE;
42 case ExpressionTool.CALL_EVALUATE_SINGLE_ITEM:
43 return Evaluator.SINGLE_ITEM;
44 case ExpressionTool.CALL_EVALUATE_OPTIONAL_ITEM:
45 return Evaluator.OPTIONAL_ITEM;
46 case ExpressionTool.ITERATE_AND_MATERIALIZE:
47 return Evaluator.EAGER_SEQUENCE;
48 case ExpressionTool.PROCESS:
49 return Evaluator.PROCESS;
50 case ExpressionTool.LAZY_TAIL_EXPRESSION:
51 return Evaluator.LAZY_TAIL;
52 case ExpressionTool.SHARED_APPEND_EXPRESSION:
53 return Evaluator.SHARED_APPEND;
54 case ExpressionTool.MAKE_INDEXED_VARIABLE:
55 return Evaluator.MAKE_INDEXED_VARIABLE;
56 case ExpressionTool.MAKE_SINGLETON_CLOSURE:
57 return Evaluator.SINGLETON_CLOSURE;
58 case ExpressionTool.EVALUATE_SUPPLIED_PARAMETER:
59 return Evaluator.SUPPLIED_PARAMETER;
60 case ExpressionTool.STREAMING_ARGUMENT:
61 return Evaluator.STREAMING_ARGUMENT;
62 default:
63 return Evaluator.EAGER_SEQUENCE;
64 }
65 }
66
67 /**
68 * Evaluate an expression to return a sequence
69 * @param expr the expression to be evaluated
70 * @param context the dynamic context for evaluation
71 * @return the result of the evaluation
72 * @throws XPathException if any dynamic error occurs during the evaluation
73 */
74
75 public abstract Sequence<?> evaluate(Expression expr, XPathContext context) throws XPathException;
76
77 /**
78 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
79 * to be compatible with argument evaluation modes in earlier releases in the interests of
80 * SEF compatibility.
81 */
82
83 public abstract int getCode();
84
85 /**
86 * An evaluator that always returns the empty sequence
87 */
88
89 public final static Evaluator EMPTY_SEQUENCE = new Evaluator() {
90 @Override
91 public Sequence<?> evaluate(Expression expr, XPathContext context) {
92 return EmptySequence.getInstance();
93 }
94
95 /**
96 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
97 * to be compatible with argument evaluation modes in earlier releases in the interests of
98 * SEF compatibility.
99 */
100 @Override
101 public int getCode() {
102 return ExpressionTool.RETURN_EMPTY_SEQUENCE;
103 }
104 };
105
106 /**
107 * An evaluator for arguments supplied as a literal
108 */
109
110 public final static Evaluator LITERAL = new Evaluator() {
111 @Override
112 public Sequence<?> evaluate(Expression expr, XPathContext context) {
113 return ((Literal)expr).getValue();
114 }
115
116 /**
117 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
118 * to be compatible with argument evaluation modes in earlier releases in the interests of
119 * SEF compatibility.
120 */
121 @Override
122 public int getCode() {
123 return ExpressionTool.EVALUATE_LITERAL;
124 }
125 };
126
127 /**
128 * An evaluator for arguments supplied as a variable reference
129 */
130
131 public final static Evaluator VARIABLE = new Evaluator() {
132 @Override
133 public Sequence<?> evaluate(Expression expr, XPathContext context) throws XPathException {
134 try {
135 return ((VariableReference) expr).evaluateVariable(context);
136 } catch (ClassCastException e) {
137 // should not happen
138 assert false;
139 // but if it does...
140 return LAZY_SEQUENCE.evaluate(expr, context);
141 }
142 }
143
144 /**
145 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
146 * to be compatible with argument evaluation modes in earlier releases in the interests of
147 * SEF compatibility.
148 */
149 @Override
150 public int getCode() {
151 return ExpressionTool.EVALUATE_AND_MATERIALIZE_VARIABLE;
152 }
153 };
154
155 /**
156 * An evaluator for a reference to an external parameter value
157 */
158
159 public final static Evaluator SUPPLIED_PARAMETER = new Evaluator() {
160 @Override
161 public Sequence<?> evaluate(Expression expr, XPathContext context) throws XPathException {
162 try {
163 return ((SuppliedParameterReference) expr).evaluateVariable(context);
164 } catch (ClassCastException e) {
165 // should not happen
166 assert false;
167 // but if it does...
168 return LAZY_SEQUENCE.evaluate(expr, context);
169 }
170 }
171
172 /**
173 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
174 * to be compatible with argument evaluation modes in earlier releases in the interests of
175 * SEF compatibility.
176 */
177 @Override
178 public int getCode() {
179 return ExpressionTool.EVALUATE_SUPPLIED_PARAMETER;
180 }
181 };
182
183 /**
184 * A (default) evaluator for arguments supplied as an expression that will always return a
185 * singleton item
186 */
187
188 public final static Evaluator SINGLE_ITEM = new Evaluator() {
189 @Override
190 public Item<?> evaluate(Expression expr, XPathContext context) throws XPathException {
191 return expr.evaluateItem(context);
192 }
193
194 /**
195 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
196 * to be compatible with argument evaluation modes in earlier releases in the interests of
197 * SEF compatibility.
198 */
199 @Override
200 public int getCode() {
201 return ExpressionTool.CALL_EVALUATE_SINGLE_ITEM;
202 }
203 };
204
205 /**
206 * A (default) evaluator for arguments supplied as an expression that will return either a
207 * singleton item, or an empty sequence
208 */
209
210 public final static Evaluator OPTIONAL_ITEM = new Evaluator() {
211 @Override
212 public Sequence<?> evaluate(Expression expr, XPathContext context) throws XPathException {
213 Item<?> item = expr.evaluateItem(context);
214 return item==null ? EmptySequence.getInstance() : item;
215 }
216
217 /**
218 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
219 * to be compatible with argument evaluation modes in earlier releases in the interests of
220 * SEF compatibility.
221 */
222 @Override
223 public int getCode() {
224 return ExpressionTool.CALL_EVALUATE_OPTIONAL_ITEM;
225 }
226 };
227
228 /**
229 * An evaluator for arguments that in general return a sequence, where the sequence is evaluated
230 * lazily on first use. This is appropriate when calling a function which might not use the value, or
231 * might not use all of it. It returns a {@code LazySequence}, which can only be read once, so
232 * this is only suitable for use when calling a function that can be trusted to read the argument
233 * once only.
234 */
235
236 public final static Evaluator LAZY_SEQUENCE = new Evaluator() {
237 @Override
238 public Sequence<?> evaluate(Expression expr, XPathContext context) throws XPathException {
239 SequenceIterator<?> iter = expr.iterate(context);
240 return new LazySequence<>(iter);
241 }
242
243 /**
244 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
245 * to be compatible with argument evaluation modes in earlier releases in the interests of
246 * SEF compatibility.
247 */
248 @Override
249 public int getCode() {
250 return ExpressionTool.MAKE_CLOSURE;
251 }
252 };
253
254 /**
255 * An evaluator for arguments that in general return a sequence, where the sequence is evaluated
256 * lazily on first use, and where the value might be needed more than once.
257 */
258
259 public final static Evaluator MEMO_SEQUENCE = new Evaluator() {
260 @Override
261 public Sequence<?> evaluate(Expression expr, XPathContext context) throws XPathException {
262 SequenceIterator<?> iter = expr.iterate(context);
263 return new MemoSequence<>(iter);
264 }
265
266 /**
267 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
268 * to be compatible with argument evaluation modes in earlier releases in the interests of
269 * SEF compatibility.
270 */
271 @Override
272 public int getCode() {
273 return ExpressionTool.MAKE_MEMO_CLOSURE;
274 }
275 };
276
277 /**
278 * An evaluator for arguments that in general return a sequence, where the sequence is evaluated
279 * lazily on first use, and where the value might be needed more than once.
280 */
281
282 public final static Evaluator MEMO_CLOSURE = new Evaluator() {
283 @Override
284 public Sequence<?> evaluate(Expression expr, XPathContext context) throws XPathException {
285 return new MemoClosure(expr, context);
286 }
287
288 /**
289 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
290 * to be compatible with argument evaluation modes in earlier releases in the interests of
291 * SEF compatibility.
292 */
293 @Override
294 public int getCode() {
295 return ExpressionTool.MAKE_MEMO_CLOSURE;
296 }
297 };
298
299 /**
300 * An evaluator for arguments that in general return a sequence, where the sequence is evaluated
301 * lazily on first use, and where the value might be needed more than once.
302 */
303
304 public final static Evaluator SINGLETON_CLOSURE = new Evaluator() {
305 @Override
306 public Sequence<?> evaluate(Expression expr, XPathContext context) throws XPathException {
307 return new SingletonClosure(expr, context);
308 }
309
310 /**
311 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
312 * to be compatible with argument evaluation modes in earlier releases in the interests of
313 * SEF compatibility.
314 */
315 @Override
316 public int getCode() {
317 return ExpressionTool.MAKE_SINGLETON_CLOSURE;
318 }
319 };
320
321 /**
322 * An evaluator for arguments that in general return a sequence, where the sequence is evaluated
323 * eagerly. This is appropriate when it is known that the function will always use the entire value,
324 * or when it will use it more than once.
325 */
326
327 public final static Evaluator EAGER_SEQUENCE = new Evaluator() {
328 @Override
329 public Sequence<?> evaluate(Expression expr, XPathContext context) throws XPathException {
330 SequenceIterator<?> iter = expr.iterate(context);
331 return iter.materialize();
332 }
333
334 /**
335 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
336 * to be compatible with argument evaluation modes in earlier releases in the interests of
337 * SEF compatibility.
338 */
339 @Override
340 public int getCode() {
341 return ExpressionTool.ITERATE_AND_MATERIALIZE;
342 }
343 };
344
345 /**
346 * An evaluator for "shared append" expressions: used when the argument to a function
347 * is a block potentially containing a recursive call.
348 */
349
350 public final static Evaluator SHARED_APPEND = new Evaluator() {
351 @Override
352 public Sequence<?> evaluate(Expression expr, XPathContext context) throws XPathException {
353 if (expr instanceof Block) {
354 Block block = (Block) expr;
355 Operand[] children = block.getOperanda();
356 List<GroundedValue<?>> subsequences = new ArrayList<>(children.length);
357 for (Operand o : children) {
358 Expression child = o.getChildExpression();
359 if (Cardinality.allowsMany(child.getCardinality())) {
360 subsequences.add(child.iterate(context).materialize());
361 } else {
362 Item<?> j = child.evaluateItem(context);
363 if (j != null) {
364 subsequences.add(j);
365 }
366 }
367 }
368 return new Chain(subsequences);
369 } else {
370 // it's not a Block: it must have been rewritten after deciding to use this evaluation mode
371 return expr.iterate(context).materialize();
372 }
373 }
374
375 /**
376 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
377 * to be compatible with argument evaluation modes in earlier releases in the interests of
378 * SEF compatibility.
379 */
380 @Override
381 public int getCode() {
382 return ExpressionTool.SHARED_APPEND_EXPRESSION;
383 }
384 };
385
386 /**
387 * An evaluator for the first (streamed) argument of a streamable function call.
388 */
389
390 public final static Evaluator STREAMING_ARGUMENT = new Evaluator() {
391 @Override
392 public Sequence<?> evaluate(Expression expr, XPathContext context) throws XPathException {
393 return context.getConfiguration().obtainOptimizer().evaluateStreamingArgument(expr, context);
394 }
395
396 /**
397 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
398 * to be compatible with argument evaluation modes in earlier releases in the interests of
399 * SEF compatibility.
400 */
401 @Override
402 public int getCode() {
403 return ExpressionTool.STREAMING_ARGUMENT;
404 }
405 };
406
407 /**
408 * An evaluator for an expression that makes use of an indexed variable
409 */
410
411 public final static Evaluator MAKE_INDEXED_VARIABLE = new Evaluator() {
412 @Override
413 public Sequence<?> evaluate(Expression expr, XPathContext context) throws XPathException {
414 return context.getConfiguration().obtainOptimizer().makeIndexedValue(expr.iterate(context));
415 }
416
417 /**
418 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
419 * to be compatible with argument evaluation modes in earlier releases in the interests of
420 * SEF compatibility.
421 */
422 @Override
423 public int getCode() {
424 return ExpressionTool.MAKE_INDEXED_VARIABLE;
425 }
426 };
427
428 /**
429 * A push-mode evaluator for an expression
430 */
431
432 public final static Evaluator PROCESS = new Evaluator() {
433 @Override
434 public Sequence<?> evaluate(Expression expr, XPathContext context) throws XPathException {
435 Controller controller = context.getController();
436 Receiver saved = context.getReceiver();
437 SequenceOutputter seq = controller.allocateSequenceOutputter(20);
438 seq.getPipelineConfiguration().setHostLanguage(expr.getPackageData().getHostLanguage());
439 context.setReceiver(seq);
440 seq.open();
441 expr.process(context);
442 seq.close();
443 context.setReceiver(saved);
444 Sequence<?> val = seq.getSequence();
445 seq.reset();
446 return val;
447 }
448
449 /**
450 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
451 * to be compatible with argument evaluation modes in earlier releases in the interests of
452 * SEF compatibility.
453 */
454 @Override
455 public int getCode() {
456 return ExpressionTool.PROCESS;
457 }
458 };
459
460 /**
461 * An evaluator for arguments that in general return a sequence, where the sequence is evaluated
462 * lazily on first use. This is appropriate when calling a function which might not use the value, or
463 * might not use all of it. It returns a {@code LazySequence}, which can only be read once, so
464 * this is only suitable for use when calling a function that can be trusted to read the argument
465 * once only.
466 */
467
468 public final static Evaluator LAZY_TAIL = new Evaluator() {
469 @Override
470 public Sequence<?> evaluate(Expression expr, XPathContext context) throws XPathException {
471 TailExpression tail = (TailExpression) expr;
472 VariableReference vr = (VariableReference) tail.getBaseExpression();
473 Sequence<?> base = Evaluator.VARIABLE.evaluate(vr, context);
474 if (base instanceof MemoClosure) {
475 SequenceIterator<?> it = base.iterate();
476 base = it.materialize();
477 }
478 if (base instanceof IntegerRange) {
479 long start = ((IntegerRange) base).getStart() + tail.getStart() - 1;
480 long end = ((IntegerRange) base).getEnd();
481 if (start == end) {
482 return Int64Value.makeIntegerValue(end);
483 } else if (start > end) {
484 return EmptySequence.getInstance();
485 } else {
486 return new IntegerRange(start, end);
487 }
488 }
489 if (base instanceof SequenceExtent) {
490 SequenceExtent<?> baseSeq = (SequenceExtent<?>)base;
491 if (tail.getStart() > ((SequenceExtent) base).getLength()) {
492 return EmptySequence.getInstance();
493 } else {
494 return new SequenceExtent<>(
495 baseSeq,
496 tail.getStart() - 1,
497 baseSeq.getLength() - tail.getStart() + 1).reduce();
498 }
499 }
500
501 return new MemoClosure(tail, context);
502 }
503
504 /**
505 * Get an integer code identifying this evaluator (for use in the SEF file). The codes are chosen
506 * to be compatible with argument evaluation modes in earlier releases in the interests of
507 * SEF compatibility.
508 */
509 @Override
510 public int getCode() {
511 return ExpressionTool.LAZY_TAIL_EXPRESSION;
512 }
513 };
514
515
516 }
517
518
519