|
|
|
Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1037126 is a reply to message #1037103] |
Tue, 09 April 2013 07:44 |
Hao Zhang Messages: 107 Registered: April 2013 |
Senior Member |
|
|
Hi
I saw this link http://www.eclipse.org/forums/index.php/m/947690/?srch=toEnumerationType#msg_947690.
Should I define a speicial function for "EnumDef" or I only can have one def dispatch void infer() in the inferrer? For me, I would like to have the keyword of "enum" and then I can define many "enum" datatypes in the ".dmodel" file, just like the "int, double, String, etc". Thank you in advance!
def dispatch void infer(EnumDef enumDef, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
val enumName = enumDef.fullyQualifiedName.toString
acceptor.accept(enumDef.toEnumerationType(enumName) [
]).initializeLater [
documentation = enumDef.documentation
val intTypeRef = enumDef.newTypeRef(typeof(int))
for (const1 : enumDef.enumConsts) {
members += const1.toEnumerationLiteral(const1.name)
}
members += enumDef.toMethod("getValue", intTypeRef) [
body = [append('''
switch (this) {
«FOR const1 : enumDef.enumConsts»
case «const1.name»: return «const1.id»;
«ENDFOR»
default: throw new IllegalArgumentException("Constant " + name() + " is not an «enumName»");
}
''')]
]
]
}
Christian Dietrich wrote on Tue, 09 April 2013 03:11Hi,
i am not sure if you mean enum on xtext side or on jvm/java side.
for enums in xtext see docs.
for enums on jvm/xbase/javaside call toEnumerationType in the inferrer from whatever you have in the model.
|
|
|
|
Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1037193 is a reply to message #1037144] |
Tue, 09 April 2013 09:12 |
Hao Zhang Messages: 107 Registered: April 2013 |
Senior Member |
|
|
Thank you very much for your so much help!
Currently, I have the "domain-model" example. In the "DomainmodelJvmModelInferrer.xtend" file, I have the definition of "DomainmodelJvmModelInferrer ". The contents are as follows. Could you show me how to change it in order to have the "enum" keyword in the ".dmodel" file? Sorry, I am really a very very bbeginner.
class DomainmodelJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
@Inject extension IQualifiedNameProvider
@Inject XbaseCompiler xbaseCompiler
def dispatch infer(Entity entity, IJvmDeclaredTypeAcceptor acceptor, boolean prelinkingPhase) {
acceptor.accept(
entity.toClass( entity.fullyQualifiedName )
).initializeLater [
documentation = entity.documentation
if (entity.superType != null)
superTypes += entity.superType.cloneWithProxies
val procedure = entity.newTypeRef(typeof(Procedure1), it.newTypeRef())
members += entity.toConstructor() []
members += entity.toConstructor() [
parameters += entity.toParameter("initializer", procedure)
body = [it.append("initializer.apply(this);")]
]
val fields = <JvmField>newArrayList()
for ( f : entity.features ) {
switch f {
Property : {
val field = f.toField(f.name, f.type)
fields += field
members += field
members += f.toGetter(f.name, f.type)
members += f.toSetter(f.name, f.type)
}
Operation : {
members += f.toMethod(f.name, f.type) [
documentation = f.documentation
for (p : f.params) {
parameters += p.toParameter(p.name, p.parameterType)
}
//body = f.body
body = [
for (oneExp : f.body){
xbaseCompiler.compile(oneExp,it,f.type)
}
]
]
}
}
}
members += entity.toToStringMethod(it)
]
}
}
Christian Dietrich wrote on Tue, 09 April 2013 04:03Hi,
it shoub be 1:1 the same as if your infer a class, method, field etc
|
|
|
|
Re: Is it possible to define and use "enum" in the ".dmodel" file? [message #1037243 is a reply to message #1037208] |
Tue, 09 April 2013 10:29 |
Hao Zhang Messages: 107 Registered: April 2013 |
Senior Member |
|
|
Thank you!
But I cannot write the "Domainmodel.xtext" and the "DomainmodelJvmModelInferrer.xtend" files corretly. Currently, part of the "Domainmodel.xtext" file are as follows:
DomainModel:
elements+=AbstractElement*;
AbstractElement:
PackageDeclaration | Entity | Import | MyEnum;
MyEnum:
'myEnum' name=ValidID '{'
enumConsts += ValidID*
'}'
;
Correspondingly, I have to write the "DomainmodelJvmModelInferrer.xtend" file. Currently, this file is as follows. I really do not know how to write them correctly, especially the ".xtext" part.
class DomainmodelJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
@Inject extension IQualifiedNameProvider
@Inject XbaseCompiler xbaseCompiler
def dispatch infer(Entity entity, IJvmDeclaredTypeAcceptor acceptor, boolean prelinkingPhase) {
acceptor.accept(
entity.toClass( entity.fullyQualifiedName )
).initializeLater [
documentation = entity.documentation
if (entity.superType != null)
superTypes += entity.superType.cloneWithProxies
val procedure = entity.newTypeRef(typeof(Procedure1), it.newTypeRef())
members += entity.toConstructor() []
members += entity.toConstructor() [
parameters += entity.toParameter("initializer", procedure)
body = [it.append("initializer.apply(this);")]
]
val fields = <JvmField>newArrayList()
for ( f : entity.features ) {
switch f {
Property : {
val field = f.toField(f.name, f.type)
fields += field
members += field
members += f.toGetter(f.name, f.type)
members += f.toSetter(f.name, f.type)
}
Operation : {
members += f.toMethod(f.name, f.type) [
documentation = f.documentation
for (p : f.params) {
parameters += p.toParameter(p.name, p.parameterType)
}
//body = f.body
body = [
for (oneExp : f.body){
xbaseCompiler.compile(oneExp,it,f.type)
}
]
]
}
}
}
members += entity.toToStringMethod(it)
]
}
def dispatch infer(MyEnum myenum, IJvmDeclaredTypeAcceptor acceptor, boolean prelinkingPhase) {
val enumName = myenum.fullyQualifiedName.toString
acceptor.accept(myenum.toEnumerationType(enumName) [
]).initializeLater [
documentation = myenum.documentation
val intTypeRef = myenum.newTypeRef(typeof(int))
for (const1 : myenum.enumConsts) {
members += const1.toEnumerationLiteral(const1.name)
}
members += myenum.toMethod("getValue", intTypeRef) [
body = [append('''
switch (this) {
«FOR const1 : myenum.enumConsts»
case «const1.name»: return «const1.id»;
«ENDFOR»
default: throw new IllegalArgumentException("Constant " + name() + " is not an «enumName»");
}
''')]
]
]
}
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03075 seconds