Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Optional arguments
Optional arguments [message #1009506] Fri, 15 February 2013 08:47 Go to next message
Gaspar Din
Messages: 21
Registered: September 2012
Junior Member
Hi,

in my grammar there is a definition Func which can hold 0 to n params.
Simplified xtext fragment:
Func: {Func} '(' ( params+=INT(',' params+=INT)* )? ')' ';';


This allows to edit code like:
Func(4, 5, 10);


Now I want some parameters to be optional to edit code like:
Func (7, , 15);


My question:
How can I change the grammar to omit single params? It then must be possible accessing the values where the omitted values keep some empty/null value like:
params[0] // value is 7
params[1] // value is empty/null
params[2] // value is 15

Therefore, I can not make the single params completely optional like
(params+=INT)? // this would not set an empty/null value to an omitted param

Any suggestions?
Thanks in advance,
Gaspar

Re: Optional arguments [message #1009585 is a reply to message #1009506] Fri, 15 February 2013 10:50 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander Nittka
Messages: 1156
Registered: July 2009
Senior Member
Hi,

(params+=INT)? makes the assignment optional, not only the text

params+=INT? should work

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Optional arguments [message #1009689 is a reply to message #1009585] Fri, 15 February 2013 14:28 Go to previous messageGo to next message
Gaspar Din
Messages: 21
Registered: September 2012
Junior Member
Hi Alexander,


first, thank you for the quick answer.

But unfortunately, when I define
Func: {Func} '(' ( params+=INT(',' params+=INT?)* )? ')' ';';
it is really possible editing
Func (7, , , 15);
but calling func.getParams() only holds the two values 7 and 15.
The size of func.getParams() is two.

But how can I find out at which position the arguments are located?


Regards,
Gaspar
Re: Optional arguments [message #1009694 is a reply to message #1009689] Fri, 15 February 2013 14:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6325
Registered: July 2009
Senior Member
Hi,

what about

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

Func: {Func} '(' ( params+=INT( params+=OINT)* )? ')' ';';
OINT returns ecore::EInt: ',' INT?;

and a custom valueconverter that handles the "null/strip away the comma" problem

[Updated on: Fri, 15 February 2013 14:41]

Report message to a moderator

Re: Optional arguments [message #1010831 is a reply to message #1009694] Mon, 18 February 2013 10:04 Go to previous message
Gaspar Din
Messages: 21
Registered: September 2012
Junior Member
Hi Christian,

I've tested with the OINT and it works for me!

Thank you
Gaspar
Previous Topic:Problems with Cross-Reference resolving in standalone app
Next Topic:XText project from XSD
Goto Forum:
  


Current Time: Tue Sep 30 11:50:14 GMT 2014

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

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