[Pyparsing] Bug report: setResultsName works inconsistently on Optional in Each
Brought to you by:
ptmcg
From: Max R. <max...@gm...> - 2015-11-09 02:14:58
|
It seems that Optional.setResultsName joined by Each does not behave consistently with other ParserElements. For example: >>> (Optional('foo')('one') & pp.Optional('bar')('two')).parseString('foo bar') (['foo', 'bar'], {}) >>> (Optional('bar')('two') & Optional('foo')('one')).parseString('foo bar') (['foo', 'bar'], {'two': [('bar', 1)]}) A workaround is to name the Literals themselves instead of the Optionals: >>> (Optional(Literal('bar')('two')) & Optional(Literal('foo')('one'))).parseString('foo bar') (['foo', 'bar'], {'two': [('bar', 1)], 'one': [('foo', 0)]}) >>> (Optional(Literal('foo')('one')) & Optional(Literal('bar')('two'))).parseString('foo bar') (['foo', 'bar'], {'two': [('bar', 1)], 'one': [('foo', 0)]}) The problem does not manifest when joining Optional objects with And: >>> (Optional('foo')('one') + pp.Optional('bar')('two')).parseString('foo bar') (['foo', 'bar'], {'two': [('bar', 1)], 'one': [('foo', 0)]}) Thanks, Max |