Search in sources :

Example 6 with AbstractMetamodelDeclaration

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

the class XtextValidationTest method testNameClash_03.

@Test
public void testNameClash_03() throws Exception {
    String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Foo: myVars=ID my_vars=ID;\n";
    Grammar grammar = (Grammar) getModel(grammarAsText);
    AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0);
    XtextValidator validator = get(XtextValidator.class);
    ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
    CompoundElement element = (CompoundElement) grammar.getRules().get(0).getAlternatives();
    messageAcceptor.expectedContext(grammar.getRules().get(0).getType(), element.getElements().get(0), element.getElements().get(1));
    validator.setMessageAcceptor(messageAcceptor);
    validator.checkGeneratedPackage((GeneratedMetamodel) metamodelDeclaration, Diagnostician.INSTANCE, Collections.EMPTY_MAP);
    messageAcceptor.validate();
}
Also used : Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) CompoundElement(org.eclipse.xtext.CompoundElement) Test(org.junit.Test)

Example 7 with AbstractMetamodelDeclaration

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

the class XtextValidationTest method testDuplicateFeatures_01.

@Test
public void testDuplicateFeatures_01() throws Exception {
    String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Model: Parent1 | Parent2 | NoParent;\n" + "NoParent: foo=ID;" + "Parent1: Sub1 | Sub2;\n" + "Parent2: Sub2 | Sub3;\n" + "Sub1: x=ID;\n" + "Sub2: x=ID;\n" + "Sub3: x=ID;\n";
    Grammar grammar = (Grammar) getModel(grammarAsText);
    AbstractMetamodelDeclaration metamodelDeclaration = grammar.getMetamodelDeclarations().get(0);
    XtextValidator validator = get(XtextValidator.class);
    ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
    messageAcceptor.expectedContext(grammar.getRules().get(2).getType(), grammar.getRules().get(3).getType(), grammar.getRules().get(5).getType(), grammar.getRules().get(4).getAlternatives(), grammar.getRules().get(5).getAlternatives(), grammar.getRules().get(6).getAlternatives());
    validator.setMessageAcceptor(messageAcceptor);
    validator.checkGeneratedPackage((GeneratedMetamodel) metamodelDeclaration, Diagnostician.INSTANCE, Collections.EMPTY_MAP);
    messageAcceptor.validate();
}
Also used : Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) Test(org.junit.Test)

Example 8 with AbstractMetamodelDeclaration

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

the class LazyLinker method globalFindInstantiableCompatible.

protected EClass globalFindInstantiableCompatible(EClass eType) {
    Set<String> visitedPackages = Sets.newHashSet(eType.getEPackage().getNsURI());
    for (AbstractMetamodelDeclaration metamodel : GrammarUtil.allMetamodelDeclarations(grammarAccess.getGrammar())) {
        if (visitedPackages.add(metamodel.getEPackage().getNsURI())) {
            EClass result = findSubTypeInEPackage(metamodel.getEPackage(), eType);
            if (result != null)
                return result;
        }
    }
    log.warn("Traversing EPackage registry to find instantiable subtype for '" + eType.getName() + "'");
    log.warn("You may override LazyLinker#globalFindInstantiableCompatible(..) to prevent this.");
    for (String nsURI : getRegisteredNsUris()) {
        if (visitedPackages.add(nsURI)) {
            try {
                EClass result = findSubTypeInEPackage(getRegistry().getEPackage(nsURI), eType);
                if (result != null)
                    return result;
            } catch (WrappedException ex) {
                log.error("Error when loading EPackage '" + nsURI + "'");
                log.error("You may override LazyLinker#globalFindInstantiableCompatible(..) to prevent this.");
                log.error("Error when loading EPackage '" + nsURI + "'", ex);
            }
        }
    }
    throw new IllegalStateException("Could not find an instantiable subtype for type: '" + eType.getName() + "' (" + eType.getEPackage().getNsURI() + ").");
}
Also used : EClass(org.eclipse.emf.ecore.EClass) WrappedException(org.eclipse.emf.common.util.WrappedException) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration)

Example 9 with AbstractMetamodelDeclaration

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

the class GrammarUtilTest method testAllMetamodelDeclarations_02.

@Test
public void testAllMetamodelDeclarations_02() throws Exception {
    this.with(XtextStandaloneSetup.class);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar foo with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("import \'http://www.eclipse.org/emf/2002/Ecore\' as bar");
    _builder.newLine();
    _builder.append("generate g \'http://3\' 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 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(1);
    Assert.assertEquals("bar", decl.getAlias());
    Assert.assertNotNull(decl.getEPackage());
    Assert.assertEquals("http://3", decl.getEPackage().getNsURI());
    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(1), abstractRule.getType().getMetamodel());
}
Also used : 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 10 with AbstractMetamodelDeclaration

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

the class Xtext2EcoreTransformerTest method testBug296496_02.

@Test
public void testBug296496_02() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar test with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("import \"classpath:/org/eclipse/xtext/enumrules/enums.ecore\"");
    _builder.newLine();
    _builder.append("generate myDsl \"http://example.xtext.org/MyDsl\"");
    _builder.newLine();
    _builder.append("CreatedType:");
    _builder.newLine();
    _builder.append("  ");
    _builder.append("enumFeature=ExistingEnum otherEnumFeature=ExistingEnum;");
    _builder.newLine();
    _builder.append("enum ExistingEnum:");
    _builder.newLine();
    _builder.append("  ");
    _builder.append("SameName | OverriddenLiteral = \"overridden\" | DifferentName;");
    _builder.newLine();
    final String grammarAsString = _builder.toString();
    final XtextResource resource = this.getResourceFromStringAndExpect(grammarAsString, 0);
    Assert.assertTrue(resource.getErrors().toString(), resource.getErrors().isEmpty());
    EObject _head = IterableExtensions.<EObject>head(resource.getContents());
    final Grammar grammar = ((Grammar) _head);
    AbstractMetamodelDeclaration _get = grammar.getMetamodelDeclarations().get(1);
    GeneratedMetamodel generatedMetamodel = ((GeneratedMetamodel) _get);
    Assert.assertEquals("myDsl", generatedMetamodel.getName());
    EClassifier _type = this.<EClassifier>type(generatedMetamodel.getEPackage(), "CreatedType");
    EClass createdType = ((EClass) _type);
    Assert.assertEquals(this.<EStructuralFeature>feature(createdType, "enumFeature").getEType(), this.<EStructuralFeature>feature(createdType, "otherEnumFeature").getEType());
}
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) 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