[Pyparsing] Continue parsing on mismatches
Brought to you by:
ptmcg
From: donn <don...@gm...> - 2010-03-07 15:51:19
|
Hi, How would I get PP to *continue* parsing a string, skipping over any mismatched (badly formed) patterns? Raising an exception that I can then continue from would be a bonus. Here's my best effort so far to demo the thing: ----- import pyparsing as PP dot = PP.Literal(".") comma = PP.Literal(",").suppress() ## A single digit like 0 really means 0.0 so the .0 is optional: numberMaybeDotnumber = PP.Combine( PP.Word(PP.nums) + PP.Optional( dot + PP.Word(PP.nums) ) ) ## I have seen Inkscape include E- in the string... e.g 0.8683224E-07, hence all the gumph in floater. floater = PP.Combine(PP.Optional("-") + numberMaybeDotnumber + PP.Optional( PP.Literal("E") + PP.Literal("-") + PP.Word(PP.nums) ) ) floater.setParseAction(lambda toks:float(toks[0])) open_bracket=PP.Literal("(").suppress() close_bracket = PP.Literal(")").suppress() opt_couple = PP.Optional( comma + floater + comma + floater ) ## rotate( angle (,x,y) ) where ,x,y are optional. rotate_command = "rotate" + open_bracket + PP.Group( floater + opt_couple ) + close_bracket matrix_commands = rotate_command phrase_matrix = PP.OneOrMore(PP.Group(matrix_commands)) ## right wrong right ## PP stops here ## with no error. ## I want this too s="rotate(5,5,6) rotate(99,7) rotate(7)" print phrase_matrix.parseString(s) ## swapped wrong/right at end to test. s="rotate(5,5,6) rotate(99) rotate(7,7)" print phrase_matrix.parseString(s) ----- Output: [['rotate', [5.0, 5.0, 6.0]]] [['rotate', [5.0, 5.0, 6.0]], ['rotate', [99.0]]] rotate( arg1 ) is okay. rotate( arg1, arg2, arg3) is okay. rotate( arg1,arg2 ) is wrong. I want to skip over that like it was never there (or raise an warning). (same for rotate( ..empty.. ) and other mad patterns.) Holding thumbs, \d |