Search in sources :

Example 16 with AbstractMetamodelDeclaration

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

the class Xtext2EcoreTransformer method getOrComputeReturnType.

private TypeRef getOrComputeReturnType(AbstractRule rule) {
    TypeRef result = rule.getType();
    if (result == null) {
        EClassifier classifier = getClassifierFor(rule);
        if (classifier == null) {
            if (rule.getName() == null)
                return null;
            result = getTypeRef(rule.getName());
        } else
            result = getTypeRef(classifier);
        if (result.getMetamodel() == null) {
            AbstractMetamodelDeclaration bestMatch = null;
            for (AbstractMetamodelDeclaration decl : grammar.getMetamodelDeclarations()) {
                if (decl instanceof GeneratedMetamodel && Strings.isEmpty(decl.getAlias())) {
                    bestMatch = decl;
                    break;
                }
            }
            if (result.getMetamodel() == null)
                result.setMetamodel(bestMatch);
        }
        rule.setType(result);
    }
    return result;
}
Also used : GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) TypeRef(org.eclipse.xtext.TypeRef) EClassifier(org.eclipse.emf.ecore.EClassifier) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration)

Example 17 with AbstractMetamodelDeclaration

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

the class Xtext2EcoreTransformer method createEClassifierInfo.

private EClassifierInfo createEClassifierInfo(TypeRef typeRef, String name) throws TransformationException {
    if (eClassifierInfos.getInfo(typeRef) != null)
        throw new IllegalArgumentException("Cannot create EClass for same type twice " + typeRef.getClassifier().getName());
    // + GrammarUtil.getQualifiedName(typeRef));
    String classifierName = GrammarUtil.getTypeRefName(typeRef);
    if (classifierName == null)
        classifierName = name;
    if (classifierName == null)
        throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Cannot reference unnamed type.", typeRef);
    AbstractMetamodelDeclaration metaModel = typeRef.getMetamodel();
    if (metaModel == null)
        throw new TransformationException(TransformationErrorCode.UnknownMetaModelAlias, "Cannot create type for " + classifierName + " because its EPackage is unknown.", typeRef);
    EPackage generatedEPackage = getGeneratedEPackage(metaModel);
    if (generatedEPackage == null) {
        throw new TransformationException(TransformationErrorCode.CannotCreateTypeInSealedMetamodel, "Cannot create type '" + classifierName + "' in alias " + typeRef.getMetamodel().getAlias(), typeRef);
    }
    EClassifier classifier = generatedEPackage.getEClassifier(classifierName);
    if (classifier == null) {
        if (GrammarUtil.containingParserRule(typeRef) != null) {
            classifier = EcoreFactory.eINSTANCE.createEClass();
        } else if (GrammarUtil.containingEnumRule(typeRef) != null) {
            classifier = EcoreFactory.eINSTANCE.createEEnum();
        } else {
            for (AbstractMetamodelDeclaration mmd : grammar.getMetamodelDeclarations()) {
                if (mmd instanceof ReferencedMetamodel && mmd.getEPackage() != null && mmd.getEPackage().getNsURI().equals(EcorePackage.eNS_URI)) {
                    throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Cannot create datatype " + classifierName, typeRef);
                }
            }
            throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Cannot create datatype " + classifierName + ". If this is supposed to return EString, make sure you have imported '" + EcorePackage.eNS_URI + "'", typeRef);
        }
        classifier.setName(classifierName);
        generatedEPackage.getEClassifiers().add(classifier);
        typeRef.setClassifier(classifier);
        EClassifierInfo result;
        if (classifier instanceof EClass)
            result = EClassifierInfo.createEClassInfo((EClass) classifier, true, getGeneratedEPackageURIs(), GrammarUtil.getGrammar(typeRef));
        else
            // datatype or enum
            result = EClassifierInfo.createEDataTypeInfo((EDataType) classifier, true);
        if (!eClassifierInfos.addInfo(typeRef, result))
            throw new IllegalStateException("cannot add type for typeRef twice: '" + classifierName + "'");
        SourceAdapter.adapt(classifier, typeRef);
        return result;
    }
    typeRef.setClassifier(classifier);
    SourceAdapter.adapt(classifier, typeRef);
    return eClassifierInfos.getInfo(classifier);
}
Also used : EClass(org.eclipse.emf.ecore.EClass) EClassifier(org.eclipse.emf.ecore.EClassifier) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) ReferencedMetamodel(org.eclipse.xtext.ReferencedMetamodel) EPackage(org.eclipse.emf.ecore.EPackage)

Example 18 with AbstractMetamodelDeclaration

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

the class Xtext2EcoreTransformer method getTypeRef.

