@@ -193,6 +193,65 @@ def _create_astmissing_lines(self):
193193 self ._current_line = len (lines )
194194 return lines
195195
196+ def add_noops (self , node , visit_dict , root ):
197+ if not isinstance (visit_dict , dict ):
198+ return visit_dict
199+
200+ def _create_nooplines_list (startline , noops_previous ):
201+ nooplines = []
202+ curline = startline
203+ for noopline in noops_previous :
204+ nooplines .append ({
205+ "ast_type" : "NoopLine" ,
206+ "noop_line" : noopline ,
207+ "lineno" : curline ,
208+ "col_offset" : 1 ,
209+ })
210+ curline += 1
211+ return nooplines
212+
213+ # Add all the noop (whitespace and comments) lines between the
214+ # last node and this one
215+ noops_previous , startline , endline , endcol = \
216+ self .previous_nooplines (node )
217+ if noops_previous :
218+ visit_dict ['noops_previous' ] = {
219+ "ast_type" : "PreviousNoops" ,
220+ "lineno" : startline ,
221+ "col_offset" : 1 ,
222+ "end_lineno" : endline ,
223+ "end_col_offset" : max (endcol , 1 ),
224+ "lines" : _create_nooplines_list (startline , noops_previous )
225+ }
226+
227+ # Other noops at the end of its significative line except the implicit
228+ # finishing newline
229+ noops_sameline = self .sameline_remainder_noops (node )
230+ joined_sameline = '' .join ([x ['value' ] for x in noops_sameline ])
231+ if noops_sameline :
232+ visit_dict ['noops_sameline' ] = {
233+ "ast_type" : "SameLineNoops" ,
234+ "lineno" : node .get ("lineno" , 0 ),
235+ "col_offset" : noops_sameline [0 ]["colstart" ],
236+ "noop_line" : joined_sameline ,
237+ "end_lineno" : node .get ("lineno" , 0 ),
238+ "end_col_offset" : max (noops_sameline [- 1 ]["colend" ], 1 )
239+ }
240+
241+ # Finally, if this is the root node, add all noops after the last op node
242+ if root :
243+ noops_remainder , startline , endline , endcol = \
244+ self .remainder_noops ()
245+ if noops_remainder :
246+ visit_dict ['noops_remainder' ] = {
247+ "ast_type" : "RemainderNoops" ,
248+ "lineno" : startline ,
249+ "col_offset" : 1 ,
250+ "end_lineno" : endline ,
251+ "end_col_offset" : max (endcol , 1 ),
252+ "lines" : _create_nooplines_list (startline , noops_remainder )
253+ }
254+
196255 def previous_nooplines (self , nodedict ):
197256 """Return a list of the preceding comment and blank lines"""
198257 previous = []
@@ -360,65 +419,6 @@ def __init__(self, codestr, astdict):
360419
361420 self .visit_Global = self .visit_Nonlocal = self ._promote_names
362421
363- def _add_noops (self , node , visit_dict , root ):
364- if not isinstance (visit_dict , dict ):
365- return visit_dict
366-
367- def _create_nooplines_list (startline , noops_previous ):
368- nooplines = []
369- curline = startline
370- for noopline in noops_previous :
371- nooplines .append ({
372- "ast_type" : "NoopLine" ,
373- "noop_line" : noopline ,
374- "lineno" : curline ,
375- "col_offset" : 1 ,
376- })
377- curline += 1
378- return nooplines
379-
380- # Add all the noop (whitespace and comments) lines between the
381- # last node and this one
382- noops_previous , startline , endline , endcol = \
383- self .noops_sync .previous_nooplines (node )
384- if noops_previous :
385- visit_dict ['noops_previous' ] = {
386- "ast_type" : "PreviousNoops" ,
387- "lineno" : startline ,
388- "col_offset" : 1 ,
389- "end_lineno" : endline ,
390- "end_col_offset" : max (endcol , 1 ),
391- "lines" : _create_nooplines_list (startline , noops_previous )
392- }
393-
394- # Other noops at the end of its significative line except the implicit
395- # finishing newline
396- noops_sameline = self .noops_sync .sameline_remainder_noops (node )
397- joined_sameline = '' .join ([x ['value' ] for x in noops_sameline ])
398- if noops_sameline :
399- visit_dict ['noops_sameline' ] = {
400- "ast_type" : "SameLineNoops" ,
401- "lineno" : node .get ("lineno" , 0 ),
402- "col_offset" : noops_sameline [0 ]["colstart" ],
403- "noop_line" : joined_sameline ,
404- "end_lineno" : node .get ("lineno" , 0 ),
405- "end_col_offset" : max (noops_sameline [- 1 ]["colend" ], 1 )
406- }
407-
408- # Finally, if this is the root node, add all noops after the last op node
409- if root :
410- noops_remainder , startline , endline , endcol = \
411- self .noops_sync .remainder_noops ()
412- if noops_remainder :
413- visit_dict ['noops_remainder' ] = {
414- "ast_type" : "RemainderNoops" ,
415- "lineno" : startline ,
416- "col_offset" : 1 ,
417- "end_lineno" : endline ,
418- "end_col_offset" : max (endcol , 1 ),
419- "lines" : _create_nooplines_list (startline , noops_remainder )
420- }
421-
422422 def parse (self ):
423423 res = self .visit (self ._astdict , root = True )
424424 return res
@@ -434,7 +434,7 @@ def visit(self, node, root=False):
434434
435435 meth = getattr (self , "visit_" + node_type , self .visit_other )
436436 visit_result = meth (node )
437- self ._add_noops (node , visit_result , root )
437+ self .noops_sync . add_noops (node , visit_result , root )
438438 self .pos_sync .sync_node_pos (visit_result )
439439
440440 if not self .codestr :
@@ -499,23 +499,6 @@ def visit_NameConstant(self, node):
499499 node ["ast_type" ] = "NameConstant"
500500 return node
501501
502- # def visit_Num(self, node):
503- # if isinstance(node["n"], int):
504- # ret_dict = { "NumType": "int", "LiteralValue": node["n"] }
505- # elif isinstance(node["n"], float):
506- # ret_dict = { "NumType": "float", "LiteralValue": node["n"] }
507- # elif isinstance(node["n"], complex):
508- # ret_dict = {
509- # "NumType": "complex",
510- # "LiteralValue": {"real": node["n"]["real"],
511- # "imaginary": node["n"]["imag"]},
512- # }
513-
514- # node["ast_type"] = "NumLiteral"
515- # node.update(ret_dict)
516- # node.pop("n", None)
517- # return node
518-
519502 def visit_other (self , node ):
520503 for field in node .get ("_fields" , []):
521504 meth = getattr (self , "visit_" + node ["ast_type" ], self .visit_other_field )
0 commit comments