Question

I need to generate Java enums via DSL, using xtend 2.5. My grammar looks something like this:

grammar de.mytest.xtext.enumdsl.EnumDsl with org.eclipse.xtext.xbase.annotations.XbaseWithAnnotations

import "http://www.eclipse.org/emf/2002/Ecore" as ecore
import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase

EnumModel:
    pckg=EnumPackage;

EnumPackage:
    {EnumPackage} 
    importSection=XImportSection?
    'package' name=QualifiedName '{'
        (enums+=MyEnum)*
    '}';

MyEnum:
    'enum' name=ValidID '{' (items+=ValidID)* '}';

With this grammar the DSL could look like this:

package de.mytest.enumpackage {
    enum MyEnumTest {
        YES
        NO
        MAYBE
    }
}

The resulting Java enum expected would be:

package de.mytest.enumpackage;

public enum MyEnumTest {
    YES,
    NO,
    MAYBE
}

I tried to use implement the xtend method infer() using toEnumerateType(), but without success. Also I couldn't find any examples for that! My google-fu doesn't seem to be good enough!

Using toEnumerateLiteral(), which also works inside toClass(), but it generates a class obviously...

Do you have any ideas?

Thanks in advance, Michael

Was it helpful?

Solution 2

Thank you very much for hint, Sebastian. I had to modify it a little. Here is the method, that worked for me finally :-)

class EnumDslJvmModelInferrer extends AbstractModelInferrer {

    @Inject extension JvmTypesBuilder
    def dispatch void infer(EnumModel element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
        element.pckg.enums.forEach[ myEnum |
            acceptor.accept(myEnum.toEnumerationType(element.pckg.name+"."+myEnum.name)[]).initializeLater[
                myEnum.items.forEach [ literal |
                    it.members += myEnum.toEnumerationLiteral(literal)
                ]
            ]
        ]
    }
}

OTHER TIPS

This should do the trick for you:

myEnum.toEnumerationType(myEnum.name) [
  myEnum.items.forEach [ literal |
    myEnum.toEnumerationLiteral(literal)
  ]
]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top