Search in sources :

Example 21 with EEnumLiteral

use of org.eclipse.emf.ecore.EEnumLiteral in project xtext-core by eclipse.

the class GrammarParserTest method testEnum_07.

@Test
public void testEnum_07() throws Exception {
    String modelAsString = "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" + "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/7'\n" + "Model: enumValue=MyEnum;\n" + "enum MyEnum: Value | Value;";
    Grammar grammar = (Grammar) getModel(modelAsString);
    assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
    checkEnums(grammar);
    EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
    assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/7", pack.getNsURI());
    EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
    assertNotNull(eEnum);
    assertEquals(1, eEnum.getELiterals().size());
    EEnumLiteral value = eEnum.getELiterals().get(0);
    assertEquals("Value", value.getName());
    assertEquals(0, value.getValue());
    assertEquals("Value", value.getLiteral());
}
Also used : EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) Grammar(org.eclipse.xtext.Grammar) EEnum(org.eclipse.emf.ecore.EEnum) EPackage(org.eclipse.emf.ecore.EPackage) Test(org.junit.Test)

Example 22 with EEnumLiteral

use of org.eclipse.emf.ecore.EEnumLiteral in project xtext-core by eclipse.

the class GrammarParserTest method testEnum_01.

@Test
public void testEnum_01() throws Exception {
    String modelAsString = "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" + "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/1'\n" + "Model: enumValue=MyEnum;\n" + "enum MyEnum: Value1;";
    Grammar grammar = (Grammar) getModel(modelAsString);
    assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
    checkEnums(grammar);
    EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
    assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/1", pack.getNsURI());
    EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
    assertNotNull(eEnum);
    assertEquals(1, eEnum.getELiterals().size());
    EEnumLiteral value = eEnum.getELiterals().get(0);
    assertEquals("Value1", value.getName());
    assertEquals(0, value.getValue());
    assertEquals("Value1", value.getLiteral());
}
Also used : EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) Grammar(org.eclipse.xtext.Grammar) EEnum(org.eclipse.emf.ecore.EEnum) EPackage(org.eclipse.emf.ecore.EPackage) Test(org.junit.Test)

Example 23 with EEnumLiteral

use of org.eclipse.emf.ecore.EEnumLiteral in project xtext-core by eclipse.

the class GrammarParserTest method testEnum_05.

@Test
public void testEnum_05() throws Exception {
    String modelAsString = "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" + "import 'classpath:/org/eclipse/xtext/enumrules/enums.ecore'\n" + "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/5'\n" + "Model: enumValue=ExistingEnum;\n" + "enum ExistingEnum: SameName = 'value';";
    Grammar grammar = (Grammar) getModel(modelAsString);
    assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
    checkEnums(grammar);
    EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
    assertEquals("http://www.eclipse.org/2009/tmf/xtext/EnumRulesTestLanguage/imported", pack.getNsURI());
    EEnum eEnum = (EEnum) pack.getEClassifier("ExistingEnum");
    assertNotNull(eEnum);
    assertEquals(3, eEnum.getELiterals().size());
    EEnumLiteral value = eEnum.getELiterals().get(0);
    assertEquals(ExistingEnum.SAME_NAME.getName(), value.getName());
    assertEquals(ExistingEnum.SAME_NAME.getValue(), value.getValue());
    assertEquals(ExistingEnum.SAME_NAME.getLiteral(), value.getLiteral());
    EnumRule rule = (EnumRule) grammar.getRules().get(1);
    assertEquals(eEnum, rule.getType().getClassifier());
    EnumLiteralDeclaration decl = (EnumLiteralDeclaration) rule.getAlternatives();
    assertEquals(value, decl.getEnumLiteral());
    assertNotNull(decl.getLiteral());
    assertEquals("value", decl.getLiteral().getValue());
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) Grammar(org.eclipse.xtext.Grammar) EEnum(org.eclipse.emf.ecore.EEnum) EPackage(org.eclipse.emf.ecore.EPackage) Test(org.junit.Test)

Example 24 with EEnumLiteral

use of org.eclipse.emf.ecore.EEnumLiteral in project xtext-core by eclipse.

the class GrammarParserTest method testEnum_03.

