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