pyparsing: odd listAllMatches behavior

Peter Fein pfein at
Fri Mar 11 17:59:30 CET 2005

I'm trying to write a parser for a simple query language (for a web
search form).  Things are ok, except that if the first match is an
OR_clause, I only get one token back when I access by name, instead of
a ParseResults.  See below.  TIA!

from pyparsing import *

class Downcase(TokenConverter):
    """Converter to lower case all matching tokens."""
    def postParse( self, instring, loc, tokenlist ):
        return map( string.lower, tokenlist )


phrase=Combine(Literal('"').suppress() + OneOrMore(word) +
               adjacent=False, joinString=" ")

OR_clause=Literal('OR').suppress() + (word.setResultsName("OR_word",
listAllMatches=True) ^

term=OR_clause ^ \
      word.setResultsName('word', listAllMatches=True) ^ \
      phrase.setResultsName('phrase', listAllMatches=True)


Python 2.3.4 (#1, Sep 20 2004, 15:34:06) 
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on
Type "help", "copyright", "credits" or "license" for more information.
>>> ## working on region in file /usr/tmp/python-a7Bl5m...
>>> r=query.parseString('foo OR bar')
>>> r
(['foo', 'bar'], {'OR_word': [('bar', 1)], 'word': [('foo', 0)]})
>>> r.OR_word
(['bar'], {})
>>> r.word
(['foo'], {})
>>> r=query.parseString('OR bar foo')
>>> r
(['bar', 'foo'], {'OR_word': [('bar', 0)], 'word': [('foo', 1)]})
>>> r.OR_word
>>> r.word

Peter Fein                 pfein at                 773-575-0694

Basically, if you're not a utopianist, you're a schmuck. -J. Feldman

More information about the Python-list mailing list