Search in sources :

Example 51 with ParserRule

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();
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Parameter(org.eclipse.xtext.Parameter)

Example 52 with ParserRule

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);
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Parameter(org.eclipse.xtext.Parameter)

Example 53 with 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);
        }
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 54 with ParserRule

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);
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) EnumRule(org.eclipse.xtext.EnumRule) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) TypeRef(org.eclipse.xtext.TypeRef) CrossReference(org.eclipse.xtext.CrossReference) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 55 with ParserRule

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);
                    }
                }
            }
        }
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Parameter(org.eclipse.xtext.Parameter) NamedArgument(org.eclipse.xtext.NamedArgument) AbstractRule(org.eclipse.xtext.AbstractRule) Check(org.eclipse.xtext.validation.Check)

Aggregations

ParserRule (org.eclipse.xtext.ParserRule)380 Test (org.junit.Test)172 Grammar (org.eclipse.xtext.Grammar)151 Action (org.eclipse.xtext.Action)144 Parameter (org.eclipse.xtext.Parameter)137 EPackage (org.eclipse.emf.ecore.EPackage)126 AbstractRule (org.eclipse.xtext.AbstractRule)94 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)74 AbstractXtextRuleInspectorTest (org.eclipse.xtext.xtext.AbstractXtextRuleInspectorTest)49 RuleCall (org.eclipse.xtext.RuleCall)41 EObject (org.eclipse.emf.ecore.EObject)27 EnumRule (org.eclipse.xtext.EnumRule)21 TerminalRule (org.eclipse.xtext.TerminalRule)21 AbstractElement (org.eclipse.xtext.AbstractElement)18 Assignment (org.eclipse.xtext.Assignment)15 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)12 EClass (org.eclipse.emf.ecore.EClass)11 Group (org.eclipse.xtext.Group)11 NamedArgument (org.eclipse.xtext.NamedArgument)11 TypeRef (org.eclipse.xtext.TypeRef)11