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