I want to parse function calls that can call functions 'returned' by other functions:
thisReturnsFunction()()
I have done this:
id = Regex(r'[_a-zA-Z][_a-zA-Z0-9]*')
funcal = Forward()
value = funcal | id
funcal << value + Literal("(").suppress() + Literal(")").suppress()
Accidentally, Python stack overflows and the whole Python crashes.
I know that the value
is recursive and matches infinitely thisReturnsFunction("Hello!")
, because the funcal
is matching, because the value
is mathing, because the funcal
is matching...
How to avoid that and design valid pattern??
I am not sure, that Pyparsing can parse high-order function in 'pure' way. But there are some 'hacky' way: create token for function, add handler for the token and make correct syntax structure inside the handler. Example (I also add tokens for function arguments)
It produces: