-
Notifications
You must be signed in to change notification settings - Fork 3
/
AstEvaluator.java
455 lines (400 loc) · 13.1 KB
/
AstEvaluator.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
/*******************************************************************************
* Copyright (c) 2015, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.query.parser;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.acceleo.query.ast.And;
import org.eclipse.acceleo.query.ast.Binding;
import org.eclipse.acceleo.query.ast.BooleanLiteral;
import org.eclipse.acceleo.query.ast.Call;
import org.eclipse.acceleo.query.ast.ClassTypeLiteral;
import org.eclipse.acceleo.query.ast.Conditional;
import org.eclipse.acceleo.query.ast.EClassifierTypeLiteral;
import org.eclipse.acceleo.query.ast.EnumLiteral;
import org.eclipse.acceleo.query.ast.Expression;
import org.eclipse.acceleo.query.ast.Implies;
import org.eclipse.acceleo.query.ast.IntegerLiteral;
import org.eclipse.acceleo.query.ast.Lambda;
import org.eclipse.acceleo.query.ast.Let;
import org.eclipse.acceleo.query.ast.NullLiteral;
import org.eclipse.acceleo.query.ast.Or;
import org.eclipse.acceleo.query.ast.RealLiteral;
import org.eclipse.acceleo.query.ast.SequenceInExtensionLiteral;
import org.eclipse.acceleo.query.ast.SetInExtensionLiteral;
import org.eclipse.acceleo.query.ast.StringLiteral;
import org.eclipse.acceleo.query.ast.TypeLiteral;
import org.eclipse.acceleo.query.ast.TypeSetLiteral;
import org.eclipse.acceleo.query.ast.VarRef;
import org.eclipse.acceleo.query.ast.util.AstSwitch;
import org.eclipse.acceleo.query.runtime.EvaluationResult;
import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
import org.eclipse.acceleo.query.runtime.impl.EvaluationServices;
import org.eclipse.acceleo.query.runtime.impl.LambdaValue;
import org.eclipse.acceleo.query.runtime.impl.Nothing;
import org.eclipse.acceleo.query.runtime.impl.NullValue;
import org.eclipse.acceleo.query.validation.type.ClassType;
import org.eclipse.acceleo.query.validation.type.IType;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EEnumLiteral;
/**
* Evaluates the asts.
*
* @author <a href="mailto:romain.guider@obeo.fr">Romain Guider</a>
*/
public class AstEvaluator extends AstSwitch<Object> {
/**
* Message used to report bad predicate typing runtime detection.
*/
private static final String BAD_PREDICATE_TYPE_MSG = "Conditional's predicate must evaluate to a boolean value but was %s instead.";
/**
* Variable definitions used during evaluation.
*/
private final Deque<Map<String, Object>> variablesStack = new ArrayDeque<Map<String, Object>>();
/**
* The evaluation services.
*/
private final EvaluationServices services;
/** Aggregated status of an evaluation. */
private Diagnostic diagnostic;
/**
* Creates a new {@link AstEvaluator} instance given an {@link IReadOnlyQueryEnvironment} instance.
*
* @param queryEnv
* the environment used to evaluate
* @deprecated use {@link #AstEvaluator(EvaluationServices)}
*/
public AstEvaluator(IReadOnlyQueryEnvironment queryEnv) {
this(new EvaluationServices(queryEnv));
}
/**
* Creates a new {@link AstEvaluator} instance given an {@link EvaluationServices} instance.
*
* @param services
* the {@link EvaluationServices} used to evaluate
*/
public AstEvaluator(EvaluationServices services) {
this.services = services;
}
/**
* Pushes the given variables into the stack.
*
* @param variables
* the variables to push
*/
protected void pushVariables(Map<String, Object> variables) {
variablesStack.addLast(variables);
}
/**
* Peeks the last {@link #pushVariables(Map) pushed} variables from the stack.
*
* @return the last {@link #pushVariables(Map) pushed} variables from the stack
*/
protected Map<String, Object> peekVariables() {
return variablesStack.peekLast();
}
/**
* Pops the last {@link #pushVariables(Map) pushed} variables from the stack.
*
* @return the last {@link #pushVariables(Map) pushed} variables from the stack
*/
protected Map<String, Object> popVariables() {
return variablesStack.removeLast();
}
/**
* Evaluates the specified expression given the specified variable definitions.
*
* @param varDefinitions
* the variable definitions
* @param ast
* the ast to evaluate.
* @return the evaluation of the specified ast.
*/
public EvaluationResult eval(Map<String, Object> varDefinitions, Expression ast) {
final Object result;
pushVariables(varDefinitions);
diagnostic = new BasicDiagnostic();
final Object value = doSwitch(ast);
final IType nullType;
if (value != null && value.getClass() == NullValue.class) {
result = null;
nullType = ((NullValue)value).getType();
} else if (value == null) {
result = value;
nullType = new ClassType(services.getQueryEnvironment(), null);
} else {
result = value;
nullType = null;
}
popVariables();
return new EvaluationResult(result, nullType, diagnostic);
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseBooleanLiteral(org.eclipse.acceleo.query.ast.BooleanLiteral)
*/
@Override
public Object caseBooleanLiteral(BooleanLiteral object) {
return object.isValue();
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseIntegerLiteral(org.eclipse.acceleo.query.ast.IntegerLiteral)
*/
@Override
public Object caseIntegerLiteral(IntegerLiteral object) {
return object.getValue();
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseRealLiteral(org.eclipse.acceleo.query.ast.RealLiteral)
*/
@Override
public Object caseRealLiteral(RealLiteral object) {
return object.getValue();
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseStringLiteral(org.eclipse.acceleo.query.ast.StringLiteral)
*/
@Override
public Object caseStringLiteral(StringLiteral object) {
return object.getValue();
}
@Override
public Object caseClassTypeLiteral(ClassTypeLiteral object) {
return object.getValue();
}
@Override
public Object caseEClassifierTypeLiteral(EClassifierTypeLiteral object) {
final Object result;
final EClassifier eClassifier = services.getEClassifier(object);
if (eClassifier != null) {
result = eClassifier;
} else {
final Nothing nothing = new Nothing("Invalid classifier.");
Diagnostic diag = new BasicDiagnostic(Diagnostic.ERROR, AstBuilderListener.PLUGIN_ID, 0, nothing
.getMessage(), new Object[] {object });
((BasicDiagnostic)diagnostic).add(diag);
result = nothing;
}
return result;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseCall(org.eclipse.acceleo.query.ast.Call)
*/
@Override
public Object caseCall(Call object) {
final Object result;
List<Expression> exprArgs = object.getArguments();
final int argc = exprArgs.size();
final Object[] args = new Object[argc];
int i = 0;
for (Expression arg : exprArgs) {
args[i++] = doSwitch(arg);
}
result = services.call(object, args, diagnostic);
return result;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseAnd(org.eclipse.acceleo.query.ast.And)
*/
@Override
public Object caseAnd(And object) {
final Object result;
final Object[] args = new Object[2];
args[0] = doSwitch(object.getArguments().get(0));
if (args[0] != null && args[0].getClass() == Boolean.class && Boolean.FALSE.equals(args[0])) {
result = Boolean.FALSE;
} else {
args[1] = doSwitch(object.getArguments().get(1));
result = services.call(object, args, diagnostic);
}
return result;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseOr(org.eclipse.acceleo.query.ast.Or)
*/
@Override
public Object caseOr(Or object) {
final Object result;
final Object[] args = new Object[2];
args[0] = doSwitch(object.getArguments().get(0));
if (args[0] != null && args[0].getClass() == Boolean.class && Boolean.TRUE.equals(args[0])) {
result = Boolean.TRUE;
} else {
args[1] = doSwitch(object.getArguments().get(1));
result = services.call(object, args, diagnostic);
}
return result;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseImplies(org.eclipse.acceleo.query.ast.Implies)
*/
@Override
public Object caseImplies(Implies object) {
final Object result;
final Object[] args = new Object[2];
args[0] = doSwitch(object.getArguments().get(0));
if (args[0] != null && args[0].getClass() == Boolean.class && Boolean.FALSE.equals(args[0])) {
result = Boolean.TRUE;
} else {
args[1] = doSwitch(object.getArguments().get(1));
result = services.call(object, args, diagnostic);
}
return result;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseVarRef(org.eclipse.acceleo.query.ast.VarRef)
*/
@Override
public Object caseVarRef(VarRef object) {
return services.getVariableValue(peekVariables(), object.getVariableName(), diagnostic);
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseLambda(org.eclipse.acceleo.query.ast.Lambda)
*/
@Override
public Object caseLambda(Lambda object) {
return new LambdaValue(object, new HashMap<String, Object>(peekVariables()), this, diagnostic);
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseNullLiteral(org.eclipse.acceleo.query.ast.NullLiteral)
*/
@Override
public Object caseNullLiteral(NullLiteral object) {
return null;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseEnumLiteral(org.eclipse.acceleo.query.ast.EnumLiteral)
*/
@Override
public Object caseEnumLiteral(EnumLiteral object) {
final Object result;
final EEnumLiteral literal = services.getEEnumLiteral(object);
if (literal != null) {
result = literal.getInstance();
} else {
final Nothing nothing = new Nothing("Invalid enum literal.");
Diagnostic diag = new BasicDiagnostic(Diagnostic.ERROR, AstBuilderListener.PLUGIN_ID, 0, nothing
.getMessage(), new Object[] {object });
((BasicDiagnostic)diagnostic).add(diag);
result = nothing;
}
return result;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseSetInExtensionLiteral(org.eclipse.acceleo.query.ast.SetInExtensionLiteral)
*/
@Override
public Object caseSetInExtensionLiteral(SetInExtensionLiteral object) {
final Set<Object> result = new LinkedHashSet<Object>();
for (Expression expression : object.getValues()) {
result.add(doSwitch(expression));
}
return result;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseSequenceInExtensionLiteral(org.eclipse.acceleo.query.ast.SequenceInExtensionLiteral)
*/
@Override
public Object caseSequenceInExtensionLiteral(SequenceInExtensionLiteral object) {
final List<Object> result = new ArrayList<Object>();
for (Expression expression : object.getValues()) {
result.add(doSwitch(expression));
}
return result;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseConditional(org.eclipse.acceleo.query.ast.Conditional)
*/
@Override
public Object caseConditional(Conditional object) {
Object selector = this.doSwitch(object.getPredicate());
Object result;
if (selector instanceof Boolean) {
if ((Boolean)selector) {
result = this.doSwitch(object.getTrueBranch());
} else {
result = this.doSwitch(object.getFalseBranch());
}
} else {
Nothing nothing = new Nothing(String.format(BAD_PREDICATE_TYPE_MSG, selector));
Diagnostic diag = new BasicDiagnostic(Diagnostic.WARNING, AstBuilderListener.PLUGIN_ID, 0, nothing
.getMessage(), new Object[] {object.getPredicate() });
((BasicDiagnostic)diagnostic).add(diag);
result = nothing;
}
return result;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseLet(org.eclipse.acceleo.query.ast.Let)
*/
@Override
public Object caseLet(Let object) {
Map<String, Object> letEnv = new HashMap<String, Object>(peekVariables());
for (Binding binding : object.getBindings()) {
letEnv.put(binding.getName(), doSwitch(binding.getValue()));
}
pushVariables(letEnv);
Object result = doSwitch(object.getBody());
popVariables();
return result;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.ast.util.AstSwitch#caseTypeSetLiteral(org.eclipse.acceleo.query.ast.TypeSetLiteral)
*/
@Override
public Object caseTypeSetLiteral(TypeSetLiteral object) {
final Set<Object> result = new LinkedHashSet<Object>(object.getTypes().size());
for (TypeLiteral type : object.getTypes()) {
result.add(doSwitch(type));
}
return result;
}
}