By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

pyparsing: odd listAllMatches behavior

P: n/a
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 pf***@pobox.com 773-575-0694

Basically, if you're not a utopianist, you're a schmuck. -J. Feldman
Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.