code: def assign_comments(self, tree, comments):
          
          comments = list(comments)
          comments.sort(key=lambda c: c.line)
  
          idx_by_line = {0: 0}  # {line_no: comment_idx}
  
          for i, c in enumerate(comments):
              if c.line not in idx_by_line:
                  idx_by_line[c.line] = i
  
          idx = []
  
          # convert comment tokens to strings, and remove any line breaks
          self.comments = [c.value.strip() for c in comments]
          last_comment_line = max(idx_by_line.keys())
  
          # make a list with an entry for each line
          # number associated with a comment list index
  
          for i in range(last_comment_line, 0, -1):
              if i in idx_by_line:
                  # associate line with new comment
                  idx.append(idx_by_line[i])
              else:
                  # associate line with following comment
                  idx.append(idx[-1])
  
          idx.append(0)  # line numbers start from 1
          idx.reverse()
          self.idx = idx
          self._assign_comments(tree, 0)
docs: Capture any comments in the tree
  
          header_comments stores comments preceding a node