Variadic functions in Irony

Mar 26, 2013 at 7:43 AM
Edited Mar 26, 2013 at 7:43 AM
Hi Roman

I am using Irony to parse simple functions of the form "foo(x1, x2)". Until now, the number of function parameters has been known when the grammar was set created.

I would like to use variadic functions. My approach has been (simplified, without AST Node creation):
var argument = new StringLiteral("argument");

var openParameterList = new NonTerminal("openParameterList");
openParameterList = argument | argument + ", " + openParameterList;

var function = new NonTerminal("function").
function.Rule = ToTerm("foo") + "(" + openParameterList + ");

this.MarkTransient(function);
Is this the correct way or would you recommend another way?

Thank you for your help.

Cheers Ivan
Coordinator
Mar 26, 2013 at 6:29 PM
it's better to use MakeStarList function for openParameterList, see sample grammars for examples
Apr 4, 2013 at 9:04 AM
Thanks a lot for the help. Parsing works like a charm.

During testing, I observed that no ast nodes are created for the subtree of the openParameterList. Is this behavior correct?

I will have a look at the code.

Cheers Ivan
Apr 4, 2013 at 10:17 AM
I found the the following behavior in the class AstBuilder:
If a node is flagged with "TermFlags.NoAstNode", no AstNode for itsself and its child nodes are created.

I expected the following behavior:
If a node is flagged with "TermFlags.NoAstNode", no AstNode for itsself is created. AstNodes for child nodes are created.

Did I miss the point?
Coordinator
Apr 5, 2013 at 7:14 AM
yep, the point is - who will own these child AST nodes? who will be parent if parse node's term is marked NoAstNode.
You might argue that maybe grandparent's Ast node might own these grandchildren, but it gets messy. The idea of NoAstNode flag is to tell to parser 'do not create node automatically, it's parent (AST node of the parent) will probably take care of this and will just use the information directly'.
Like BinaryOp node - it's middle child holding operation symbol is not a real AST node, so it might not have one - the BinaryOpNode would just use the symbol (+ or -) to setup itself.
Apr 5, 2013 at 8:53 AM
Good Morning

Thanks for the help. Ah, I missed that one:-). As you already pointed out, I would like to argue about
grandparent's Ast node might own these grandchildren.
but I agree that this approach would be messy.

Going back to my code example in the first post. I changed the grammar in the following way:
var argument = new StringLiteral("argument");

var openParameterList = new NonTerminal("openParameterList");
openParameterList .Rule = this.MakeStarRule(openParameterList, this.ToTerm(",", "Comma"), argument );

var function = new NonTerminal("function").
function.Rule = ToTerm("foo") + "(" + openParameterList + ");

this.MarkTransient(function, openParameterList);
Shouldn't the parse node "openParameterList" be replaced by its children?

Cheers Ivan
Apr 5, 2013 at 9:06 AM
The idea of NoAstNode flag is to tell to parser 'do not create node automatically, it's parent (AST node of the parent) will probably take care of this and will just use the information directly'.
If I correctly understand this statement, the ast node of function "foo" in my previous example is responsible for creating ast nodes for its argument using the "openParameterList" as intermediate node for accessing its children .

How would I achieve that?
Coordinator
Apr 5, 2013 at 5:44 PM
Just look at expression evaluator or miniPython sample, it's all there