Closed izgzhen closed 5 years ago
The code generators like JavaPrinter
in codegen/java.py
stream their output to a text I/O handle. Their visit
methods do not return values, they write code directly to the handle statefully. You can always just write a comment directly to the handle during code generation when the visitor visits a particular AST node.
Another clean way to do this might be to use SEscape
to produce a no-op statement that just contains a comment, if you really need to put a comment in the middle of a big syntax blob that is being passed elsewhere.
Another benefit of attaching comment directly to the AST node is that the later optimization pass will automatically get rid of the comment, if the node is removed. They are semantically relevant anyway.
Because sometimes I want to add comment to a AST node inside a large AST tree, e.g. the modified part in
codege/java.py
. I don't usually have access to visitor