Discussion forum for David Beazley

SLY - Multiple arguments


#1

Hello I would like to know how to implement non-static arguments!
An example:

Static statement /
@_(‘PRINT “(” STRING “)”’)
def statement(self, p):
return (‘print_stmt’, STRING)

cmd>print(“Hello”)
returns -> (‘print_stmt’, “Hello”)

And I would like it to return -> (‘print_stmt’, ‘Hello’, ‘world’, ‘a’, ‘123’) if print(arg1, arg2, arg*) is called!

Thank you, Roberto!


#2

If you want to collect multiple things, you need to write parsing rules for it. For example:

@_('PRINT "(" things ")"')
def statement(self, p):
      return ('print_stmt', *p.things)

@_('things COMMA thing')
def things(self, p):
      p.things.append(p.thing)
      return p.things

@_('thing')
def things(self, p):
     return [ p.thing ]

Cheers,
Dave