pyparsing: odd listAllMatches behavior

Peter Fein pfein at pobox.com
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 )

word=Downcase(Word(alphas))

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

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

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

query=OneOrMore(term)


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
linux2
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
'bar'
>>> r.word
'foo'

-- 
Peter Fein                 pfein at pobox.com                 773-575-0694

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



More information about the Python-list mailing list