Search in sources :

Example 21 with AbstractMetamodelDeclaration

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

the class XtextLinker method setDefaultValueImpl.

@Override
protected void setDefaultValueImpl(EObject obj, EReference ref, IDiagnosticProducer producer) {
    if (XtextPackage.eINSTANCE.getTypeRef_Metamodel() == ref) {
        final TypeRef typeRef = (TypeRef) obj;
        final String typeRefName = GrammarUtil.getTypeRefName(typeRef);
        final List<EObject> metamodels = XtextMetamodelReferenceHelper.findBestMetamodelForType(typeRef, "", typeRefName, scopeProvider.getScope(typeRef, ref));
        if (metamodels.isEmpty() || metamodels.size() > 1)
            producer.addDiagnostic(new DiagnosticMessage("Cannot find meta model for type '" + typeRefName + "'", Severity.ERROR, null));
        else
            typeRef.setMetamodel((AbstractMetamodelDeclaration) metamodels.get(0));
    } else if (XtextPackage.eINSTANCE.getCrossReference_Terminal() == ref) {
        AbstractRule rule = GrammarUtil.findRuleForName(GrammarUtil.getGrammar(obj), "ID");
        if (rule == null)
            producer.addDiagnostic(new DiagnosticMessage("Cannot resolve implicit reference to rule 'ID'", Severity.ERROR, null));
        else {
            RuleCall call = XtextFactory.eINSTANCE.createRuleCall();
            call.setRule(rule);
            ((CrossReference) obj).setTerminal(call);
        }
    } else if (XtextPackage.eINSTANCE.getNamedArgument_Parameter() == ref) {
        final NamedArgument argument = (NamedArgument) obj;
        if (!argument.isCalledByName()) {
            RuleCall ruleCall = EcoreUtil2.getContainerOfType(argument, RuleCall.class);
            AbstractRule calledRule = ruleCall.getRule();
            if (!(calledRule instanceof ParserRule)) {
                producer.addDiagnostic(new DiagnosticMessage("Arguments can only be used with parser rules.", Severity.ERROR, null));
                return;
            }
            if (!calledRule.eIsProxy()) {
                ParserRule casted = (ParserRule) calledRule;
                int idx = ruleCall.getArguments().indexOf(argument);
                if (idx < casted.getParameters().size()) {
                    argument.setParameter(casted.getParameters().get(idx));
                    return;
                } else if (casted.getParameters().size() == 0) {
                    producer.addDiagnostic(new DiagnosticMessage("Rule " + calledRule.getName() + " has no arguments.", Severity.ERROR, null));
                } else {
                    String message = "Invalid number of arguments for rule " + calledRule.getName() + ", expecting " + casted.getParameters().size() + " but was " + (idx + 1);
                    producer.addDiagnostic(new DiagnosticMessage(message, Severity.ERROR, null));
                }
            }
        }
    } else {
        super.setDefaultValueImpl(obj, ref, producer);
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) DiagnosticMessage(org.eclipse.xtext.diagnostics.DiagnosticMessage) TypeRef(org.eclipse.xtext.TypeRef) EObject(org.eclipse.emf.ecore.EObject) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) NamedArgument(org.eclipse.xtext.NamedArgument) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 22 with AbstractMetamodelDeclaration

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

the class XtextScopeProvider method getScope.

