Search in sources :

Example 11 with GeneratedMetamodel

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

the class XtextValidator method getInheritedGeneratedMetamodels.

protected List<GeneratedMetamodel> getInheritedGeneratedMetamodels(ReferencedMetamodel metamodel) {
    List<GeneratedMetamodel> allGeneratedMetamodels = new ArrayList<GeneratedMetamodel>();
    Grammar grammar = GrammarUtil.getGrammar(metamodel);
    Set<Grammar> visited = Sets.newHashSet();
    for (Grammar usedGrammar : grammar.getUsedGrammars()) Iterables.addAll(allGeneratedMetamodels, getAllGeneratedMetamodels(usedGrammar, visited));
    if (allGeneratedMetamodels.isEmpty())
        return Collections.emptyList();
    return allGeneratedMetamodels;
}
Also used : GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) ArrayList(java.util.ArrayList) Grammar(org.eclipse.xtext.Grammar)

Example 12 with GeneratedMetamodel

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

the class XtextValidator method checkReferencedMetamodel.

@Check
public void checkReferencedMetamodel(ReferencedMetamodel metamodel) throws ValueConverterException {
    if (metamodel.getEPackage() == null)
        return;
    String nsURI = metamodel.getEPackage().getNsURI();
    List<GeneratedMetamodel> allGeneratedMetamodels = getInheritedGeneratedMetamodels(metamodel);
    String text = getUsedUri(metamodel);
    for (GeneratedMetamodel generatedMetamodel : allGeneratedMetamodels) {
        EPackage generatedPackage = generatedMetamodel.getEPackage();
        if (generatedPackage != null && nsURI.equals((generatedPackage.getNsURI()))) {
            if (!text.equals(nsURI)) {
                addIssue("Metamodels that have been generated by a super grammar must be referenced by nsURI: " + nsURI, metamodel, XtextPackage.Literals.ABSTRACT_METAMODEL_DECLARATION__EPACKAGE, INVALID_PACKAGE_REFERENCE_INHERITED, nsURI);
                return;
            }
            return;
        }
    }
    checkExternalPackage(metamodel, text);
}
Also used : GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) EPackage(org.eclipse.emf.ecore.EPackage) Check(org.eclipse.xtext.validation.Check)

Example 13 with GeneratedMetamodel

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

the class GrammarUtilTest method testAllMetamodelDeclarations_03.

@Test
public void testAllMetamodelDeclarations_03() throws Exception {
    this.with(XtextStandaloneSetup.class);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar foo with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("generate g \'http://3\' as bar");
    _builder.newLine();
    _builder.append("import \'http://www.eclipse.org/emf/2002/Ecore\' as bar");
    _builder.newLine();
    _builder.append("startrule returns bar::startrule: name=ID;");
    _builder.newLine();
    String model = _builder.toString();
    Resource r = this.getResourceFromString(model);
    EObject _get = r.getContents().get(0);
    Grammar g = ((Grammar) _get);
    List<AbstractMetamodelDeclaration> decls = GrammarUtil.allMetamodelDeclarations(g);
    Assert.assertEquals(3, decls.size());
    AbstractMetamodelDeclaration decl = decls.get(0);
    Assert.assertTrue((decl instanceof GeneratedMetamodel));
    Assert.assertEquals("bar", decl.getAlias());
    Assert.assertNotNull(decl.getEPackage());
    Assert.assertEquals("http://3", decl.getEPackage().getNsURI());
    decl = decls.get(1);
    Assert.assertTrue((decl instanceof ReferencedMetamodel));
    Assert.assertNotNull(decl.getEPackage());
    Assert.assertEquals("http://www.eclipse.org/emf/2002/Ecore", decl.getEPackage().getNsURI());
    Assert.assertEquals("bar", decl.getAlias());
    decl = decls.get(2);
    Assert.assertTrue((decl instanceof ReferencedMetamodel));
    Assert.assertNotNull(decl.getEPackage());
    Assert.assertEquals("http://www.eclipse.org/emf/2002/Ecore", decl.getEPackage().getNsURI());
    Assert.assertEquals("ecore", decl.getAlias());
    AbstractRule abstractRule = g.getRules().get(0);
    Assert.assertSame(decls.get(0), abstractRule.getType().getMetamodel());
}
Also used : GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) AbstractRule(org.eclipse.xtext.AbstractRule) ReferencedMetamodel(org.eclipse.xtext.ReferencedMetamodel) Test(org.junit.Test)

Example 14 with GeneratedMetamodel

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

the class Xtext2EcoreTransformerTest method testPostProcessorHook.

@Test
public void testPostProcessorHook() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar test with org.eclipse.xtext.common.Terminals import \'http://www.eclipse.org/emf/2002/Ecore\' as ecore  generate test \'http://test\' MyRule: myFeature=INT;");
    final String xtextGrammar = _builder.toString();
    EObject _model = this.getModel(xtextGrammar);
    final Grammar grammar = ((Grammar) _model);
    final Xtext2EcoreTransformer transformer = new Xtext2EcoreTransformer(grammar);
    AbstractMetamodelDeclaration _get = grammar.getMetamodelDeclarations().get(1);
    GeneratedMetamodel testMetamodel = ((GeneratedMetamodel) _get);
    final Xtext2EcoreTransformerTest.MockedXtext2EcorePostProcessor postProcessor = new Xtext2EcoreTransformerTest.MockedXtext2EcorePostProcessor(testMetamodel);
    transformer.setPostProcessor(postProcessor);
    transformer.transform();
    Assert.assertEquals("process mthde called once", 1, postProcessor.proccessMethCalled());
}
Also used : GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) Xtext2EcoreTransformer(org.eclipse.xtext.xtext.ecoreInference.Xtext2EcoreTransformer) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) Test(org.junit.Test)

Aggregations

GeneratedMetamodel (org.eclipse.xtext.GeneratedMetamodel)14 Grammar (org.eclipse.xtext.Grammar)8 EObject (org.eclipse.emf.ecore.EObject)6 AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)6 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)5 EPackage (org.eclipse.emf.ecore.EPackage)4 Test (org.junit.Test)4 EClassifier (org.eclipse.emf.ecore.EClassifier)3 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)3 EnumRule (org.eclipse.xtext.EnumRule)3 ReferencedMetamodel (org.eclipse.xtext.ReferencedMetamodel)3 XtextResource (org.eclipse.xtext.resource.XtextResource)3 EClass (org.eclipse.emf.ecore.EClass)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)2 AbstractRule (org.eclipse.xtext.AbstractRule)2 EnumLiteralDeclaration (org.eclipse.xtext.EnumLiteralDeclaration)2 TypeRef (org.eclipse.xtext.TypeRef)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1