0

I am trying to generate code from my grammar. I would like to know how to get the final value e not the object. For example, I have:

Interval:
        '[' lower_bound=Expr ',' upper_bound=Expr ']' 
      | '[' lower_bound=Expr ',' upper_bound=Expr'[' 
      | ']' lower_bound=Expr ',' upper_bound=Expr ']' 
      | ']' lower_bound=Expr ',' upper_bound=Expr '['
;

Expr:
        literal=INT 
      | integer=INT '.' decimal=INT 
      | no_constraint?='infty' 
      | group=GroupName 
      | metric=NM 
      | right_side=Atomic Operator left_side=Atomic
;

In the code generator template I have:

'''
Term Value = «interval.lower_bound», «interval.upper_bound»
'''

I entered in the file of the language:

[10, 30]

When it generates a code it puts the object:

Term Value = org.xtext.sla.dyslacc.impl.ExprImpl@2a421168 (literal: 10, integer: 0, decimal: 0, no_constraint: false, metric: null), org.xtext.sla.dyslacc.impl.ExprImpl@5784d884 (literal: 30, integer: 0, decimal: 0, no_constraint: false, metric: null)

and what I wanted is simple the value entered in the template no matter if it is a literal or constraint :

10,30

(everything as string is fine)

any idea??

thank you

Sebastian Zarnekow
  • 6,609
  • 20
  • 23
user2801023
  • 487
  • 1
  • 4
  • 8

1 Answers1

0

You'd have to interpret the expression rather than print it by means of toString. Therefore I'd recommend to use subtypes of expr rather than a lengthy alternative. This would allow to use a dispatch from Xtend to decide how to visualize the expression.

Expr:
  IntLiteral | DoubleLiteal | ...
;
IntLiteral:
  literal=INT
;
DoubleLiteral:
  integer=INT '.' decimal=INT 
;

The dispatching could look like this (some cases omitted):

'''
Term Value = «interval.lower_bound.asString», «interval.upper_bound.asString»
'''

def dispatch asString(IntLiteral intLiteral) {
  return String.valueOf(intLiteral.literal)
}
def dispatch asString(DoubleLiteral it) {
  return '''«it.integer».«it.decimal»'''
}
Sebastian Zarnekow
  • 6,609
  • 20
  • 23