Xtext to Acceleo

102 Views Asked by At

I have a xtext code for an expression like this:

expr    : RelExp                            ( {LogicExp.args+=current}  op=LO args+=RelExp)* ;
RelExp      returns expr : ArithExp ( {RelExp.args+=current}    op=RO args+=ArithExp)* ;
ArithExp        returns expr : Term         ( {ArithExp.args+=current}  op=AO1 args+=Term)* ;
Term            returns expr : Factor       ( {Term.args+=current}      op=AO2 args+=Factor)* ;
Factor      returns expr : Atom         ({PostfixOp.arg=current} uo=UO)? 
            | {PrefixOp} uo=UO arg=Atom ;
            
Atom            returns expr : Literal
            | {Parenteses} '(' exp=expr ')' 
            | lValue ;
            
lValue      returns expr : {Var} valor=ID (
                   ({FuncCall.def=current} '(' arg=Argument? ')') | 
                   ({FieldAccess.obj=current} '.' field=ID) |
                   ({ArrayAccess.arr=current} '[' index=expr ']')
                   )*
            | PointerExp ;          

PointerExp  : {PointerExp} '**' '(' exp=expr ')' ;

//Case          : 'case' val=Atom ':' (commands+=Command)* ;

//Type          : tipo=TYPELIT ('[' exp=expr? ']')?;
Literal     : {IntLit} val=NUMBER | {TrueLit} val='TRUE' | {FalseLit} val='FALSE' | {StrLit} val=STRING;

I am trying to write an acceleo code to print an expression. But everytime I write (stat.exp/) in acceleo it prints org.xtext.example.scldsl.sclDsl.impl.TrueLitImpl@67af833b(val: 0).

But I needed only (val: 0)

Can anyone please help!!

1

There are 1 best solutions below

0
Banoffee On

When you call [stat.exp/] in Acceleo, it adds an implicit toString() to obtain a String representation from your AST element.

If you want to use your Xtext grammar to obtain a String representation, you will need to find a way to use the Xtext serializer generated for your DSL. As a first step, you should add a Java service in your Acceleo, and implement your Java service in such a way that it takes an AST element (probably EObject or some common super-type in your metamodel if you have one), has access to the Xtext serializer, and returns the serialized version of your AST element.