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 matchedSymbol
to thename
attr of the class. In your case the attachment ofb
overwritesa
.The official doc lists the following:
Here each
Parameter
has aname
. TheParameters
looks for thename
attribute of the matchedParameter
and put it into is internaldict
.