python - pyparsing Optional() & Optional() allows repetitions -
i've simple grammar:
word = word(alphanums + '_') with_stmt = suppress('with') + oneormore(group(word('key') + suppress('=') + word('value')))('overrides') using_stmt = suppress('using') + regex('id-[0-9a-f]{8}')('id') modifiers = optional(with_stmt('with_stmt')) & optional(using_stmt('using_stmt')) pattern = stringstart() + modifiers + stringend() it seems optional() & optional() erroneously allows multiple repetitions of either modifier, , labels last one:
>>> print dict(pattern.parsestring('with foo=bar bing=baz using id-deadbeef using id-feedfeed')) { 'with_stmt': ( [ (['foo', 'bar'], {'value': [('bar', 1)], 'key': [('foo', 0)]}), (['bing', 'baz'], {'value': [('baz', 1)], 'key': [('bing', 0)]}) ], {'overrides': [(([ (['foo', 'bar'], {'value': [('bar', 1)], 'key': [('foo', 0)]}), (['bing', 'baz'], {'value': [('baz', 1)], 'key': [('bing', 0)]}) ], {}), 0)] } ), 'overrides': ( [(['foo', 'bar'], {'value': [('bar', 1)], 'key': [('foo', 0)]}), (['bing', 'baz'], {'value': [('baz', 1)], 'key': [('bing', 0)]})], {} ), 'id': (['id-deadbeef', 'id-feedfeed'], {}), 'using_stmt': (['id-deadbeef', 'id-feedfeed'], {'id': [('id-deadbeef', 0), ('id-feedfeed', 1)]}) } using_stmt matches both id-deadbeef , id-feedfeed instead of throwing error @ using id-feedfeed.
strangely enough, if make modifiers non-optional, repitition problem goes away , parsing fails expected:
>>> dict(pattern.parsestring('with foo=bar bing=baz using id-deadbeef using id-feedfeed')) traceback (most recent call last): file "parse.py", line 10, in <module> print dict(pattern.parsestring('with foo=bar bing=baz using id-deadbeef using id-feedfeed')) file "/path/to/lib/python2.7/site-packages/pyparsing.py", line 1139, in parsestring raise exc pyparsing.parseexception: expected end of text (at char 40), (line:1, col:41) switching + instead of & causes fail expected. with_stmt exhibits same problem, , making non-optional fixes it.
what marking pattern optional allows repetition inside each()?
this bug in pyparsing's each class - fixed in 2.0.6.
Comments
Post a Comment