@Test
public void testEnum_03() throws Exception {
    String modelAsString = "grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" + "generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/3'\n" + "Model: enumValue=MyEnumRule;\n" + "enum MyEnumRule returns MyEnum: Value1 = 'value';";
    Grammar grammar = (Grammar) getModel(modelAsString);
    assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
    checkEnums(grammar);
    EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
    assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/3", pack.getNsURI());
    EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
    assertNotNull(eEnum);
    assertEquals(1, eEnum.getELiterals().size());
    EEnumLiteral value = eEnum.getELiterals().get(0);
    assertEquals("Value1", value.getName());
    assertEquals(0, value.getValue());
    assertEquals("value", value.getLiteral());
}
Also used : EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) Grammar(org.eclipse.xtext.Grammar) EEnum(org.eclipse.emf.ecore.EEnum) EPackage(org.eclipse.emf.ecore.EPackage) Test(org.junit.Test)

Example 25 with EEnumLiteral

use of org.eclipse.emf.ecore.EEnumLiteral in project xtext-core by eclipse.

the class Xtext2EcoreTransformer method deriveEnums.

private void deriveEnums(EnumRule rule) {
    EEnum returnType = (EEnum) rule.getType().getClassifier();
    if (returnType != null) {
        List<EnumLiteralDeclaration> decls = EcoreUtil2.getAllContentsOfType(rule, EnumLiteralDeclaration.class);
        for (EnumLiteralDeclaration decl : decls) {
            if (decl.getEnumLiteral() == null) {
                List<INode> nodes = NodeModelUtils.findNodesForFeature(decl, XtextPackage.Literals.ENUM_LITERAL_DECLARATION__ENUM_LITERAL);
                if (!nodes.isEmpty()) {
                    if (nodes.size() > 1)
                        throw new IllegalStateException("Unexpected nodes found: " + nodes);
                    INode node = nodes.get(0);
                    String text = NodeModelUtils.getTokenText(node).replace("^", "");
                    EEnumLiteral literal = null;
                    if (rule.getType().getMetamodel() instanceof ReferencedMetamodel) {
                        literal = returnType.getEEnumLiteral(text);
                    } else {
                        EEnumLiteral existing = returnType.getEEnumLiteral(text);
                        if (existing == null) {
                            literal = EcoreFactory.eINSTANCE.createEEnumLiteral();
                            int index = returnType.getELiterals().size();
                            returnType.getELiterals().add(literal);
                            literal.setName(text);
                            literal.setValue(index);
                            if (decl.getLiteral() != null) {
                                literal.setLiteral(decl.getLiteral().getValue());
                            } else {
                                literal.setLiteral(text);
                            }
                        } else {
                            literal = existing;
                        }
                        SourceAdapter.adapt(literal, decl);
                    }
                    if (literal == null) {
                        reportError(new TransformationException(TransformationErrorCode.InvalidFeature, "Enum literal '" + text + "' does not exist.", decl));
                    } else {
                        decl.setEnumLiteral(literal);
                    }
                }
            }
            if (decl.getLiteral() == null && decl.getEnumLiteral() != null) {
                Keyword kw = XtextFactory.eINSTANCE.createKeyword();
                kw.setValue(decl.getEnumLiteral().getLiteral());
                decl.setLiteral(kw);
            }
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) Keyword(org.eclipse.xtext.Keyword) EEnumLiteral(org.eclipse.emf.ecore.EEnumLiteral) EEnum(org.eclipse.emf.ecore.EEnum) ReferencedMetamodel(org.eclipse.xtext.ReferencedMetamodel)

Aggregations

EEnumLiteral (org.eclipse.emf.ecore.EEnumLiteral)35 EEnum (org.eclipse.emf.ecore.EEnum)22 EPackage (org.eclipse.emf.ecore.EPackage)10 Grammar (org.eclipse.xtext.Grammar)10 Test (org.junit.Test)9 EClass (org.eclipse.emf.ecore.EClass)8 EDataType (org.eclipse.emf.ecore.EDataType)6 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)6 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)5 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)4 EClassifier (org.eclipse.emf.ecore.EClassifier)4 EnumLiteralDeclaration (org.eclipse.xtext.EnumLiteralDeclaration)4 IdEObject (org.bimserver.emf.IdEObject)3 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)3 EAttribute (org.eclipse.emf.ecore.EAttribute)3 EObject (org.eclipse.emf.ecore.EObject)3 DTDEnumGroupKind (org.eclipse.wst.dtd.core.internal.emf.DTDEnumGroupKind)3 DTDEnumerationType (org.eclipse.wst.dtd.core.internal.emf.DTDEnumerationType)3 EnumRule (org.eclipse.xtext.EnumRule)3 BufferOverflowException (java.nio.BufferOverflowException)2