1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file implements printing of AST nodes; specifically
// expressions, statements, declarations, and files. It uses
// the print functionality implemented in printer.go.
package printer
import (
"bytes"
"go/ast"
"go/token"
)
// Other formatting issues:
// - better comment formatting for /*-style comments at the end of a line (e.g. a declaration)
// when the comment spans multiple lines; if such a comment is just two lines, formatting is
// not idempotent
// - formatting of expression lists
// - should use blank instead of tab to separate one-line function bodies from
// the function header unless there is a group of consecutive one-liners
// ----------------------------------------------------------------------------
// Common AST nodes.
// Print as many newlines as necessary (but at least min and and at most
// max newlines) to get to the current line. ws is printed before the first
// line break. If newSection is set, the first line break is printed as
// formfeed. Returns true if any line break was printed; returns false otherwise.
//
// TODO(gri): Reconsider signature (provide position instead of line)
//
func (p *printer) linebreak(line, min, max int, ws whiteSpace, newSection bool) (printedBreak bool) {
n := line - p.pos.Line
switch {
case n < min:
n = min
case n > max:
n = max
}
if n > 0 {
p.print(ws)
if newSection {
p.print(formfeed)
n--
printedBreak = true
}
}
for ; n > 0; n-- {
p.print(newline)
printedBreak = true
}
return
}
// setComment sets g as the next comment if g != nil and if node comments
// are enabled - this mode is used when printing source code fragments such
// as exports only. It assumes that there are no other pending comments to
// intersperse.
func (p *printer) setComment(g *ast.CommentGroup) {
if g == nil || !p.useNodeComments {
return
}
if p.comments == nil {
// initialize p.comments lazily
p.comments = make([]*ast.CommentGroup, 1)
} else if p.cindex < len(p.comments) {
// for some reason there are pending comments; this
// should never happen - handle gracefully and flush
// all comments up to g, ignore anything after that
p.flush(g.List[0].Pos(), false)
}
p.comments[0] = g
p.cindex = 0
}
type exprListMode uint
const (
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 // list is optionally terminated by a comma
noIndent // no extra indentation in multi-line lists
)
// Sets multiLine to true if the identifier list spans multiple lines.
// If ident is set, a multi-line identifier list is indented after the
// first linebreak encountered.
func (p *printer) identList(list []*ast.Ident, indent bool, multiLine *bool) {
// 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
}
mode := commaSep
if !indent {
mode |= noIndent
}
p.exprList(noPos, xlist, 1, mode, multiLine, noPos)
}
// Compute the key size of a key:value expression.
// Returns 0 if the expression doesn't fit onto a single line.
func (p *printer) keySize(pair *ast.KeyValueExpr) int {
const infinity = 1e6 // larger than any source line
if p.nodeSize(pair, infinity) <= infinity {
// entire expression fits on one line - return key size
return p.nodeSize(pair.Key, infinity)
}
return 0
}
// Print a list of expressions. If the list spans multiple
// source lines, the original line breaks are respected between
// expressions. Sets multiLine to true if the list spans multiple
// lines.
//
// TODO(gri) Consider rewriting this to be independent of []ast.Expr
// so that we can use the algorithm for any kind of list
// (e.g., pass list via a channel over which to range).
func (p *printer) exprList(prev token.Position, list []ast.Expr, depth int, mode exprListMode, multiLine *bool, next token.Position) {
if len(list) == 0 {
return
}
if mode&blankStart != 0 {
p.print(blank)
}
line := list[0].Pos().Line
endLine := next.Line
if endLine == 0 {
// TODO(gri): endLine may be incorrect as it is really the beginning
// of the last list entry. There may be only one, very long
// entry in which case line == endLine.
endLine = list[len(list)-1].Pos().Line
}
if prev.IsValid() && prev.Line == line && line == endLine {
// all list entries on a single line
for i, x := range list {
if i > 0 {
if mode&commaSep != 0 {
p.print(token.COMMA)
}
p.print(blank)
}
p.expr0(x, depth, multiLine)
}
if mode&blankEnd != 0 {
p.print(blank)
}
return
}
// list entries span multiple lines;
// use source code positions to guide line breaks
// don't add extra indentation if noIndent is set;
// i.e., pretend that the first line is already indented
ws := ignore
if mode&noIndent == 0 {
ws = indent
}
// the first linebreak is always a formfeed since this section must not
// depend on any previous formatting
prevBreak := -1 // index of last expression that was followed by a linebreak
if prev.IsValid() && prev.Line < line && p.linebreak(line, 1, 2, ws, true) {
ws = ignore
*multiLine = true
prevBreak = 0
}
// initialize expression/key size: a zero value indicates expr/key doesn't fit on a single line
size := 0
// print all list elements
for i, x := range list {
prevLine := line
line = x.Pos().Line
// determine if the next linebreak, if any, needs to use formfeed:
// in general, use the entire node size to make the decision; for
// key:value expressions, use the key size
// TODO(gri) for a better result, should probably incorporate both
// the key and the node size into the decision process
useFF := true
// determine size
prevSize := size
const infinity = 1e6 // larger than any source line
size = p.nodeSize(x, infinity)
pair, isPair := x.(*ast.KeyValueExpr)
if size <= infinity {
// x fits on a single line
if isPair {
size = p.nodeSize(pair.Key, infinity) // size <= infinity
}
} else {
size = 0
}
// if the previous line and the current line had single-
// line-expressions and the key sizes are small or the
// the ratio between the key sizes does not exceed a
// threshold, align columns and do not use formfeed
if prevSize > 0 && size > 0 {
const smallSize = 20
if prevSize <= smallSize && size <= smallSize {
useFF = false
} else {
const r = 4 // threshold
ratio := float(size) / float(prevSize)
useFF = ratio <= 1/r || r <= ratio
}
}
if i > 0 {
if mode&commaSep != 0 {
p.print(token.COMMA)
}
if prevLine < line && prevLine > 0 && line > 0 {
// lines are broken using newlines so comments remain aligned
// unless forceFF is set or there are multiple expressions on
// the same line in which case formfeed is used
// broken with a formfeed
if p.linebreak(line, 1, 2, ws, useFF || prevBreak+1 < i) {
ws = ignore
*multiLine = true
prevBreak = i
}
} else {
p.print(blank)
}
}
if isPair && size > 0 && len(list) > 1 {
// we have a key:value expression that fits onto one line and
// is in a list with more then one entry: use a column for the
// key such that consecutive entries can align if possible
p.expr(pair.Key, multiLine)
p.print(pair.Colon, token.COLON, vtab)
p.expr(pair.Value, multiLine)
} else {
p.expr0(x, depth, multiLine)
}
}
if mode&commaTerm != 0 && next.IsValid() && p.pos.Line < next.Line {
// print a terminating comma if the next token is on a new line
p.print(token.COMMA)
if ws == ignore && mode&noIndent == 0 {
// unindent if we indented
p.print(unindent)
}
p.print(formfeed) // terminating comma needs a line break to look good
return
}
if mode&blankEnd != 0 {
p.print(blank)
}
if ws == ignore && mode&noIndent == 0 {
// unindent if we indented
p.print(unindent)
}
}
// Sets multiLine to true if the the parameter list spans multiple lines.
func (p *printer) parameters(fields *ast.FieldList, multiLine *bool) {
p.print(fields.Opening, token.LPAREN)
if len(fields.List) > 0 {
for i, par := range fields.List {
if i > 0 {
p.print(token.COMMA, blank)
}
if len(par.Names) > 0 {
p.identList(par.Names, false, multiLine)
p.print(blank)
}
p.expr(par.Type, multiLine)
}
}
p.print(fields.Closing, token.RPAREN)
}
// Sets multiLine to true if the signature spans multiple lines.
func (p *printer) signature(params, result *ast.FieldList, multiLine *bool) {
p.parameters(params, multiLine)
n := result.NumFields()
if n > 0 {
p.print(blank)
if n == 1 && result.List[0].Names == nil {
// single anonymous result; no ()'s
p.expr(result.List[0].Type, multiLine)
return
}
p.parameters(result, multiLine)
}
}
func identListSize(list []*ast.Ident, maxSize int) (size int) {
for i, x := range list {
if i > 0 {
size += 2 // ", "
}
size += len(x.Name())
if size >= maxSize {
break
}
}
return
}
func (p *printer) isOneLineFieldList(list []*ast.Field) bool {
if len(list) != 1 {
return false // allow only one field
}
f := list[0]
if f.Tag != nil || f.Comment != nil {
return false // don't allow tags or comments
}
// only name(s) and type
const maxSize = 30 // adjust as appropriate, this is an approximate value
namesSize := identListSize(f.Names, maxSize)
if namesSize > 0 {
namesSize = 1 // blank between names and types
}
typeSize := p.nodeSize(f.Type, maxSize)
return namesSize+typeSize <= maxSize
}
func (p *printer) setLineComment(text string) {
p.setComment(&ast.CommentGroup{[]*ast.Comment{&ast.Comment{noPos, []byte(text)}}})
}
func (p *printer) fieldList(fields *ast.FieldList, isIncomplete bool, ctxt exprContext) {
lbrace := fields.Opening
list := fields.List
rbrace := fields.Closing
if !isIncomplete && !p.commentBefore(rbrace) {
// possibly a one-line struct/interface
if len(list) == 0 {
// no blank between keyword and {} in this case
p.print(lbrace, token.LBRACE, rbrace, token.RBRACE)
return
} 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)
p.print(lbrace, token.LBRACE, blank)
f := list[0]
for i, x := range f.Names {
if i > 0 {
p.print(token.COMMA, blank)
}
p.expr(x, ignoreMultiLine)
}
if len(f.Names) > 0 {
p.print(blank)
}
p.expr(f.Type, ignoreMultiLine)
p.print(blank, rbrace, token.RBRACE)
return
}
}
// at least one entry or incomplete
p.print(blank, lbrace, token.LBRACE, indent, formfeed)
if ctxt&structType != 0 {
sep := vtab
if len(list) == 1 {
sep = blank
}
var ml bool
for i, f := range list {
if i > 0 {
p.linebreak(f.Pos().Line, 1, 2, ignore, ml)
}
ml = false
extraTabs := 0
p.setComment(f.Doc)
if len(f.Names) > 0 {
// named fields
p.identList(f.Names, false, &ml)
p.print(sep)
p.expr(f.Type, &ml)
extraTabs = 1
} else {
// anonymous field
p.expr(f.Type, &ml)
extraTabs = 2
}
if f.Tag != nil {
if len(f.Names) > 0 && sep == vtab {
p.print(sep)
}
p.print(sep)
p.expr(f.Tag, &ml)
extraTabs = 0
}
if f.Comment != nil {
for ; extraTabs > 0; extraTabs-- {
p.print(sep)
}
p.setComment(f.Comment)
}
}
if isIncomplete {
if len(list) > 0 {
p.print(formfeed)
}
p.flush(rbrace, false) // make sure we don't loose the last line comment
p.setLineComment("// contains unexported fields")
}
} else { // interface
var ml bool
for i, f := range list {
if i > 0 {
p.linebreak(f.Pos().Line, 1, 2, ignore, ml)
}
ml = false
p.setComment(f.Doc)
if ftyp, isFtyp := f.Type.(*ast.FuncType); isFtyp {
// method
p.expr(f.Names[0], &ml)
p.signature(ftyp.Params, ftyp.Results, &ml)
} else {
// embedded interface
p.expr(f.Type, &ml)
}
p.setComment(f.Comment)
}
if isIncomplete {
if len(list) > 0 {
p.print(formfeed)
}
p.flush(rbrace, false) // make sure we don't loose the last line comment
p.setLineComment("// contains unexported methods")
}
}
p.print(unindent, formfeed, rbrace, token.RBRACE)
}
// ----------------------------------------------------------------------------
// Expressions
// exprContext describes the syntactic environment in which an expression node is printed.
type exprContext uint
const (
compositeLit = 1 << iota
structType
)
func walkBinary(e *ast.BinaryExpr) (has5, has6 bool, maxProblem int) {
switch e.Op.Precedence() {
case 5:
has5 = true
case 6:
has6 = true
}
switch l := e.X.(type) {
case *ast.BinaryExpr:
if l.Op.Precedence() < e.Op.Precedence() {
// parens will be inserted.
// pretend this is an *ast.ParenExpr and do nothing.
break
}
h5, h6, mp := walkBinary(l)
has5 = has5 || h5
has6 = has6 || h6
if maxProblem < mp {
maxProblem = mp
}
}
switch r := e.Y.(type) {
case *ast.BinaryExpr:
if r.Op.Precedence() <= e.Op.Precedence() {
// parens will be inserted.
// pretend this is an *ast.ParenExpr and do nothing.
break
}
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 {
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
}
func reduceDepth(depth int) int {
depth--
if depth < 1 {
depth = 1
}
return depth
}
// 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, 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.
p.print(token.LPAREN)
p.expr0(x, reduceDepth(depth), multiLine) // parentheses undo one level of depth
p.print(token.RPAREN)
return
}
printBlank := prec < cutoff
ws := indent
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 && xline > 0 && yline > 0 {
// 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
}
}
if printBlank {
p.print(blank)
}
p.expr1(x.Y, prec+1, depth+1, 0, multiLine)
if ws == ignore {
p.print(unindent)
}
}
func isBinary(expr ast.Expr) bool {
_, ok := expr.(*ast.BinaryExpr)
return ok
}
// Sets multiLine to true if the expression spans multiple lines.
func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multiLine *bool) {
p.print(expr.Pos())
switch x := expr.(type) {
case *ast.BadExpr:
p.print("BadExpr")
case *ast.Ident:
p.print(x)
case *ast.BinaryExpr:
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.print(x.Colon, token.COLON, blank)
p.expr(x.Value, multiLine)
case *ast.StarExpr:
const prec = token.UnaryPrec
if prec < prec1 {
// parenthesis needed
p.print(token.LPAREN)
p.print(token.MUL)
p.expr(x.X, multiLine)
p.print(token.RPAREN)
} else {
// no parenthesis needed
p.print(token.MUL)
p.expr(x.X, multiLine)
}
case *ast.UnaryExpr:
const prec = token.UnaryPrec
if prec < prec1 {
// parenthesis needed
p.print(token.LPAREN)
p.expr(x, multiLine)
p.print(token.RPAREN)
} else {
// no parenthesis needed
p.print(x.Op)
if x.Op == token.RANGE {
// TODO(gri) Remove this code if it cannot be reached.
p.print(blank)
}
p.expr1(x.X, prec, depth, 0, multiLine)
}
case *ast.BasicLit:
p.print(x)
case *ast.FuncLit:
p.expr(x.Type, multiLine)
p.funcBody(x.Body, distance(x.Type.Pos(), p.pos), true, multiLine)
case *ast.ParenExpr:
p.print(token.LPAREN)
p.expr0(x.X, reduceDepth(depth), multiLine) // parentheses undo one level of depth
p.print(x.Rparen, token.RPAREN)
case *ast.SelectorExpr:
p.expr1(x.X, token.HighestPrec, depth, 0, multiLine)
p.print(token.PERIOD)
p.expr1(x.Sel, token.HighestPrec, depth, 0, multiLine)
case *ast.TypeAssertExpr:
p.expr1(x.X, token.HighestPrec, depth, 0, multiLine)
p.print(token.PERIOD, token.LPAREN)
if x.Type != nil {
p.expr(x.Type, multiLine)
} else {
p.print(token.TYPE)
}
p.print(token.RPAREN)
case *ast.IndexExpr:
// TODO(gri): should treat[] like parentheses and undo one level of depth
p.expr1(x.X, token.HighestPrec, 1, 0, multiLine)
p.print(token.LBRACK)
p.expr0(x.Index, depth+1, multiLine)
p.print(token.RBRACK)
case *ast.SliceExpr:
// TODO(gri): should treat[] like parentheses and undo one level of depth
p.expr1(x.X, token.HighestPrec, 1, 0, multiLine)
p.print(token.LBRACK)
p.expr0(x.Index, depth+1, multiLine)
// blanks around ":" if both sides exist and either side is a binary expression
if depth <= 1 && x.End != nil && (isBinary(x.Index) || isBinary(x.End)) {
p.print(blank, token.COLON, blank)
} else {
p.print(token.COLON)
}
if x.End != nil {
p.expr0(x.End, depth+1, multiLine)
}
p.print(token.RBRACK)
case *ast.CallExpr:
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, depth, commaSep|commaTerm, multiLine, x.Rparen)
p.print(x.Rparen, token.RPAREN)
case *ast.CompositeLit:
p.expr1(x.Type, token.HighestPrec, depth, compositeLit, multiLine)
p.print(x.Lbrace, token.LBRACE)
p.exprList(x.Lbrace, x.Elts, 1, commaSep|commaTerm, multiLine, x.Rbrace)
p.print(x.Rbrace, token.RBRACE)
case *ast.Ellipsis:
p.print(token.ELLIPSIS)
if x.Elt != nil {
p.expr(x.Elt, multiLine)
}
case *ast.ArrayType:
p.print(token.LBRACK)
if x.Len != nil {
p.expr(x.Len, multiLine)
}
p.print(token.RBRACK)
p.expr(x.Elt, multiLine)
case *ast.StructType:
p.print(token.STRUCT)
p.fieldList(x.Fields, x.Incomplete, ctxt|structType)
case *ast.FuncType:
p.print(token.FUNC)
p.signature(x.Params, x.Results, multiLine)
case *ast.InterfaceType:
p.print(token.INTERFACE)
p.fieldList(x.Methods, x.Incomplete, ctxt)
case *ast.MapType:
p.print(token.MAP, token.LBRACK)
p.expr(x.Key, multiLine)
p.print(token.RBRACK)
p.expr(x.Value, multiLine)
case *ast.ChanType:
switch x.Dir {
case ast.SEND | ast.RECV:
p.print(token.CHAN)
case ast.RECV:
p.print(token.ARROW, token.CHAN)
case ast.SEND:
p.print(token.CHAN, token.ARROW)
}
p.print(blank)
p.expr(x.Value, multiLine)
default:
panic("unreachable")
}
return
}
func (p *printer) expr0(x ast.Expr, depth int, multiLine *bool) {
p.expr1(x, token.LowestPrec, depth, 0, multiLine)
}
// Sets multiLine to true if the expression spans multiple lines.
func (p *printer) expr(x ast.Expr, multiLine *bool) {
const depth = 1
p.expr1(x, token.LowestPrec, depth, 0, multiLine)
}
// ----------------------------------------------------------------------------
// Statements
const maxStmtNewlines = 2 // maximum number of newlines between statements
// Print the statement list indented, but without a newline after the last statement.
// Extra line breaks between statements in the source are respected but at most one
// empty line is printed between statements.
func (p *printer) stmtList(list []ast.Stmt, _indent int) {
// TODO(gri): fix _indent code
if _indent > 0 {
p.print(indent)
}
var multiLine bool
for i, s := range list {
// _indent == 0 only for lists of switch/select case clauses;
// in those cases each clause is a new section
p.linebreak(s.Pos().Line, 1, maxStmtNewlines, ignore, i == 0 || _indent == 0 || multiLine)
multiLine = false
p.stmt(s, &multiLine)
}
if _indent > 0 {
p.print(unindent)
}
}
// block prints an *ast.BlockStmt; it always spans at least two lines.
func (p *printer) block(s *ast.BlockStmt, indent int) {
p.print(s.Pos(), token.LBRACE)
p.stmtList(s.List, indent)
p.linebreak(s.Rbrace.Line, 1, maxStmtNewlines, ignore, true)
p.print(s.Rbrace, token.RBRACE)
}
// TODO(gri): Decide if this should be used more broadly. The printing code
// knows when to insert parentheses for precedence reasons, but
// need to be careful to keep them around type expressions.
func stripParens(x ast.Expr) ast.Expr {
if px, hasParens := x.(*ast.ParenExpr); hasParens {
return stripParens(px.X)
}
return x
}
func (p *printer) controlClause(isForStmt bool, init ast.Stmt, expr ast.Expr, post ast.Stmt) {
p.print(blank)
needsBlank := false
if init == nil && post == nil {
// no semicolons required
if expr != nil {
p.expr(stripParens(expr), ignoreMultiLine)
needsBlank = true
}
} else {
// all semicolons required
// (they are not separators, print them explicitly)
if init != nil {
p.stmt(init, ignoreMultiLine)
}
p.print(token.SEMICOLON, blank)
if expr != nil {
p.expr(stripParens(expr), ignoreMultiLine)
needsBlank = true
}
if isForStmt {
p.print(token.SEMICOLON, blank)
needsBlank = false
if post != nil {
p.stmt(post, ignoreMultiLine)
needsBlank = true
}
}
}
if needsBlank {
p.print(blank)
}
}
// Sets multiLine to true if the statements spans multiple lines.
func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) {
p.print(stmt.Pos())
switch s := stmt.(type) {
case *ast.BadStmt:
p.print("BadStmt")
case *ast.DeclStmt:
p.decl(s.Decl, inStmtList, multiLine)
case *ast.EmptyStmt:
// nothing to do
case *ast.LabeledStmt:
// a "correcting" unindent immediately following a line break
// is applied before the line break if there is no comment
// between (see writeWhitespace)
p.print(unindent)
p.expr(s.Label, multiLine)
p.print(token.COLON, vtab, indent)
p.linebreak(s.Stmt.Pos().Line, 0, 1, ignore, true)
p.stmt(s.Stmt, multiLine)
case *ast.ExprStmt:
const depth = 1
p.expr0(s.X, depth, multiLine)
case *ast.IncDecStmt:
const depth = 1
p.expr0(s.X, depth+1, multiLine)
p.print(s.Tok)
case *ast.AssignStmt:
var depth = 1
if len(s.Lhs) > 1 && len(s.Rhs) > 1 {
depth++
}
p.exprList(s.Pos(), s.Lhs, depth, commaSep, multiLine, s.TokPos)
p.print(blank, s.TokPos, s.Tok)
p.exprList(s.TokPos, s.Rhs, depth, blankStart|commaSep, multiLine, noPos)
case *ast.GoStmt:
p.print(token.GO, blank)
p.expr(s.Call, multiLine)
case *ast.DeferStmt:
p.print(token.DEFER, blank)
p.expr(s.Call, multiLine)
case *ast.ReturnStmt:
p.print(token.RETURN)
if s.Results != nil {
p.exprList(s.Pos(), s.Results, 1, blankStart|commaSep, multiLine, noPos)
}
case *ast.BranchStmt:
p.print(s.Tok)
if s.Label != nil {
p.print(blank)
p.expr(s.Label, multiLine)
}
case *ast.BlockStmt:
p.block(s, 1)
*multiLine = true
case *ast.IfStmt:
p.print(token.IF)
p.controlClause(false, s.Init, s.Cond, nil)
p.block(s.Body, 1)
*multiLine = true
if s.Else != nil {
p.print(blank, token.ELSE, blank)
switch s.Else.(type) {
case *ast.BlockStmt, *ast.IfStmt:
p.stmt(s.Else, ignoreMultiLine)
default:
p.print(token.LBRACE, indent, formfeed)
p.stmt(s.Else, ignoreMultiLine)
p.print(unindent, formfeed, token.RBRACE)
}
}
case *ast.CaseClause:
if s.Values != nil {
p.print(token.CASE)
p.exprList(s.Pos(), s.Values, 1, blankStart|commaSep, multiLine, s.Colon)
} else {
p.print(token.DEFAULT)
}
p.print(s.Colon, token.COLON)
p.stmtList(s.Body, 1)
case *ast.SwitchStmt:
p.print(token.SWITCH)
p.controlClause(false, s.Init, s.Tag, nil)
p.block(s.Body, 0)
*multiLine = true
case *ast.TypeCaseClause:
if s.Types != nil {
p.print(token.CASE)
p.exprList(s.Pos(), s.Types, 1, blankStart|commaSep, multiLine, s.Colon)
} else {
p.print(token.DEFAULT)
}
p.print(s.Colon, token.COLON)
p.stmtList(s.Body, 1)
case *ast.TypeSwitchStmt:
p.print(token.SWITCH)
if s.Init != nil {
p.print(blank)
p.stmt(s.Init, ignoreMultiLine)
p.print(token.SEMICOLON)
}
p.print(blank)
p.stmt(s.Assign, ignoreMultiLine)
p.print(blank)
p.block(s.Body, 0)
*multiLine = true
case *ast.CommClause:
if s.Rhs != nil {
p.print(token.CASE, blank)
if s.Lhs != nil {
p.expr(s.Lhs, multiLine)
p.print(blank, s.Tok, blank)
}
p.expr(s.Rhs, multiLine)
} else {
p.print(token.DEFAULT)
}
p.print(s.Colon, token.COLON)
p.stmtList(s.Body, 1)
case *ast.SelectStmt:
p.print(token.SELECT, blank)
p.block(s.Body, 0)
*multiLine = true
case *ast.ForStmt:
p.print(token.FOR)
p.controlClause(true, s.Init, s.Cond, s.Post)
p.block(s.Body, 1)
*multiLine = true
case *ast.RangeStmt:
p.print(token.FOR, blank)
p.expr(s.Key, multiLine)
if s.Value != nil {
p.print(token.COMMA, blank)
p.expr(s.Value, multiLine)
}
p.print(blank, s.TokPos, s.Tok, blank, token.RANGE, blank)
p.expr(stripParens(s.X), multiLine)
p.print(blank)
p.block(s.Body, 1)
*multiLine = true
default:
panic("unreachable")
}
return
}
// ----------------------------------------------------------------------------
// Declarations
type declContext uint
const (
atTop declContext = iota
inGroup
inStmtList
)
// The parameter n is the number of specs in the group; context specifies
// the surroundings of the declaration. Separating semicolons are printed
// depending on the context. If indent is set, a multi-line identifier lists
// in the spec are indented when the first linebreak is encountered. Sets
// multiLine to true if the spec spans multiple lines.
//
func (p *printer) spec(spec ast.Spec, n int, context declContext, indent bool, multiLine *bool) {
switch s := spec.(type) {
case *ast.ImportSpec:
p.setComment(s.Doc)
if s.Name != nil {
p.expr(s.Name, multiLine)
p.print(blank)
}
p.expr(s.Path, multiLine)
p.setComment(s.Comment)
case *ast.ValueSpec:
p.setComment(s.Doc)
p.identList(s.Names, indent, multiLine) // always present
if n == 1 {
if s.Type != nil {
p.print(blank)
p.expr(s.Type, multiLine)
}
if s.Values != nil {
p.print(blank, token.ASSIGN)
p.exprList(noPos, s.Values, 1, blankStart|commaSep, multiLine, noPos)
}
p.setComment(s.Comment)
} else {
extraTabs := 3
if s.Type != nil {
p.print(vtab)
p.expr(s.Type, multiLine)
extraTabs--
}
if s.Values != nil {
p.print(vtab, token.ASSIGN)
p.exprList(noPos, s.Values, 1, blankStart|commaSep, multiLine, noPos)
extraTabs--
}
if s.Comment != nil {
for ; extraTabs > 0; extraTabs-- {
p.print(vtab)
}
p.setComment(s.Comment)
}
}
case *ast.TypeSpec:
p.setComment(s.Doc)
p.expr(s.Name, multiLine)
if n == 1 {
p.print(blank)
} else {
p.print(vtab)
}
p.expr(s.Type, multiLine)
p.setComment(s.Comment)
default:
panic("unreachable")
}
}
// Sets multiLine to true if the declaration spans multiple lines.
func (p *printer) genDecl(d *ast.GenDecl, context declContext, multiLine *bool) {
p.setComment(d.Doc)
p.print(d.Pos(), d.Tok, blank)
if d.Lparen.IsValid() {
// group of parenthesized declarations
p.print(d.Lparen, token.LPAREN)
if len(d.Specs) > 0 {
p.print(indent, formfeed)
var ml bool
for i, s := range d.Specs {
if i > 0 {
p.linebreak(s.Pos().Line, 1, 2, ignore, ml)
}
ml = false
p.spec(s, len(d.Specs), inGroup, false, &ml)
}
p.print(unindent, formfeed)
*multiLine = true
}
p.print(d.Rparen, token.RPAREN)
} else {
// single declaration
p.spec(d.Specs[0], 1, context, true, multiLine)
}
}
// nodeSize determines the size of n in chars after formatting.
// The result is <= maxSize if the node fits on one line with at
// most maxSize chars and the formatted output doesn't contain
// 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
// nodeSize computation must be indendent of particular
// style so that we always get the same decision; print
// in RawFormat
cfg := Config{Mode: RawFormat}
var buf bytes.Buffer
if _, err := cfg.Fprint(&buf, n); err != nil {
return
}
if buf.Len() <= maxSize {
for _, ch := range buf.Bytes() {
if ch < ' ' {
return
}
}
size = buf.Len() // n fits
}
return
}
func (p *printer) isOneLineFunc(b *ast.BlockStmt, headerSize int) bool {
const maxSize = 90 // adjust as appropriate, this is an approximate value
bodySize := 0
switch {
case len(b.List) > 1 || p.commentBefore(b.Rbrace):
return false // too many statements or there is a comment - all bets are off
case len(b.List) == 1:
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
}
// Sets multiLine to true if the function body spans multiple lines.
func (p *printer) funcBody(b *ast.BlockStmt, headerSize int, isLit bool, multiLine *bool) {
if b == nil {
return
}
if p.isOneLineFunc(b, headerSize) {
sep := vtab
if isLit {
sep = blank
}
if len(b.List) > 0 {
p.print(sep, b.Pos(), token.LBRACE, blank)
p.stmt(b.List[0], ignoreMultiLine)
p.print(blank, b.Rbrace, token.RBRACE)
} else {
p.print(sep, b.Pos(), token.LBRACE, b.Rbrace, token.RBRACE)
}
return
}
p.print(blank)
p.block(b, 1)
*multiLine = true
}
// distance returns the column difference between from and to if both
// are on the same line; if they are on different lines (or unknown)
// the result is infinity (1<<30).
func distance(from, to token.Position) int {
if from.IsValid() && to.IsValid() && from.Line == to.Line {
return to.Column - from.Column
}
return 1 << 30
}
// Sets multiLine to true if the declaration spans multiple lines.
func (p *printer) funcDecl(d *ast.FuncDecl, multiLine *bool) {
p.setComment(d.Doc)
p.print(d.Pos(), token.FUNC, blank)
if d.Recv != nil {
p.parameters(d.Recv, multiLine) // method: print receiver
p.print(blank)
}
p.expr(d.Name, multiLine)
p.signature(d.Type.Params, d.Type.Results, multiLine)
p.funcBody(d.Body, distance(d.Pos(), p.pos), false, multiLine)
}
// Sets multiLine to true if the declaration spans multiple lines.
func (p *printer) decl(decl ast.Decl, context declContext, multiLine *bool) {
switch d := decl.(type) {
case *ast.BadDecl:
p.print(d.Pos(), "BadDecl")
case *ast.GenDecl:
p.genDecl(d, context, multiLine)
case *ast.FuncDecl:
p.funcDecl(d, multiLine)
default:
panic("unreachable")
}
}
// ----------------------------------------------------------------------------
// Files
const maxDeclNewlines = 3 // maximum number of newlines between declarations
func declToken(decl ast.Decl) (tok token.Token) {
tok = token.ILLEGAL
switch d := decl.(type) {
case *ast.GenDecl:
tok = d.Tok
case *ast.FuncDecl:
tok = token.FUNC
}
return
}
func (p *printer) file(src *ast.File) {
p.setComment(src.Doc)
p.print(src.Pos(), token.PACKAGE, blank)
p.expr(src.Name, ignoreMultiLine)
if len(src.Decls) > 0 {
tok := token.ILLEGAL
for _, d := range src.Decls {
prev := tok
tok = declToken(d)
// if the declaration token changed (e.g., from CONST to TYPE)
// print an empty line between top-level declarations
min := 1
if prev != tok {
min = 2
}
p.linebreak(d.Pos().Line, min, maxDeclNewlines, ignore, false)
p.decl(d, atTop, ignoreMultiLine)
}
}
p.print(newline)
}
|