Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [XTEND] Code generation
[XTEND] Code generation [message #775657] Fri, 06 January 2012 13:26 Go to next message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
Hi,

I have a grammar that looks something like this (simplified):

Y:
('optional')? 'X' name = ID ...


In my template I would like to add optional to the generated file if this word
is present in the source file, e.g.:

optional X somename

should yield:

optional X somename

in the generated file as well. However, is it possible to test whether optional is used in the source file? This would probably appear at ??? below.

def compile( Y y )
'''
??? X <<y.name>>
'''

Any idea guys? Or would I need to rewrite the grammar?

Thanks!

[Updated on: Fri, 06 January 2012 13:27]

Report message to a moderator

Re: [XTEND] Code generation [message #775660 is a reply to message #775657] Fri, 06 January 2012 13:31 Go to previous messageGo to next message
Ingo Meyer is currently offline Ingo MeyerFriend
Messages: 162
Registered: July 2009
Senior Member
You can rewrite the grammar to
Y:
(optional ?= 'optional')? 'X' name = ID ...


and you get a boolean feature.

I think getting that information out of the parser/lexer is much more complicated, so this would be the fastest solution if you can change the grammar.

Ingo
Re: [XTEND] Code generation [message #775681 is a reply to message #775660] Fri, 06 January 2012 14:10 Go to previous messageGo to next message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
Thanks Sir for your ultra-rapid reply! And your solution works excellent! Smile

A small follow-up (not that related though).

I have this rule (again very simplified):

Var:
  name = ('XVar', 'YVar', 'ZVar')


In the template I want to add the value of name to the target file (either of the three alternatives as they appear in the source file), like this:

def compile( Var v )
'''
«v.name»
'''


However, I only get the address to the object of the Var class. The XTend API has a method getName() that returns this name, but I want the name of the name attribute (of the grammar) written to the file, not the name of the object. Any suggestion?

[Updated on: Fri, 06 January 2012 14:11]

Report message to a moderator

Re: [XTEND] Code generation [message #775717 is a reply to message #775681] Fri, 06 January 2012 15:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i do not understand this. can you show us a sample model and a sample output you want to have?
if you want to do some reflection/introspection you can ask every EObject for its eClass
and the EClass for its EStructuralFeatures

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [XTEND] Code generation [message #775732 is a reply to message #775717] Fri, 06 January 2012 15:51 Go to previous messageGo to next message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
Hi Christian,

I'll try to exemplify this. It is probably a simple thing for you pros. Smile

First the grammar:

Class:
  '{' ... '}'

Variable:
  'var' name = ID '->' type = ClassOrType

ClassOrType:
  type = [Class] | Type

Type:
  name = ('int' | 'float')


Input file:
class X {}
   
var varA -> int
var varB -> X


Desirable output file:
int varA
X varB


In text, I would like to generate a file where the type of variable is put in front of the variable name. It may be that my grammar is incorrect?

[Updated on: Fri, 06 January 2012 15:53]

Report message to a moderator

Re: [XTEND] Code generation [message #775743 is a reply to message #775732] Fri, 06 January 2012 16:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

let us say your grammar is a bit "strange since the or (|) has implications on the type hierarchy too in your case

never the less you can test in your case if the ClassOrType is a Type and then ask it for its name or if not for its type and the type for its name

maybe a grammar like:

ClassOrType:
ClassRef | Type;

Type:
name = ('int' | 'float');

ClassRef: type = [Class] ;

makes it more easy for you.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [XTEND] Code generation [message #775758 is a reply to message #775743] Fri, 06 January 2012 16:33 Go to previous messageGo to next message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
Splendid! Thanks very much! Smile Works great now!

Now I only have one small issue to resolve: when the code is generated and added to my target file, unwanted
new lines are introduced. For instance, I get something like this:

op( int param1, 
    float param2,
  )


when it would be cleaner with:

op( int param1, float param2 )


Is there a fix for this?

[Updated on: Fri, 06 January 2012 16:34]

Report message to a moderator

Re: [XTEND] Code generation [message #775783 is a reply to message #775758] Fri, 06 January 2012 17:05 Go to previous messageGo to next message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
Update: this happens when the compile(...) methods are invoked. How can these new lines be removed? Smile
Re: [XTEND] Code generation [message #775789 is a reply to message #775758] Fri, 06 January 2012 17:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi you have to format the xtend file right

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [XTEND] Code generation [message #775803 is a reply to message #775789] Fri, 06 January 2012 17:39 Go to previous messageGo to next message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
I see. But if I have methods for generating operation and parameter code like this:

def compile( Operation o )
'''
(«FOR p : o.params» «p.compile» «ENDFOR»)
'''

def compile( Param p )
'''
«p.type.compile» «p.name»
'''


How can I format it differently so that new lines don't appear in the parameter list?

[Updated on: Fri, 06 January 2012 17:39]

Report message to a moderator

Re: [XTEND] Code generation [message #775831 is a reply to message #775803] Fri, 06 January 2012 18:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
def compile( Param p )
'''«p.type.compile» «p.name»'''


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [XTEND] Code generation [message #775856 is a reply to message #775831] Fri, 06 January 2012 19:55 Go to previous message
Peter Johnsen is currently offline Peter JohnsenFriend
Messages: 60
Registered: January 2012
Member
Thanks very much for being patient with a xtext/template newbie and your very rapid and correct answers! Have a great weekend! Smile
Previous Topic:ResourceDescriptionsProvider
Next Topic:label provider
Goto Forum:
  


Current Time: Fri Apr 26 12:47:19 GMT 2024

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

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

Back to the top