class SyntaxTree::Parser

def attach_comments(program, comments)

the location of the comments.
Attaches comments to the nodes in the tree that most closely correspond to
def attach_comments(program, comments)
  comments.each do |comment|
    preceding, enclosing, following = nearest_nodes(program, comment)
    if comment.inline?
      if preceding
        preceding.comments << comment
        comment.trailing!
      elsif following
        following.comments << comment
        comment.leading!
      elsif enclosing
        enclosing.comments << comment
      else
        program.comments << comment
      end
    else
      # If a comment exists on its own line, prefer a leading comment.
      if following
        following.comments << comment
        comment.leading!
      elsif preceding
        preceding.comments << comment
        comment.trailing!
      elsif enclosing
        enclosing.comments << comment
      else
        program.comments << comment
      end
    end
  end
end