I have the following syntax:
kv = Word(alphanums + '_') | QuotedString('"', escQuote="\\") | QuotedString("'", escQuote="\\")
kv = kv.setResultsName('literal', listAllMatches=True)
cskv = kv + Optional(ZeroOrMore(Suppress(',') + kv)) # comma separated kv
and with this example:
>>> res=cskv.parseString('a,b,c,d,e')
>>> res
(['a', 'b', 'c', 'd', 'e'], {'literal': [('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4)]})
>>> res[0]
'a'
>>> type(res[0])
<type 'str'>
Note:
>>> type(res[0])
<type 'str'>
I really want him to be ParseResultsinstead of a string so that I can go res[0].getName()to him, and hopefully I should get one literal.
Second question: how can I get the index of some token?
let's say I want to know the index literal d, it must return 3.
this is trivial in this example, since I have only one type of token, but in my problem I need to know the relative position of different types of tokens so that it helps during processing.
any way to achieve them?
EDIT:
I do not know why my question is so confusing, and why people do not focus on my example, and not on my question. following explanation:
pyparsing , , , , , - , , ( , ):
variable = string
- , , , , , ( , , ).
:
expr = Word(alphanums+'_')('leftval') + '=' + Word(alphanums+'_')('rightval')
, :
In [3]: res = expr.parseString('variable = string')
In [4]: res
Out[4]: (['variable', '=', 'string'], {'rightval': [('string', 2)], 'leftval': [('variable', 0)]})
:
In [5]: res[0]
Out[5]: 'variable'
In [6]: type(res[0])
Out[6]: str
res . , , , , leftval.
, __repr__ of res :
, - , res.
, ?
. .