diff options
author | Robert Griesemer <gri@golang.org> | 2009-11-09 21:13:17 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2009-11-09 21:13:17 -0800 |
commit | 073e240233589933c43143c997247c33206bb066 (patch) | |
tree | e6953aa6ac9f97ad730c7509a39cf15d82131fab /src/pkg/go | |
parent | ef50a171462c00444b6bf8d205ae8e97079ab2b9 (diff) | |
download | golang-073e240233589933c43143c997247c33206bb066.tar.gz |
- replaced gofmt expression formatting algorithm with
rsc's algorithm
- applied gofmt -w misc src
- partial CL (remaining files in other CLs)
R=rsc, r
http://go/go-review/1026036
Diffstat (limited to 'src/pkg/go')
-rw-r--r-- | src/pkg/go/ast/ast.go | 2 | ||||
-rw-r--r-- | src/pkg/go/ast/filter.go | 4 | ||||
-rw-r--r-- | src/pkg/go/doc/comment.go | 14 | ||||
-rw-r--r-- | src/pkg/go/doc/doc.go | 2 | ||||
-rw-r--r-- | src/pkg/go/parser/interface.go | 2 | ||||
-rw-r--r-- | src/pkg/go/parser/parser.go | 20 | ||||
-rw-r--r-- | src/pkg/go/printer/nodes.go | 315 | ||||
-rw-r--r-- | src/pkg/go/printer/printer.go | 28 | ||||
-rw-r--r-- | src/pkg/go/printer/printer_test.go | 6 | ||||
-rw-r--r-- | src/pkg/go/printer/testdata/declarations.golden | 2 | ||||
-rw-r--r-- | src/pkg/go/printer/testdata/expressions.golden | 118 | ||||
-rw-r--r-- | src/pkg/go/printer/testdata/expressions.input | 72 | ||||
-rw-r--r-- | src/pkg/go/printer/testdata/expressions.raw | 118 | ||||
-rw-r--r-- | src/pkg/go/printer/testdata/linebreaks.golden | 2 | ||||
-rw-r--r-- | src/pkg/go/scanner/scanner.go | 24 | ||||
-rw-r--r-- | src/pkg/go/scanner/scanner_test.go | 2 |
16 files changed, 515 insertions, 216 deletions
diff --git a/src/pkg/go/ast/ast.go b/src/pkg/go/ast/ast.go index b501247cb..1dfe2a7ea 100644 --- a/src/pkg/go/ast/ast.go +++ b/src/pkg/go/ast/ast.go @@ -237,7 +237,7 @@ type ( type ChanDir int const ( - SEND ChanDir = 1<<iota; + SEND ChanDir = 1 << iota; RECV; ) diff --git a/src/pkg/go/ast/filter.go b/src/pkg/go/ast/filter.go index 2e86435bf..772407400 100644 --- a/src/pkg/go/ast/filter.go +++ b/src/pkg/go/ast/filter.go @@ -203,7 +203,7 @@ func MergePackageFiles(pkg *Package) *File { ndecls := 0; for _, f := range pkg.Files { if f.Doc != nil { - ncomments += len(f.Doc.List)+1 // +1 for separator + ncomments += len(f.Doc.List) + 1 // +1 for separator } ndecls += len(f.Decls); } @@ -215,7 +215,7 @@ func MergePackageFiles(pkg *Package) *File { // than drop them on the floor. var doc *CommentGroup; if ncomments > 0 { - list := make([]*Comment, ncomments - 1); // -1: no separator before first group + list := make([]*Comment, ncomments-1); // -1: no separator before first group i := 0; for _, f := range pkg.Files { if f.Doc != nil { diff --git a/src/pkg/go/doc/comment.go b/src/pkg/go/doc/comment.go index ba9371439..cf1452ac9 100644 --- a/src/pkg/go/doc/comment.go +++ b/src/pkg/go/doc/comment.go @@ -31,7 +31,7 @@ func CommentText(comment *ast.CommentGroup) string { // Remove comment markers. // The parser has given us exactly the comment text. switch n := len(c); { - case n >= 4 && c[0:2] == "/*" && c[n-2 : n] == "*/": + case n >= 4 && c[0:2] == "/*" && c[n-2:n] == "*/": c = c[2 : n-2] case n >= 2 && c[0:2] == "//": c = c[2:n]; @@ -95,7 +95,7 @@ func split(text []byte) [][]byte { last := 0; for i, c := range text { if c == '\n' { - last = i+1; + last = i + 1; n++; } } @@ -110,7 +110,7 @@ func split(text []byte) [][]byte { for i, c := range text { if c == '\n' { out[n] = text[last : i+1]; - last = i+1; + last = i + 1; n++; } } @@ -134,7 +134,7 @@ func commentEscape(w io.Writer, s []byte) { for i := 0; i < len(s)-1; i++ { if s[i] == s[i+1] && (s[i] == '`' || s[i] == '\'') { template.HTMLEscape(w, s[last:i]); - last = i+2; + last = i + 2; switch s[i] { case '`': w.Write(ldquo) @@ -183,10 +183,10 @@ func unindent(block [][]byte) { } // compute maximum common white prefix - prefix := block[0][0 : indentLen(block[0])]; + prefix := block[0][0:indentLen(block[0])]; for _, line := range block { if !isBlank(line) { - prefix = commonPrefix(prefix, line[0 : indentLen(line)]) + prefix = commonPrefix(prefix, line[0:indentLen(line)]) } } n := len(prefix); @@ -242,7 +242,7 @@ func ToHTML(w io.Writer, s []byte) { close(); // count indented or blank lines - j := i+1; + j := i + 1; for j < len(lines) && (isBlank(lines[j]) || indentLen(lines[j]) > 0) { j++ } diff --git a/src/pkg/go/doc/doc.go b/src/pkg/go/doc/doc.go index 9c85c20be..4ef12641b 100644 --- a/src/pkg/go/doc/doc.go +++ b/src/pkg/go/doc/doc.go @@ -135,7 +135,7 @@ func (doc *docReader) addValue(decl *ast.GenDecl) { // determine values list const threshold = 0.75; values := doc.values; - if domName != "" && domFreq >= int(float(len(decl.Specs)) * threshold) { + if domName != "" && domFreq >= int(float(len(decl.Specs))*threshold) { // typed entries are sufficiently frequent typ := doc.lookupTypeDoc(domName); if typ != nil { diff --git a/src/pkg/go/parser/interface.go b/src/pkg/go/parser/interface.go index 50d7dff65..670489462 100644 --- a/src/pkg/go/parser/interface.go +++ b/src/pkg/go/parser/interface.go @@ -154,7 +154,7 @@ func ParsePkgFile(pkgname, filename string, mode uint) (*ast.File, os.Error) { } // ignore flags that control partial parsing - return ParseFile(filename, src, mode&^(PackageClauseOnly | ImportsOnly)); + return ParseFile(filename, src, mode&^(PackageClauseOnly|ImportsOnly)); } diff --git a/src/pkg/go/parser/parser.go b/src/pkg/go/parser/parser.go index 5e0c7307b..ba91ceb52 100644 --- a/src/pkg/go/parser/parser.go +++ b/src/pkg/go/parser/parser.go @@ -27,7 +27,7 @@ var noPos token.Position // parser functionality. // const ( - PackageClauseOnly uint = 1<<iota; // parsing stops after package clause + 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 @@ -68,7 +68,7 @@ type parser struct { // scannerMode returns the scanner mode bits given the parser's mode bits. func scannerMode(mode uint) uint { - if mode & ParseComments != 0 { + if mode&ParseComments != 0 { return scanner.ScanComments } return 0; @@ -127,7 +127,7 @@ func (p *parser) next0() { case p.tok.IsLiteral(): p.printTrace(s, string(p.lit)) case p.tok.IsOperator(), p.tok.IsKeyword(): - p.printTrace("\""+s+"\"") + p.printTrace("\"" + s + "\"") default: p.printTrace(s) } @@ -246,7 +246,7 @@ func (p *parser) errorExpected(pos token.Position, msg string) { // make the error message more specific msg += ", found '" + p.tok.String() + "'"; if p.tok.IsLiteral() { - msg += " "+string(p.lit) + msg += " " + string(p.lit) } } p.Error(pos, msg); @@ -256,7 +256,7 @@ func (p *parser) errorExpected(pos token.Position, msg string) { func (p *parser) expect(tok token.Token) token.Position { pos := p.pos; if p.tok != tok { - p.errorExpected(pos, "'" + tok.String() + "'") + p.errorExpected(pos, "'"+tok.String()+"'") } p.next(); // make progress in any case return pos; @@ -278,7 +278,7 @@ func close(p *parser) { p.topScope = p.topScope.Outer } func (p *parser) declare(ident *ast.Ident) { if !p.topScope.Declare(ident) { - p.Error(p.pos, "'" + ident.Value + "' declared already") + p.Error(p.pos, "'"+ident.Value+"' declared already") } } @@ -1220,7 +1220,7 @@ func (p *parser) parseBinaryExpr(prec1 int) ast.Expr { for p.tok.Precedence() == prec { pos, op := p.pos, p.tok; p.next(); - y := p.parseBinaryExpr(prec+1); + y := p.parseBinaryExpr(prec + 1); x = &ast.BinaryExpr{p.checkExpr(x), pos, op, p.checkExpr(y)}; } } @@ -1806,7 +1806,7 @@ func parseVarSpec(p *parser, doc *ast.CommentGroup, getSemi bool) (spec ast.Spec func (p *parser) parseGenDecl(keyword token.Token, f parseSpecFunction, getSemi bool) (decl *ast.GenDecl, gotSemi bool) { if p.trace { - defer un(trace(p, keyword.String() + "Decl")) + defer un(trace(p, keyword.String()+"Decl")) } doc := p.leadComment; @@ -1977,7 +1977,7 @@ func (p *parser) parseFile() *ast.File { // 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 { + if p.ErrorCount() == 0 && p.mode&PackageClauseOnly == 0 { // import decls list := vector.New(0); for p.tok == token.IMPORT { @@ -1985,7 +1985,7 @@ func (p *parser) parseFile() *ast.File { list.Push(decl); } - if p.mode & ImportsOnly == 0 { + if p.mode&ImportsOnly == 0 { // rest of package body for p.tok != token.EOF { decl, _ := p.parseDecl(true); // consume optional semicolon diff --git a/src/pkg/go/printer/nodes.go b/src/pkg/go/printer/nodes.go index 639779690..d3c1b4072 100644 --- a/src/pkg/go/printer/nodes.go +++ b/src/pkg/go/printer/nodes.go @@ -10,7 +10,6 @@ package printer import ( "bytes"; - "container/vector"; "go/ast"; "go/token"; ) @@ -116,7 +115,7 @@ func (p *printer) identList(list []*ast.Ident, multiLine *bool) { for i, x := range list { xlist[i] = x } - p.exprList(noPos, xlist, commaSep, multiLine); + p.exprList(noPos, xlist, 1, commaSep, multiLine); } @@ -127,14 +126,14 @@ func (p *printer) stringList(list []*ast.BasicLit, multiLine *bool) { for i, x := range list { xlist[i] = x } - p.exprList(noPos, xlist, stringListMode, multiLine); + p.exprList(noPos, xlist, 1, stringListMode, multiLine); } type exprListMode uint const ( - blankStart exprListMode = 1<<iota; // print a blank before a non-empty list + blankStart exprListMode = 1 << iota; // print a blank before a non-empty list blankEnd; // print a blank after a non-empty list commaSep; // elements are separated by commas commaTerm; // elements are terminated by comma @@ -146,12 +145,12 @@ const ( // source lines, the original line breaks are respected between // expressions. Sets multiLine to true if the list spans multiple // lines. -func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMode, multiLine *bool) { +func (p *printer) exprList(prev token.Position, list []ast.Expr, depth int, mode exprListMode, multiLine *bool) { if len(list) == 0 { return } - if mode & blankStart != 0 { + if mode&blankStart != 0 { p.print(blank) } @@ -170,7 +169,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMo } p.print(blank); } - p.expr(x, multiLine); + p.expr0(x, depth, multiLine); } if mode&blankEnd != 0 { p.print(blank) @@ -209,10 +208,10 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMo p.print(blank) } } - p.expr(x, multiLine); + p.expr0(x, depth, multiLine); } - if mode & commaTerm != 0 { + if mode&commaTerm != 0 { p.print(token.COMMA); if ws == ignore && mode&noIndent == 0 { // unindent if we indented @@ -307,7 +306,7 @@ func (p *printer) isOneLineFieldList(list []*ast.Field) bool { } typeSize := p.nodeSize(f.Type, maxSize); - return namesSize + typeSize <= maxSize; + return namesSize+typeSize <= maxSize; } @@ -318,7 +317,7 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok // no blank between keyword and {} in this case p.print(lbrace, token.LBRACE, rbrace, token.RBRACE); return; - } else if ctxt&(compositeLit | structType) == compositeLit | structType && + } else if ctxt&(compositeLit|structType) == compositeLit|structType && p.isOneLineFieldList(list) { // for now ignore interfaces // small enough - print on one line // (don't use identList and ignore source line breaks) @@ -341,7 +340,7 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok // at least one entry or incomplete p.print(blank, lbrace, token.LBRACE, indent, formfeed); - if ctxt & structType != 0 { + if ctxt&structType != 0 { sep := vtab; if len(list) == 1 { @@ -430,111 +429,179 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok type exprContext uint const ( - compositeLit = 1<<iota; + compositeLit = 1 << iota; structType; ) -func needsBlanks(expr ast.Expr) bool { - switch x := expr.(type) { - case *ast.Ident: - // "long" identifiers look better with blanks around them - return len(x.Value) > 8 - case *ast.BasicLit: - // "long" literals look better with blanks around them - return len(x.Value) > 8 - case *ast.ParenExpr: - // parenthesized expressions don't need blanks around them - return false - case *ast.IndexExpr: - // index expressions don't need blanks if the indexed expressions are simple - return needsBlanks(x.X) - case *ast.CallExpr: - // call expressions need blanks if they have more than one - // argument or if the function expression needs blanks - return len(x.Args) > 1 || needsBlanks(x.Fun) +func walkBinary(e *ast.BinaryExpr) (has5, has6 bool, maxProblem int) { + switch e.Op.Precedence() { + case 5: + has5 = true + case 6: + has6 = true } - return true; + + switch l := e.X.(type) { + case *ast.BinaryExpr: + h5, h6, mp := walkBinary(l); + has5 = has5 || h5; + has6 = has6 || h6; + if maxProblem < mp { + maxProblem = mp + } + } + + switch r := e.Y.(type) { + case *ast.BinaryExpr: + h5, h6, mp := walkBinary(r); + has5 = has5 || h5; + has6 = has6 || h6; + if maxProblem < mp { + maxProblem = mp + } + + case *ast.StarExpr: + if e.Op.String() == "/" { + maxProblem = 6 + } + + case *ast.UnaryExpr: + switch e.Op.String() + r.Op.String() { + case "/*": + maxProblem = 6 + case "++", "--": + if maxProblem < 5 { + maxProblem = 5 + } + } + } + return; +} + + +func cutoff(e *ast.BinaryExpr, depth int) int { + if depth < 1 { + // handle gracefully unless in debug mode + if debug { + panicln("negative depth:", depth) + } + depth = 1; + } + has5, has6, maxProblem := walkBinary(e); + if maxProblem > 0 { + return maxProblem + 1 + } + if has5 && has6 { + if depth == 1 { + return 6 + } + return 5; + } + if depth == 1 { + return 7 + } + return 5; } +func diffPrec(expr ast.Expr, prec int) int { + x, ok := expr.(*ast.BinaryExpr); + if !ok || prec != x.Op.Precedence() { + return 1 + } + return 0; +} + + +// Format the binary expression: decide the cutoff and then format. +// Let's call depth == 1 Normal mode, and depth > 1 Compact mode. +// (Algorithm suggestion by Russ Cox.) +// +// The precedences are: +// 6 * / % << >> & &^ +// 5 + - | ^ +// 4 == != < <= > >= +// 3 <- +// 2 && +// 1 || +// +// The only decision is whether there will be spaces around levels 5 and 6. +// There are never spaces at level 7 (unary), and always spaces at levels 4 and below. +// +// To choose the cutoff, look at the whole expression but excluding primary +// expressions (function calls, parenthesized exprs), and apply these rules: +// +// 1) If there is a binary operator with a right side unary operand +// that would clash without a space, the cutoff must be (in order): +// +// &^ 7 +// /* 7 +// ++ 6 +// -- 6 +// +// 2) If there is a mix of level 6 and level 5 operators, then the cutoff +// is 6 (use spaces to distinguish precedence) in Normal mode +// and 5 (never use spaces) in Compact mode. +// +// 3) If there are no level 5 operators or no level 6 operators, then the +// cutoff is 7 (always use spaces) in Normal mode +// and 5 (never use spaces) in Compact mode. +// // Sets multiLine to true if the binary expression spans multiple lines. -func (p *printer) binaryExpr(x *ast.BinaryExpr, prec1 int, multiLine *bool) { +func (p *printer) binaryExpr(x *ast.BinaryExpr, prec1, cutoff, depth int, multiLine *bool) { prec := x.Op.Precedence(); if prec < prec1 { // parenthesis needed // Note: The parser inserts an ast.ParenExpr node; thus this case // can only occur if the AST is created in a different way. + // parentheses undo one level of depth + depth--; p.print(token.LPAREN); - p.expr(x, multiLine); + p.expr0(x, depth, multiLine); p.print(token.RPAREN); return; } - // Traverse left, collect all operations at same precedence - // and determine if blanks should be printed around operators. - // - // This algorithm assumes that the right-hand side of a binary - // operation has a different (higher) precedence then the current - // node, which is how the parser creates the AST. - var list vector.Vector; - line := x.Y.Pos().Line; - printBlanks := prec <= token.EQL.Precedence() || needsBlanks(x.Y); - for { - list.Push(x); - if t, ok := x.X.(*ast.BinaryExpr); ok && t.Op.Precedence() == prec { - x = t; - prev := line; - line = x.Y.Pos().Line; - if needsBlanks(x.Y) || prev != line { - printBlanks = true - } - } else { - break - } - } - prev := line; - line = x.X.Pos().Line; - if needsBlanks(x.X) || prev != line { - printBlanks = true - } + printBlank := prec < cutoff; - // Print collected operations left-to-right, with blanks if necessary. ws := indent; - p.expr1(x.X, prec, 0, multiLine); - for list.Len() > 0 { - x = list.Pop().(*ast.BinaryExpr); - prev := line; - line = x.Y.Pos().Line; - if printBlanks { - if prev != line { - p.print(blank, x.OpPos, x.Op); - // at least one line break, but respect an extra empty line - // in the source - if p.linebreak(line, 1, 2, ws, true) { - ws = ignore; - *multiLine = true; - } - } else { - p.print(blank, x.OpPos, x.Op, blank) - } - } else { - if prev != line { - panic("internal error") - } - p.print(x.OpPos, x.Op); + p.expr1(x.X, prec, depth+diffPrec(x.X, prec), 0, multiLine); + if printBlank { + p.print(blank) + } + xline := p.pos.Line; // before the operator (it may be on the next line!) + yline := x.Y.Pos().Line; + p.print(x.OpPos, x.Op); + if xline != yline { + //println(x.OpPos.String()); + // at least one line break, but respect an extra empty line + // in the source + if p.linebreak(yline, 1, 2, ws, true) { + ws = ignore; + *multiLine = true; + printBlank = false; // no blank after line break } - p.expr1(x.Y, prec, 0, multiLine); } + if printBlank { + p.print(blank) + } + p.expr1(x.Y, prec, depth+1, 0, multiLine); if ws == ignore { p.print(unindent) } } +func isBinary(expr ast.Expr) bool { + _, ok := expr.(*ast.BinaryExpr); + return ok; +} + + // Returns true if a separating semicolon is optional. // Sets multiLine to true if the expression spans multiple lines. -func (p *printer) expr1(expr ast.Expr, prec1 int, ctxt exprContext, multiLine *bool) (optSemi bool) { +func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multiLine *bool) (optSemi bool) { p.print(expr.Pos()); switch x := expr.(type) { @@ -545,7 +612,7 @@ func (p *printer) expr1(expr ast.Expr, prec1 int, ctxt exprContext, multiLine *b p.print(x) case *ast.BinaryExpr: - p.binaryExpr(x, prec1, multiLine) + p.binaryExpr(x, prec1, cutoff(x, depth), depth, multiLine) case *ast.KeyValueExpr: p.expr(x.Key, multiLine); @@ -569,7 +636,7 @@ func (p *printer) expr1(expr ast.Expr, prec1 int, ctxt exprContext, multiLine *b if x.Op == token.RANGE { p.print(blank) } - p.expr1(x.X, prec, 0, multiLine); + p.expr1(x.X, prec, depth, 0, multiLine); } case *ast.BasicLit: @@ -583,17 +650,19 @@ func (p *printer) expr1(expr ast.Expr, prec1 int, ctxt exprContext, multiLine *b p.funcBody(x.Body, distance(x.Type.Pos(), p.pos), true, multiLine); case *ast.ParenExpr: + // parentheses undo one level of depth + depth--; p.print(token.LPAREN); - p.expr(x.X, multiLine); + p.expr0(x.X, depth, multiLine); p.print(x.Rparen, token.RPAREN); case *ast.SelectorExpr: - p.expr1(x.X, token.HighestPrec, 0, multiLine); + p.expr1(x.X, token.HighestPrec, depth, 0, multiLine); p.print(token.PERIOD); - p.expr1(x.Sel, token.HighestPrec, 0, multiLine); + p.expr1(x.Sel, token.HighestPrec, depth, 0, multiLine); case *ast.TypeAssertExpr: - p.expr1(x.X, token.HighestPrec, 0, multiLine); + p.expr1(x.X, token.HighestPrec, depth, 0, multiLine); p.print(token.PERIOD, token.LPAREN); if x.Type != nil { p.expr(x.Type, multiLine) @@ -603,29 +672,31 @@ func (p *printer) expr1(expr ast.Expr, prec1 int, ctxt exprContext, multiLine *b p.print(token.RPAREN); case *ast.IndexExpr: - p.expr1(x.X, token.HighestPrec, 0, multiLine); + p.expr1(x.X, token.HighestPrec, 1, 0, multiLine); p.print(token.LBRACK); - p.expr1(x.Index, token.LowestPrec, 0, multiLine); + p.expr0(x.Index, depth+1, multiLine); if x.End != nil { - if needsBlanks(x.Index) || needsBlanks(x.End) { - // blanks around ":" + // blanks around ":" if either side is a binary expression + if depth <= 1 && (isBinary(x.Index) || isBinary(x.End)) { p.print(blank, token.COLON, blank) } else { - // no blanks around ":" p.print(token.COLON) } - p.expr(x.End, multiLine); + p.expr0(x.End, depth+1, multiLine); } p.print(token.RBRACK); case *ast.CallExpr: - p.expr1(x.Fun, token.HighestPrec, 0, multiLine); + if len(x.Args) > 1 { + depth++ + } + p.expr1(x.Fun, token.HighestPrec, depth, 0, multiLine); p.print(x.Lparen, token.LPAREN); - p.exprList(x.Lparen, x.Args, commaSep, multiLine); + p.exprList(x.Lparen, x.Args, depth, commaSep, multiLine); p.print(x.Rparen, token.RPAREN); case *ast.CompositeLit: - p.expr1(x.Type, token.HighestPrec, compositeLit, multiLine); + p.expr1(x.Type, token.HighestPrec, depth, compositeLit, multiLine); mode := commaSep | commaTerm; if compositeLitBlank { // add blank padding around composite literal @@ -640,7 +711,7 @@ func (p *printer) expr1(expr ast.Expr, prec1 int, ctxt exprContext, multiLine *b } } p.print(x.Lbrace, token.LBRACE); - p.exprList(x.Lbrace, x.Elts, mode, multiLine); + p.exprList(x.Lbrace, x.Elts, 1, mode, multiLine); p.print(x.Rbrace, token.RBRACE); case *ast.Ellipsis: @@ -656,7 +727,7 @@ func (p *printer) expr1(expr ast.Expr, prec1 int, ctxt exprContext, multiLine *b case *ast.StructType: p.print(token.STRUCT); - p.fieldList(x.Lbrace, x.Fields, x.Rbrace, x.Incomplete, ctxt | structType); + p.fieldList(x.Lbrace, x.Fields, x.Rbrace, x.Incomplete, ctxt|structType); optSemi = true; case *ast.FuncType: @@ -694,10 +765,16 @@ func (p *printer) expr1(expr ast.Expr, prec1 int, ctxt exprContext, multiLine *b } +func (p *printer) expr0(x ast.Expr, depth int, multiLine *bool) (optSemi bool) { + return p.expr1(x, token.LowestPrec, depth, 0, multiLine) +} + + // Returns true if a separating semicolon is optional. // Sets multiLine to true if the expression spans multiple lines. func (p *printer) expr(x ast.Expr, multiLine *bool) (optSemi bool) { - return p.expr1(x, token.LowestPrec, 0, multiLine) + const depth = 1; + return p.expr1(x, token.LowestPrec, depth, 0, multiLine); } @@ -812,16 +889,22 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) { optSemi = p.stmt(s.Stmt, multiLine); case *ast.ExprStmt: - p.expr(s.X, multiLine) + const depth = 1; + p.expr0(s.X, depth, multiLine); case *ast.IncDecStmt: - p.expr(s.X, multiLine); + const depth = 1; + p.expr0(s.X, depth+1, multiLine); p.print(s.Tok); case *ast.AssignStmt: - p.exprList(s.Pos(), s.Lhs, commaSep, multiLine); + var depth = 1; + if len(s.Lhs) > 1 && len(s.Rhs) > 1 { + depth++ + } + p.exprList(s.Pos(), s.Lhs, depth, commaSep, multiLine); p.print(blank, s.TokPos, s.Tok); - p.exprList(s.TokPos, s.Rhs, blankStart | commaSep, multiLine); + p.exprList(s.TokPos, s.Rhs, depth, blankStart|commaSep, multiLine); case *ast.GoStmt: p.print(token.GO, blank); @@ -834,7 +917,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) { case *ast.ReturnStmt: p.print(token.RETURN); if s.Results != nil { - p.exprList(s.Pos(), s.Results, blankStart | commaSep, multiLine) + p.exprList(s.Pos(), s.Results, 1, blankStart|commaSep, multiLine) } case *ast.BranchStmt: @@ -870,7 +953,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) { case *ast.CaseClause: if s.Values != nil { p.print(token.CASE); - p.exprList(s.Pos(), s.Values, blankStart | commaSep, multiLine); + p.exprList(s.Pos(), s.Values, 1, blankStart|commaSep, multiLine); } else { p.print(token.DEFAULT) } @@ -888,7 +971,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) { case *ast.TypeCaseClause: if s.Types != nil { p.print(token.CASE); - p.exprList(s.Pos(), s.Types, blankStart | commaSep, multiLine); + p.exprList(s.Pos(), s.Types, 1, blankStart|commaSep, multiLine); } else { p.print(token.DEFAULT) } @@ -1003,7 +1086,7 @@ func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *boo } if s.Values != nil { p.print(blank, token.ASSIGN); - p.exprList(noPos, s.Values, blankStart | commaSep, multiLine); + p.exprList(noPos, s.Values, 1, blankStart|commaSep, multiLine); optSemi = false; } } else { @@ -1018,7 +1101,7 @@ func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *boo if s.Values != nil { p.print(vtab); p.print(token.ASSIGN); - p.exprList(noPos, s.Values, blankStart | commaSep, multiLine); + p.exprList(noPos, s.Values, 1, blankStart|commaSep, multiLine); optSemi = false; extraTabs = 0; } @@ -1089,7 +1172,7 @@ func (p *printer) genDecl(d *ast.GenDecl, context declContext, multiLine *bool) // any control chars. Otherwise, the result is > maxSize. // func (p *printer) nodeSize(n ast.Node, maxSize int) (size int) { - size = maxSize+1; // assume n doesn't fit + size = maxSize + 1; // assume n doesn't fit // nodeSize computation must be indendent of particular // style so that we always get the same decision; print // in RawFormat @@ -1120,7 +1203,7 @@ func (p *printer) isOneLineFunc(b *ast.BlockStmt, headerSize int) bool { bodySize = p.nodeSize(b.List[0], maxSize) } // require both headers and overall size to be not "too large" - return headerSize <= maxSize/2 && headerSize + bodySize <= maxSize; + return headerSize <= maxSize/2 && headerSize+bodySize <= maxSize; } @@ -1158,7 +1241,7 @@ func distance(from, to token.Position) int { if from.IsValid() && to.IsValid() && from.Line == to.Line { return to.Column - from.Column } - return 1<<30; + return 1 << 30; } diff --git a/src/pkg/go/printer/printer.go b/src/pkg/go/printer/printer.go index 4c3511c61..6497fc81a 100644 --- a/src/pkg/go/printer/printer.go +++ b/src/pkg/go/printer/printer.go @@ -127,7 +127,7 @@ func (p *printer) write(data []byte) { p.write0(data[i0 : i+1]); // update p.pos - p.pos.Offset += i+1-i0; + p.pos.Offset += i + 1 - i0; p.pos.Line++; p.pos.Column = 1; @@ -147,10 +147,10 @@ func (p *printer) write(data []byte) { } // next segment start - i0 = i+1; + i0 = i + 1; case '"', '\'', '&', '<', '>': - if p.Mode & GenHTML != 0 { + if p.Mode&GenHTML != 0 { // write segment ending in b p.write0(data[i0:i]); @@ -171,12 +171,12 @@ func (p *printer) write(data []byte) { p.write0(esc); // update p.pos - d := i+1-i0; + d := i + 1 - i0; p.pos.Offset += d; p.pos.Column += d; // next segment start - i0 = i+1; + i0 = i + 1; } case tabwriter.Escape: @@ -188,7 +188,7 @@ func (p *printer) write(data []byte) { p.write0(data[i0:len(data)]); // update p.pos - d := len(data)-i0; + d := len(data) - i0; p.pos.Offset += d; p.pos.Column += d; } @@ -241,7 +241,7 @@ func (p *printer) writeItem(pos token.Position, data []byte, tag HTMLTag) { // do not update p.pos - use write0 p.write0(strings.Bytes(fmt.Sprintf("[%d:%d]", pos.Line, pos.Column))) } - if p.Mode & GenHTML != 0 { + if p.Mode&GenHTML != 0 { // write line tag if on a new line // TODO(gri): should write line tags on each line at the start // will be more useful (e.g. to show line numbers) @@ -387,7 +387,7 @@ func split(text []byte) [][]byte { for j, c := range text { if c == '\n' { lines[n] = text[i:j]; // exclude newline - i = j+1; // discard newline + i = j + 1; // discard newline n++; } } @@ -740,7 +740,7 @@ func (p *printer) print(args ...) { // (note that valid Go programs cannot contain esc ('\xff') // bytes since they do not appear in legal UTF-8 sequences) // TODO(gri): this this more efficiently. - data = strings.Bytes("\xff"+string(data)+"\xff"); + data = strings.Bytes("\xff" + string(data) + "\xff"); case token.Token: if p.Styler != nil { data, tag = p.Styler.Token(x) @@ -884,7 +884,7 @@ func (p *trimmer) Write(data []byte) (n int, err os.Error) { // General printing is controlled with these Config.Mode flags. const ( - GenHTML uint = 1<<iota; // generate HTML + GenHTML uint = 1 << iota; // generate HTML RawFormat; // do not use a tabwriter; if set, UseSpaces is ignored UseSpaces; // use spaces instead of tabs for indentation and alignment ) @@ -930,13 +930,13 @@ func (cfg *Config) Fprint(output io.Writer, node interface{}) (int, os.Error) { // setup tabwriter if needed and redirect output var tw *tabwriter.Writer; - if cfg.Mode & RawFormat == 0 { + if cfg.Mode&RawFormat == 0 { padchar := byte('\t'); - if cfg.Mode & UseSpaces != 0 { + if cfg.Mode&UseSpaces != 0 { padchar = ' ' } twmode := tabwriter.DiscardEmptyColumns; - if cfg.Mode & GenHTML != 0 { + if cfg.Mode&GenHTML != 0 { twmode |= tabwriter.FilterHTML } tw = tabwriter.NewWriter(output, cfg.Tabwidth, 1, padchar, twmode); @@ -961,7 +961,7 @@ func (cfg *Config) Fprint(output io.Writer, node interface{}) (int, os.Error) { p.errors <- os.NewError(fmt.Sprintf("printer.Fprint: unsupported node type %T", n)); runtime.Goexit(); } - p.flush(token.Position{Offset: 1<<30, Line: 1<<30}, false); // flush to "infinity" + p.flush(token.Position{Offset: 1 << 30, Line: 1 << 30}, false); // flush to "infinity" p.errors <- nil; // no errors }(); err := <-p.errors; // wait for completion of goroutine diff --git a/src/pkg/go/printer/printer_test.go b/src/pkg/go/printer/printer_test.go index 6cafc5820..5c10d4a85 100644 --- a/src/pkg/go/printer/printer_test.go +++ b/src/pkg/go/printer/printer_test.go @@ -36,7 +36,7 @@ func lineString(text []byte, i int) string { type checkMode uint const ( - export checkMode = 1<<iota; + export checkMode = 1 << iota; rawFormat; ) @@ -57,7 +57,7 @@ func check(t *testing.T, source, golden string, mode checkMode) { // determine printer configuration cfg := Config{Tabwidth: tabwidth}; - if mode & rawFormat != 0 { + if mode&rawFormat != 0 { cfg.Mode |= RawFormat } @@ -99,7 +99,7 @@ func check(t *testing.T, source, golden string, mode checkMode) { } if ch == '\n' { line++; - offs = i+1; + offs = i + 1; } } } diff --git a/src/pkg/go/printer/testdata/declarations.golden b/src/pkg/go/printer/testdata/declarations.golden index 43ba1fd53..70be9aa2e 100644 --- a/src/pkg/go/printer/testdata/declarations.golden +++ b/src/pkg/go/printer/testdata/declarations.golden @@ -454,5 +454,5 @@ func _() {} func _() {} func _() { f(1, 2, 3) } -func _(x int) int { return x+1 } +func _(x int) int { return x + 1 } func _() int { type T struct{} } diff --git a/src/pkg/go/printer/testdata/expressions.golden b/src/pkg/go/printer/testdata/expressions.golden index efca110ca..1d785d91f 100644 --- a/src/pkg/go/printer/testdata/expressions.golden +++ b/src/pkg/go/printer/testdata/expressions.golden @@ -20,27 +20,27 @@ var ( func _() { // no spaces around simple or parenthesized expressions - _ = a+b; - _ = a+b+c; - _ = a+b-c; - _ = a-b-c; - _ = a+(b*c); - _ = a+(b/c); - _ = a-(b%c); - _ = 1+a; - _ = a+1; - _ = a+b+1; + _ = a + b; + _ = a + b + c; + _ = a + b - c; + _ = a - b - c; + _ = a + (b * c); + _ = a + (b / c); + _ = a - (b % c); + _ = 1 + a; + _ = a + 1; + _ = a + b + 1; _ = s[1:2]; _ = s[a:b]; _ = s[0:len(s)]; - _ = s[0]<<1; - _ = (s[0]<<1)&0xf; + _ = s[0] << 1; + _ = (s[0] << 1) & 0xf; _ = s[0]<<2 | s[1]>>4; - _ = "foo"+s; - _ = s+"foo"; - _ = 'a'+'b'; - _ = len(s)/2; - _ = len(t0.x)/a; + _ = "foo" + s; + _ = s + "foo"; + _ = 'a' + 'b'; + _ = len(s) / 2; + _ = len(t0.x) / a; // spaces around expressions of different precedence or expressions containing spaces _ = a + -b; @@ -54,9 +54,9 @@ func _() { _ = s[1 : 2*3]; _ = s[a : b-c]; _ = s[a+b : len(s)]; - _ = s[len(s) : -a]; + _ = s[len(s):-a]; _ = s[a : len(s)+1]; - _ = s[a : len(s)+1]+s; + _ = s[a:len(s)+1] + s; // spaces around operators with equal or lower precedence than comparisons _ = a == b; @@ -71,16 +71,16 @@ func _() { // spaces around "long" operands _ = a + longIdentifier1; _ = longIdentifier1 + a; - _ = longIdentifier1 + longIdentifier2 * longIdentifier3; + _ = longIdentifier1 + longIdentifier2*longIdentifier3; _ = s + "a longer string"; // some selected cases _ = a + t0.x; - _ = a + t0.x + t1.x * t2.x; + _ = a + t0.x + t1.x*t2.x; _ = a + b + c + d + e + 2*3; _ = a + b + c + 2*3 + d + e; - _ = (a+b+c)*2; - _ = a - b + c - d + (a+b+c) + d&e; + _ = (a + b + c) * 2; + _ = a - b + c - d + (a + b + c) + d&e; _ = under_bar - 1; _ = Open(dpath+"/file", O_WRONLY|O_CREAT, 0666); _ = int(c0&_Mask4)<<18 | int(c1&_Maskx)<<12 | int(c2&_Maskx)<<6 | int(c3&_Maskx); @@ -88,6 +88,78 @@ func _() { func _() { + a + b; + a + b + c; + a + b*c; + a + (b * c); + (a + b) * c; + a + (b * c * d); + a + (b*c + d); + + 1 << x; + -1 << x; + 1<<x - 1; + -1<<x - 1; + + f(a + b); + f(a + b + c); + f(a + b*c); + f(a + (b * c)); + f(1<<x-1, 1<<x-2); + + 1<<d.logWindowSize - 1; + + buf = make(x, 2*cap(b.buf)+n); + + dst[i*3+2] = dbuf[0] << 2; + dst[i*3+2] = dbuf[0]<<2 | dbuf[1]>>4; + + b.buf = b.buf[0 : b.off+m+n]; + b.buf = b.buf[0 : b.off+m*n]; + f(b.buf[0 : b.off+m+n]); + + signed += ' ' * 8; + tw.octal(header[148:155], chksum); + + x > 0 && i >= 0; + + x1, x0 := x>>w2, x&m2; + z0 = t1<<w2 + t0; + z1 = (t1 + t0>>w2) >> w2; + q1, r1 := x1/d1, x1%d1; + r1 = r1*b2 | x0>>w2; + x1 = (x1 << z) | (x0 >> (uint(w) - z)); + x1 = x1<<z | x0>>(uint(w)-z); + + buf[0 : len(buf)+1]; + buf[0 : n+1]; + + a, b = b, a; + a = b + c; + a = b*c + d; + a*b + c; + a - b - c; + a - (b - c); + a - b*c; + a - (b * c); + a * b / c; + a / *b; + x[a|^b]; + x[a / *b]; + a & ^b; + a + +b; + a - -b; + x[a*-b]; + x[a + +b]; + x ^ y ^ z; + b[a>>24] ^ b[(a>>16)&0xFF] ^ b[(a>>8)&0xFF] ^ b[a&0xFF]; + len(longVariableName) * 2; + + token(matchType + xlength<<lengthShift + xoffset); +} + + +func _() { _ = T{}; _ = struct{}{}; _ = [10]T{}; diff --git a/src/pkg/go/printer/testdata/expressions.input b/src/pkg/go/printer/testdata/expressions.input index 387a1a976..6ccc9a833 100644 --- a/src/pkg/go/printer/testdata/expressions.input +++ b/src/pkg/go/printer/testdata/expressions.input @@ -88,6 +88,78 @@ func _() { func _() { + a+b; + a+b+c; + a+b*c; + a+(b*c); + (a+b)*c; + a+(b*c*d); + a+(b*c+d); + + 1<<x; + -1<<x; + 1<<x-1; + -1<<x-1; + + f(a+b); + f(a+b+c); + f(a+b*c); + f(a+(b*c)); + f(1<<x-1, 1<<x-2); + + 1<<d.logWindowSize-1; + + buf = make(x, 2*cap(b.buf) + n); + + dst[i*3+2] = dbuf[0]<<2; + dst[i*3+2] = dbuf[0]<<2 | dbuf[1]>>4; + + b.buf = b.buf[0:b.off+m+n]; + b.buf = b.buf[0:b.off+m*n]; + f(b.buf[0:b.off+m+n]); + + signed += ' '*8; + tw.octal(header[148:155], chksum); + + x > 0 && i >= 0; + + x1, x0 := x>>w2, x&m2; + z0 = t1<<w2+t0; + z1 = (t1+t0>>w2)>>w2; + q1, r1 := x1/d1, x1%d1; + r1 = r1*b2 | x0>>w2; + x1 = (x1<<z)|(x0>>(uint(w)-z)); + x1 = x1<<z | x0>>(uint(w)-z); + + buf[0:len(buf)+1]; + buf[0:n+1]; + + a,b = b,a; + a = b+c; + a = b*c+d; + a*b+c; + a-b-c; + a-(b-c); + a-b*c; + a-(b*c); + a*b/c; + a/ *b; + x[a|^b]; + x[a/ *b]; + a& ^b; + a+ +b; + a- -b; + x[a*-b]; + x[a+ +b]; + x^y^z; + b[a>>24] ^ b[(a>>16)&0xFF] ^ b[(a>>8)&0xFF] ^ b[a&0xFF]; + len(longVariableName)*2; + + token(matchType + xlength<<lengthShift + xoffset); +} + + +func _() { _ = T{}; _ = struct{}{}; _ = [10]T{}; diff --git a/src/pkg/go/printer/testdata/expressions.raw b/src/pkg/go/printer/testdata/expressions.raw index 29109ba61..55986e2e6 100644 --- a/src/pkg/go/printer/testdata/expressions.raw +++ b/src/pkg/go/printer/testdata/expressions.raw @@ -20,27 +20,27 @@ var ( func _() { // no spaces around simple or parenthesized expressions - _ = a+b; - _ = a+b+c; - _ = a+b-c; - _ = a-b-c; - _ = a+(b*c); - _ = a+(b/c); - _ = a-(b%c); - _ = 1+a; - _ = a+1; - _ = a+b+1; + _ = a + b; + _ = a + b + c; + _ = a + b - c; + _ = a - b - c; + _ = a + (b * c); + _ = a + (b / c); + _ = a - (b % c); + _ = 1 + a; + _ = a + 1; + _ = a + b + 1; _ = s[1:2]; _ = s[a:b]; _ = s[0:len(s)]; - _ = s[0]<<1; - _ = (s[0]<<1)&0xf; + _ = s[0] << 1; + _ = (s[0] << 1) & 0xf; _ = s[0]<<2 | s[1]>>4; - _ = "foo"+s; - _ = s+"foo"; - _ = 'a'+'b'; - _ = len(s)/2; - _ = len(t0.x)/a; + _ = "foo" + s; + _ = s + "foo"; + _ = 'a' + 'b'; + _ = len(s) / 2; + _ = len(t0.x) / a; // spaces around expressions of different precedence or expressions containing spaces _ = a + -b; @@ -54,9 +54,9 @@ func _() { _ = s[1 : 2*3]; _ = s[a : b-c]; _ = s[a+b : len(s)]; - _ = s[len(s) : -a]; + _ = s[len(s):-a]; _ = s[a : len(s)+1]; - _ = s[a : len(s)+1]+s; + _ = s[a:len(s)+1] + s; // spaces around operators with equal or lower precedence than comparisons _ = a == b; @@ -71,16 +71,16 @@ func _() { // spaces around "long" operands _ = a + longIdentifier1; _ = longIdentifier1 + a; - _ = longIdentifier1 + longIdentifier2 * longIdentifier3; + _ = longIdentifier1 + longIdentifier2*longIdentifier3; _ = s + "a longer string"; // some selected cases _ = a + t0.x; - _ = a + t0.x + t1.x * t2.x; + _ = a + t0.x + t1.x*t2.x; _ = a + b + c + d + e + 2*3; _ = a + b + c + 2*3 + d + e; - _ = (a+b+c)*2; - _ = a - b + c - d + (a+b+c) + d&e; + _ = (a + b + c) * 2; + _ = a - b + c - d + (a + b + c) + d&e; _ = under_bar - 1; _ = Open(dpath+"/file", O_WRONLY|O_CREAT, 0666); _ = int(c0&_Mask4)<<18 | int(c1&_Maskx)<<12 | int(c2&_Maskx)<<6 | int(c3&_Maskx); @@ -88,6 +88,78 @@ func _() { func _() { + a + b; + a + b + c; + a + b*c; + a + (b * c); + (a + b) * c; + a + (b * c * d); + a + (b*c + d); + + 1 << x; + -1 << x; + 1<<x - 1; + -1<<x - 1; + + f(a + b); + f(a + b + c); + f(a + b*c); + f(a + (b * c)); + f(1<<x-1, 1<<x-2); + + 1<<d.logWindowSize - 1; + + buf = make(x, 2*cap(b.buf)+n); + + dst[i*3+2] = dbuf[0] << 2; + dst[i*3+2] = dbuf[0]<<2 | dbuf[1]>>4; + + b.buf = b.buf[0 : b.off+m+n]; + b.buf = b.buf[0 : b.off+m*n]; + f(b.buf[0 : b.off+m+n]); + + signed += ' ' * 8; + tw.octal(header[148:155], chksum); + + x > 0 && i >= 0; + + x1, x0 := x>>w2, x&m2; + z0 = t1<<w2 + t0; + z1 = (t1 + t0>>w2) >> w2; + q1, r1 := x1/d1, x1%d1; + r1 = r1*b2 | x0>>w2; + x1 = (x1 << z) | (x0 >> (uint(w) - z)); + x1 = x1<<z | x0>>(uint(w)-z); + + buf[0 : len(buf)+1]; + buf[0 : n+1]; + + a, b = b, a; + a = b + c; + a = b*c + d; + a*b + c; + a - b - c; + a - (b - c); + a - b*c; + a - (b * c); + a * b / c; + a / *b; + x[a|^b]; + x[a / *b]; + a & ^b; + a + +b; + a - -b; + x[a*-b]; + x[a + +b]; + x ^ y ^ z; + b[a>>24] ^ b[(a>>16)&0xFF] ^ b[(a>>8)&0xFF] ^ b[a&0xFF]; + len(longVariableName) * 2; + + token(matchType + xlength<<lengthShift + xoffset); +} + + +func _() { _ = T{}; _ = struct{}{}; _ = [10]T{}; diff --git a/src/pkg/go/printer/testdata/linebreaks.golden b/src/pkg/go/printer/testdata/linebreaks.golden index afc5e7b4f..22ac8dd30 100644 --- a/src/pkg/go/printer/testdata/linebreaks.golden +++ b/src/pkg/go/printer/testdata/linebreaks.golden @@ -70,7 +70,7 @@ var writerTests = []*writerTest{ Mode: 0640, Uid: 73025, Gid: 5000, - Size: 16<<30, + Size: 16 << 30, Mtime: 1254699560, Typeflag: '0', Uname: "dsymonds", diff --git a/src/pkg/go/scanner/scanner.go b/src/pkg/go/scanner/scanner.go index cc4ff9cc4..8391c693e 100644 --- a/src/pkg/go/scanner/scanner.go +++ b/src/pkg/go/scanner/scanner.go @@ -52,7 +52,7 @@ func (S *Scanner) next() { S.pos.Column = 0; case r >= 0x80: // not ASCII - r, w = utf8.DecodeRune(S.src[S.offset : len(S.src)]) + r, w = utf8.DecodeRune(S.src[S.offset:len(S.src)]) } S.offset += w; S.ch = r; @@ -67,7 +67,7 @@ func (S *Scanner) next() { // They control scanner behavior. // const ( - ScanComments = 1<<iota; // return comments as COMMENT tokens + ScanComments = 1 << iota; // return comments as COMMENT tokens AllowIllegalChars; // do not report an error for illegal chars ) @@ -132,7 +132,7 @@ func (S *Scanner) error(pos token.Position, msg string) { func (S *Scanner) expect(ch int) { if S.ch != ch { - S.error(S.pos, "expected " + charString(ch) + ", found " + charString(S.ch)) + S.error(S.pos, "expected "+charString(ch)+", found "+charString(S.ch)) } S.next(); // always make progress } @@ -151,7 +151,7 @@ func (S *Scanner) scanComment(pos token.Position) { // '\n' is not part of the comment // (the comment ends on the same line where it started) if pos.Column == 1 { - text := S.src[pos.Offset + 2 : S.pos.Offset]; + text := S.src[pos.Offset+2 : S.pos.Offset]; if bytes.HasPrefix(text, prefix) { // comment starts at beginning of line with "//line "; // get filename and line number, if any @@ -202,18 +202,18 @@ func (S *Scanner) scanIdentifier() token.Token { for isLetter(S.ch) || isDigit(S.ch) { S.next() } - return token.Lookup(S.src[pos : S.pos.Offset]); + return token.Lookup(S.src[pos:S.pos.Offset]); } func digitVal(ch int) int { switch { case '0' <= ch && ch <= '9': - return ch-'0' + return ch - '0' case 'a' <= ch && ch <= 'f': - return ch-'a'+10 + return ch - 'a' + 10 case 'A' <= ch && ch <= 'F': - return ch-'A'+10 + return ch - 'A' + 10 } return 16; // larger than any legal digit val } @@ -501,7 +501,7 @@ scan_again: if S.ch == '/' || S.ch == '*' { S.scanComment(pos); tok = token.COMMENT; - if S.mode & ScanComments == 0 { + if S.mode&ScanComments == 0 { goto scan_again } } else { @@ -534,13 +534,13 @@ scan_again: case '|': tok = S.switch3(token.OR, token.OR_ASSIGN, '|', token.LOR) default: - if S.mode & AllowIllegalChars == 0 { - S.error(pos, "illegal character " + charString(ch)) + if S.mode&AllowIllegalChars == 0 { + S.error(pos, "illegal character "+charString(ch)) } } } - return pos, tok, S.src[pos.Offset : S.pos.Offset]; + return pos, tok, S.src[pos.Offset:S.pos.Offset]; } diff --git a/src/pkg/go/scanner/scanner_test.go b/src/pkg/go/scanner/scanner_test.go index 3bdd71e64..be1b44ec7 100644 --- a/src/pkg/go/scanner/scanner_test.go +++ b/src/pkg/go/scanner/scanner_test.go @@ -233,7 +233,7 @@ func TestScan(t *testing.T) { if tokenclass(tok) != e.class { t.Errorf("bad class for %s: got %d, expected %d", lit, tokenclass(tok), e.class) } - epos.Offset += len(lit)+len(whitespace); + epos.Offset += len(lit) + len(whitespace); epos.Line += NewlineCount(lit) + whitespace_linecount; if tok == token.COMMENT && litb[1] == '/' { // correct for unaccounted '/n' in //-style comment |