I'm currently trying to describe some basic Ruby grammar but I'm now stuck with function definition. Indeed, I don't know how to handle 'n' argument. Here is the code I use to handle functions containing from 0 to 2 args :
rule function_definition
'def' space? identifier space? '(' space? expression? space? ','? expression? space? ')'
block
space? 'end' <FunctionDefinition>
end
How could I do to handle 'n' argument ? Is there any recursive way to do that ?
EDIT :
I wanted to highlight the fact that I need the arguments to be in the result tree. Like :
Argument offset=42, "arg1"
Argument offset=43, "arg2"
Argument offset=44, "arg3"
So I need to do a cstom SyntaxNode Subclass declaration, just like I did for function_definition rule for instance.
You want something like (untested):
(NB if this is a ruby style
def
then the parens are also optional in the case where there are no arguments)Edit to demonstrate extracting the arguments from the parse tree -- here I spit out the
text_value
of each argument (FunctionArg
) syntax node but you could of course do anything:foo.rb:
def.tt:
Output: