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
|
/*
* SPDX-FileCopyrightText: 2021-2024 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
/*
* generated by Xtext 2.25.0
*/
package tools.refinery.language.scoping;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.Scopes;
import tools.refinery.language.model.problem.*;
import java.util.Collection;
import java.util.LinkedHashSet;
/**
* This class contains custom scoping description.
* <p>
* See
* <a href="https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#scoping">...</a>
* on how and when to use it.
*/
public class ProblemScopeProvider extends AbstractProblemScopeProvider {
@Override
public IScope getScope(EObject context, EReference reference) {
var scope = super.getScope(context, reference);
if (reference == ProblemPackage.Literals.NODE_ASSERTION_ARGUMENT__NODE) {
return getNodesScope(context, scope);
}
if (reference == ProblemPackage.Literals.VARIABLE_OR_NODE_EXPR__VARIABLE_OR_NODE
|| reference == ProblemPackage.Literals.NEW_ACTION__PARENT
|| reference == ProblemPackage.Literals.DELETE_ACTION__VARIABLE_OR_NODE) {
return getVariableScope(context, scope);
}
if (reference == ProblemPackage.Literals.REFERENCE_DECLARATION__OPPOSITE) {
return getOppositeScope(context);
}
return scope;
}
protected IScope getNodesScope(EObject context, IScope delegateScope) {
var problem = EcoreUtil2.getContainerOfType(context, Problem.class);
if (problem == null) {
return delegateScope;
}
return Scopes.scopeFor(problem.getNodes(), delegateScope);
}
protected IScope getVariableScope(EObject context, IScope delegateScope) {
Collection<Variable> variables = new LinkedHashSet<>();
addSingletonVariableToScope(context, variables);
EObject currentContext = context;
while (currentContext != null && !(currentContext instanceof ParametricDefinition)) {
addExistentiallyQualifiedVariableToScope(currentContext, variables);
currentContext = currentContext.eContainer();
}
IScope parentScope = getNodesScope(context, delegateScope);
if (currentContext != null) {
ParametricDefinition definition = (ParametricDefinition) currentContext;
parentScope = Scopes.scopeFor(definition.getParameters(), parentScope);
}
return Scopes.scopeFor(variables, parentScope);
}
protected void addSingletonVariableToScope(EObject context, Collection<Variable> variables) {
if (context instanceof VariableOrNodeExpr expr) {
Variable singletonVariable = expr.getSingletonVariable();
if (singletonVariable != null) {
variables.add(singletonVariable);
}
}
}
protected void addExistentiallyQualifiedVariableToScope(EObject currentContext, Collection<Variable> variables) {
switch (currentContext) {
case ExistentialQuantifier quantifier -> variables.addAll(quantifier.getImplicitVariables());
case Match match -> variables.addAll(match.getCondition().getImplicitVariables());
case Consequent consequent -> {
for (var literal : consequent.getActions()) {
if (literal instanceof NewAction newAction && newAction.getVariable() != null) {
variables.add(newAction.getVariable());
}
}
}
default -> {
// Nothing to add.
}
}
}
protected IScope getOppositeScope(EObject context) {
var referenceDeclaration = EcoreUtil2.getContainerOfType(context, ReferenceDeclaration.class);
if (referenceDeclaration == null) {
return IScope.NULLSCOPE;
}
var relation = referenceDeclaration.getReferenceType();
if (!(relation instanceof ClassDeclaration classDeclaration)) {
return IScope.NULLSCOPE;
}
var referenceDeclarations = classDeclaration.getFeatureDeclarations();
return Scopes.scopeFor(referenceDeclarations);
}
}
|