[Pyparsing] Choppping up search terms
Brought to you by:
ptmcg
From: Donn I. <don...@gm...> - 2009-06-07 08:46:09
|
Hello pyparsers. As much as pyparsing astounds my small mind, I have yet to write my own 'grammar' without having to ask for help! And here I am again. I hope someone will have mercy on me. I am adding a search function to Fonty Python and this starts with chopping- up a string into tokens. I have a simple set of rules: phrase1 phrase2 field: value1 value2 "value three" field2: value1 etc Any word on it's own if it's NOT after a fieldname is a phrase alone. Anything after a fieldname is a sub-phrase of that field, unless it's another fieldname. I am just not getting anything like what I am looking for. \d My hacking and surfing and re-hacking have ended here: import pyparsing as PP #testing with vers 1.4.8 and 1.5.0 simple = PP.Word(PP.alphas) quoted = PP.dblQuotedString.setParseAction(PP.removeQuotes) single = PP.OneOrMore(simple | quoted) special = PP.Combine(PP.Word(PP.alphas) + ":") + single #term = special | single query = PP.OneOrMore( special | single )# + PP.StringEnd() <- buggy tests=[ u"WTF Huh aField: aValue", # want: [u'WTF', u'Huh', {u'aField:':[u'aValue']}] u'aField : "aValue blah" bloop AnotherField: two', # want: [{u'aField:':[u'aValue blah',u'bloop']},{u'AnotherField:':u'two'}] u"aField: Someval AnotherField: two" # want : [{u'aField:':[u'SomeVal']},{u'AnotherField:':u'two'}] ] for test in tests: print test try: tokens=query.parseString(test) print tokens except: print "BUG" |