use of org.eclipse.xtext.ParserRule in project xtext-core by eclipse.
the class ParameterConfigHelper method getAssignedParameters.
public static Set<Parameter> getAssignedParameters(AbstractElement element, int parameterConfig) {
if (parameterConfig != 0) {
ParserRule parserRule = GrammarUtil.containingParserRule(element);
Set<Parameter> allParameters = ImmutableSet.copyOf(parserRule.getParameters());
int seen = 0;
for (Set<Parameter> candidate : Sets.powerSet(allParameters)) {
if (seen == parameterConfig) {
return candidate;
}
seen++;
}
}
return Collections.emptySet();
}
use of org.eclipse.xtext.ParserRule in project xtext-core by eclipse.
the class ParameterConfigHelper method getParameterConfig.
public static int getParameterConfig(Set<Parameter> params) {
if (params.isEmpty()) {
return 0;
}
Parameter firstParam = params.iterator().next();
ParserRule parserRule = GrammarUtil.containingParserRule(firstParam);
return getParameterConfig(params, parserRule);
}
use of org.eclipse.xtext.ParserRule 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.ParserRule 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.ParserRule 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