Search code examples
dslxtextxtend

Comma separated variable list in Xtext


I have a grammar for a specific DSL. Here is a snippet (written in Xtext):

Vars: 'var' (vars += Var) 
Var: ID (',' ID) * ':' Type ';'

And here is an example input:

var
  a,b,c : int;
  d,e: bool;

I'm really interested in Xtend automatic code generation option and want to have a single object for every Variable, storing it's id and it's type. Using Xtex grammar synatx all I can do is :

Var: ids+=ID (',' ids+=ID)* ':' type =[Type] ';'

Meaning that I may have more than one ID in a single object. How can I store each 'a','b','c' in a single object?


Solution

  • It's not possible to store each variable together with it's type. You'd have to implement some logic that traverses your model to find the type. You could do this with derived properties on the Var itself.