Problems with code generator [message #1720929] |
Thu, 21 January 2016 16:12  |
Eclipse User |
|
|
|
Hi at everyone,
i have some problems with the transform my grammar in a correct java code.
To be precise i would like to get enum type information for a method definition, in particularly, this is a part of my grammar:
Elemento:
VarD | FuncD
;
FuncD:
("void"|type=Type) name=ID "(" (args+=Argument ("," args+=Argument)* )? ")" body=Body
;
Type:
prova=BaseType
;
enum BaseType:
INT_TYPE = "int" | DOUBLE_TYPE = "double" | BOOL_TYPE = "bool";
And this is my xtend file for code generation:
def CharSequence compile(Model model)
{
'''
public class test{
«FOR e:model.elementi.filter(typeof(FuncD))»
public «e.type.compileType» «e.name» ()
{
}
«ENDFOR»
}
'''
}
def compileType(Type type)
{
'''
«IF type == null »void«ELSE»
HOW CAN COMPARE WITH ENUM BASETYPE HERE???!
«ENDIF»
'''
}
With my "type" how can access to BaseType for compare with ENUM value?
Thanks in advance,
Daniele
|
|
|
|
|
|
Re: Problems with code generator [message #1721123 is a reply to message #1721117] |
Sun, 24 January 2016 18:33   |
Eclipse User |
|
|
|
Why don't you use this kind of expressions which use Xbase and JVM Model?
grammar org.xtext.example.forum1.Ex1 with org.eclipse.xtext.xbase.Xbase
...
Elemento:
VarD | FuncD;
FuncD:
ftype=JvmTypeReference name=ID "(" (params+=FullJvmFormalParameter ("," params+=FullJvmFormalParameter)*)? ")"
body=XBlockExpression;
VarD:
name=ValidID ':' type=JvmTypeReference;
and the dsl is something like this:
a: int
result: int
void sum (int a,int b){
val myStringValue = 'A final string value'
var myIntValue_i = 0
var myIntValue_j = 5
}
and infer method can be used as following:
def dispatch void infer(Model model, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
acceptor.accept(model.toClass("my.company.forum1.Ex1")) [
for (elem : model.elementos) {
switch elem {
VarD:{
val field = elem.toField(elem.name, elem.type)
members += field
members += elem.toGetter(elem.name, elem.type)
members += elem.toSetter(elem.name, elem.type)
}
FuncD : {
members += elem.toMethod(elem.name, elem.ftype ?: inferredType) [
for (p : elem.params) {
parameters += p.toParameter(p.name, p.parameterType)
}
body = elem.body
]
}
}
}
]
}
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04947 seconds