1
0
mirror of https://github.com/golang/go synced 2024-11-21 13:14:40 -07:00

go/parser cleanup: remove some state by writing more functional code

R=rsc
CC=golang-dev
https://golang.org/cl/223071
This commit is contained in:
Robert Griesemer 2010-02-26 14:50:12 -08:00
parent bc6878331a
commit 059bc2742a

View File

@ -46,7 +46,6 @@ type parser struct {
// Comments
comments vector.Vector // list of *CommentGroup
lastComment *ast.CommentGroup // last comment in the comments list
leadComment *ast.CommentGroup // the last lead comment
lineComment *ast.CommentGroup // the last line comment
@ -163,13 +162,13 @@ func (p *parser) consumeComment() (comment *ast.Comment, endline int) {
// Consume a group of adjacent comments, add it to the parser's
// comments list, and return the line of which the last comment
// in the group ends. An empty line or non-comment token terminates
// a comment group.
// comments list, and return it together with the line at which
// the last comment in the group ends. An empty line or non-comment
// token terminates a comment group.
//
func (p *parser) consumeCommentGroup() int {
func (p *parser) consumeCommentGroup() (comments *ast.CommentGroup, endline int) {
var list vector.Vector
endline := p.pos.Line
endline = p.pos.Line
for p.tok == token.COMMENT && endline+1 >= p.pos.Line {
var comment *ast.Comment
comment, endline = p.consumeComment()
@ -183,11 +182,10 @@ func (p *parser) consumeCommentGroup() int {
}
// add comment group to the comments list
g := &ast.CommentGroup{group}
p.comments.Push(g)
p.lastComment = g
comments = &ast.CommentGroup{group}
p.comments.Push(comments)
return endline
return
}
@ -213,27 +211,30 @@ func (p *parser) next() {
p.next0()
if p.tok == token.COMMENT {
var comment *ast.CommentGroup
var endline int
if p.pos.Line == line {
// The comment is on same line as previous token; it
// cannot be a lead comment but may be a line comment.
endline := p.consumeCommentGroup()
comment, endline = p.consumeCommentGroup()
if p.pos.Line != endline {
// The next token is on a different line, thus
// the last comment group is a line comment.
p.lineComment = p.lastComment
p.lineComment = comment
}
}
// consume successor comments, if any
endline := -1
endline = -1
for p.tok == token.COMMENT {
endline = p.consumeCommentGroup()
comment, endline = p.consumeCommentGroup()
}
if endline >= 0 && endline+1 == p.pos.Line {
if endline+1 == p.pos.Line {
// The next token is following on the line immediately after the
// comment group, thus the last comment group is a lead comment.
p.leadComment = p.lastComment
p.leadComment = comment
}
}
}