diff options
Diffstat (limited to 'src/pkg/go/parser/parser.go')
-rw-r--r-- | src/pkg/go/parser/parser.go | 1050 |
1 files changed, 525 insertions, 525 deletions
diff --git a/src/pkg/go/parser/parser.go b/src/pkg/go/parser/parser.go index fa6fb545e..bd7ca158e 100644 --- a/src/pkg/go/parser/parser.go +++ b/src/pkg/go/parser/parser.go @@ -10,11 +10,11 @@ package parser import ( - "container/vector"; - "fmt"; - "go/ast"; - "go/scanner"; - "go/token"; + "container/vector" + "fmt" + "go/ast" + "go/scanner" + "go/token" ) @@ -27,59 +27,59 @@ var noPos token.Position // parser functionality. // const ( - PackageClauseOnly uint = 1 << iota; // parsing stops after package clause - ImportsOnly; // parsing stops after import declarations - ParseComments; // parse comments and add them to AST - Trace; // print a trace of parsed productions + PackageClauseOnly uint = 1 << iota // parsing stops after package clause + ImportsOnly // parsing stops after import declarations + ParseComments // parse comments and add them to AST + Trace // print a trace of parsed productions ) // The parser structure holds the parser's internal state. type parser struct { - scanner.ErrorVector; - scanner scanner.Scanner; + scanner.ErrorVector + scanner scanner.Scanner // Tracing/debugging - mode uint; // parsing mode - trace bool; // == (mode & Trace != 0) - indent uint; // indentation used for tracing output + mode uint // parsing mode + trace bool // == (mode & Trace != 0) + indent uint // indentation used for tracing output // Comments - comments *ast.CommentGroup; // list of collected comments - lastComment *ast.CommentGroup; // last comment in the comments list - leadComment *ast.CommentGroup; // the last lead comment - lineComment *ast.CommentGroup; // the last line comment + comments *ast.CommentGroup // list of collected comments + lastComment *ast.CommentGroup // last comment in the comments list + leadComment *ast.CommentGroup // the last lead comment + lineComment *ast.CommentGroup // the last line comment // Next token - pos token.Position; // token position - tok token.Token; // one token look-ahead - lit []byte; // token literal + pos token.Position // token position + tok token.Token // one token look-ahead + lit []byte // token literal // Non-syntactic parser control - exprLev int; // < 0: in control clause, >= 0: in expression + exprLev int // < 0: in control clause, >= 0: in expression // Scopes - pkgScope *ast.Scope; - fileScope *ast.Scope; - topScope *ast.Scope; + pkgScope *ast.Scope + fileScope *ast.Scope + topScope *ast.Scope } // scannerMode returns the scanner mode bits given the parser's mode bits. func scannerMode(mode uint) uint { - var m uint = scanner.InsertSemis; + var m uint = scanner.InsertSemis if mode&ParseComments != 0 { m |= scanner.ScanComments } - return m; + return m } func (p *parser) init(filename string, src []byte, mode uint) { - p.scanner.Init(filename, src, p, scannerMode(mode)); - p.mode = mode; - p.trace = mode&Trace != 0; // for convenience (p.trace is used frequently) - p.next(); + p.scanner.Init(filename, src, p, scannerMode(mode)) + p.mode = mode + p.trace = mode&Trace != 0 // for convenience (p.trace is used frequently) + p.next() } @@ -88,29 +88,29 @@ func (p *parser) init(filename string, src []byte, mode uint) { func (p *parser) printTrace(a ...) { const dots = ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . " + - ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "; - const n = uint(len(dots)); - fmt.Printf("%5d:%3d: ", p.pos.Line, p.pos.Column); - i := 2 * p.indent; + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . " + const n = uint(len(dots)) + fmt.Printf("%5d:%3d: ", p.pos.Line, p.pos.Column) + i := 2 * p.indent for ; i > n; i -= n { fmt.Print(dots) } - fmt.Print(dots[0:i]); - fmt.Println(a); + fmt.Print(dots[0:i]) + fmt.Println(a) } func trace(p *parser, msg string) *parser { - p.printTrace(msg, "("); - p.indent++; - return p; + p.printTrace(msg, "(") + p.indent++ + return p } // Usage pattern: defer un(trace(p, "...")); func un(p *parser) { - p.indent--; - p.printTrace(")"); + p.indent-- + p.printTrace(")") } @@ -121,7 +121,7 @@ func (p *parser) next0() { // very first token (p.pos.Line == 0) is not initialized (it // is token.ILLEGAL), so don't print it . if p.trace && p.pos.Line > 0 { - s := p.tok.String(); + s := p.tok.String() switch { case p.tok.IsLiteral(): p.printTrace(s, string(p.lit)) @@ -132,14 +132,14 @@ func (p *parser) next0() { } } - p.pos, p.tok, p.lit = p.scanner.Scan(); + p.pos, p.tok, p.lit = p.scanner.Scan() } // Consume a comment and return it and the line on which it ends. func (p *parser) consumeComment() (comment *ast.Comment, endline int) { // /*-style comments may end on a different line than where they start. // Scan the comment for '\n' chars and adjust endline accordingly. - endline = p.pos.Line; + endline = p.pos.Line if p.lit[1] == '*' { for _, b := range p.lit { if b == '\n' { @@ -148,10 +148,10 @@ func (p *parser) consumeComment() (comment *ast.Comment, endline int) { } } - comment = &ast.Comment{p.pos, p.lit}; - p.next0(); + comment = &ast.Comment{p.pos, p.lit} + p.next0() - return; + return } @@ -161,30 +161,30 @@ func (p *parser) consumeComment() (comment *ast.Comment, endline int) { // a comment group. // func (p *parser) consumeCommentGroup() int { - var list vector.Vector; - endline := p.pos.Line; + var list vector.Vector + endline := p.pos.Line for p.tok == token.COMMENT && endline+1 >= p.pos.Line { - var comment *ast.Comment; - comment, endline = p.consumeComment(); - list.Push(comment); + var comment *ast.Comment + comment, endline = p.consumeComment() + list.Push(comment) } // convert list - group := make([]*ast.Comment, list.Len()); + group := make([]*ast.Comment, list.Len()) for i := 0; i < list.Len(); i++ { group[i] = list.At(i).(*ast.Comment) } // add comment group to the comments list - g := &ast.CommentGroup{group, nil}; + g := &ast.CommentGroup{group, nil} if p.lastComment != nil { p.lastComment.Next = g } else { p.comments = g } - p.lastComment = g; + p.lastComment = g - return endline; + return endline } @@ -204,16 +204,16 @@ func (p *parser) consumeCommentGroup() int { // stored in the AST. // func (p *parser) next() { - p.leadComment = nil; - p.lineComment = nil; - line := p.pos.Line; // current line - p.next0(); + p.leadComment = nil + p.lineComment = nil + line := p.pos.Line // current line + p.next0() if p.tok == token.COMMENT { 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(); + 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. @@ -222,7 +222,7 @@ func (p *parser) next() { } // consume successor comments, if any - endline := -1; + endline := -1 for p.tok == token.COMMENT { endline = p.consumeCommentGroup() } @@ -237,26 +237,26 @@ func (p *parser) next() { func (p *parser) errorExpected(pos token.Position, msg string) { - msg = "expected " + msg; + msg = "expected " + msg if pos.Offset == p.pos.Offset { // the error happened at the current position; // make the error message more specific - msg += ", found '" + p.tok.String() + "'"; + msg += ", found '" + p.tok.String() + "'" if p.tok.IsLiteral() { msg += " " + string(p.lit) } } - p.Error(pos, msg); + p.Error(pos, msg) } func (p *parser) expect(tok token.Token) token.Position { - pos := p.pos; + pos := p.pos if p.tok != tok { p.errorExpected(pos, "'"+tok.String()+"'") } - p.next(); // make progress in any case - return pos; + p.next() // make progress in any case + return pos } @@ -271,13 +271,13 @@ func (p *parser) expectSemi() { // Scope support func openScope(p *parser) *parser { - p.topScope = ast.NewScope(p.topScope); - return p; + p.topScope = ast.NewScope(p.topScope) + return p } // Usage pattern: defer close(openScope(p)); -func close(p *parser) { p.topScope = p.topScope.Outer } +func close(p *parser) { p.topScope = p.topScope.Outer } func (p *parser) declare(ident *ast.Ident) { @@ -299,12 +299,12 @@ func (p *parser) declareList(idents []*ast.Ident) { func (p *parser) parseIdent() *ast.Ident { if p.tok == token.IDENT { - x := &ast.Ident{p.pos, string(p.lit)}; - p.next(); - return x; + x := &ast.Ident{p.pos, string(p.lit)} + p.next() + return x } - p.expect(token.IDENT); // use expect() error handling - return &ast.Ident{p.pos, ""}; + p.expect(token.IDENT) // use expect() error handling + return &ast.Ident{p.pos, ""} } @@ -313,29 +313,29 @@ func (p *parser) parseIdentList() []*ast.Ident { defer un(trace(p, "IdentList")) } - var list vector.Vector; - list.Push(p.parseIdent()); + var list vector.Vector + list.Push(p.parseIdent()) for p.tok == token.COMMA { - p.next(); - list.Push(p.parseIdent()); + p.next() + list.Push(p.parseIdent()) } // convert vector - idents := make([]*ast.Ident, list.Len()); + idents := make([]*ast.Ident, list.Len()) for i := 0; i < list.Len(); i++ { idents[i] = list.At(i).(*ast.Ident) } - return idents; + return idents } func makeExprList(list *vector.Vector) []ast.Expr { - exprs := make([]ast.Expr, list.Len()); + exprs := make([]ast.Expr, list.Len()) for i := 0; i < list.Len(); i++ { exprs[i] = list.At(i).(ast.Expr) } - return exprs; + return exprs } @@ -344,14 +344,14 @@ func (p *parser) parseExprList() []ast.Expr { defer un(trace(p, "ExpressionList")) } - var list vector.Vector; - list.Push(p.parseExpr()); + var list vector.Vector + list.Push(p.parseExpr()) for p.tok == token.COMMA { - p.next(); - list.Push(p.parseExpr()); + p.next() + list.Push(p.parseExpr()) } - return makeExprList(&list); + return makeExprList(&list) } @@ -363,15 +363,15 @@ func (p *parser) parseType() ast.Expr { defer un(trace(p, "Type")) } - typ := p.tryType(); + typ := p.tryType() if typ == nil { - p.errorExpected(p.pos, "type"); - p.next(); // make progress - return &ast.BadExpr{p.pos}; + p.errorExpected(p.pos, "type") + p.next() // make progress + return &ast.BadExpr{p.pos} } - return typ; + return typ } @@ -380,14 +380,14 @@ func (p *parser) parseQualifiedIdent() ast.Expr { defer un(trace(p, "QualifiedIdent")) } - var x ast.Expr = p.parseIdent(); + var x ast.Expr = p.parseIdent() if p.tok == token.PERIOD { // first identifier is a package identifier - p.next(); - sel := p.parseIdent(); - x = &ast.SelectorExpr{x, sel}; + p.next() + sel := p.parseIdent() + x = &ast.SelectorExpr{x, sel} } - return x; + return x } @@ -396,7 +396,7 @@ func (p *parser) parseTypeName() ast.Expr { defer un(trace(p, "TypeName")) } - return p.parseQualifiedIdent(); + return p.parseQualifiedIdent() } @@ -405,33 +405,33 @@ func (p *parser) parseArrayType(ellipsisOk bool) ast.Expr { defer un(trace(p, "ArrayType")) } - lbrack := p.expect(token.LBRACK); - var len ast.Expr; + lbrack := p.expect(token.LBRACK) + var len ast.Expr if ellipsisOk && p.tok == token.ELLIPSIS { - len = &ast.Ellipsis{p.pos}; - p.next(); + len = &ast.Ellipsis{p.pos} + p.next() } else if p.tok != token.RBRACK { len = p.parseExpr() } - p.expect(token.RBRACK); - elt := p.parseType(); + p.expect(token.RBRACK) + elt := p.parseType() - return &ast.ArrayType{lbrack, len, elt}; + return &ast.ArrayType{lbrack, len, elt} } func (p *parser) makeIdentList(list *vector.Vector) []*ast.Ident { - idents := make([]*ast.Ident, list.Len()); + idents := make([]*ast.Ident, list.Len()) for i := 0; i < list.Len(); i++ { - ident, isIdent := list.At(i).(*ast.Ident); + ident, isIdent := list.At(i).(*ast.Ident) if !isIdent { - pos := list.At(i).(ast.Expr).Pos(); - p.errorExpected(pos, "identifier"); - idents[i] = &ast.Ident{pos, ""}; + pos := list.At(i).(ast.Expr).Pos() + p.errorExpected(pos, "identifier") + idents[i] = &ast.Ident{pos, ""} } - idents[i] = ident; + idents[i] = ident } - return idents; + return idents } @@ -440,32 +440,32 @@ func (p *parser) parseFieldDecl() *ast.Field { defer un(trace(p, "FieldDecl")) } - doc := p.leadComment; + doc := p.leadComment // a list of identifiers looks like a list of type names - var list vector.Vector; + var list vector.Vector for { // TODO(gri): do not allow ()'s here - list.Push(p.parseType()); + list.Push(p.parseType()) if p.tok != token.COMMA { break } - p.next(); + p.next() } // if we had a list of identifiers, it must be followed by a type - typ := p.tryType(); + typ := p.tryType() // optional tag - var tag []*ast.BasicLit; + var tag []*ast.BasicLit if p.tok == token.STRING { - x := &ast.BasicLit{p.pos, p.tok, p.lit}; - p.next(); - tag = []*ast.BasicLit{x}; + x := &ast.BasicLit{p.pos, p.tok, p.lit} + p.next() + tag = []*ast.BasicLit{x} } // analyze case - var idents []*ast.Ident; + var idents []*ast.Ident if typ != nil { // IdentifierList Type idents = p.makeIdentList(&list) @@ -475,14 +475,14 @@ func (p *parser) parseFieldDecl() *ast.Field { // TODO(gri): check that this looks like a type typ = list.At(0).(ast.Expr) } else { - p.errorExpected(p.pos, "anonymous field"); - typ = &ast.BadExpr{p.pos}; + p.errorExpected(p.pos, "anonymous field") + typ = &ast.BadExpr{p.pos} } } - p.expectSemi(); + p.expectSemi() - return &ast.Field{doc, idents, typ, tag, p.lineComment}; + return &ast.Field{doc, idents, typ, tag, p.lineComment} } @@ -491,21 +491,21 @@ func (p *parser) parseStructType() *ast.StructType { defer un(trace(p, "StructType")) } - pos := p.expect(token.STRUCT); - lbrace := p.expect(token.LBRACE); - var list vector.Vector; + pos := p.expect(token.STRUCT) + lbrace := p.expect(token.LBRACE) + var list vector.Vector for p.tok == token.IDENT || p.tok == token.MUL { list.Push(p.parseFieldDecl()) } - rbrace := p.expect(token.RBRACE); + rbrace := p.expect(token.RBRACE) // convert vector - fields := make([]*ast.Field, list.Len()); + fields := make([]*ast.Field, list.Len()) for i := list.Len() - 1; i >= 0; i-- { fields[i] = list.At(i).(*ast.Field) } - return &ast.StructType{pos, lbrace, fields, rbrace, false}; + return &ast.StructType{pos, lbrace, fields, rbrace, false} } @@ -514,35 +514,35 @@ func (p *parser) parsePointerType() *ast.StarExpr { defer un(trace(p, "PointerType")) } - star := p.expect(token.MUL); - base := p.parseType(); + star := p.expect(token.MUL) + base := p.parseType() - return &ast.StarExpr{star, base}; + return &ast.StarExpr{star, base} } func (p *parser) tryParameterType(ellipsisOk bool) ast.Expr { if ellipsisOk && p.tok == token.ELLIPSIS { - pos := p.pos; - p.next(); + pos := p.pos + p.next() if p.tok != token.RPAREN { // "..." always must be at the very end of a parameter list p.Error(pos, "expected type, found '...'") } - return &ast.Ellipsis{pos}; + return &ast.Ellipsis{pos} } - return p.tryType(); + return p.tryType() } func (p *parser) parseParameterType(ellipsisOk bool) ast.Expr { - typ := p.tryParameterType(ellipsisOk); + typ := p.tryParameterType(ellipsisOk) if typ == nil { - p.errorExpected(p.pos, "type"); - p.next(); // make progress - typ = &ast.BadExpr{p.pos}; + p.errorExpected(p.pos, "type") + p.next() // make progress + typ = &ast.BadExpr{p.pos} } - return typ; + return typ } @@ -552,20 +552,20 @@ func (p *parser) parseParameterDecl(ellipsisOk bool) (*vector.Vector, ast.Expr) } // a list of identifiers looks like a list of type names - var list vector.Vector; + var list vector.Vector for { // TODO(gri): do not allow ()'s here - list.Push(p.parseParameterType(ellipsisOk)); + list.Push(p.parseParameterType(ellipsisOk)) if p.tok != token.COMMA { break } - p.next(); + p.next() } // if we had a list of identifiers, it must be followed by a type - typ := p.tryParameterType(ellipsisOk); + typ := p.tryParameterType(ellipsisOk) - return &list, typ; + return &list, typ } @@ -574,24 +574,24 @@ func (p *parser) parseParameterList(ellipsisOk bool) []*ast.Field { defer un(trace(p, "ParameterList")) } - list, typ := p.parseParameterDecl(ellipsisOk); + list, typ := p.parseParameterDecl(ellipsisOk) if typ != nil { // IdentifierList Type - idents := p.makeIdentList(list); - list.Resize(0, 0); - list.Push(&ast.Field{nil, idents, typ, nil, nil}); + idents := p.makeIdentList(list) + list.Resize(0, 0) + list.Push(&ast.Field{nil, idents, typ, nil, nil}) if p.tok == token.COMMA { p.next() } for p.tok != token.RPAREN && p.tok != token.EOF { - idents := p.parseIdentList(); - typ := p.parseParameterType(ellipsisOk); - list.Push(&ast.Field{nil, idents, typ, nil, nil}); + idents := p.parseIdentList() + typ := p.parseParameterType(ellipsisOk) + list.Push(&ast.Field{nil, idents, typ, nil, nil}) if p.tok != token.COMMA { break } - p.next(); + p.next() } } else { @@ -603,12 +603,12 @@ func (p *parser) parseParameterList(ellipsisOk bool) []*ast.Field { } // convert list - params := make([]*ast.Field, list.Len()); + params := make([]*ast.Field, list.Len()) for i := 0; i < list.Len(); i++ { params[i] = list.At(i).(*ast.Field) } - return params; + return params } @@ -617,14 +617,14 @@ func (p *parser) parseParameters(ellipsisOk bool) []*ast.Field { defer un(trace(p, "Parameters")) } - var params []*ast.Field; - p.expect(token.LPAREN); + var params []*ast.Field + p.expect(token.LPAREN) if p.tok != token.RPAREN { params = p.parseParameterList(ellipsisOk) } - p.expect(token.RPAREN); + p.expect(token.RPAREN) - return params; + return params } @@ -633,18 +633,18 @@ func (p *parser) parseResult() []*ast.Field { defer un(trace(p, "Result")) } - var results []*ast.Field; + var results []*ast.Field if p.tok == token.LPAREN { results = p.parseParameters(false) } else if p.tok != token.FUNC { - typ := p.tryType(); + typ := p.tryType() if typ != nil { - results = make([]*ast.Field, 1); - results[0] = &ast.Field{Type: typ}; + results = make([]*ast.Field, 1) + results[0] = &ast.Field{Type: typ} } } - return results; + return results } @@ -653,10 +653,10 @@ func (p *parser) parseSignature() (params []*ast.Field, results []*ast.Field) { defer un(trace(p, "Signature")) } - params = p.parseParameters(true); - results = p.parseResult(); + params = p.parseParameters(true) + results = p.parseResult() - return; + return } @@ -665,10 +665,10 @@ func (p *parser) parseFuncType() *ast.FuncType { defer un(trace(p, "FuncType")) } - pos := p.expect(token.FUNC); - params, results := p.parseSignature(); + pos := p.expect(token.FUNC) + params, results := p.parseSignature() - return &ast.FuncType{pos, params, results}; + return &ast.FuncType{pos, params, results} } @@ -677,22 +677,22 @@ func (p *parser) parseMethodSpec() *ast.Field { defer un(trace(p, "MethodSpec")) } - doc := p.leadComment; - var idents []*ast.Ident; - var typ ast.Expr; - x := p.parseQualifiedIdent(); + doc := p.leadComment + var idents []*ast.Ident + var typ ast.Expr + x := p.parseQualifiedIdent() if ident, isIdent := x.(*ast.Ident); isIdent && p.tok == token.LPAREN { // method - idents = []*ast.Ident{ident}; - params, results := p.parseSignature(); - typ = &ast.FuncType{noPos, params, results}; + idents = []*ast.Ident{ident} + params, results := p.parseSignature() + typ = &ast.FuncType{noPos, params, results} } else { // embedded interface typ = x } - p.expectSemi(); + p.expectSemi() - return &ast.Field{doc, idents, typ, nil, p.lineComment}; + return &ast.Field{doc, idents, typ, nil, p.lineComment} } @@ -701,21 +701,21 @@ func (p *parser) parseInterfaceType() *ast.InterfaceType { defer un(trace(p, "InterfaceType")) } - pos := p.expect(token.INTERFACE); - lbrace := p.expect(token.LBRACE); - var list vector.Vector; + pos := p.expect(token.INTERFACE) + lbrace := p.expect(token.LBRACE) + var list vector.Vector for p.tok == token.IDENT { list.Push(p.parseMethodSpec()) } - rbrace := p.expect(token.RBRACE); + rbrace := p.expect(token.RBRACE) // convert vector - methods := make([]*ast.Field, list.Len()); + methods := make([]*ast.Field, list.Len()) for i := list.Len() - 1; i >= 0; i-- { methods[i] = list.At(i).(*ast.Field) } - return &ast.InterfaceType{pos, lbrace, methods, rbrace, false}; + return &ast.InterfaceType{pos, lbrace, methods, rbrace, false} } @@ -724,13 +724,13 @@ func (p *parser) parseMapType() *ast.MapType { defer un(trace(p, "MapType")) } - pos := p.expect(token.MAP); - p.expect(token.LBRACK); - key := p.parseType(); - p.expect(token.RBRACK); - value := p.parseType(); + pos := p.expect(token.MAP) + p.expect(token.LBRACK) + key := p.parseType() + p.expect(token.RBRACK) + value := p.parseType() - return &ast.MapType{pos, key, value}; + return &ast.MapType{pos, key, value} } @@ -739,22 +739,22 @@ func (p *parser) parseChanType() *ast.ChanType { defer un(trace(p, "ChanType")) } - pos := p.pos; - dir := ast.SEND | ast.RECV; + pos := p.pos + dir := ast.SEND | ast.RECV if p.tok == token.CHAN { - p.next(); + p.next() if p.tok == token.ARROW { - p.next(); - dir = ast.SEND; + p.next() + dir = ast.SEND } } else { - p.expect(token.ARROW); - p.expect(token.CHAN); - dir = ast.RECV; + p.expect(token.ARROW) + p.expect(token.CHAN) + dir = ast.RECV } - value := p.parseType(); + value := p.parseType() - return &ast.ChanType{pos, dir, value}; + return &ast.ChanType{pos, dir, value} } @@ -777,30 +777,30 @@ func (p *parser) tryRawType(ellipsisOk bool) ast.Expr { case token.CHAN, token.ARROW: return p.parseChanType() case token.LPAREN: - lparen := p.pos; - p.next(); - typ := p.parseType(); - rparen := p.expect(token.RPAREN); - return &ast.ParenExpr{lparen, typ, rparen}; + lparen := p.pos + p.next() + typ := p.parseType() + rparen := p.expect(token.RPAREN) + return &ast.ParenExpr{lparen, typ, rparen} } // no type found - return nil; + return nil } -func (p *parser) tryType() ast.Expr { return p.tryRawType(false) } +func (p *parser) tryType() ast.Expr { return p.tryRawType(false) } // ---------------------------------------------------------------------------- // Blocks func makeStmtList(list *vector.Vector) []ast.Stmt { - stats := make([]ast.Stmt, list.Len()); + stats := make([]ast.Stmt, list.Len()) for i := 0; i < list.Len(); i++ { stats[i] = list.At(i).(ast.Stmt) } - return stats; + return stats } @@ -809,12 +809,12 @@ func (p *parser) parseStmtList() []ast.Stmt { defer un(trace(p, "StatementList")) } - var list vector.Vector; + var list vector.Vector for p.tok != token.CASE && p.tok != token.DEFAULT && p.tok != token.RBRACE && p.tok != token.EOF { list.Push(p.parseStmt()) } - return makeStmtList(&list); + return makeStmtList(&list) } @@ -823,13 +823,13 @@ func (p *parser) parseBlockStmt() *ast.BlockStmt { defer un(trace(p, "BlockStmt")) } - defer close(openScope(p)); + defer close(openScope(p)) - lbrace := p.expect(token.LBRACE); - list := p.parseStmtList(); - rbrace := p.expect(token.RBRACE); + lbrace := p.expect(token.LBRACE) + list := p.parseStmtList() + rbrace := p.expect(token.RBRACE) - return &ast.BlockStmt{lbrace, list, rbrace}; + return &ast.BlockStmt{lbrace, list, rbrace} } @@ -841,17 +841,17 @@ func (p *parser) parseFuncTypeOrLit() ast.Expr { defer un(trace(p, "FuncTypeOrLit")) } - typ := p.parseFuncType(); + typ := p.parseFuncType() if p.tok != token.LBRACE { // function type only return typ } - p.exprLev++; - body := p.parseBlockStmt(); - p.exprLev--; + p.exprLev++ + body := p.parseBlockStmt() + p.exprLev-- - return &ast.FuncLit{typ, body}; + return &ast.FuncLit{typ, body} } @@ -868,32 +868,32 @@ func (p *parser) parseOperand() ast.Expr { return p.parseIdent() case token.INT, token.FLOAT, token.CHAR, token.STRING: - x := &ast.BasicLit{p.pos, p.tok, p.lit}; - p.next(); - return x; + x := &ast.BasicLit{p.pos, p.tok, p.lit} + p.next() + return x case token.LPAREN: - lparen := p.pos; - p.next(); - p.exprLev++; - x := p.parseExpr(); - p.exprLev--; - rparen := p.expect(token.RPAREN); - return &ast.ParenExpr{lparen, x, rparen}; + lparen := p.pos + p.next() + p.exprLev++ + x := p.parseExpr() + p.exprLev-- + rparen := p.expect(token.RPAREN) + return &ast.ParenExpr{lparen, x, rparen} case token.FUNC: return p.parseFuncTypeOrLit() default: - t := p.tryRawType(true); // could be type for composite literal or conversion + t := p.tryRawType(true) // could be type for composite literal or conversion if t != nil { return t } } - p.errorExpected(p.pos, "operand"); - p.next(); // make progress - return &ast.BadExpr{p.pos}; + p.errorExpected(p.pos, "operand") + p.next() // make progress + return &ast.BadExpr{p.pos} } @@ -902,25 +902,25 @@ func (p *parser) parseSelectorOrTypeAssertion(x ast.Expr) ast.Expr { defer un(trace(p, "SelectorOrTypeAssertion")) } - p.expect(token.PERIOD); + p.expect(token.PERIOD) if p.tok == token.IDENT { // selector - sel := p.parseIdent(); - return &ast.SelectorExpr{x, sel}; + sel := p.parseIdent() + return &ast.SelectorExpr{x, sel} } // type assertion - p.expect(token.LPAREN); - var typ ast.Expr; + p.expect(token.LPAREN) + var typ ast.Expr if p.tok == token.TYPE { // type switch: typ == nil p.next() } else { typ = p.parseType() } - p.expect(token.RPAREN); + p.expect(token.RPAREN) - return &ast.TypeAssertExpr{x, typ}; + return &ast.TypeAssertExpr{x, typ} } @@ -929,23 +929,23 @@ func (p *parser) parseIndexOrSlice(x ast.Expr) ast.Expr { defer un(trace(p, "IndexOrSlice")) } - p.expect(token.LBRACK); - p.exprLev++; - index := p.parseExpr(); + p.expect(token.LBRACK) + p.exprLev++ + index := p.parseExpr() if p.tok == token.COLON { - p.next(); - var end ast.Expr; + p.next() + var end ast.Expr if p.tok != token.RBRACK { end = p.parseExpr() } - x = &ast.SliceExpr{x, index, end}; + x = &ast.SliceExpr{x, index, end} } else { x = &ast.IndexExpr{x, index} } - p.exprLev--; - p.expect(token.RBRACK); + p.exprLev-- + p.expect(token.RBRACK) - return x; + return x } @@ -954,20 +954,20 @@ func (p *parser) parseCallOrConversion(fun ast.Expr) *ast.CallExpr { defer un(trace(p, "CallOrConversion")) } - lparen := p.expect(token.LPAREN); - p.exprLev++; - var list vector.Vector; + lparen := p.expect(token.LPAREN) + p.exprLev++ + var list vector.Vector for p.tok != token.RPAREN && p.tok != token.EOF { - list.Push(p.parseExpr()); + list.Push(p.parseExpr()) if p.tok != token.COMMA { break } - p.next(); + p.next() } - p.exprLev--; - rparen := p.expect(token.RPAREN); + p.exprLev-- + rparen := p.expect(token.RPAREN) - return &ast.CallExpr{fun, lparen, makeExprList(&list), rparen}; + return &ast.CallExpr{fun, lparen, makeExprList(&list), rparen} } @@ -976,14 +976,14 @@ func (p *parser) parseElement() ast.Expr { defer un(trace(p, "Element")) } - x := p.parseExpr(); + x := p.parseExpr() if p.tok == token.COLON { - colon := p.pos; - p.next(); - x = &ast.KeyValueExpr{x, colon, p.parseExpr()}; + colon := p.pos + p.next() + x = &ast.KeyValueExpr{x, colon, p.parseExpr()} } - return x; + return x } @@ -992,16 +992,16 @@ func (p *parser) parseElementList() []ast.Expr { defer un(trace(p, "ElementList")) } - var list vector.Vector; + var list vector.Vector for p.tok != token.RBRACE && p.tok != token.EOF { - list.Push(p.parseElement()); + list.Push(p.parseElement()) if p.tok != token.COMMA { break } - p.next(); + p.next() } - return makeExprList(&list); + return makeExprList(&list) } @@ -1010,13 +1010,13 @@ func (p *parser) parseCompositeLit(typ ast.Expr) ast.Expr { defer un(trace(p, "CompositeLit")) } - lbrace := p.expect(token.LBRACE); - var elts []ast.Expr; + lbrace := p.expect(token.LBRACE) + var elts []ast.Expr if p.tok != token.RBRACE { elts = p.parseElementList() } - rbrace := p.expect(token.RBRACE); - return &ast.CompositeLit{typ, lbrace, elts, rbrace}; + rbrace := p.expect(token.RBRACE) + return &ast.CompositeLit{typ, lbrace, elts, rbrace} } @@ -1042,24 +1042,24 @@ func (p *parser) checkExpr(x ast.Expr) ast.Expr { case *ast.TypeAssertExpr: if t.Type == nil { // the form X.(type) is only allowed in type switch expressions - p.errorExpected(x.Pos(), "expression"); - x = &ast.BadExpr{x.Pos()}; + p.errorExpected(x.Pos(), "expression") + x = &ast.BadExpr{x.Pos()} } case *ast.CallExpr: case *ast.StarExpr: case *ast.UnaryExpr: if t.Op == token.RANGE { // the range operator is only allowed at the top of a for statement - p.errorExpected(x.Pos(), "expression"); - x = &ast.BadExpr{x.Pos()}; + p.errorExpected(x.Pos(), "expression") + x = &ast.BadExpr{x.Pos()} } case *ast.BinaryExpr: default: // all other nodes are not proper expressions - p.errorExpected(x.Pos(), "expression"); - x = &ast.BadExpr{x.Pos()}; + p.errorExpected(x.Pos(), "expression") + x = &ast.BadExpr{x.Pos()} } - return x; + return x } @@ -1070,13 +1070,13 @@ func isTypeName(x ast.Expr) bool { case *ast.BadExpr: case *ast.Ident: case *ast.ParenExpr: - return isTypeName(t.X) // TODO(gri): should (TypeName) be illegal? + return isTypeName(t.X) // TODO(gri): should (TypeName) be illegal? case *ast.SelectorExpr: return isTypeName(t.X) default: - return false // all other nodes are not type names + return false // all other nodes are not type names } - return true; + return true } @@ -1094,9 +1094,9 @@ func isCompositeLitType(x ast.Expr) bool { case *ast.StructType: case *ast.MapType: default: - return false // all other nodes are not legal composite literal types + return false // all other nodes are not legal composite literal types } - return true; + return true } @@ -1109,18 +1109,18 @@ func (p *parser) checkExprOrType(x ast.Expr) ast.Expr { case *ast.UnaryExpr: if t.Op == token.RANGE { // the range operator is only allowed at the top of a for statement - p.errorExpected(x.Pos(), "expression"); - x = &ast.BadExpr{x.Pos()}; + p.errorExpected(x.Pos(), "expression") + x = &ast.BadExpr{x.Pos()} } case *ast.ArrayType: if len, isEllipsis := t.Len.(*ast.Ellipsis); isEllipsis { - p.Error(len.Pos(), "expected array length, found '...'"); - x = &ast.BadExpr{x.Pos()}; + p.Error(len.Pos(), "expected array length, found '...'") + x = &ast.BadExpr{x.Pos()} } } // all other nodes are expressions or types - return x; + return x } @@ -1129,8 +1129,8 @@ func (p *parser) parsePrimaryExpr() ast.Expr { defer un(trace(p, "PrimaryExpr")) } - x := p.parseOperand(); -L: for { + x := p.parseOperand() +L: for { switch p.tok { case token.PERIOD: x = p.parseSelectorOrTypeAssertion(p.checkExpr(x)) @@ -1149,7 +1149,7 @@ L: for { } } - return x; + return x } @@ -1160,20 +1160,20 @@ func (p *parser) parseUnaryExpr() ast.Expr { switch p.tok { case token.ADD, token.SUB, token.NOT, token.XOR, token.ARROW, token.AND, token.RANGE: - pos, op := p.pos, p.tok; - p.next(); - x := p.parseUnaryExpr(); - return &ast.UnaryExpr{pos, op, p.checkExpr(x)}; + pos, op := p.pos, p.tok + p.next() + x := p.parseUnaryExpr() + return &ast.UnaryExpr{pos, op, p.checkExpr(x)} case token.MUL: // unary "*" expression or pointer type - pos := p.pos; - p.next(); - x := p.parseUnaryExpr(); - return &ast.StarExpr{pos, p.checkExprOrType(x)}; + pos := p.pos + p.next() + x := p.parseUnaryExpr() + return &ast.StarExpr{pos, p.checkExprOrType(x)} } - return p.parsePrimaryExpr(); + return p.parsePrimaryExpr() } @@ -1182,17 +1182,17 @@ func (p *parser) parseBinaryExpr(prec1 int) ast.Expr { defer un(trace(p, "BinaryExpr")) } - x := p.parseUnaryExpr(); + x := p.parseUnaryExpr() for prec := p.tok.Precedence(); prec >= prec1; prec-- { for p.tok.Precedence() == prec { - pos, op := p.pos, p.tok; - p.next(); - y := p.parseBinaryExpr(prec + 1); - x = &ast.BinaryExpr{p.checkExpr(x), pos, op, p.checkExpr(y)}; + pos, op := p.pos, p.tok + p.next() + y := p.parseBinaryExpr(prec + 1) + x = &ast.BinaryExpr{p.checkExpr(x), pos, op, p.checkExpr(y)} } } - return x; + return x } @@ -1203,7 +1203,7 @@ func (p *parser) parseExpr() ast.Expr { defer un(trace(p, "Expression")) } - return p.parseBinaryExpr(token.LowestPrec + 1); + return p.parseBinaryExpr(token.LowestPrec + 1) } @@ -1215,19 +1215,19 @@ func (p *parser) parseSimpleStmt(labelOk bool) ast.Stmt { defer un(trace(p, "SimpleStmt")) } - x := p.parseExprList(); + x := p.parseExprList() switch p.tok { case token.COLON: // labeled statement - p.next(); + p.next() if labelOk && len(x) == 1 { if label, isIdent := x[0].(*ast.Ident); isIdent { return &ast.LabeledStmt{label, p.parseStmt()} } } - p.Error(x[0].Pos(), "illegal label declaration"); - return &ast.BadStmt{x[0].Pos()}; + p.Error(x[0].Pos(), "illegal label declaration") + return &ast.BadStmt{x[0].Pos()} case token.DEFINE, token.ASSIGN, token.ADD_ASSIGN, @@ -1235,13 +1235,13 @@ func (p *parser) parseSimpleStmt(labelOk bool) ast.Stmt { token.REM_ASSIGN, token.AND_ASSIGN, token.OR_ASSIGN, token.XOR_ASSIGN, token.SHL_ASSIGN, token.SHR_ASSIGN, token.AND_NOT_ASSIGN: // assignment statement - pos, tok := p.pos, p.tok; - p.next(); - y := p.parseExprList(); + pos, tok := p.pos, p.tok + p.next() + y := p.parseExprList() if len(x) > 1 && len(y) > 1 && len(x) != len(y) { p.Error(x[0].Pos(), "arity of lhs doesn't match rhs") } - return &ast.AssignStmt{x, pos, tok, y}; + return &ast.AssignStmt{x, pos, tok, y} } if len(x) > 1 { @@ -1251,23 +1251,23 @@ func (p *parser) parseSimpleStmt(labelOk bool) ast.Stmt { if p.tok == token.INC || p.tok == token.DEC { // increment or decrement - s := &ast.IncDecStmt{x[0], p.tok}; - p.next(); // consume "++" or "--" - return s; + s := &ast.IncDecStmt{x[0], p.tok} + p.next() // consume "++" or "--" + return s } // expression - return &ast.ExprStmt{x[0]}; + return &ast.ExprStmt{x[0]} } func (p *parser) parseCallExpr() *ast.CallExpr { - x := p.parseExpr(); + x := p.parseExpr() if call, isCall := x.(*ast.CallExpr); isCall { return call } - p.errorExpected(x.Pos(), "function/method call"); - return nil; + p.errorExpected(x.Pos(), "function/method call") + return nil } @@ -1276,14 +1276,14 @@ func (p *parser) parseGoStmt() ast.Stmt { defer un(trace(p, "GoStmt")) } - pos := p.expect(token.GO); - call := p.parseCallExpr(); - p.expectSemi(); + pos := p.expect(token.GO) + call := p.parseCallExpr() + p.expectSemi() if call == nil { return &ast.BadStmt{pos} } - return &ast.GoStmt{pos, call}; + return &ast.GoStmt{pos, call} } @@ -1292,14 +1292,14 @@ func (p *parser) parseDeferStmt() ast.Stmt { defer un(trace(p, "DeferStmt")) } - pos := p.expect(token.DEFER); - call := p.parseCallExpr(); - p.expectSemi(); + pos := p.expect(token.DEFER) + call := p.parseCallExpr() + p.expectSemi() if call == nil { return &ast.BadStmt{pos} } - return &ast.DeferStmt{pos, call}; + return &ast.DeferStmt{pos, call} } @@ -1308,15 +1308,15 @@ func (p *parser) parseReturnStmt() *ast.ReturnStmt { defer un(trace(p, "ReturnStmt")) } - pos := p.pos; - p.expect(token.RETURN); - var x []ast.Expr; + pos := p.pos + p.expect(token.RETURN) + var x []ast.Expr if p.tok != token.SEMICOLON && p.tok != token.RBRACE { x = p.parseExprList() } - p.expectSemi(); + p.expectSemi() - return &ast.ReturnStmt{pos, x}; + return &ast.ReturnStmt{pos, x} } @@ -1325,14 +1325,14 @@ func (p *parser) parseBranchStmt(tok token.Token) *ast.BranchStmt { defer un(trace(p, "BranchStmt")) } - s := &ast.BranchStmt{p.pos, tok, nil}; - p.expect(tok); + s := &ast.BranchStmt{p.pos, tok, nil} + p.expect(tok) if tok != token.FALLTHROUGH && p.tok == token.IDENT { s.Label = p.parseIdent() } - p.expectSemi(); + p.expectSemi() - return s; + return s } @@ -1343,27 +1343,27 @@ func (p *parser) makeExpr(s ast.Stmt) ast.Expr { if es, isExpr := s.(*ast.ExprStmt); isExpr { return p.checkExpr(es.X) } - p.Error(s.Pos(), "expected condition, found simple statement"); - return &ast.BadExpr{s.Pos()}; + p.Error(s.Pos(), "expected condition, found simple statement") + return &ast.BadExpr{s.Pos()} } func (p *parser) parseControlClause(isForStmt bool) (s1, s2, s3 ast.Stmt) { if p.tok != token.LBRACE { - prevLev := p.exprLev; - p.exprLev = -1; + prevLev := p.exprLev + p.exprLev = -1 if p.tok != token.SEMICOLON { s1 = p.parseSimpleStmt(false) } if p.tok == token.SEMICOLON { - p.next(); + p.next() if p.tok != token.LBRACE && p.tok != token.SEMICOLON { s2 = p.parseSimpleStmt(false) } if isForStmt { // for statements have a 3rd section - p.expectSemi(); + p.expectSemi() if p.tok != token.LBRACE { s3 = p.parseSimpleStmt(false) } @@ -1372,10 +1372,10 @@ func (p *parser) parseControlClause(isForStmt bool) (s1, s2, s3 ast.Stmt) { s1, s2 = nil, s1 } - p.exprLev = prevLev; + p.exprLev = prevLev } - return s1, s2, s3; + return s1, s2, s3 } @@ -1385,20 +1385,20 @@ func (p *parser) parseIfStmt() *ast.IfStmt { } // IfStmt block - defer close(openScope(p)); + defer close(openScope(p)) - pos := p.expect(token.IF); - s1, s2, _ := p.parseControlClause(false); - body := p.parseBlockStmt(); - var else_ ast.Stmt; + pos := p.expect(token.IF) + s1, s2, _ := p.parseControlClause(false) + body := p.parseBlockStmt() + var else_ ast.Stmt if p.tok == token.ELSE { - p.next(); - else_ = p.parseStmt(); + p.next() + else_ = p.parseStmt() } else { p.expectSemi() } - return &ast.IfStmt{pos, s1, p.makeExpr(s2), body, else_}; + return &ast.IfStmt{pos, s1, p.makeExpr(s2), body, else_} } @@ -1408,22 +1408,22 @@ func (p *parser) parseCaseClause() *ast.CaseClause { } // CaseClause block - defer close(openScope(p)); + defer close(openScope(p)) // SwitchCase - pos := p.pos; - var x []ast.Expr; + pos := p.pos + var x []ast.Expr if p.tok == token.CASE { - p.next(); - x = p.parseExprList(); + p.next() + x = p.parseExprList() } else { p.expect(token.DEFAULT) } - colon := p.expect(token.COLON); - body := p.parseStmtList(); + colon := p.expect(token.COLON) + body := p.parseStmtList() - return &ast.CaseClause{pos, x, colon, body}; + return &ast.CaseClause{pos, x, colon, body} } @@ -1432,14 +1432,14 @@ func (p *parser) parseTypeList() []ast.Expr { defer un(trace(p, "TypeList")) } - var list vector.Vector; - list.Push(p.parseType()); + var list vector.Vector + list.Push(p.parseType()) for p.tok == token.COMMA { - p.next(); - list.Push(p.parseType()); + p.next() + list.Push(p.parseType()) } - return makeExprList(&list); + return makeExprList(&list) } @@ -1449,22 +1449,22 @@ func (p *parser) parseTypeCaseClause() *ast.TypeCaseClause { } // TypeCaseClause block - defer close(openScope(p)); + defer close(openScope(p)) // TypeSwitchCase - pos := p.pos; - var types []ast.Expr; + pos := p.pos + var types []ast.Expr if p.tok == token.CASE { - p.next(); - types = p.parseTypeList(); + p.next() + types = p.parseTypeList() } else { p.expect(token.DEFAULT) } - colon := p.expect(token.COLON); - body := p.parseStmtList(); + colon := p.expect(token.COLON) + body := p.parseStmtList() - return &ast.TypeCaseClause{pos, types, colon, body}; + return &ast.TypeCaseClause{pos, types, colon, body} } @@ -1474,11 +1474,11 @@ func isExprSwitch(s ast.Stmt) bool { } if e, ok := s.(*ast.ExprStmt); ok { if a, ok := e.X.(*ast.TypeAssertExpr); ok { - return a.Type != nil // regular type assertion + return a.Type != nil // regular type assertion } - return true; + return true } - return false; + return false } @@ -1488,34 +1488,34 @@ func (p *parser) parseSwitchStmt() ast.Stmt { } // SwitchStmt block - defer close(openScope(p)); + defer close(openScope(p)) - pos := p.expect(token.SWITCH); - s1, s2, _ := p.parseControlClause(false); + pos := p.expect(token.SWITCH) + s1, s2, _ := p.parseControlClause(false) if isExprSwitch(s2) { - lbrace := p.expect(token.LBRACE); - var cases vector.Vector; + lbrace := p.expect(token.LBRACE) + var cases vector.Vector for p.tok == token.CASE || p.tok == token.DEFAULT { cases.Push(p.parseCaseClause()) } - rbrace := p.expect(token.RBRACE); - body := &ast.BlockStmt{lbrace, makeStmtList(&cases), rbrace}; - p.expectSemi(); - return &ast.SwitchStmt{pos, s1, p.makeExpr(s2), body}; + rbrace := p.expect(token.RBRACE) + body := &ast.BlockStmt{lbrace, makeStmtList(&cases), rbrace} + p.expectSemi() + return &ast.SwitchStmt{pos, s1, p.makeExpr(s2), body} } // type switch // TODO(gri): do all the checks! - lbrace := p.expect(token.LBRACE); - var cases vector.Vector; + lbrace := p.expect(token.LBRACE) + var cases vector.Vector for p.tok == token.CASE || p.tok == token.DEFAULT { cases.Push(p.parseTypeCaseClause()) } - rbrace := p.expect(token.RBRACE); - p.expectSemi(); - body := &ast.BlockStmt{lbrace, makeStmtList(&cases), rbrace}; - return &ast.TypeSwitchStmt{pos, s1, s2, body}; + rbrace := p.expect(token.RBRACE) + p.expectSemi() + body := &ast.BlockStmt{lbrace, makeStmtList(&cases), rbrace} + return &ast.TypeSwitchStmt{pos, s1, s2, body} } @@ -1525,29 +1525,29 @@ func (p *parser) parseCommClause() *ast.CommClause { } // CommClause block - defer close(openScope(p)); + defer close(openScope(p)) // CommCase - pos := p.pos; - var tok token.Token; - var lhs, rhs ast.Expr; + pos := p.pos + var tok token.Token + var lhs, rhs ast.Expr if p.tok == token.CASE { - p.next(); + p.next() if p.tok == token.ARROW { // RecvExpr without assignment rhs = p.parseExpr() } else { // SendExpr or RecvExpr - rhs = p.parseExpr(); + rhs = p.parseExpr() if p.tok == token.ASSIGN || p.tok == token.DEFINE { // RecvExpr with assignment - tok = p.tok; - p.next(); - lhs = rhs; + tok = p.tok + p.next() + lhs = rhs if p.tok == token.ARROW { rhs = p.parseExpr() } else { - p.expect(token.ARROW) // use expect() error handling + p.expect(token.ARROW) // use expect() error handling } } // else SendExpr @@ -1556,10 +1556,10 @@ func (p *parser) parseCommClause() *ast.CommClause { p.expect(token.DEFAULT) } - colon := p.expect(token.COLON); - body := p.parseStmtList(); + colon := p.expect(token.COLON) + body := p.parseStmtList() - return &ast.CommClause{pos, tok, lhs, rhs, colon, body}; + return &ast.CommClause{pos, tok, lhs, rhs, colon, body} } @@ -1568,17 +1568,17 @@ func (p *parser) parseSelectStmt() *ast.SelectStmt { defer un(trace(p, "SelectStmt")) } - pos := p.expect(token.SELECT); - lbrace := p.expect(token.LBRACE); - var cases vector.Vector; + pos := p.expect(token.SELECT) + lbrace := p.expect(token.LBRACE) + var cases vector.Vector for p.tok == token.CASE || p.tok == token.DEFAULT { cases.Push(p.parseCommClause()) } - rbrace := p.expect(token.RBRACE); - p.expectSemi(); - body := &ast.BlockStmt{lbrace, makeStmtList(&cases), rbrace}; + rbrace := p.expect(token.RBRACE) + p.expectSemi() + body := &ast.BlockStmt{lbrace, makeStmtList(&cases), rbrace} - return &ast.SelectStmt{pos, body}; + return &ast.SelectStmt{pos, body} } @@ -1588,50 +1588,50 @@ func (p *parser) parseForStmt() ast.Stmt { } // ForStmt block - defer close(openScope(p)); + defer close(openScope(p)) - pos := p.expect(token.FOR); - s1, s2, s3 := p.parseControlClause(true); - body := p.parseBlockStmt(); - p.expectSemi(); + pos := p.expect(token.FOR) + s1, s2, s3 := p.parseControlClause(true) + body := p.parseBlockStmt() + p.expectSemi() if as, isAssign := s2.(*ast.AssignStmt); isAssign { // possibly a for statement with a range clause; check assignment operator if as.Tok != token.ASSIGN && as.Tok != token.DEFINE { - p.errorExpected(as.TokPos, "'=' or ':='"); - return &ast.BadStmt{pos}; + p.errorExpected(as.TokPos, "'=' or ':='") + return &ast.BadStmt{pos} } // check lhs - var key, value ast.Expr; + var key, value ast.Expr switch len(as.Lhs) { case 2: - value = as.Lhs[1]; - fallthrough; + value = as.Lhs[1] + fallthrough case 1: key = as.Lhs[0] default: - p.errorExpected(as.Lhs[0].Pos(), "1 or 2 expressions"); - return &ast.BadStmt{pos}; + p.errorExpected(as.Lhs[0].Pos(), "1 or 2 expressions") + return &ast.BadStmt{pos} } // check rhs if len(as.Rhs) != 1 { - p.errorExpected(as.Rhs[0].Pos(), "1 expressions"); - return &ast.BadStmt{pos}; + p.errorExpected(as.Rhs[0].Pos(), "1 expressions") + return &ast.BadStmt{pos} } if rhs, isUnary := as.Rhs[0].(*ast.UnaryExpr); isUnary && rhs.Op == token.RANGE { // rhs is range expression; check lhs return &ast.RangeStmt{pos, key, value, as.TokPos, as.Tok, rhs.X, body} } else { - p.errorExpected(s2.Pos(), "range clause"); - return &ast.BadStmt{pos}; + p.errorExpected(s2.Pos(), "range clause") + return &ast.BadStmt{pos} } } else { // regular for statement return &ast.ForStmt{pos, s1, p.makeExpr(s2), s3, body} } - panic(); // unreachable - return nil; + panic() // unreachable + return nil } @@ -1645,10 +1645,10 @@ func (p *parser) parseStmt() (s ast.Stmt) { s = &ast.DeclStmt{p.parseDecl()} case // tokens that may start a top-level expression - token.IDENT, token.INT, token.FLOAT, token.CHAR, token.STRING, token.FUNC, token.LPAREN, // operand - token.LBRACK, token.STRUCT, // composite type - token.MUL, token.AND, token.ARROW, token.ADD, token.SUB, token.XOR: // unary operators - s = p.parseSimpleStmt(true); + token.IDENT, token.INT, token.FLOAT, token.CHAR, token.STRING, token.FUNC, token.LPAREN, // operand + token.LBRACK, token.STRUCT, // composite type + token.MUL, token.AND, token.ARROW, token.ADD, token.SUB, token.XOR: // unary operators + s = p.parseSimpleStmt(true) // because of the required look-ahead, labeled statements are // parsed by parseSimpleStmt - don't expect a semicolon after // them @@ -1664,8 +1664,8 @@ func (p *parser) parseStmt() (s ast.Stmt) { case token.BREAK, token.CONTINUE, token.GOTO, token.FALLTHROUGH: s = p.parseBranchStmt(p.tok) case token.LBRACE: - s = p.parseBlockStmt(); - p.expectSemi(); + s = p.parseBlockStmt() + p.expectSemi() case token.IF: s = p.parseIfStmt() case token.SWITCH: @@ -1675,19 +1675,19 @@ func (p *parser) parseStmt() (s ast.Stmt) { case token.FOR: s = p.parseForStmt() case token.SEMICOLON: - p.next(); - fallthrough; + p.next() + fallthrough case token.RBRACE: // a semicolon may be omitted before a closing "}" s = &ast.EmptyStmt{p.pos} default: // no statement found - p.errorExpected(p.pos, "statement"); - p.next(); // make progress - s = &ast.BadStmt{p.pos}; + p.errorExpected(p.pos, "statement") + p.next() // make progress + s = &ast.BadStmt{p.pos} } - return; + return } @@ -1702,25 +1702,25 @@ func parseImportSpec(p *parser, doc *ast.CommentGroup) ast.Spec { defer un(trace(p, "ImportSpec")) } - var ident *ast.Ident; + var ident *ast.Ident if p.tok == token.PERIOD { - ident = &ast.Ident{p.pos, "."}; - p.next(); + ident = &ast.Ident{p.pos, "."} + p.next() } else if p.tok == token.IDENT { ident = p.parseIdent() } - var path []*ast.BasicLit; + var path []*ast.BasicLit if p.tok == token.STRING { - x := &ast.BasicLit{p.pos, p.tok, p.lit}; - p.next(); - path = []*ast.BasicLit{x}; + x := &ast.BasicLit{p.pos, p.tok, p.lit} + p.next() + path = []*ast.BasicLit{x} } else { - p.expect(token.STRING) // use expect() error handling + p.expect(token.STRING) // use expect() error handling } - p.expectSemi(); + p.expectSemi() - return &ast.ImportSpec{doc, ident, path, p.lineComment}; + return &ast.ImportSpec{doc, ident, path, p.lineComment} } @@ -1729,16 +1729,16 @@ func parseConstSpec(p *parser, doc *ast.CommentGroup) ast.Spec { defer un(trace(p, "ConstSpec")) } - idents := p.parseIdentList(); - typ := p.tryType(); - var values []ast.Expr; + idents := p.parseIdentList() + typ := p.tryType() + var values []ast.Expr if typ != nil || p.tok == token.ASSIGN { - p.expect(token.ASSIGN); - values = p.parseExprList(); + p.expect(token.ASSIGN) + values = p.parseExprList() } - p.expectSemi(); + p.expectSemi() - return &ast.ValueSpec{doc, idents, typ, values, p.lineComment}; + return &ast.ValueSpec{doc, idents, typ, values, p.lineComment} } @@ -1747,11 +1747,11 @@ func parseTypeSpec(p *parser, doc *ast.CommentGroup) ast.Spec { defer un(trace(p, "TypeSpec")) } - ident := p.parseIdent(); - typ := p.parseType(); - p.expectSemi(); + ident := p.parseIdent() + typ := p.parseType() + p.expectSemi() - return &ast.TypeSpec{doc, ident, typ, p.lineComment}; + return &ast.TypeSpec{doc, ident, typ, p.lineComment} } @@ -1760,16 +1760,16 @@ func parseVarSpec(p *parser, doc *ast.CommentGroup) ast.Spec { defer un(trace(p, "VarSpec")) } - idents := p.parseIdentList(); - typ := p.tryType(); - var values []ast.Expr; + idents := p.parseIdentList() + typ := p.tryType() + var values []ast.Expr if typ == nil || p.tok == token.ASSIGN { - p.expect(token.ASSIGN); - values = p.parseExprList(); + p.expect(token.ASSIGN) + values = p.parseExprList() } - p.expectSemi(); + p.expectSemi() - return &ast.ValueSpec{doc, idents, typ, values, p.lineComment}; + return &ast.ValueSpec{doc, idents, typ, values, p.lineComment} } @@ -1778,29 +1778,29 @@ func (p *parser) parseGenDecl(keyword token.Token, f parseSpecFunction) *ast.Gen defer un(trace(p, keyword.String()+"Decl")) } - doc := p.leadComment; - pos := p.expect(keyword); - var lparen, rparen token.Position; - var list vector.Vector; + doc := p.leadComment + pos := p.expect(keyword) + var lparen, rparen token.Position + var list vector.Vector if p.tok == token.LPAREN { - lparen = p.pos; - p.next(); + lparen = p.pos + p.next() for p.tok != token.RPAREN && p.tok != token.EOF { list.Push(f(p, p.leadComment)) } - rparen = p.expect(token.RPAREN); - p.expectSemi(); + rparen = p.expect(token.RPAREN) + p.expectSemi() } else { list.Push(f(p, nil)) } // convert vector - specs := make([]ast.Spec, list.Len()); + specs := make([]ast.Spec, list.Len()) for i := 0; i < list.Len(); i++ { specs[i] = list.At(i).(ast.Spec) } - return &ast.GenDecl{doc, pos, keyword, lparen, specs, rparen}; + return &ast.GenDecl{doc, pos, keyword, lparen, specs, rparen} } @@ -1809,19 +1809,19 @@ func (p *parser) parseReceiver() *ast.Field { defer un(trace(p, "Receiver")) } - pos := p.pos; - par := p.parseParameters(false); + pos := p.pos + par := p.parseParameters(false) // must have exactly one receiver if len(par) != 1 || len(par) == 1 && len(par[0].Names) > 1 { - p.errorExpected(pos, "exactly one receiver"); - return &ast.Field{Type: &ast.BadExpr{noPos}}; + p.errorExpected(pos, "exactly one receiver") + return &ast.Field{Type: &ast.BadExpr{noPos}} } - recv := par[0]; + recv := par[0] // recv type must be TypeName or *TypeName - base := recv.Type; + base := recv.Type if ptr, isPtr := base.(*ast.StarExpr); isPtr { base = ptr.X } @@ -1829,7 +1829,7 @@ func (p *parser) parseReceiver() *ast.Field { p.errorExpected(base.Pos(), "type name") } - return recv; + return recv } @@ -1838,24 +1838,24 @@ func (p *parser) parseFunctionDecl() *ast.FuncDecl { defer un(trace(p, "FunctionDecl")) } - doc := p.leadComment; - pos := p.expect(token.FUNC); + doc := p.leadComment + pos := p.expect(token.FUNC) - var recv *ast.Field; + var recv *ast.Field if p.tok == token.LPAREN { recv = p.parseReceiver() } - ident := p.parseIdent(); - params, results := p.parseSignature(); + ident := p.parseIdent() + params, results := p.parseSignature() - var body *ast.BlockStmt; + var body *ast.BlockStmt if p.tok == token.LBRACE { body = p.parseBlockStmt() } - p.expectSemi(); + p.expectSemi() - return &ast.FuncDecl{doc, recv, ident, &ast.FuncType{pos, params, results}, body}; + return &ast.FuncDecl{doc, recv, ident, &ast.FuncType{pos, params, results}, body} } @@ -1864,7 +1864,7 @@ func (p *parser) parseDecl() ast.Decl { defer un(trace(p, "Declaration")) } - var f parseSpecFunction; + var f parseSpecFunction switch p.tok { case token.CONST: f = parseConstSpec @@ -1879,14 +1879,14 @@ func (p *parser) parseDecl() ast.Decl { return p.parseFunctionDecl() default: - pos := p.pos; - p.errorExpected(pos, "declaration"); - decl := &ast.BadDecl{pos}; - p.next(); // make progress in any case - return decl; + pos := p.pos + p.errorExpected(pos, "declaration") + decl := &ast.BadDecl{pos} + p.next() // make progress in any case + return decl } - return p.parseGenDecl(p.tok, f); + return p.parseGenDecl(p.tok, f) } @@ -1895,18 +1895,18 @@ func (p *parser) parseDeclList() []ast.Decl { defer un(trace(p, "DeclList")) } - var list vector.Vector; + var list vector.Vector for p.tok != token.EOF { list.Push(p.parseDecl()) } // convert vector - decls := make([]ast.Decl, list.Len()); + decls := make([]ast.Decl, list.Len()) for i := 0; i < list.Len(); i++ { decls[i] = list.At(i).(ast.Decl) } - return decls; + return decls } @@ -1919,22 +1919,22 @@ func (p *parser) parseFile() *ast.File { } // file block - defer close(openScope(p)); + defer close(openScope(p)) // package clause - doc := p.leadComment; - pos := p.expect(token.PACKAGE); - ident := p.parseIdent(); - p.expectSemi(); + doc := p.leadComment + pos := p.expect(token.PACKAGE) + ident := p.parseIdent() + p.expectSemi() - var decls []ast.Decl; + var decls []ast.Decl // Don't bother parsing the rest if we had errors already. // Likely not a Go source file at all. if p.ErrorCount() == 0 && p.mode&PackageClauseOnly == 0 { // import decls - var list vector.Vector; + var list vector.Vector for p.tok == token.IMPORT { list.Push(p.parseGenDecl(token.IMPORT, parseImportSpec)) } @@ -1947,11 +1947,11 @@ func (p *parser) parseFile() *ast.File { } // convert declaration list - decls = make([]ast.Decl, list.Len()); + decls = make([]ast.Decl, list.Len()) for i := 0; i < list.Len(); i++ { decls[i] = list.At(i).(ast.Decl) } } - return &ast.File{doc, pos, ident, decls, p.comments}; + return &ast.File{doc, pos, ident, decls, p.comments} } |