// sortComments sorts the list of comment groups in source order. func sortComments(list []*CommentGroup) { … } type CommentMap … func (cmap CommentMap) addComment(n Node, c *CommentGroup) { … } // nodeList returns the list of nodes of the AST n in source order. func nodeList(n Node) []Node { … } type commentListReader … func (r *commentListReader) eol() bool { … } func (r *commentListReader) next() { … } type nodeStack … // push pops all nodes that appear lexically before n // and then pushes n on the stack. func (s *nodeStack) push(n Node) { … } // pop pops all nodes that appear lexically before pos // (i.e., whose lexical extent has ended before or at pos). // It returns the last node popped. func (s *nodeStack) pop(pos token.Pos) (top Node) { … } // NewCommentMap creates a new comment map by associating comment groups // of the comments list with the nodes of the AST specified by node. // // A comment group g is associated with a node n if: // // - g starts on the same line as n ends // - g starts on the line immediately following n, and there is // at least one empty line after g and before the next node // - g starts before n and is not associated to the node before n // via the previous rules // // NewCommentMap tries to associate a comment group to the "largest" // node possible: For instance, if the comment is a line comment // trailing an assignment, the comment is associated with the entire // assignment rather than just the last operand in the assignment. func NewCommentMap(fset *token.FileSet, node Node, comments []*CommentGroup) CommentMap { … } // Update replaces an old node in the comment map with the new node // and returns the new node. Comments that were associated with the // old node are associated with the new node. func (cmap CommentMap) Update(old, new Node) Node { … } // Filter returns a new comment map consisting of only those // entries of cmap for which a corresponding node exists in // the AST specified by node. func (cmap CommentMap) Filter(node Node) CommentMap { … } // Comments returns the list of comment groups in the comment map. // The result is sorted in source order. func (cmap CommentMap) Comments() []*CommentGroup { … } func summary(list []*CommentGroup) string { … } func (cmap CommentMap) String() string { … }