Re: [Pyparsing] need help building parsing framework for disability access tool
Brought to you by:
ptmcg
From: Paul M. <pt...@au...> - 2012-09-07 08:59:59
|
Eric - See if this gets you close: =============== from pyparsing import * LBRACK,RBRACK = map(Suppress,'[]') escapedChar = Combine('\\' + oneOf(list(printables))) keyword = Word(alphas,alphanums).setName("keyword")#.setDebug() argword = Word(alphas,alphanums).setName("argword")#.setDebug() arg = Forward() dss = Forward() text = ZeroOrMore(escapedChar | originalTextFor(OneOrMore(Word(printables, excludeChars='[]"\'\\'))) | quotedString | dss) arg << Group(LBRACK + argword("arg") + Group(text)("text") + RBRACK) arg.setName("arg")#.setDebug() dss << Group(LBRACK + keyword("keyword") + Group(ZeroOrMore(arg))("args") + Group(text)("text") + RBRACK) parser = ZeroOrMore(dss) ============== I was able to parse much of the posted sample input you provided, although there seem to be a number of missing terminating ']'s in your sample. Without the matching ']', the parser will not know when a particular dss or arg is supposed to end. If you want to view the progress as the parser works through your input, uncomment one or more of the setDebug() calls. When you process the results from after parsing, please try to make use of the 'keyword', 'args', and 'text' results names, I think they will be helpful. Hope this gets you further in your project! -- Paul |