use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.
the class SuperCallScope method getElements.
@Override
public Iterable<IEObjectDescription> getElements(EObject object) {
if (object instanceof AbstractRule) {
Grammar grammar = GrammarUtil.getGrammar(context);
AbstractRule rule = (AbstractRule) object;
if (GrammarUtil.getGrammar(rule) == grammar) {
return Lists.newArrayList(EObjectDescription.create(GrammarUtil.getSimpleName(grammar) + "." + rule.getName(), rule), EObjectDescription.create(grammar.getName() + "." + rule.getName(), rule));
}
List<IEObjectDescription> result = Lists.newArrayList(EObjectDescription.create(SUPER + "." + rule.getName(), rule), EObjectDescription.create(GrammarUtil.getSimpleName(grammar) + "." + rule.getName(), rule), EObjectDescription.create(grammar.getName() + "." + rule.getName(), rule));
AbstractRule contextRule = GrammarUtil.containingRule(context);
if (contextRule != null && contextRule.getName().equals(rule.getName())) {
result.add(0, EObjectDescription.create(SUPER, rule));
}
return result;
}
return Collections.emptyList();
}
use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.
the class UsedRulesFinder method compute.
public void compute(Grammar grammar) {
if (!grammar.getRules().isEmpty()) {
AbstractRule firstRule = grammar.getRules().get(0);
if (firstRule instanceof ParserRule) {
doSwitch(firstRule);
doSwitch(grammar);
}
}
}
use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.
the class XtextResourceDescriptionStrategy method createEObjectDescriptions.
@Override
public boolean createEObjectDescriptions(EObject eObject, IAcceptor<IEObjectDescription> acceptor) {
if (eObject instanceof Grammar) {
String grammarName = ((Grammar) eObject).getName();
if (!Strings.isEmpty(grammarName)) {
QualifiedName qualifiedName = defaultQualifiedNameConverter.toQualifiedName(grammarName);
acceptor.accept(EObjectDescription.create(qualifiedName, eObject));
}
} else if (eObject instanceof AbstractMetamodelDeclaration || eObject instanceof AbstractRule)
return super.createEObjectDescriptions(eObject, acceptor);
return eObject instanceof Grammar;
}
use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.
the class XtextTransientValueService2 method isValueTransient.
@Override
public ValueTransient isValueTransient(EObject owner, EStructuralFeature feature) {
if (feature == XtextPackage.eINSTANCE.getTypeRef_Metamodel()) {
final TypeRef typeRef = (TypeRef) owner;
final AbstractMetamodelDeclaration m = typeRef.getMetamodel();
if (m == null || Strings.isEmpty(m.getAlias()))
return YES;
} else if (feature == XtextPackage.eINSTANCE.getAbstractRule_Type()) {
final AbstractRule rule = (AbstractRule) owner;
if (!GrammarUtil.isDatatypeRule(rule) && (rule instanceof ParserRule || rule instanceof EnumRule)) {
final TypeRef returnType = rule.getType();
if (returnType == null || returnType.getClassifier() == null)
return YES;
else if (rule.getName().equals(returnType.getClassifier().getName())) {
if (isValueTransient(returnType, XtextPackage.eINSTANCE.getTypeRef_Metamodel()) == YES)
return PREFERABLY;
else
return NO;
} else if (GrammarUtil.isDatatypeRule(rule)) {
return NodeModelUtils.getNode(returnType) == null ? YES : NO;
}
} else if (rule instanceof TerminalRule || GrammarUtil.isDatatypeRule(rule)) {
final TypeRef returnType = rule.getType();
if (returnType == null)
return YES;
if (rule instanceof TerminalRule) {
if (((TerminalRule) rule).isFragment()) {
return YES;
}
}
if (GrammarUtil.findEString(GrammarUtil.getGrammar(owner)).equals(rule.getType().getClassifier()))
return PREFERABLY;
return NO;
}
} else if (feature == XtextPackage.eINSTANCE.getCrossReference_Terminal()) {
final CrossReference ref = (CrossReference) owner;
if (ref.getTerminal() instanceof RuleCall && ((RuleCall) ref.getTerminal()).getRule() != null) {
if ("ID".equals(((RuleCall) ref.getTerminal()).getRule().getName()))
return PREFERABLY;
else
return NO;
}
return YES;
} else if (feature == XtextPackage.eINSTANCE.getEnumLiteralDeclaration_Literal()) {
final EnumLiteralDeclaration decl = (EnumLiteralDeclaration) owner;
if (decl.getEnumLiteral() != null && decl.getLiteral() != null) {
if (Strings.equal(decl.getLiteral().getValue(), decl.getEnumLiteral().getName()))
return PREFERABLY;
return NO;
}
return YES;
} else if (feature == XtextPackage.eINSTANCE.getRuleCall_ExplicitlyCalled()) {
return YES;
} else if (feature == XtextPackage.eINSTANCE.getNamedArgument_Parameter()) {
if (((NamedArgument) owner).isCalledByName()) {
return NO;
}
return YES;
}
return super.isValueTransient(owner, feature);
}
use of org.eclipse.xtext.AbstractRule in project xtext-core by eclipse.
the class XtextValidator method checkOrderOfArguments.
@Check
public void checkOrderOfArguments(RuleCall call) {
AbstractRule rule = call.getRule();
if (rule instanceof ParserRule) {
Set<Parameter> usedParameters = Sets.newHashSet();
boolean hasError = false;
boolean hasPositionalArgument = false;
boolean hasNamedArgument = false;
for (NamedArgument argument : call.getArguments()) {
Parameter parameter = argument.getParameter();
if (parameter == null || parameter.eIsProxy()) {
hasError = true;
} else if (!usedParameters.add(parameter)) {
hasError = true;
error("Duplicate value for parameter " + parameter.getName(), argument, XtextPackage.Literals.NAMED_ARGUMENT__PARAMETER);
}
if (!argument.isCalledByName()) {
hasPositionalArgument = true;
} else {
hasNamedArgument = true;
}
}
if (hasError) {
return;
}
List<Parameter> parameters = ((ParserRule) rule).getParameters();
if (!hasPositionalArgument) {
if (usedParameters.size() != parameters.size()) {
StringBuilder missing = new StringBuilder();
int count = 0;
for (Parameter parameter : parameters) {
if (!usedParameters.contains(parameter)) {
if (count > 0) {
missing.append(", ");
}
missing.append(parameter.getName());
count++;
}
}
if (count == 1) {
error("Missing argument for parameter " + missing, call, XtextPackage.Literals.RULE_CALL__RULE);
} else {
error(count + " missing arguments for the following parameters: " + missing, call, XtextPackage.Literals.RULE_CALL__RULE);
}
}
} else {
if (usedParameters.size() != parameters.size()) {
error(String.format("Expected %d arguments but got %d", parameters.size(), usedParameters.size()), call, XtextPackage.Literals.RULE_CALL__RULE);
} else if (hasNamedArgument) {
for (int i = 0, max = usedParameters.size(); i < max; i++) {
NamedArgument argument = call.getArguments().get(i);
Parameter param = parameters.get(i);
if (argument.isCalledByName() && argument.getParameter() != param) {
error("Out of sequence named argument. Expected value for " + param.getName(), argument, XtextPackage.Literals.NAMED_ARGUMENT__PARAMETER);
}
}
}
}
}
}
Aggregations