@Override
public IScope getScope(final EObject context, EReference reference) {
    if (reference == XtextPackage.eINSTANCE.getTypeRef_Classifier()) {
        if (context instanceof TypeRef) {
            final TypeRef typeRef = (TypeRef) context;
            final AbstractMetamodelDeclaration metaModel = typeRef.getMetamodel();
            if (metaModel != null) {
                EPackage pack = metaModel.getEPackage();
                if (pack != null)
                    return createClassifierScope(pack.getEClassifiers());
            } else {
                return createReferencedPackagesScope(GrammarUtil.getGrammar(context));
            }
        } else {
            return createReferencedPackagesScope(GrammarUtil.getGrammar(context));
        }
        return IScope.NULLSCOPE;
    }
    if (reference == XtextPackage.eINSTANCE.getEnumLiteralDeclaration_EnumLiteral()) {
        final EnumRule rule = GrammarUtil.containingEnumRule(context);
        if (rule.getType() != null && rule.getType().getClassifier() != null && rule.getType().getClassifier() instanceof EEnum) {
            return createEnumLiteralsScope((EEnum) rule.getType().getClassifier());
        }
        return IScope.NULLSCOPE;
    }
    if (reference == XtextPackage.eINSTANCE.getGrammar_UsedGrammars()) {
        return globalScopeProvider.getScope(context.eResource(), reference, new Predicate<IEObjectDescription>() {

            @Override
            public boolean apply(IEObjectDescription input) {
                return !input.getEObjectURI().equals(EcoreUtil.getURI(context));
            }
        });
    }
    if (reference == XtextPackage.eINSTANCE.getRuleCall_Rule()) {
        return createScope(context.eResource(), reference.getEReferenceType(), new SuperCallScope(context));
    }
    if (reference == XtextPackage.eINSTANCE.getParameterReference_Parameter()) {
        ParserRule rule = GrammarUtil.containingParserRule(context);
        if (rule == null) {
            return IScope.NULLSCOPE;
        }
        return Scopes.scopeFor(rule.getParameters());
    }
    if (reference == XtextPackage.eINSTANCE.getNamedArgument_Parameter()) {
        RuleCall ruleCall = EcoreUtil2.getContainerOfType(context, RuleCall.class);
        if (ruleCall == null) {
            return IScope.NULLSCOPE;
        }
        AbstractRule referencedRule = ruleCall.getRule();
        if (referencedRule instanceof ParserRule) {
            return Scopes.scopeFor(((ParserRule) referencedRule).getParameters());
        }
        return IScope.NULLSCOPE;
    }
    return createScope(context.eResource(), reference.getEReferenceType(), IScope.NULLSCOPE);
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) ParserRule(org.eclipse.xtext.ParserRule) TypeRef(org.eclipse.xtext.TypeRef) EEnum(org.eclipse.emf.ecore.EEnum) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall) EPackage(org.eclipse.emf.ecore.EPackage) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 23 with AbstractMetamodelDeclaration

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

the class XtextTransientValueService method isTransient.

@Override
public boolean isTransient(EObject owner, EStructuralFeature feature, int index) {
    if (feature == XtextPackage.eINSTANCE.getTypeRef_Metamodel()) {
        final TypeRef typeRef = (TypeRef) owner;
        final AbstractMetamodelDeclaration m = typeRef.getMetamodel();
        if (m == null || Strings.isEmpty(m.getAlias()))
            return true;
    } else if (feature == XtextPackage.eINSTANCE.getAbstractRule_Type()) {
        final AbstractRule rule = (AbstractRule) owner;
        if (rule instanceof ParserRule || rule instanceof EnumRule) {
            final TypeRef returnType = rule.getType();
            if (returnType == null || returnType.getClassifier() == null)
                return true;
            else if (rule.getName().equals(returnType.getClassifier().getName())) {
                return isTransient(returnType, XtextPackage.eINSTANCE.getTypeRef_Metamodel(), -1);
            } else if (GrammarUtil.isDatatypeRule(rule)) {
                return NodeModelUtils.getNode(returnType) == null;
            }
        } else if (rule instanceof TerminalRule) {
            final TypeRef returnType = rule.getType();
            return ((TerminalRule) rule).isFragment() || returnType == null || GrammarUtil.findEString(GrammarUtil.getGrammar(owner)).equals(rule.getType().getClassifier());
        }
    } else if (feature == XtextPackage.eINSTANCE.getCrossReference_Terminal()) {
        final CrossReference ref = (CrossReference) owner;
        if (ref.getTerminal() instanceof RuleCall && ((RuleCall) ref.getTerminal()).getRule() != null && "ID".equals(((RuleCall) ref.getTerminal()).getRule().getName()))
            return true;
    } else if (feature == XtextPackage.eINSTANCE.getEnumLiteralDeclaration_Literal()) {
        final EnumLiteralDeclaration decl = (EnumLiteralDeclaration) owner;
        return decl.getEnumLiteral() != null && decl.getLiteral() != null && Strings.equal(decl.getLiteral().getValue(), decl.getEnumLiteral().getName());
    } else if (feature == XtextPackage.eINSTANCE.getRuleCall_ExplicitlyCalled()) {
        return true;
    } else if (feature == XtextPackage.eINSTANCE.getNamedArgument_Parameter()) {
        return !((NamedArgument) owner).isCalledByName();
    }
    return super.isTransient(owner, feature, index);
}
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) NamedArgument(org.eclipse.xtext.NamedArgument) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 24 with AbstractMetamodelDeclaration

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

