[Pyparsing] Fwd: Returning an actual `list`
Brought to you by:
ptmcg
|
From: Athanasios A. <ath...@gm...> - 2017-10-20 10:08:03
|
Hello everyone
Any ideas on the attached?
All the best
AA
---------- Forwarded message ----------
From: Athanasios Anastasiou <ath...@gm...>
Date: Wed, Oct 4, 2017 at 11:44 AM
Subject: Returning an actual `list`
To: pyp...@li...
Hello
I have set up a very simple "primitive data type" parsing system to parse
numbers, quoted strings, lists and dictionaries. The last two are following
Python's convention.
While this is working, I am having trouble returning specific data types
from the "action" of the LIST definition.
Here is a snippet:
LIST = pyparsing.Forward()
DICT = pyparsing.Forward()
VALUE = (NUMBER|IDENTIFIER|DICT|pyparsing.Group(LIST))
KEY_VALUE_PAIR = pyparsing.Group(IDENTIFIER("key") +
pyparsing.Suppress(":") + VALUE("value"))
LIST << pyparsing.Suppress("[") + pyparsing.delimitedList(VALUE) +
pyparsing.Suppress("]")
DICT << pyparsing.Suppress("{") + pyparsing.delimitedList(KEY_VALUE_PAIR) +
pyparsing.Suppress("}")
So, when you are trying to parse something like: [1,2,3,4,[5,6]], this is
returned as a pyparsing.ParseResults type of object, rather than a `list`.
I have tried to set a simple parseAction with `lambda s,l,t: list(t)` or
`lambda s,l,t:list(t[0])` but I am still getting back a ParseResults object.
Ideally, I would like the rule to return a list, just like the INT rule
returns a proper Python integer.
Any ideas about what am I missing?
All the best
AA
|