compiler AST

A

Avner Ben

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")?

Avner.
 
P

Phil Frost

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top