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;
}
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);
}
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;
}
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"));
}
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());
}
}
Aggregations