TypeRef getTypeRef(EClassifier classifier) {
    TypeRef result = XtextFactory.eINSTANCE.createTypeRef();
    result.setClassifier(classifier);
    EPackage pack = classifier.getEPackage();
    for (AbstractMetamodelDeclaration decl : GrammarUtil.allMetamodelDeclarations(grammar)) {
        if (pack.equals(decl.getEPackage())) {
            result.setMetamodel(decl);
            return result;
        }
    }
    return result;
}
Also used : TypeRef(org.eclipse.xtext.TypeRef) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) EPackage(org.eclipse.emf.ecore.EPackage)

Example 19 with AbstractMetamodelDeclaration

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

the class Xtext2EcoreTransformerTest method testTypesOfImplicitSuperGrammar.

@Test
public void testTypesOfImplicitSuperGrammar() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar test with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("generate test \'http://test\'");
    _builder.newLine();
    _builder.append("MyRule: myFeature=INT;");
    _builder.newLine();
    final String xtextGrammar = _builder.toString();
    EObject _model = this.getModel(xtextGrammar);
    final Grammar grammar = ((Grammar) _model);
    final Xtext2EcoreTransformer transformer = new Xtext2EcoreTransformer(grammar);
    transformer.removeGeneratedPackages();
    transformer.transform();
    final AbstractRule rule = IterableExtensions.<AbstractRule>head(grammar.getRules());
    TypeRef type = rule.getType();
    Assert.assertNotNull(type);
    Assert.assertNotNull(transformer.getEClassifierInfos().getInfo(type));
    AbstractMetamodelDeclaration _get = GrammarUtil.allMetamodelDeclarations(grammar).get(1);
    final ReferencedMetamodel referenced = ((ReferencedMetamodel) _get);
    Assert.assertNotNull(referenced);
    Assert.assertEquals("ecore", referenced.getAlias());
    Assert.assertNull(transformer.getEClassifierInfos().getInfo(referenced, "EString"));
    Assert.assertNull(transformer.getEClassifierInfos().getInfo(referenced, "EInt"));
    EClassifierInfos parentInfos = IterableExtensions.<EClassifierInfos>head(transformer.getEClassifierInfos().getParents());
    Assert.assertNotNull(parentInfos.getInfo(referenced, "EString"));
    Assert.assertNotNull(parentInfos.getInfo(referenced, "EInt"));
}
Also used : Xtext2EcoreTransformer(org.eclipse.xtext.xtext.ecoreInference.Xtext2EcoreTransformer) TypeRef(org.eclipse.xtext.TypeRef) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) AbstractRule(org.eclipse.xtext.AbstractRule) EClassifierInfos(org.eclipse.xtext.xtext.ecoreInference.EClassifierInfos) ReferencedMetamodel(org.eclipse.xtext.ReferencedMetamodel) Test(org.junit.Test)

Example 20 with AbstractMetamodelDeclaration

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

the class Xtext2EcoreTransformerTest method testBug310122.

@Test
public void testBug310122() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar test with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("generate myDsl \"http://example.xtext.org/MyDsl\"");
    _builder.newLine();
    _builder.append("Model: Sub1 | name=ID \'somekeyword\';");
    _builder.newLine();
    _builder.append("Sub1 returns Model: \'(\' Model \')\';");
    _builder.newLine();
    final String grammarAsString = _builder.toString();
    final XtextResource resource = this.getResourceFromString(grammarAsString);
    EObject _head = IterableExtensions.<EObject>head(resource.getContents());
    final Grammar grammar = ((Grammar) _head);
    AbstractMetamodelDeclaration _head_1 = IterableExtensions.<AbstractMetamodelDeclaration>head(grammar.getMetamodelDeclarations());
    GeneratedMetamodel generatedMetamodel = ((GeneratedMetamodel) _head_1);
    Assert.assertEquals("myDsl", generatedMetamodel.getName());
    Assert.assertEquals(1, generatedMetamodel.getEPackage().getEClassifiers().size());
    EClassifier _type = this.<EClassifier>type(generatedMetamodel.getEPackage(), "Model");
    EClass createdModel = ((EClass) _type);
    Assert.assertEquals(EcorePackage.Literals.ESTRING, this.<EStructuralFeature>feature(createdModel, "name").getEType());
    EList<AbstractRule> _rules = grammar.getRules();
    for (final AbstractRule rule : _rules) {
        Assert.assertEquals(createdModel, rule.getType().getClassifier());
    }
}
Also used : GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) EClass(org.eclipse.emf.ecore.EClass) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EClassifier(org.eclipse.emf.ecore.EClassifier) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) AbstractRule(org.eclipse.xtext.AbstractRule) 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