]> Cypherpunks repositories - gostls13.git/commitdiff
- minor cleanups
authorRobert Griesemer <gri@golang.org>
Tue, 10 Nov 2009 06:30:07 +0000 (22:30 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 10 Nov 2009 06:30:07 +0000 (22:30 -0800)
- better debugging support
- gofmt -l src misc | wc -l is 0

R=rsc
http://go/go-review/1024042

src/pkg/go/printer/nodes.go
src/pkg/go/printer/printer.go

index d3c1b40722ac51aeebf9a29ad1905e7ab9c1a01c..243c168a7fca339531c6aa9476e2375edd55b99e 100644 (file)
@@ -110,7 +110,7 @@ func (p *printer) lineComment(d *ast.CommentGroup) {
 
 // Sets multiLine to true if the identifier list spans multiple lines.
 func (p *printer) identList(list []*ast.Ident, multiLine *bool) {
-       // convert into an expression list
+       // convert into an expression list so we can re-use exprList formatting
        xlist := make([]ast.Expr, len(list));
        for i, x := range list {
                xlist[i] = x
@@ -121,7 +121,7 @@ func (p *printer) identList(list []*ast.Ident, multiLine *bool) {
 
 // Sets multiLine to true if the string list spans multiple lines.
 func (p *printer) stringList(list []*ast.BasicLit, multiLine *bool) {
-       // convert into an expression list
+       // convert into an expression list so we can re-use exprList formatting
        xlist := make([]ast.Expr, len(list));
        for i, x := range list {
                xlist[i] = x
@@ -481,13 +481,6 @@ func walkBinary(e *ast.BinaryExpr) (has5, has6 bool, maxProblem int) {
 
 
 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
@@ -555,10 +548,8 @@ func (p *printer) binaryExpr(x *ast.BinaryExpr, prec1, cutoff, depth int, multiL
                // 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.expr0(x, depth, multiLine);
+               p.expr0(x, depth-1, multiLine); // parentheses undo one level of depth
                p.print(token.RPAREN);
                return;
        }
@@ -612,7 +603,11 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
                p.print(x)
 
        case *ast.BinaryExpr:
-               p.binaryExpr(x, prec1, cutoff(x, depth), depth, multiLine)
+               if depth < 1 {
+                       p.internalError("depth < 1:", depth);
+                       depth = 1;
+               }
+               p.binaryExpr(x, prec1, cutoff(x, depth), depth, multiLine);
 
        case *ast.KeyValueExpr:
                p.expr(x.Key, multiLine);
@@ -650,10 +645,8 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
                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.expr0(x.X, depth, multiLine);
+               p.expr0(x.X, depth-1, multiLine);       // parentheses undo one level of depth
                p.print(x.Rparen, token.RPAREN);
 
        case *ast.SelectorExpr:
index 6497fc81ab98d719a5fea7d1bbfc3d5bff6065c0..a0b64c56cf8b80fb7e281acd411b33af011b986c 100644 (file)
@@ -101,6 +101,15 @@ func (p *printer) init(output io.Writer, cfg *Config) {
 }
 
 
+func (p *printer) internalError(msg ...) {
+       if debug {
+               fmt.Print(p.pos.String() + ": ");
+               fmt.Println(msg);
+               panic();
+       }
+}
+
+
 // write0 writes raw (uninterpreted) data to p.output and handles errors.
 // write0 does not indent after newlines, and does not HTML-escape or update p.pos.
 //
@@ -635,10 +644,7 @@ func (p *printer) writeWhitespace(n int) {
                case unindent:
                        p.indent--;
                        if p.indent < 0 {
-                               // handle gracefully unless in debug mode
-                               if debug {
-                                       panicln("negative indentation:", p.indent)
-                               }
+                               p.internalError("negative indentation:", p.indent);
                                p.indent = 0;
                        }
                case newline, formfeed: