// 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
// 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
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
// 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;
}
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);
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:
}
+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.
//
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: