Search in sources :

Example 26 with AbstractMetamodelDeclaration

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

the class XtextValidationTest method testDuplicateFeatures_02.

@Test
public void testDuplicateFeatures_02() throws Exception {
    String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "A : (b+=B)*;\n" + "B : C | D;\n" + "C : 'c' name=ID ('e' e+=E)+;\n" + "E : name=ID;\n" + "F : C | E;\n" + "D : 'd' name=ID 'ref' ref=[F];";
    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(1).getType(), grammar.getRules().get(2).getType(), grammar.getRules().get(4).getType(), ((CompoundElement) grammar.getRules().get(2).getAlternatives()).getElements().get(1), grammar.getRules().get(3).getAlternatives(), ((CompoundElement) grammar.getRules().get(5).getAlternatives()).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 27 with AbstractMetamodelDeclaration

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

the class XtextValidationTest method testNameClash_02.

@Test
public void testNameClash_02() throws Exception {
    String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Class returns Class: {Class_} name=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(), ((Action) element.getElements().get(0)).getType());
    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 28 with AbstractMetamodelDeclaration

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

the class XtextValidationTest method testNameClash_01.

@Test
public void testNameClash_01() throws Exception {
    String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "PRINTF: vars=PRINTF_Vars;\n" + "PRINTF_Vars: arg1=ID;";
    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(0).getAlternatives(), grammar.getRules().get(1).getType());
    validator.setMessageAcceptor(messageAcceptor);
    validator.checkGeneratedPackageForNameClashes((GeneratedMetamodel) metamodelDeclaration);
    messageAcceptor.validate();
}
Also used : Grammar(org.eclipse.xtext.Grammar) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) Test(org.junit.Test)

Example 29 with AbstractMetamodelDeclaration

use of org.eclipse.xtext.AbstractMetamodelDeclaration 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 30 with AbstractMetamodelDeclaration

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

the class GrammarUtilTest method testAllMetamodelDeclarations_01.

@Test
public void testAllMetamodelDeclarations_01() 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 ecore");
    _builder.newLine();
    _builder.append("generate g \'http://3\' as ecore");
    _builder.newLine();
    _builder.append("startrule returns ecore::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(2, 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("ecore", decl.getAlias());
    decl = decls.get(1);
    Assert.assertEquals("ecore", decl.getAlias());
    Assert.assertNotNull(decl.getEPackage());
    Assert.assertEquals("http://3", decl.getEPackage().getNsURI());
}
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) ReferencedMetamodel(org.eclipse.xtext.ReferencedMetamodel) 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