Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [xtend2] Creating a generator
[xtend2] Creating a generator [message #754890] Fri, 04 November 2011 19:39 Go to next message
Peter  is currently offline Peter Friend
Messages: 6
Registered: October 2011
Junior Member
Hey,

It's probably a simple answer to this, but here goes. For a grammar that looks like this:

Model:
(elements += AbstractElement)*
;

AbstractElement:
PackageDeclaration | Class ;

PackageDeclaration:
'package' name = QualifiedName
'{'
(elements += AbstractElement)*
'}'
;

Class:
class' name = ID ('inherits' superType = [Class | QualifiedName]
(features += Feature)*
;

How can I refer to the compile function for PackageDeclaration and Class directly in the .xtend file? (Since AbstractElement is "between" Model and PackageDeclaration / Class.) Note that I start with Model.

The .xtend file looks like this:

def compile( Model m )
'''
«FOR ae : m.elements»
«ae.compile»
«ENDFOR»
'''

def compile( AbstractElement ae )
'''
«var x = o as Class» ???

'''

That is, how can I differentiate between the different abstract elements easily without casting, etc.? As an example, I would like to write the name of classes to file.

Tips are welcomed!
Re: [xtend2] Creating a generator [message #754895 is a reply to message #754890] Fri, 04 November 2011 19:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

what about using some Xtend "magic"

def compile( Model m )
'''
«FOR ae : m.elements»
«ae.compile»
«ENDFOR»
'''

def dispatch compile( PackageDeclaration p )
'''
'''

def dispatch compile( org.xtext.example.mydsl.myDsl.Class p )
'''
'''


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [xtend2] Creating a generator [message #754912 is a reply to message #754895] Fri, 04 November 2011 21:43 Go to previous message
Peter  is currently offline Peter Friend
Messages: 6
Registered: October 2011
Junior Member
Christian Dietrich wrote on Fri, 04 November 2011 15:53
Hi,

what about using some Xtend "magic"

def compile( Model m )
'''
«FOR ae : m.elements»
«ae.compile»
«ENDFOR»
'''

def dispatch compile( PackageDeclaration p )
'''
'''

def dispatch compile( org.xtext.example.mydsl.myDsl.Class p )
'''
'''


~Christian


It was exactly something like this I was looking for! Thanks very much for your quick and precise help!
Previous Topic:multiple grammar mixin troubles
Next Topic:[xtend2] assert
Goto Forum:
  


Current Time: Thu Apr 25 23:54:30 GMT 2024

Powered by FUDForum. Page generated in 0.02964 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top