/*
* generated by Xtext
*/
package it.xsemantics.dsl.scoping;
import it.xsemantics.dsl.util.XsemanticsUtils;
import it.xsemantics.dsl.xsemantics.CheckRule;
import it.xsemantics.dsl.xsemantics.JudgmentDescription;
import it.xsemantics.dsl.xsemantics.Rule;
import it.xsemantics.dsl.xsemantics.RuleInvocation;
import java.util.Collections;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.Scopes;
import org.eclipse.xtext.xbase.XBlockExpression;
import org.eclipse.xtext.xbase.XExpression;
import org.eclipse.xtext.xbase.XVariableDeclaration;
import org.eclipse.xtext.xbase.scoping.LocalVariableScopeContext;
import org.eclipse.xtext.xbase.scoping.XbaseScopeProvider;
import org.eclipse.xtext.xbase.scoping.featurecalls.IValidatedEObjectDescription;
import org.eclipse.xtext.xbase.scoping.featurecalls.JvmFeatureScope;
import org.eclipse.xtext.xbase.validation.IssueCodes;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
/**
* This class contains custom scoping description.
*
* see : http://www.eclipse.org/Xtext/documentation/latest/xtext.html#scoping on
* how and when to use it
*
*/
@SuppressWarnings("restriction")
public class XsemanticsScopeProvider extends XbaseScopeProvider {
@Inject
protected XsemanticsUtils utils;
@Override
protected IScope createLocalVarScope(IScope parentScope,
LocalVariableScopeContext scopeContext) {
parentScope = super.createLocalVarScope(parentScope, scopeContext);
if (scopeContext != null && scopeContext.getContext() != null) {
EObject context = scopeContext.getContext();
if (context instanceof Rule) {
parentScope = Scopes.scopeFor(
utils.getJvmParameters((Rule) context), parentScope);
} else if (context instanceof CheckRule) {
CheckRule checkRule = (CheckRule) context;
parentScope = Scopes.scopeFor(Collections
.singletonList(checkRule.getElement().getParameter()),
parentScope);
} else if (context instanceof JudgmentDescription) {
parentScope = Scopes.scopeFor(
utils.getJvmParameters((JudgmentDescription) context),
parentScope);
}
}
return parentScope;
}
@Override
protected IScope createLocalVarScopeForBlock(XBlockExpression block,
int indexOfContextExpressionInBlock, boolean referredFromClosure,
IScope parentScope) {
// copied from XbaseScopeProvider with the case for RuleInvocation's
// variable declarations
List<IValidatedEObjectDescription> descriptions = Lists.newArrayList();
for (int i = 0; i < indexOfContextExpressionInBlock; i++) {
XExpression expression = block.getExpressions().get(i);
if (expression instanceof XVariableDeclaration) {
XVariableDeclaration varDecl = (XVariableDeclaration) expression;
addVariableDeclaration(descriptions, varDecl,
referredFromClosure);
} else if (expression instanceof RuleInvocation) {
RuleInvocation ruleInvocation = (RuleInvocation) expression;
List<XVariableDeclaration> variableDeclarations = utils
.getVariableDeclarations(ruleInvocation);
for (XVariableDeclaration varDecl : variableDeclarations) {
addVariableDeclaration(descriptions, varDecl,
referredFromClosure);
}
}
}
if (descriptions.isEmpty())
return parentScope;
return new JvmFeatureScope(parentScope, "XBlockExpression",
descriptions);
}
protected void addVariableDeclaration(
List<IValidatedEObjectDescription> descriptions,
XVariableDeclaration varDecl, boolean referredFromClosure) {
if (varDecl.getName() != null) {
IValidatedEObjectDescription desc = createLocalVarDescription(varDecl);
if (referredFromClosure && varDecl.isWriteable())
desc.setIssueCode(IssueCodes.INVALID_MUTABLE_VARIABLE_ACCESS);
descriptions.add(desc);
}
}
}