the class XtextLinkerTest method checkPackageRemovalAfterGrammarChange.

private void checkPackageRemovalAfterGrammarChange(final boolean isRemoved, final String originalGrammar, final int offset, final int length, final String replacement) throws Exception {
    final XtextResource resource = this.getResourceFromStringAndExpect(originalGrammar, 1);
    EObject _get = resource.getContents().get(0);
    Grammar grammar = ((Grammar) _get);
    AbstractMetamodelDeclaration generatedMetamodel = grammar.getMetamodelDeclarations().get(0);
    EPackage ePackage = generatedMetamodel.getEPackage();
    Assert.assertEquals(ePackage.eResource().getResourceSet(), resource.getResourceSet());
    resource.update(offset, length, replacement);
    if (isRemoved) {
        Assert.assertNull(ePackage.eResource().getResourceSet());
    } else {
        Assert.assertEquals(ePackage.eResource().getResourceSet(), resource.getResourceSet());
    }
    EObject _get_1 = resource.getContents().get(0);
    grammar = ((Grammar) _get_1);
    generatedMetamodel = grammar.getMetamodelDeclarations().get(0);
    ePackage = generatedMetamodel.getEPackage();
    Assert.assertEquals(resource.getResourceSet(), ePackage.eResource().getResourceSet());
}
Also used : EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) EPackage(org.eclipse.emf.ecore.EPackage)

Example 25 with AbstractMetamodelDeclaration

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

the class XtextLinkerTest method checkRegisteredPackageNotUnloadedAfterGrammarChange.

private void checkRegisteredPackageNotUnloadedAfterGrammarChange(final String originalGrammar, final int offset, final int length, final String replacement) throws Exception {
    final XtextResource resource = this.getResourceFromString(originalGrammar);
    EObject _get = resource.getContents().get(0);
    final Grammar grammar = ((Grammar) _get);
    final AbstractMetamodelDeclaration generatedMetamodel = grammar.getMetamodelDeclarations().get(0);
    final EPackage ePackage = generatedMetamodel.getEPackage();
    Assert.assertNull(((InternalEObject) ePackage).eProxyURI());
    resource.update(offset, length, replacement);
    Assert.assertNull(((InternalEObject) ePackage).eProxyURI());
}
Also used : EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) EPackage(org.eclipse.emf.ecore.EPackage)

Aggregations

AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)33 Grammar (org.eclipse.xtext.Grammar)21 Test (org.junit.Test)16 EObject (org.eclipse.emf.ecore.EObject)11 EPackage (org.eclipse.emf.ecore.EPackage)9 AbstractRule (org.eclipse.xtext.AbstractRule)9 GeneratedMetamodel (org.eclipse.xtext.GeneratedMetamodel)8 XtextResource (org.eclipse.xtext.resource.XtextResource)8 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)7 TypeRef (org.eclipse.xtext.TypeRef)7 ReferencedMetamodel (org.eclipse.xtext.ReferencedMetamodel)6 EClass (org.eclipse.emf.ecore.EClass)4 EClassifier (org.eclipse.emf.ecore.EClassifier)4 Resource (org.eclipse.emf.ecore.resource.Resource)4 ParserRule (org.eclipse.xtext.ParserRule)4 RuleCall (org.eclipse.xtext.RuleCall)4 CompoundElement (org.eclipse.xtext.CompoundElement)3 EnumRule (org.eclipse.xtext.EnumRule)3 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)2 InternalEObject (org.eclipse.emf.ecore.InternalEObject)2