Skip to main content



      Home
Home » Modeling » TMF (Xtext) » [xtend2] Creating a generator
[xtend2] Creating a generator [message #754890] Fri, 04 November 2011 15:39 Go to next message
Eclipse UserFriend
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 15:53 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: [xtend2] Creating a generator [message #754912 is a reply to message #754895] Fri, 04 November 2011 17:43 Go to previous message
Eclipse UserFriend
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: Sun Jul 13 17:11:58 EDT 2025

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

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

Back to the top