@@ -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 = []
@@ -286,7 +345,7 @@ def remainder_noops(self):
286345
287346
288347_TOKEN_KEYS = set (
289- ("module" , "name" , "id" , "attr" , "arg" , "LiteralValue" )
348+ ("module" , "name" , "id" , "attr" , "arg" , "LiteralValue" , "s" , "n" )
290349)
291350
292351_SYNTHETIC_TOKENS = {
@@ -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 :
@@ -461,11 +461,6 @@ def visit_str(self, node):
461461 """
462462 return str (node )
463463
464- def visit_Str (self , node ):
465- node .update ({"LiteralValue" : node ["s" ], "ast_type" : "StringLiteral" })
466- node .pop ("s" , None )
467- return node
468-
469464 def visit_Bytes (self , node ):
470465 try :
471466 s = node ["s" ].decode ()
@@ -475,15 +470,7 @@ def visit_Bytes(self, node):
475470 s = encode (node ["s" ], 'base64' ).decode ().strip ()
476471 encoding = 'base64'
477472
478- node .update ({"LiteralValue" : s ,
479- "encoding" : encoding ,
480- "ast_type" : "ByteLiteral" })
481- node .pop ("s" , None )
482- return node
483-
484- def visit_NoneType (self , node ):
485- node .update ({"LiteralValue" : "None" ,
486- "ast_type" : "NoneLiteral" })
473+ node .update ({"s" : s , "encoding" : encoding })
487474 return node
488475
489476 def _promote_names (self , node ):
@@ -512,23 +499,6 @@ def visit_NameConstant(self, node):
512499 node ["ast_type" ] = "NameConstant"
513500 return node
514501
515- def visit_Num (self , node ):
516- if isinstance (node ["n" ], int ):
517- ret_dict = { "NumType" : "int" , "LiteralValue" : node ["n" ] }
518- elif isinstance (node ["n" ], float ):
519- ret_dict = { "NumType" : "float" , "LiteralValue" : node ["n" ] }
520- elif isinstance (node ["n" ], complex ):
521- ret_dict = {
522- "NumType" : "complex" ,
523- "LiteralValue" : {"real" : node ["n" ]["real" ],
524- "imaginary" : node ["n" ]["imag" ]},
525- }
526-
527- node ["ast_type" ] = "NumLiteral"
528- node .update (ret_dict )
529- node .pop ("n" , None )
530- return node
531-
532502 def visit_other (self , node ):
533503 for field in node .get ("_fields" , []):
534504 meth = getattr (self , "visit_" + node ["ast_type" ], self .visit_other_field )
0 commit comments