1

From here I have this code to parse a function call:

functionName = Word(alphanums + '_')
functionBody = Forward()
functionBody <<= functionName + (Literal("(") + Optional( delimitedList ( functionBody | Word(alphanums + '_') | "''"),'') + Literal(")"))

But, when calling:

result = functionBody.parseString('function(param1,param2,param3)')

I got this result:

['function', '(', 'param1', 'param2', 'param3', ')']

Is there any way to get this result instead:

['function(param1, param2, param3)']

That is to say: parsing the function call is well written, but returning it as string instead of as an array without using the Python join sentence?

assembler
  • 3,098
  • 12
  • 43
  • 84
  • 1
    "...without using the Python join sentence?" and why is that? I believe that is the best way. – Jaideep Shekhar Jan 13 '20 at 13:18
  • @JaideepShekhar, I am using this code snippet within another expression, and pyparsing returns me nothing but errors if I don't call it as string. – assembler Jan 13 '20 at 13:26
  • 1
    You have 3 options for this: `originalTextFor`, `Combine`, or `function_body.addParseAction(''.join)`. Do some experimenting with these, and see which you like the best. – PaulMcG Jan 13 '20 at 21:53

1 Answers1

0

I thank Mr. PaulMcG, since he gave three possible answers in his comment, but I am posting a solution for somebody who might be needing it as well. originalTextFor did the trick for me:

functionName = Word(alphanums + '_')
functionBody = Forward()
functionBody <<= originalTextFor(functionName + (Literal("(") + Optional( delimitedList ( functionBody | Word(alphanums + '_') | "''"),'') + Literal(")")))

Now it is returning:

['function(param1,param2,param3)']
assembler
  • 3,098
  • 12
  • 43
  • 84