I am using pyPEG to parse the declaration of a function. Currently I have this written:
from pypeg2 import attr, \
optional, \
csl, \
name, \
List, \
Namespace
class Parameters(Namespace):
grammar = optional(csl(name()))
class Function(List):
grammar = "def", name(), \
"(", attr("params", Parameters), "):"
So for example if I do:
>>> import pypeg2
>>> f = pypeg2.parse("def f(a, b):", Function)
I would expect f.params to contain a and b as parameters. Instead:
>>> f.params
Parameters([], name=Symbol('b'))
only b is found. Why is b the only found symbol?
Because
name()essential attach a matchedSymbolto thenameattr of the class. In your case the attachment ofboverwritesa.The official doc lists the following:
Here each
Parameterhas aname. TheParameterslooks for thenameattribute of the matchedParameterand put it into is internaldict.