Search in sources :

Example 1 with EnumRule

use of org.eclipse.xtext.EnumRule in project xtext-core by eclipse.

the class OverriddenValueInspector method caseRuleCall.

@Override
public Boolean caseRuleCall(RuleCall object) {
    AbstractRule calledRule = object.getRule();
    if (calledRule == null || calledRule.eIsProxy() || calledRule instanceof TerminalRule || calledRule instanceof EnumRule)
        return Boolean.FALSE;
    ParserRule parserRule = (ParserRule) calledRule;
    if (GrammarUtil.isDatatypeRule(parserRule))
        return Boolean.FALSE;
    if (parserRule.isFragment()) {
        visitFragment(object);
        if (GrammarUtil.isMultipleCardinality(object))
            visitFragment(object);
    }
    if (!addVisited(parserRule))
        return Boolean.FALSE;
    Multimap<String, AbstractElement> prevAssignedFeatures = assignedFeatures;
    assignedFeatures = newMultimap();
    doSwitch(parserRule.getAlternatives());
    for (String feature : assignedFeatures.keySet()) prevAssignedFeatures.put(feature, object);
    assignedFeatures = prevAssignedFeatures;
    removeVisited(parserRule);
    return Boolean.FALSE;
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) ParserRule(org.eclipse.xtext.ParserRule) AbstractElement(org.eclipse.xtext.AbstractElement) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 2 with EnumRule

use of org.eclipse.xtext.EnumRule in project xtext-core by eclipse.

the class XtextLinker method createDiagnosticProducer.

@Override
protected IDiagnosticProducer createDiagnosticProducer(final IDiagnosticConsumer consumer) {
    return new AbstractDiagnosticProducerDecorator(super.createDiagnosticProducer(consumer)) {

        private boolean filter = false;

        @Override
        public void addDiagnostic(DiagnosticMessage message) {
            if (!filter)
                super.addDiagnostic(message);
        }

        @Override
        public void setTarget(EObject object, EStructuralFeature feature) {
            super.setTarget(object, feature);
            // we don't want to mark generated types as errors unless generation fails
            filter = isTypeRef(object, feature) || isGeneratedPackage(object, feature) || isEnumLiteral(object, feature);
        }

        private boolean isEnumLiteral(EObject object, EStructuralFeature feature) {
            if (feature == XtextPackage.eINSTANCE.getEnumLiteralDeclaration_EnumLiteral()) {
                EnumRule rule = GrammarUtil.containingEnumRule(object);
                return rule.getType() == null || rule.getType().getClassifier() == null;
            }
            return false;
        }

        private boolean isGeneratedPackage(EObject object, EStructuralFeature feature) {
            return (feature == XtextPackage.eINSTANCE.getAbstractMetamodelDeclaration_EPackage() && (object instanceof GeneratedMetamodel));
        }

        private boolean isTypeRef(EObject object, EStructuralFeature feature) {
            return (feature == XtextPackage.eINSTANCE.getTypeRef_Classifier() && ((TypeRef) object).getMetamodel() instanceof GeneratedMetamodel);
        }
    };
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) DiagnosticMessage(org.eclipse.xtext.diagnostics.DiagnosticMessage) AbstractDiagnosticProducerDecorator(org.eclipse.xtext.diagnostics.AbstractDiagnosticProducerDecorator) EObject(org.eclipse.emf.ecore.EObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature)

Example 3 with EnumRule

use of org.eclipse.xtext.EnumRule 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 4 with EnumRule

use of org.eclipse.xtext.EnumRule in project xtext-core by eclipse.

the class XtextValidator method checkEnumLiteralIsUnique.

@Check
public void checkEnumLiteralIsUnique(EnumLiteralDeclaration decl) {
    EnumRule rule = GrammarUtil.containingEnumRule(decl);
    List<EnumLiteralDeclaration> declarations = EcoreUtil2.getAllContentsOfType(rule, EnumLiteralDeclaration.class);
    String literal = decl.getLiteral().getValue();
    if (literal != null) {
        for (EnumLiteralDeclaration otherDecl : declarations) {
            if (otherDecl != decl && literal.equals(otherDecl.getLiteral().getValue())) {
                error("Enum literal '" + literal + "' is used multiple times in enum rule '" + rule.getName() + "'.", XtextPackage.Literals.ENUM_LITERAL_DECLARATION__LITERAL);
            }
        }
    }
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) Check(org.eclipse.xtext.validation.Check)

Example 5 with EnumRule

use of org.eclipse.xtext.EnumRule in project xtext-core by eclipse.

the class XtextValidator method checkGeneratedEnumIsValid.

@Check
public void checkGeneratedEnumIsValid(EnumLiteralDeclaration decl) {
    EnumRule rule = GrammarUtil.containingEnumRule(decl);
    if (!(rule.getType().getMetamodel() instanceof GeneratedMetamodel))
        return;
    if (!(rule.getType().getClassifier() instanceof EEnum))
        return;
    EEnum eEnum = (EEnum) rule.getType().getClassifier();
    List<EnumLiteralDeclaration> declarations = EcoreUtil2.getAllContentsOfType(rule, EnumLiteralDeclaration.class);
    if (declarations.size() == eEnum.getELiterals().size())
        return;
    for (EnumLiteralDeclaration otherDecl : declarations) {
        if (decl == otherDecl) {
            return;
        }
        if (otherDecl.getEnumLiteral() == decl.getEnumLiteral()) {
            if (!decl.getEnumLiteral().getLiteral().equals(decl.getLiteral().getValue()))
                addIssue("Enum literal '" + decl.getEnumLiteral().getName() + "' has already been defined with literal '" + decl.getEnumLiteral().getLiteral() + "'.", decl, XtextPackage.Literals.ENUM_LITERAL_DECLARATION__ENUM_LITERAL, DUPLICATE_ENUM_LITERAL);
            return;
        }
    }
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) EEnum(org.eclipse.emf.ecore.EEnum) Check(org.eclipse.xtext.validation.Check)

Aggregations

EnumRule (org.eclipse.xtext.EnumRule)29 ParserRule (org.eclipse.xtext.ParserRule)21 TerminalRule (org.eclipse.xtext.TerminalRule)17 AbstractRule (org.eclipse.xtext.AbstractRule)14 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)9 RuleCall (org.eclipse.xtext.RuleCall)9 EnumLiteralDeclaration (org.eclipse.xtext.EnumLiteralDeclaration)8 EEnum (org.eclipse.emf.ecore.EEnum)5 EPackage (org.eclipse.emf.ecore.EPackage)5 Grammar (org.eclipse.xtext.Grammar)5 List (java.util.List)4 EObject (org.eclipse.emf.ecore.EObject)4 Action (org.eclipse.xtext.Action)4 Assignment (org.eclipse.xtext.Assignment)4 Keyword (org.eclipse.xtext.Keyword)4 TypeRef (org.eclipse.xtext.TypeRef)4 EEnumLiteral (org.eclipse.emf.ecore.EEnumLiteral)3 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)3 AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)3 CrossReference (org.eclipse.xtext.CrossReference)3