[Pyparsing] Not sure why I'm not getting the correct tokens
Brought to you by:
ptmcg
From: Kevin <kcc...@gl...> - 2010-04-19 18:06:25
|
Hi all: Been working with pyparsing for a few days now and need some help with the following I have a file which is read on startup to either create or create and populate classes The following would be a valid input file Class1::Instance1 Class1::Instance2 Class1::Instance3 { variable1 = TRUE variable2 = FALSE variable3 = { {"valuename1", 10}, {"valuename2", 20} } variable4 = { {10, "valuename3"} } variable5 = "variable5value" } Class1::Instance4 Class2::Instance1 etc, etc. My parser looks as follows def importFile_BNF(): global importBNF # if importBNF: # We've been here before. Don't do it again # return importBNF # Literals not to be stored LCB, RCB, COMMA, DQT = map(Suppress, '{},"') DCLN = Suppress(Literal('::')) comment = Suppress(Literal('#') + Optional(restOfLine)) assign = Suppress(Literal('=')) insert = Suppress(Literal('+=')) remove = Suppress(Literal ('-=')) operation = assign ^ insert ^ remove classIdentifier = Word(alphas , alphanums + '_') instanceIdentifier = Word(alphas, alphanums + '_-') name = DQT + instanceIdentifier + DQT order = Word(nums) driverEntry = LCB + name + order + RCB valueList = LCB + OneOrMore(driverEntry) + Optional(COMMA) + RCB statement = instanceIdentifier + assign + valueList identifier = Group(classIdentifier + DCLN + instanceIdentifier) stanzaBody = LCB + OneOrMore(statement) + RCB stanza = identifier + Optional(stanzaBody) stanza.setDebug() importBNF = ZeroOrMore(stanza) importBNF.setDefaultWhitespaceChars(' \t\r') importBNF.ignore(comment) importBNF.ignore(blankline) return importBNF What happens when I run this is that when I reach the LCB on Class1::Instance3 I get an exception that says "Expected W:(abcd.....) (at char XXX), (line:YY, col ZZ) Having the stanza body marked optional should allow the opening LCB to be parsed, but it appears that the parser is insisting that I only do 'definitions' and will not allow me to populate the class. Any help is greatly appreciated. Kevin |