Optional function parameter

SyntaxEditor for WPF Forum

Posted 9 years ago by Xuesong Wu
Version: 15.1.0624
Avatar

The language I am working on supports optional parameters. For example, the function call statement below is valid:

myFunction(arg1, ,arg3, arg4);

Note that the 2nd argument is not specified.

If I use the following production rule for functionArgumentList, I can get 3 elements in the AST, but I lose the information about which argument is not specified.

functionArgumentList.Production
= expression["exp"]
+ (@comma + expression["exp"].OnErrorContinue() > AstFrom("exp")).ZeroOrMore().SetLabel("moreexps")
> Ast("FunctionArgumentList", AstFrom("exp"), AstChildrenFrom("moreexps"));

What is the proper way to handle this case?

Thanks,

Xuesong

Comments (2)

Posted 9 years ago by Xuesong Wu
Avatar

If I add Optional() to the second expression["exp"] as follows, I will get only one argument.

functionArgumentList.Production
= expression["exp"]
+ (@comma + expression["exp"].Optional().OnErrorContinue() > AstFrom("exp")).ZeroOrMore().SetLabel("moreexps")
> Ast("FunctionArgumentList", AstFrom("exp"), AstChildrenFrom("moreexps"));

Answer - Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Xuesong,

Maybe what you should do here is make an Argument AST node that has an Expression child node on it.  That way, each match of Expression could generate an Argument, even when the optional Expression was not specified.  In that case, you'd have an Argument with a null Expression child node.


Actipro Software Support

The latest build of this product (v24.1.3) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.