Re: [Pyparsing] Variable comment char
Brought to you by:
ptmcg
From: Paul M. <pt...@au...> - 2011-09-26 12:58:44
|
That *does* seem weird, that the comment character can be changed on the fly like that. I would use a self-modifying expression, similar to what you see in the pyparsing code for matchPreviousLiteral or countedArray. In this case, you would define the comment expression as a Forward() and initialize it to the default comment character (let's say it's '#'). comment = Forward() comment << '#' + restOfLine Use expr.ignore(comment) just as you normally would. Then define your comment redefinition expression as: redefine_comment = "[Comment char]" + oneOf(list(printables))("newchar") + "_char" And attach a parse action to redefine_comment like this: def redefine_current_comment_char(tokens): comment << tokens.newchar + restOfLine I've never tried dynamically modifying an expression that gets ignored, you might have to do something else like Ralph Corderoy suggested, breaking up the input text into multiple "[Comment char]"-controlled blocks, parse each one separately, and then merge the results back together. Fortunately, the merging of results is very easy now, you can just put them into a list and combine them using: all_results = sum(list_of_results) Good luck! -- Paul -----Original Message----- From: Russell Dill [mailto:Rus...@as...] Sent: Sunday, September 25, 2011 9:29 PM To: pyp...@li... Subject: [Pyparsing] Variable comment char I need to parse a rather evil format that has the following line: [Comment Char] |_char Which would change the comment char to '|'. The comment char can be any non-alphanumeric printable other than [ or ] and the comment char ... |