Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Creating functions with named arguments
Creating functions with named arguments [message #1044131] Thu, 18 April 2013 14:28 Go to next message
Erick Fonseca is currently offline Erick FonsecaFriend
Messages: 68
Registered: December 2011
Member
I'm trying to define functions which accept named parameters, like in Python. For example:

function f(x, y):
    // something

// you can call f like this
f(1, 2)

// or naming the arguments
f(x=1, y=2)
f(y=2, x=1)
f(1, y=2)

// but you can't supply a non named argument after a named one
f(x=1, 2) // wrong



I came up with these rules for the function:
Function:
    'function' name=ID '(' parameters+=Parameter (',' parameters+=Parameter)* ')' ':'
    statements += Statement+
    'end'
;

Parameter:
    name = ID
;

FunctionCall:
    function=[Function] '(' (paramNames+=[Parameter] '=' )? paramValues+=INT (',' (paramNames+=[Parameter] '=' )? paramValues+=INT )* ')'
;


I also added a ScopeProvider to to provide the parameter names.

Now, how can I enforce the condition that once you give a named parameter in a function call, all subsequent parameters must also be named?
Re: Creating functions with named arguments [message #1044232 is a reply to message #1044131] Thu, 18 April 2013 16:43 Go to previous messageGo to next message
Hallvard Traetteberg is currently offline Hallvard TraettebergFriend
Messages: 673
Registered: July 2009
Location: Trondheim, Norway
Senior Member
Some rules/condition are best "enforced" by the validator, not the grammar.

Hallvard

On 18.04.13 07.28, Erick Fonseca wrote:
> I'm trying to define functions which accept named parameters, like in
> Python. For example:
>
>
> function f(x, y):
> // something
>
> // you can call f like this
> f(1, 2)
>
> // or naming the arguments
> f(x=1, y=2)
> f(y=2, x=1)
> f(1, y=2)
>
> // but you can't supply a non named argument after a named one
> f(x=1, 2) // wrong
>
>
>
> I came up with these rules for the function:
>
> Function:
> 'function' name=ID '(' parameters+=Parameter (','
> parameters+=Parameter)* ')' ':'
> statements += Statement+
> 'end'
> ;
>
> Parameter:
> name = ID
> ;
>
> FunctionCall:
> function=[Function] '(' (paramNames+=[Parameter] '=' )?
> paramValues+=INT (',' (paramNames+=[Parameter] '=' )? paramValues+=INT
> )* ')'
> ;
>
>
> I also added a ScopeProvider to to provide the parameter names.
>
> Now, how can I enforce the condition that once you give a named
> parameter in a function call, all subsequent parameters must also be named?
Re: Creating functions with named arguments [message #1044247 is a reply to message #1044232] Thu, 18 April 2013 17:05 Go to previous messageGo to next message
Erick Fonseca is currently offline Erick FonsecaFriend
Messages: 68
Registered: December 2011
Member
I agree, but I don't know how to do it from the validator. I need to verify which paramValues had a paramName before them, but I don't see how.
Re: Creating functions with named arguments [message #1044303 is a reply to message #1044247] Thu, 18 April 2013 18:44 Go to previous messageGo to next message
Steve Kallestad is currently offline Steve KallestadFriend
Messages: 62
Registered: March 2013
Member
I am not doing this myself, but I would take a look at NodeModelUtils getPreviousSibling and getNextSibling functions. Maybe start with the function node/eobject, And from there get the child parameter list. Iterate through them. When you find a named parameter, validate that all consecutive parameters are also named.
Re: Creating functions with named arguments [message #1044655 is a reply to message #1044303] Fri, 19 April 2013 07:11 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

NodeModelUtils is relevant if you *need* to access the parse model. In this use case this is not necessary, you can do everything with the semantic one. You could write a validation for a function call, iterate over all parameters and check whether they all have the same type.

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: Creating functions with named arguments [message #1044973 is a reply to message #1044655] Fri, 19 April 2013 15:24 Go to previous messageGo to next message
Erick Fonseca is currently offline Erick FonsecaFriend
Messages: 68
Registered: December 2011
Member
Well, I actually found an easier solution based on the grammar, defining rules for named and non-named (positional) arguments:

FunctionCall:
    function=[Function] '(' args=ArgList ')'
;

ArgList:
    positionalArgs+=PositionalArgument (',' positionalArgs+=PositionalArgument)* (',' namedArgs+=NamedArgument)* |
    namedArgs+=NamedArgument (',' namedArgs+=NamedArgument)*
;

NamedArgument:
    paramName=[Parameter] '=' argument=INT
;

PositionalArgument:
    argument=INT
;

Re: Creating functions with named arguments [message #1046117 is a reply to message #1044973] Sun, 21 April 2013 11:35 Go to previous message
Steve Kallestad is currently offline Steve KallestadFriend
Messages: 62
Registered: March 2013
Member
@Erick - Good job, I like it!

@Alexander - nevermind. I figured it out. But good point.

[Updated on: Sun, 21 April 2013 12:00]

Report message to a moderator

Previous Topic:Error with own editor opener
Next Topic:content assist grammar with external lexer
Goto Forum:
  


Current Time: Fri Apr 19 06:11:23 GMT 2024

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

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

Back to the top