XTEND For-Loop indexcontrol in DomainmodelGenerator.xtend (XTEXT codegeneration project)

StackOverflow https://stackoverflow.com/questions/18476464

  •  26-06-2022
  •  | 
  •  

Question

I worked through the Tutorials at eclipse.org/Xtext/documentation and get into expanding these samples. Working with the Domainmodel.xtext sample I generate a Java-Classfile for each entity as stated in the Tut.

The DSL specifies an arbitry number of features, aka class properties:

  Entity:
  'entity' name = ID 
          ('extends' superType = [Entity | QualifiedName])?
   '{'
   (features += Feature)*
   '}'
    ;

In DomainmodelGenerator.xtend then I added code to generate a JAVA-classconstructor. The XTEND-Forloop cycles through all arguements - looks like this:

def compile_Constructors(Entity e) '''
public «e.name.toFirstUpper»
       (
      «FOR f : e.features»
           «f.type.fullyQualifiedName» «f.name.toFirstUpper», 
      «ENDFOR»
        ) 
{}  
'''

Problem With this the last parameter there is still a comma emitted. How can I get control in XTEND over the loopindex, to make the generator to emit legal JAVA code?

Était-ce utile?

La solution 2

How about:

def compile_Constructors(Entity e) '''
    public «e.name.toFirstUpper»
           (
           «e.features.map[type.fullyQualifiedName + ' ' + name.toFirstUpper].join(', ')»
           ) 
    {}
'''

Autres conseils

The «FOR» loop has some options which are quite handy:

  • BEFORE string
  • SEPARATOR string
  • AFTER string

These allows you to emit additional strings before, between and after items. If there are no items (empty list) none of them is emitted.

So in your case just use

«FOR f : e.features SEPARATOR ', '»
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top