@@ -113,8 +113,6 @@ public final class PythonTreeTranslator extends Python3BaseVisitor<Object> {
113
113
protected final AssignmentTranslator assigns ;
114
114
protected final Source source ;
115
115
protected final String name ;
116
- protected String moduleDoc ;
117
- protected boolean firstStatement = true ;
118
116
119
117
protected final boolean isInlineMode ;
120
118
@@ -234,7 +232,7 @@ public Object visitFile_input(Python3Parser.File_inputContext ctx) {
234
232
ExpressionNode file = asExpression (super .visitFile_input (ctx ));
235
233
deriveSourceSection (ctx , file );
236
234
environment .popScope ();
237
- return factory .createModuleRoot (name , moduleDoc , file , ctx .scope .getFrameDescriptor ());
235
+ return factory .createModuleRoot (name , getModuleDoc ( ctx ) , file , ctx .scope .getFrameDescriptor ());
238
236
}
239
237
240
238
@ Override
@@ -258,10 +256,34 @@ public Object visitSingle_input(Python3Parser.Single_inputContext ctx) {
258
256
if (isInlineMode ) {
259
257
return body ;
260
258
} else {
261
- return factory .createModuleRoot ("<expression>" , moduleDoc , body , ctx .scope .getFrameDescriptor ());
259
+ return factory .createModuleRoot ("<expression>" , getModuleDoc ( ctx ) , body , ctx .scope .getFrameDescriptor ());
262
260
}
263
261
}
264
262
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
+
265
287
@ Override
266
288
public Object visitAtom_expr (Python3Parser .Atom_exprContext ctx ) {
267
289
// TODO: deal with AWAIT
@@ -1286,14 +1308,7 @@ private LoopNode createForInScope(StatementNode target, ExpressionNode iterator,
1286
1308
@ Override
1287
1309
public Object visitExpr_stmt (Python3Parser .Expr_stmtContext ctx ) {
1288
1310
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 );
1297
1312
} else {
1298
1313
return assigns .translate (ctx );
1299
1314
}
0 commit comments