compiler AST

Phil Frost indigo at
Sun Oct 3 18:09:21 CEST 2004

The first comment in the source is not the docstring; iff the first
statement in the module is a string, this is the docstring. Comments are
delimited by # and \n. Strings are in quotes.

AST does not parse comments, but it does parse docstrings. There are
available in the .doc attribute of nodes that can have them.

On Sun, Oct 03, 2004 at 05:50:31PM +0200, Avner Ben wrote:
> Hi,
> I am working on a reverse-engineering system that relies on comments. I 
> have taken a look at the "compile" module in the Python library. Quite 
> expectedly, the AST does not contain comment lines. The only exception I 
>  have noticed is the very first comment in the source, which is 
> interpreted as the module's docstring and is surprisingly also stored in 
> the AST as a "discardable"-type node. This leaves me with the dismal 
> task of reading the source on my own, parsing the comments and then 
> matching them with the AST through the lineno node attribute. Which is a 
> waste, because the compiler has been there before me. (For my own 
> reasons, I had rather use comments than resort to such esoteric but 
> compiler-friendly means as dummy variables etc.)
> My question: Is there a way to configure the Python compiler to keep All 
> comments from the source as AST nodes (e.g. "discardable")?

More information about the Python-list mailing list