@@ -113,8 +113,6 @@ public final class PythonTreeTranslator extends Python3BaseVisitor<Object> {
113113 protected final AssignmentTranslator assigns ;
114114 protected final Source source ;
115115 protected final String name ;
116- protected String moduleDoc ;
117- protected boolean firstStatement = true ;
118116
119117 protected final boolean isInlineMode ;
120118
@@ -234,7 +232,7 @@ public Object visitFile_input(Python3Parser.File_inputContext ctx) {
234232 ExpressionNode file = asExpression (super .visitFile_input (ctx ));
235233 deriveSourceSection (ctx , file );
236234 environment .popScope ();
237- return factory .createModuleRoot (name , moduleDoc , file , ctx .scope .getFrameDescriptor ());
235+ return factory .createModuleRoot (name , getModuleDoc ( ctx ) , file , ctx .scope .getFrameDescriptor ());
238236 }
239237
240238 @ Override
@@ -258,10 +256,34 @@ public Object visitSingle_input(Python3Parser.Single_inputContext ctx) {
258256 if (isInlineMode ) {
259257 return body ;
260258 } else {
261- return factory .createModuleRoot ("<expression>" , moduleDoc , body , ctx .scope .getFrameDescriptor ());
259+ return factory .createModuleRoot ("<expression>" , getModuleDoc ( ctx ) , body , ctx .scope .getFrameDescriptor ());
262260 }
263261 }
264262
263+ private String getModuleDoc (ParserRuleContext ctx ) {
264+ Python3Parser .Simple_stmtContext firstStatement = null ;
265+ if (ctx instanceof Python3Parser .Single_inputContext ) {
266+ firstStatement = ((Python3Parser .Single_inputContext ) ctx ).simple_stmt ();
267+ } else if (ctx instanceof Python3Parser .File_inputContext ) {
268+ List <Python3Parser .StmtContext > stmt = ((Python3Parser .File_inputContext ) ctx ).stmt ();
269+ if (!stmt .isEmpty ()) {
270+ firstStatement = stmt .get (0 ).simple_stmt ();
271+ }
272+ }
273+
274+ if (firstStatement != null ) {
275+ try {
276+ PNode stringNode = parseString (new String []{firstStatement .getText ().trim ()});
277+ if (stringNode instanceof StringLiteralNode ) {
278+ return ((StringLiteralNode ) stringNode ).getValue ();
279+ }
280+ } catch (Exception ignored ) {
281+ // not a string literal
282+ }
283+ }
284+ return null ;
285+ }
286+
265287 @ Override
266288 public Object visitAtom_expr (Python3Parser .Atom_exprContext ctx ) {
267289 // TODO: deal with AWAIT
@@ -1286,14 +1308,7 @@ private LoopNode createForInScope(StatementNode target, ExpressionNode iterator,
12861308 @ Override
12871309 public Object visitExpr_stmt (Python3Parser .Expr_stmtContext ctx ) {
12881310 if (ctx .normassign ().isEmpty () && ctx .annassign () == null && ctx .augassign () == null ) {
1289- Object exprNode = super .visitExpr_stmt (ctx );
1290- if (firstStatement ) {
1291- firstStatement = false ;
1292- if (exprNode instanceof StringLiteralNode ) {
1293- moduleDoc = ((StringLiteralNode ) exprNode ).getValue ();
1294- }
1295- }
1296- return exprNode ;
1311+ return super .visitExpr_stmt (ctx );
12971312 } else {
12981313 return assigns .translate (ctx );
12991314 }
0 commit comments