I have a python pyparsing language grammar where the language allows both /*... */ and // style comments. Currently a regex filters the comments out before calling the parser.
The problem I am trying to solve is that I want to make the system use the comments to automatically generate documentation similar to Autodoc. My thought for a solution is, instead of pre-filtering out comments, I want to add them to the pyParsing definition of white-space. But then how would I retrieve the comments in order to associate them with (for example) the class or function definition that follows in the text?
I need to retrieve all the comments, even the ones that are not associated with classes or functions. Is there a way to do that? Or is there a better solution that I'm not seeing?
EDIT: By "adding to the definition of white space" I mean using the "ignore(expr)" function.
The problem with parsing comments as part of your syntax is that they can crop up absolutely anywhere. If they aren't ignored, then you have to insert them throughout your parser.
What you might do is use two passes, one for the syntax elements and one for the comments, and capture the locations of each using locatedExpr or parse actions, or use scanString (which returns the start and end of each matched location). Then match up the locations of the comments and the important syntax elements.