rincewind
rincewind

Reputation: 1183

Pyparsing: how to implement special processing of C-style comments?

I want to take advantage of the cStyleComment variable, but rather than just ignoring these comments I want to process them specially. Is there any way to make pyparsing call my handler on the piece of input, which it recognizes as a comment, before it's going to be thrown away?

I'm processing some C code, which contain some "special" directives inside comments.

Upvotes: 1

Views: 1924

Answers (1)

PaulMcG
PaulMcG

Reputation: 63709

There is nothing inherent in any of the xxxStyleComment expressions that are defined in pyparsing that causes them to be ignored. They are there as a convenience, especially since some comment formats are easy to get wrong. They don't get ignored unless you call the ignore method on your larger grammar, as in:

cHeaderParser.ignore(cStyleComment)

(where cHeaderParser might be something you wrote to read through .h files to extract API information, for instance.)

And having pyparsing callback to a handler is built-in, just use cStyleComment.setParseAction(commentHandler). Pyparsing can handle parse actions with any of these signatures:

def commentHandler(inputString, locn, tokens):
def commentHandler(locn, tokens):
def commentHandler(tokens):
def commentHandler():

If your commentHandler returns a string or list of strings, or a new ParseResults, these will be used to replace the input tokens - if it returns None, or omits the return statement, then the tokens object is used. You can also modify the tokens object in place (such as adding new results names).

So you could write something like this that would uppercase your comments:

def commentHandler(tokens):
    return tokens[0].upper()    
cStyleComment.setParseAction(commentHandler)

(a parse action as simple as this could even be written cStyleComment.setParseAction(lambda t:t[0].upper()))

When writing a transforming parse action like this, one would likely use transformString rather then parseString,

print cStyleComment.transformString(source)

This will print the original source, but all of the comments will be uppercased.

Upvotes: 3

Related Questions