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

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

  •  26-06-2022
  •  | 
  •  

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?

有帮助吗?

解决方案 2

How about:

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

其他提示

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 ', '»
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top