blob: b5f71813de36c872b6966ab65692947add16dce9 (
plain) (
blame)
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage.expressions;
import com.yahoo.document.DataType;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.language.Linguistics;
import com.yahoo.language.process.Embedder;
import com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.vespa.indexinglanguage.ScriptParser;
import com.yahoo.vespa.indexinglanguage.ScriptParserContext;
import com.yahoo.vespa.indexinglanguage.parser.IndexingInput;
import com.yahoo.vespa.indexinglanguage.parser.ParseException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
/**
* @author Simon Thoresen Hult
*/
public final class ScriptExpression extends ExpressionList<StatementExpression> {
public ScriptExpression() {
this(Collections.emptyList());
}
public ScriptExpression(StatementExpression... lst) {
this(Arrays.asList(lst));
}
public ScriptExpression(Collection<? extends StatementExpression> lst) {
super(lst, resolveInputType(lst));
}
@Override
protected void doExecute(ExecutionContext context) {
FieldValue input = context.getValue();
for (Expression exp : this) {
context.setValue(input).execute(exp);
}
context.setValue(input);
}
@Override
protected void doVerify(VerificationContext context) {
DataType input = context.getValueType();
for (Expression exp : this) {
context.setValueType(input).execute(exp);
}
context.setValueType(input);
}
private static DataType resolveInputType(Collection<? extends StatementExpression> list) {
DataType prev = null;
for (Expression exp : list) {
DataType next = exp.requiredInputType();
if (prev == null) {
prev = next;
} else if (next != null && !prev.isAssignableFrom(next)) {
throw new VerificationException(ScriptExpression.class, "Statements require conflicting input types, " +
prev.getName() + " vs " + next.getName() + ".");
}
}
return prev;
}
@Override
public DataType createdOutputType() {
return null;
}
@Override
public String toString() {
StringBuilder ret = new StringBuilder();
ret.append("{ ");
for (Iterator<StatementExpression> it = iterator(); it.hasNext();) {
ret.append(it.next()).append(";");
if (it.hasNext()) {
ret.append(" ");
}
}
ret.append(" }");
return ret.toString();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj) && obj instanceof ScriptExpression;
}
/** Creates an expression with simple lingustics for testing */
@SuppressWarnings("deprecation")
public static ScriptExpression fromString(String expression) throws ParseException {
return fromString(expression, new SimpleLinguistics(), Embedder.throwsOnUse);
}
public static ScriptExpression fromString(String expression, Linguistics linguistics, Embedder embedder) throws ParseException {
return newInstance(new ScriptParserContext(linguistics, embedder).setInputStream(new IndexingInput(expression)));
}
public static ScriptExpression newInstance(ScriptParserContext config) throws ParseException {
return ScriptParser.parseScript(config);
}
}
|