简体   繁体   中英

Pyparsing ignore except

I have a file, with pythonStyleComments in lines, for example:

def foo(): # declare
    # Simple function
    a = 0 # TODO: add random
    return a

So, then I want to add .ignore(pythonStyleComments) to pyparsing, but want to handle any meta (such as TODO: ). I know all meta words, so how I can exclude this comments from ignoring?

Maybe declare comment as '#' + Regex(), where Regex would be exclude meta words? Or pyparsing has more elegant method?

I would suggest handling this with multiple passes. First, define a pattern for your TODO comments, and use scanString to locate all of these instances. Then run a second pass with your parser, and match up the TODO's with the locations of the elements you locate.

OR (this is totally untested), try attaching a parse action to pythonStyleComment, then do as you normally do and call parser.ignore(pythonStyleComment). If one is matched, and if it matches your TODO format, then save something about that comment and its location off to the side. (I'm not positive that ignored expressions get their parse actions run, so you may have to go with the 2-pass approach.)

I have just declared comment = Literal('#').suppress() + Optional(restOfLine)

and then add it as Optional(comment) to the end of each statement, where it could appear. Then add

def commentHandler(t):
    result = []
    if "fixed" in t[0]:
        result.append("fixed")
    if "TODO: " in t[0]:
        try:
            message = t[0].split("TODO: ")[1].strip()
            result.append(message)
        except Exception as e:
            result.append(t[0])
    return result

comment.setParseAction(commentHandler)

So it works perfectly for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM