Search in sources :

Example 6 with AbstractRule

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();
}
Also used : Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 7 with AbstractRule

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

Example 8 with AbstractRule

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;
}
Also used : QualifiedName(org.eclipse.xtext.naming.QualifiedName) Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 9 with AbstractRule

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);
}
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 10 with AbstractRule

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);
                    }
                }
            }
        }
    }
}
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

AbstractRule (org.eclipse.xtext.AbstractRule)222 Test (org.junit.Test)128 Grammar (org.eclipse.xtext.Grammar)106 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)96 ParserRule (org.eclipse.xtext.ParserRule)94 EObject (org.eclipse.emf.ecore.EObject)59 AbstractXtextRuleInspectorTest (org.eclipse.xtext.xtext.AbstractXtextRuleInspectorTest)49 RuleCall (org.eclipse.xtext.RuleCall)36 AbstractElement (org.eclipse.xtext.AbstractElement)34 TerminalRule (org.eclipse.xtext.TerminalRule)20 XtextResource (org.eclipse.xtext.resource.XtextResource)17 EClassifier (org.eclipse.emf.ecore.EClassifier)14 EnumRule (org.eclipse.xtext.EnumRule)14 ArrayList (java.util.ArrayList)12 Assignment (org.eclipse.xtext.Assignment)12 Group (org.eclipse.xtext.Group)12 InternalEObject (org.eclipse.emf.ecore.InternalEObject)11 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)11 EClass (org.eclipse.emf.ecore.EClass)10 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)10