Search in sources :

Example 1 with AbstractMetamodelDeclaration

use of org.eclipse.xtext.AbstractMetamodelDeclaration 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 2 with AbstractMetamodelDeclaration

use of org.eclipse.xtext.AbstractMetamodelDeclaration 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 3 with AbstractMetamodelDeclaration

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

the class XtextValidator method checkMetamodelUris.

@Check
public void checkMetamodelUris(final AbstractMetamodelDeclaration declaration) {
    if (!(declaration.getEPackage() != null && declaration.getEPackage().getNsURI() != null))
        return;
    Grammar grammar = GrammarUtil.getGrammar(declaration);
    Iterable<String> nsUris = Iterables.transform(grammar.getMetamodelDeclarations(), new Function<AbstractMetamodelDeclaration, String>() {

        @Override
        public String apply(AbstractMetamodelDeclaration param) {
            if (param.getEPackage() != null)
                return param.getEPackage().getNsURI();
            return null;
        }
    });
    int count = Iterables.size(Iterables.filter(nsUris, new Predicate<String>() {

        @Override
        public boolean apply(String param) {
            return declaration.getEPackage().getNsURI().equals(param);
        }
    }));
    if (count != 1) {
        error("EPackage with ns-uri '" + declaration.getEPackage().getNsURI() + "' is used twice.", XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE);
    }
}
Also used : Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) Predicate(com.google.common.base.Predicate) Check(org.eclipse.xtext.validation.Check)

Example 4 with AbstractMetamodelDeclaration

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

the class ToEcoreTrafoTest method testConcreteLanguageToMetamodel1.

@Test
public void testConcreteLanguageToMetamodel1() throws Exception {
    XtextResource r = getResource("classpath:/" + ConcreteTestLanguage.class.getName().replace('.', '/') + ".xtext");
    EObject element = r.getContents().get(0);
    Grammar g = (Grammar) element;
    List<AbstractMetamodelDeclaration> mms = Lists.<AbstractMetamodelDeclaration>newArrayList(Iterables.filter(g.getMetamodelDeclarations(), GeneratedMetamodel.class));
    assertNotNull(mms);
    assertEquals(1, mms.size());
}
Also used : GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) Test(org.junit.Test)

Example 5 with AbstractMetamodelDeclaration

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

the class NodeModelUtilsTest method testFindNodesForFeature.

@Test
public void testFindNodesForFeature() throws Exception {
    Grammar grammar = (Grammar) getModel("grammar foo.Bar with org.eclipse.xtext.common.Terminals generate foo 'bar' Model : name=ID;");
    List<INode> nodes = NodeModelUtils.findNodesForFeature(grammar, XtextPackage.eINSTANCE.getGrammar_Name());
    assertEquals(1, nodes.size());
    assertEquals("foo.Bar", nodes.get(0).getText().trim());
    nodes = NodeModelUtils.findNodesForFeature(grammar, XtextPackage.eINSTANCE.getGrammar_Rules());
    assertEquals(1, nodes.size());
    assertEquals("Model : name=ID;", nodes.get(0).getText().trim());
    AbstractMetamodelDeclaration declaration = grammar.getMetamodelDeclarations().get(0);
    nodes = NodeModelUtils.findNodesForFeature(declaration, XtextPackage.eINSTANCE.getGeneratedMetamodel_Name());
    assertEquals(1, nodes.size());
    assertEquals("foo", nodes.get(0).getText().trim());
    nodes = NodeModelUtils.findNodesForFeature(declaration, null);
    assertEquals(0, nodes.size());
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) Test(org.junit.Test)

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