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
|
const std = @import("../index.zig");
const assert = std.debug.assert;
const SegmentedList = std.SegmentedList;
const mem = std.mem;
const ast = std.zig.ast;
const Token = std.zig.Token;
const RenderState = union(enum) {
TopLevelDecl: &ast.Node,
ParamDecl: &ast.Node,
Text: []const u8,
Expression: &ast.Node,
VarDecl: &ast.Node.VarDecl,
Statement: &ast.Node,
PrintIndent,
Indent: usize,
MaybeSemiColon: &ast.Node,
Token: ast.TokenIndex,
NonBreakToken: ast.TokenIndex,
};
const indent_delta = 4;
pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) !void {
var stack = SegmentedList(RenderState, 32).init(allocator);
defer stack.deinit();
{
try stack.push(RenderState { .Text = "\n"});
var i = tree.root_node.decls.len;
while (i != 0) {
i -= 1;
const decl = *tree.root_node.decls.at(i);
try stack.push(RenderState {.TopLevelDecl = decl});
if (i != 0) {
try stack.push(RenderState {
.Text = blk: {
const prev_node = *tree.root_node.decls.at(i - 1);
const prev_node_last_token = tree.tokens.at(prev_node.lastToken());
const loc = tree.tokenLocation(prev_node_last_token.end, decl.firstToken());
if (loc.line >= 2) {
break :blk "\n\n";
}
break :blk "\n";
},
});
}
}
}
var indent: usize = 0;
while (stack.pop()) |state| {
switch (state) {
RenderState.TopLevelDecl => |decl| {
switch (decl.id) {
ast.Node.Id.FnProto => {
const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", decl);
try renderComments(tree, stream, fn_proto, indent);
if (fn_proto.body_node) |body_node| {
stack.push(RenderState { .Expression = body_node}) catch unreachable;
try stack.push(RenderState { .Text = " "});
} else {
stack.push(RenderState { .Text = ";" }) catch unreachable;
}
try stack.push(RenderState { .Expression = decl });
},
ast.Node.Id.Use => {
const use_decl = @fieldParentPtr(ast.Node.Use, "base", decl);
if (use_decl.visib_token) |visib_token| {
try stream.print("{} ", tree.tokenSlice(visib_token));
}
try stream.print("use ");
try stack.push(RenderState { .Text = ";" });
try stack.push(RenderState { .Expression = use_decl.expr });
},
ast.Node.Id.VarDecl => {
const var_decl = @fieldParentPtr(ast.Node.VarDecl, "base", decl);
try renderComments(tree, stream, var_decl, indent);
try stack.push(RenderState { .VarDecl = var_decl});
},
ast.Node.Id.TestDecl => {
const test_decl = @fieldParentPtr(ast.Node.TestDecl, "base", decl);
try renderComments(tree, stream, test_decl, indent);
try stream.print("test ");
try stack.push(RenderState { .Expression = test_decl.body_node });
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Expression = test_decl.name });
},
ast.Node.Id.StructField => {
const field = @fieldParentPtr(ast.Node.StructField, "base", decl);
try renderComments(tree, stream, field, indent);
if (field.visib_token) |visib_token| {
try stream.print("{} ", tree.tokenSlice(visib_token));
}
try stream.print("{}: ", tree.tokenSlice(field.name_token));
try stack.push(RenderState { .Token = field.lastToken() + 1 });
try stack.push(RenderState { .Expression = field.type_expr});
},
ast.Node.Id.UnionTag => {
const tag = @fieldParentPtr(ast.Node.UnionTag, "base", decl);
try renderComments(tree, stream, tag, indent);
try stream.print("{}", tree.tokenSlice(tag.name_token));
try stack.push(RenderState { .Text = "," });
if (tag.value_expr) |value_expr| {
try stack.push(RenderState { .Expression = value_expr });
try stack.push(RenderState { .Text = " = " });
}
if (tag.type_expr) |type_expr| {
try stream.print(": ");
try stack.push(RenderState { .Expression = type_expr});
}
},
ast.Node.Id.EnumTag => {
const tag = @fieldParentPtr(ast.Node.EnumTag, "base", decl);
try renderComments(tree, stream, tag, indent);
try stream.print("{}", tree.tokenSlice(tag.name_token));
try stack.push(RenderState { .Text = "," });
if (tag.value) |value| {
try stream.print(" = ");
try stack.push(RenderState { .Expression = value});
}
},
ast.Node.Id.ErrorTag => {
const tag = @fieldParentPtr(ast.Node.ErrorTag, "base", decl);
try renderComments(tree, stream, tag, indent);
try stream.print("{}", tree.tokenSlice(tag.name_token));
},
ast.Node.Id.Comptime => {
try stack.push(RenderState { .MaybeSemiColon = decl });
try stack.push(RenderState { .Expression = decl });
},
ast.Node.Id.LineComment => {
const line_comment_node = @fieldParentPtr(ast.Node.LineComment, "base", decl);
try stream.write(tree.tokenSlice(line_comment_node.token));
},
else => unreachable,
}
},
RenderState.VarDecl => |var_decl| {
try stack.push(RenderState { .Token = var_decl.semicolon_token });
if (var_decl.init_node) |init_node| {
try stack.push(RenderState { .Expression = init_node });
const text = if (init_node.id == ast.Node.Id.MultilineStringLiteral) " =" else " = ";
try stack.push(RenderState { .Text = text });
}
if (var_decl.align_node) |align_node| {
try stack.push(RenderState { .Text = ")" });
try stack.push(RenderState { .Expression = align_node });
try stack.push(RenderState { .Text = " align(" });
}
if (var_decl.type_node) |type_node| {
try stack.push(RenderState { .Expression = type_node });
try stack.push(RenderState { .Text = ": " });
}
try stack.push(RenderState { .Text = tree.tokenSlice(var_decl.name_token) });
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Text = tree.tokenSlice(var_decl.mut_token) });
if (var_decl.comptime_token) |comptime_token| {
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Text = tree.tokenSlice(comptime_token) });
}
if (var_decl.extern_export_token) |extern_export_token| {
if (var_decl.lib_name != null) {
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Expression = ??var_decl.lib_name });
}
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Text = tree.tokenSlice(extern_export_token) });
}
if (var_decl.visib_token) |visib_token| {
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Text = tree.tokenSlice(visib_token) });
}
},
RenderState.ParamDecl => |base| {
const param_decl = @fieldParentPtr(ast.Node.ParamDecl, "base", base);
if (param_decl.comptime_token) |comptime_token| {
try stream.print("{} ", tree.tokenSlice(comptime_token));
}
if (param_decl.noalias_token) |noalias_token| {
try stream.print("{} ", tree.tokenSlice(noalias_token));
}
if (param_decl.name_token) |name_token| {
try stream.print("{}: ", tree.tokenSlice(name_token));
}
if (param_decl.var_args_token) |var_args_token| {
try stream.print("{}", tree.tokenSlice(var_args_token));
} else {
try stack.push(RenderState { .Expression = param_decl.type_node});
}
},
RenderState.Text => |bytes| {
try stream.write(bytes);
},
RenderState.Expression => |base| switch (base.id) {
ast.Node.Id.Identifier => {
const identifier = @fieldParentPtr(ast.Node.Identifier, "base", base);
try stream.print("{}", tree.tokenSlice(identifier.token));
},
ast.Node.Id.Block => {
const block = @fieldParentPtr(ast.Node.Block, "base", base);
if (block.label) |label| {
try stream.print("{}: ", tree.tokenSlice(label));
}
if (block.statements.len == 0) {
try stream.write("{}");
} else {
try stream.write("{");
try stack.push(RenderState { .Text = "}"});
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Indent = indent});
try stack.push(RenderState { .Text = "\n"});
var i = block.statements.len;
while (i != 0) {
i -= 1;
const statement_node = *block.statements.at(i);
try stack.push(RenderState { .Statement = statement_node});
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Indent = indent + indent_delta});
try stack.push(RenderState {
.Text = blk: {
if (i != 0) {
const prev_node = *block.statements.at(i - 1);
const prev_node_last_token_end = tree.tokens.at(prev_node.lastToken()).end;
const loc = tree.tokenLocation(prev_node_last_token_end, statement_node.firstToken());
if (loc.line >= 2) {
break :blk "\n\n";
}
}
break :blk "\n";
},
});
}
}
},
ast.Node.Id.Defer => {
const defer_node = @fieldParentPtr(ast.Node.Defer, "base", base);
try stream.print("{} ", tree.tokenSlice(defer_node.defer_token));
try stack.push(RenderState { .Expression = defer_node.expr });
},
ast.Node.Id.Comptime => {
const comptime_node = @fieldParentPtr(ast.Node.Comptime, "base", base);
try stream.print("{} ", tree.tokenSlice(comptime_node.comptime_token));
try stack.push(RenderState { .Expression = comptime_node.expr });
},
ast.Node.Id.AsyncAttribute => {
const async_attr = @fieldParentPtr(ast.Node.AsyncAttribute, "base", base);
try stream.print("{}", tree.tokenSlice(async_attr.async_token));
if (async_attr.allocator_type) |allocator_type| {
try stack.push(RenderState { .Text = ">" });
try stack.push(RenderState { .Expression = allocator_type });
try stack.push(RenderState { .Text = "<" });
}
},
ast.Node.Id.Suspend => {
const suspend_node = @fieldParentPtr(ast.Node.Suspend, "base", base);
if (suspend_node.label) |label| {
try stream.print("{}: ", tree.tokenSlice(label));
}
try stream.print("{}", tree.tokenSlice(suspend_node.suspend_token));
if (suspend_node.body) |body| {
try stack.push(RenderState { .Expression = body });
try stack.push(RenderState { .Text = " " });
}
if (suspend_node.payload) |payload| {
try stack.push(RenderState { .Expression = payload });
try stack.push(RenderState { .Text = " " });
}
},
ast.Node.Id.InfixOp => {
const prefix_op_node = @fieldParentPtr(ast.Node.InfixOp, "base", base);
try stack.push(RenderState { .Expression = prefix_op_node.rhs });
if (prefix_op_node.op == ast.Node.InfixOp.Op.Catch) {
if (prefix_op_node.op.Catch) |payload| {
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Expression = payload });
}
try stack.push(RenderState { .Text = " catch " });
} else {
const text = switch (prefix_op_node.op) {
ast.Node.InfixOp.Op.Add => " + ",
ast.Node.InfixOp.Op.AddWrap => " +% ",
ast.Node.InfixOp.Op.ArrayCat => " ++ ",
ast.Node.InfixOp.Op.ArrayMult => " ** ",
ast.Node.InfixOp.Op.Assign => " = ",
ast.Node.InfixOp.Op.AssignBitAnd => " &= ",
ast.Node.InfixOp.Op.AssignBitOr => " |= ",
ast.Node.InfixOp.Op.AssignBitShiftLeft => " <<= ",
ast.Node.InfixOp.Op.AssignBitShiftRight => " >>= ",
ast.Node.InfixOp.Op.AssignBitXor => " ^= ",
ast.Node.InfixOp.Op.AssignDiv => " /= ",
ast.Node.InfixOp.Op.AssignMinus => " -= ",
ast.Node.InfixOp.Op.AssignMinusWrap => " -%= ",
ast.Node.InfixOp.Op.AssignMod => " %= ",
ast.Node.InfixOp.Op.AssignPlus => " += ",
ast.Node.InfixOp.Op.AssignPlusWrap => " +%= ",
ast.Node.InfixOp.Op.AssignTimes => " *= ",
ast.Node.InfixOp.Op.AssignTimesWarp => " *%= ",
ast.Node.InfixOp.Op.BangEqual => " != ",
ast.Node.InfixOp.Op.BitAnd => " & ",
ast.Node.InfixOp.Op.BitOr => " | ",
ast.Node.InfixOp.Op.BitShiftLeft => " << ",
ast.Node.InfixOp.Op.BitShiftRight => " >> ",
ast.Node.InfixOp.Op.BitXor => " ^ ",
ast.Node.InfixOp.Op.BoolAnd => " and ",
ast.Node.InfixOp.Op.BoolOr => " or ",
ast.Node.InfixOp.Op.Div => " / ",
ast.Node.InfixOp.Op.EqualEqual => " == ",
ast.Node.InfixOp.Op.ErrorUnion => "!",
ast.Node.InfixOp.Op.GreaterOrEqual => " >= ",
ast.Node.InfixOp.Op.GreaterThan => " > ",
ast.Node.InfixOp.Op.LessOrEqual => " <= ",
ast.Node.InfixOp.Op.LessThan => " < ",
ast.Node.InfixOp.Op.MergeErrorSets => " || ",
ast.Node.InfixOp.Op.Mod => " % ",
ast.Node.InfixOp.Op.Mult => " * ",
ast.Node.InfixOp.Op.MultWrap => " *% ",
ast.Node.InfixOp.Op.Period => ".",
ast.Node.InfixOp.Op.Sub => " - ",
ast.Node.InfixOp.Op.SubWrap => " -% ",
ast.Node.InfixOp.Op.UnwrapMaybe => " ?? ",
ast.Node.InfixOp.Op.Range => " ... ",
ast.Node.InfixOp.Op.Catch => unreachable,
};
try stack.push(RenderState { .Text = text });
}
try stack.push(RenderState { .Expression = prefix_op_node.lhs });
},
ast.Node.Id.PrefixOp => {
const prefix_op_node = @fieldParentPtr(ast.Node.PrefixOp, "base", base);
try stack.push(RenderState { .Expression = prefix_op_node.rhs });
switch (prefix_op_node.op) {
ast.Node.PrefixOp.Op.AddrOf => |addr_of_info| {
try stream.write("&");
if (addr_of_info.volatile_token != null) {
try stack.push(RenderState { .Text = "volatile "});
}
if (addr_of_info.const_token != null) {
try stack.push(RenderState { .Text = "const "});
}
if (addr_of_info.align_expr) |align_expr| {
try stream.print("align(");
try stack.push(RenderState { .Text = ") "});
try stack.push(RenderState { .Expression = align_expr});
}
},
ast.Node.PrefixOp.Op.SliceType => |addr_of_info| {
try stream.write("[]");
if (addr_of_info.volatile_token != null) {
try stack.push(RenderState { .Text = "volatile "});
}
if (addr_of_info.const_token != null) {
try stack.push(RenderState { .Text = "const "});
}
if (addr_of_info.align_expr) |align_expr| {
try stream.print("align(");
try stack.push(RenderState { .Text = ") "});
try stack.push(RenderState { .Expression = align_expr});
}
},
ast.Node.PrefixOp.Op.ArrayType => |array_index| {
try stack.push(RenderState { .Text = "]"});
try stack.push(RenderState { .Expression = array_index});
try stack.push(RenderState { .Text = "["});
},
ast.Node.PrefixOp.Op.BitNot => try stream.write("~"),
ast.Node.PrefixOp.Op.BoolNot => try stream.write("!"),
ast.Node.PrefixOp.Op.Deref => try stream.write("*"),
ast.Node.PrefixOp.Op.Negation => try stream.write("-"),
ast.Node.PrefixOp.Op.NegationWrap => try stream.write("-%"),
ast.Node.PrefixOp.Op.Try => try stream.write("try "),
ast.Node.PrefixOp.Op.UnwrapMaybe => try stream.write("??"),
ast.Node.PrefixOp.Op.MaybeType => try stream.write("?"),
ast.Node.PrefixOp.Op.Await => try stream.write("await "),
ast.Node.PrefixOp.Op.Cancel => try stream.write("cancel "),
ast.Node.PrefixOp.Op.Resume => try stream.write("resume "),
}
},
ast.Node.Id.SuffixOp => {
const suffix_op = @fieldParentPtr(ast.Node.SuffixOp, "base", base);
switch (suffix_op.op) {
@TagType(ast.Node.SuffixOp.Op).Call => |*call_info| {
try stack.push(RenderState { .Text = ")"});
var i = call_info.params.len;
while (i != 0) {
i -= 1;
const param_node = *call_info.params.at(i);
try stack.push(RenderState { .Expression = param_node});
if (i != 0) {
try stack.push(RenderState { .Text = ", " });
}
}
try stack.push(RenderState { .Text = "("});
try stack.push(RenderState { .Expression = suffix_op.lhs });
if (call_info.async_attr) |async_attr| {
try stack.push(RenderState { .Text = " "});
try stack.push(RenderState { .Expression = &async_attr.base });
}
},
ast.Node.SuffixOp.Op.ArrayAccess => |index_expr| {
try stack.push(RenderState { .Text = "]"});
try stack.push(RenderState { .Expression = index_expr});
try stack.push(RenderState { .Text = "["});
try stack.push(RenderState { .Expression = suffix_op.lhs });
},
@TagType(ast.Node.SuffixOp.Op).Slice => |range| {
try stack.push(RenderState { .Text = "]"});
if (range.end) |end| {
try stack.push(RenderState { .Expression = end});
}
try stack.push(RenderState { .Text = ".."});
try stack.push(RenderState { .Expression = range.start});
try stack.push(RenderState { .Text = "["});
try stack.push(RenderState { .Expression = suffix_op.lhs });
},
ast.Node.SuffixOp.Op.StructInitializer => |*field_inits| {
if (field_inits.len == 0) {
try stack.push(RenderState { .Text = "{}" });
try stack.push(RenderState { .Expression = suffix_op.lhs });
continue;
}
if (field_inits.len == 1) {
const field_init = *field_inits.at(0);
try stack.push(RenderState { .Text = " }" });
try stack.push(RenderState { .Expression = field_init });
try stack.push(RenderState { .Text = "{ " });
try stack.push(RenderState { .Expression = suffix_op.lhs });
continue;
}
try stack.push(RenderState { .Text = "}"});
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Indent = indent });
try stack.push(RenderState { .Text = "\n" });
var i = field_inits.len;
while (i != 0) {
i -= 1;
const field_init = *field_inits.at(i);
if (field_init.id != ast.Node.Id.LineComment) {
try stack.push(RenderState { .Text = "," });
}
try stack.push(RenderState { .Expression = field_init });
try stack.push(RenderState.PrintIndent);
if (i != 0) {
try stack.push(RenderState { .Text = blk: {
const prev_node = *field_inits.at(i - 1);
const prev_node_last_token_end = tree.tokens.at(prev_node.lastToken()).end;
const loc = tree.tokenLocation(prev_node_last_token_end, field_init.firstToken());
if (loc.line >= 2) {
break :blk "\n\n";
}
break :blk "\n";
}});
}
}
try stack.push(RenderState { .Indent = indent + indent_delta });
try stack.push(RenderState { .Text = "{\n"});
try stack.push(RenderState { .Expression = suffix_op.lhs });
},
ast.Node.SuffixOp.Op.ArrayInitializer => |*exprs| {
if (exprs.len == 0) {
try stack.push(RenderState { .Text = "{}" });
try stack.push(RenderState { .Expression = suffix_op.lhs });
continue;
}
if (exprs.len == 1) {
const expr = *exprs.at(0);
try stack.push(RenderState { .Text = "}" });
try stack.push(RenderState { .Expression = expr });
try stack.push(RenderState { .Text = "{" });
try stack.push(RenderState { .Expression = suffix_op.lhs });
continue;
}
try stack.push(RenderState { .Text = "}"});
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Indent = indent });
var i = exprs.len;
while (i != 0) {
i -= 1;
const expr = *exprs.at(i);
try stack.push(RenderState { .Text = ",\n" });
try stack.push(RenderState { .Expression = expr });
try stack.push(RenderState.PrintIndent);
}
try stack.push(RenderState { .Indent = indent + indent_delta });
try stack.push(RenderState { .Text = "{\n"});
try stack.push(RenderState { .Expression = suffix_op.lhs });
},
}
},
ast.Node.Id.ControlFlowExpression => {
const flow_expr = @fieldParentPtr(ast.Node.ControlFlowExpression, "base", base);
if (flow_expr.rhs) |rhs| {
try stack.push(RenderState { .Expression = rhs });
try stack.push(RenderState { .Text = " " });
}
switch (flow_expr.kind) {
ast.Node.ControlFlowExpression.Kind.Break => |maybe_label| {
try stream.print("break");
if (maybe_label) |label| {
try stream.print(" :");
try stack.push(RenderState { .Expression = label });
}
},
ast.Node.ControlFlowExpression.Kind.Continue => |maybe_label| {
try stream.print("continue");
if (maybe_label) |label| {
try stream.print(" :");
try stack.push(RenderState { .Expression = label });
}
},
ast.Node.ControlFlowExpression.Kind.Return => {
try stream.print("return");
},
}
},
ast.Node.Id.Payload => {
const payload = @fieldParentPtr(ast.Node.Payload, "base", base);
try stack.push(RenderState { .Text = "|"});
try stack.push(RenderState { .Expression = payload.error_symbol });
try stack.push(RenderState { .Text = "|"});
},
ast.Node.Id.PointerPayload => {
const payload = @fieldParentPtr(ast.Node.PointerPayload, "base", base);
try stack.push(RenderState { .Text = "|"});
try stack.push(RenderState { .Expression = payload.value_symbol });
if (payload.ptr_token) |ptr_token| {
try stack.push(RenderState { .Text = tree.tokenSlice(ptr_token) });
}
try stack.push(RenderState { .Text = "|"});
},
ast.Node.Id.PointerIndexPayload => {
const payload = @fieldParentPtr(ast.Node.PointerIndexPayload, "base", base);
try stack.push(RenderState { .Text = "|"});
if (payload.index_symbol) |index_symbol| {
try stack.push(RenderState { .Expression = index_symbol });
try stack.push(RenderState { .Text = ", "});
}
try stack.push(RenderState { .Expression = payload.value_symbol });
if (payload.ptr_token) |ptr_token| {
try stack.push(RenderState { .Text = tree.tokenSlice(ptr_token) });
}
try stack.push(RenderState { .Text = "|"});
},
ast.Node.Id.GroupedExpression => {
const grouped_expr = @fieldParentPtr(ast.Node.GroupedExpression, "base", base);
try stack.push(RenderState { .Text = ")"});
try stack.push(RenderState { .Expression = grouped_expr.expr });
try stack.push(RenderState { .Text = "("});
},
ast.Node.Id.FieldInitializer => {
const field_init = @fieldParentPtr(ast.Node.FieldInitializer, "base", base);
try stream.print(".{} = ", tree.tokenSlice(field_init.name_token));
try stack.push(RenderState { .Expression = field_init.expr });
},
ast.Node.Id.IntegerLiteral => {
const integer_literal = @fieldParentPtr(ast.Node.IntegerLiteral, "base", base);
try stream.print("{}", tree.tokenSlice(integer_literal.token));
},
ast.Node.Id.FloatLiteral => {
const float_literal = @fieldParentPtr(ast.Node.FloatLiteral, "base", base);
try stream.print("{}", tree.tokenSlice(float_literal.token));
},
ast.Node.Id.StringLiteral => {
const string_literal = @fieldParentPtr(ast.Node.StringLiteral, "base", base);
try stream.print("{}", tree.tokenSlice(string_literal.token));
},
ast.Node.Id.CharLiteral => {
const char_literal = @fieldParentPtr(ast.Node.CharLiteral, "base", base);
try stream.print("{}", tree.tokenSlice(char_literal.token));
},
ast.Node.Id.BoolLiteral => {
const bool_literal = @fieldParentPtr(ast.Node.CharLiteral, "base", base);
try stream.print("{}", tree.tokenSlice(bool_literal.token));
},
ast.Node.Id.NullLiteral => {
const null_literal = @fieldParentPtr(ast.Node.NullLiteral, "base", base);
try stream.print("{}", tree.tokenSlice(null_literal.token));
},
ast.Node.Id.ThisLiteral => {
const this_literal = @fieldParentPtr(ast.Node.ThisLiteral, "base", base);
try stream.print("{}", tree.tokenSlice(this_literal.token));
},
ast.Node.Id.Unreachable => {
const unreachable_node = @fieldParentPtr(ast.Node.Unreachable, "base", base);
try stream.print("{}", tree.tokenSlice(unreachable_node.token));
},
ast.Node.Id.ErrorType => {
const error_type = @fieldParentPtr(ast.Node.ErrorType, "base", base);
try stream.print("{}", tree.tokenSlice(error_type.token));
},
ast.Node.Id.VarType => {
const var_type = @fieldParentPtr(ast.Node.VarType, "base", base);
try stream.print("{}", tree.tokenSlice(var_type.token));
},
ast.Node.Id.ContainerDecl => {
const container_decl = @fieldParentPtr(ast.Node.ContainerDecl, "base", base);
switch (container_decl.layout) {
ast.Node.ContainerDecl.Layout.Packed => try stream.print("packed "),
ast.Node.ContainerDecl.Layout.Extern => try stream.print("extern "),
ast.Node.ContainerDecl.Layout.Auto => { },
}
switch (container_decl.kind) {
ast.Node.ContainerDecl.Kind.Struct => try stream.print("struct"),
ast.Node.ContainerDecl.Kind.Enum => try stream.print("enum"),
ast.Node.ContainerDecl.Kind.Union => try stream.print("union"),
}
if (container_decl.fields_and_decls.len == 0) {
try stack.push(RenderState { .Text = "{}"});
} else {
try stack.push(RenderState { .Text = "}"});
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Indent = indent });
try stack.push(RenderState { .Text = "\n"});
var i = container_decl.fields_and_decls.len;
while (i != 0) {
i -= 1;
const node = *container_decl.fields_and_decls.at(i);
try stack.push(RenderState { .TopLevelDecl = node});
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState {
.Text = blk: {
if (i != 0) {
const prev_node = *container_decl.fields_and_decls.at(i - 1);
const prev_node_last_token_end = tree.tokens.at(prev_node.lastToken()).end;
const loc = tree.tokenLocation(prev_node_last_token_end, node.firstToken());
if (loc.line >= 2) {
break :blk "\n\n";
}
}
break :blk "\n";
},
});
}
try stack.push(RenderState { .Indent = indent + indent_delta});
try stack.push(RenderState { .Text = "{"});
}
switch (container_decl.init_arg_expr) {
ast.Node.ContainerDecl.InitArg.None => try stack.push(RenderState { .Text = " "}),
ast.Node.ContainerDecl.InitArg.Enum => |enum_tag_type| {
if (enum_tag_type) |expr| {
try stack.push(RenderState { .Text = ")) "});
try stack.push(RenderState { .Expression = expr});
try stack.push(RenderState { .Text = "(enum("});
} else {
try stack.push(RenderState { .Text = "(enum) "});
}
},
ast.Node.ContainerDecl.InitArg.Type => |type_expr| {
try stack.push(RenderState { .Text = ") "});
try stack.push(RenderState { .Expression = type_expr});
try stack.push(RenderState { .Text = "("});
},
}
},
ast.Node.Id.ErrorSetDecl => {
const err_set_decl = @fieldParentPtr(ast.Node.ErrorSetDecl, "base", base);
if (err_set_decl.decls.len == 0) {
try stream.write("error{}");
continue;
}
if (err_set_decl.decls.len == 1) blk: {
const node = *err_set_decl.decls.at(0);
// if there are any doc comments or same line comments
// don't try to put it all on one line
if (node.cast(ast.Node.ErrorTag)) |tag| {
if (tag.doc_comments != null) break :blk;
} else {
break :blk;
}
try stream.write("error{");
try stack.push(RenderState { .Text = "}" });
try stack.push(RenderState { .TopLevelDecl = node });
continue;
}
try stream.write("error{");
try stack.push(RenderState { .Text = "}"});
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Indent = indent });
try stack.push(RenderState { .Text = "\n"});
var i = err_set_decl.decls.len;
while (i != 0) {
i -= 1;
const node = *err_set_decl.decls.at(i);
if (node.id != ast.Node.Id.LineComment) {
try stack.push(RenderState { .Text = "," });
}
try stack.push(RenderState { .TopLevelDecl = node });
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState {
.Text = blk: {
if (i != 0) {
const prev_node = *err_set_decl.decls.at(i - 1);
const prev_node_last_token_end = tree.tokens.at(prev_node.lastToken()).end;
const loc = tree.tokenLocation(prev_node_last_token_end, node.firstToken());
if (loc.line >= 2) {
break :blk "\n\n";
}
}
break :blk "\n";
},
});
}
try stack.push(RenderState { .Indent = indent + indent_delta});
},
ast.Node.Id.MultilineStringLiteral => {
const multiline_str_literal = @fieldParentPtr(ast.Node.MultilineStringLiteral, "base", base);
try stream.print("\n");
var i : usize = 0;
while (i < multiline_str_literal.lines.len) : (i += 1) {
const t = *multiline_str_literal.lines.at(i);
try stream.writeByteNTimes(' ', indent + indent_delta);
try stream.print("{}", tree.tokenSlice(t));
}
try stream.writeByteNTimes(' ', indent);
},
ast.Node.Id.UndefinedLiteral => {
const undefined_literal = @fieldParentPtr(ast.Node.UndefinedLiteral, "base", base);
try stream.print("{}", tree.tokenSlice(undefined_literal.token));
},
ast.Node.Id.BuiltinCall => {
const builtin_call = @fieldParentPtr(ast.Node.BuiltinCall, "base", base);
try stream.print("{}(", tree.tokenSlice(builtin_call.builtin_token));
try stack.push(RenderState { .Text = ")"});
var i = builtin_call.params.len;
while (i != 0) {
i -= 1;
const param_node = *builtin_call.params.at(i);
try stack.push(RenderState { .Expression = param_node});
if (i != 0) {
try stack.push(RenderState { .Text = ", " });
}
}
},
ast.Node.Id.FnProto => {
const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", base);
switch (fn_proto.return_type) {
ast.Node.FnProto.ReturnType.Explicit => |node| {
try stack.push(RenderState { .Expression = node});
},
ast.Node.FnProto.ReturnType.InferErrorSet => |node| {
try stack.push(RenderState { .Expression = node});
try stack.push(RenderState { .Text = "!"});
},
}
if (fn_proto.align_expr) |align_expr| {
try stack.push(RenderState { .Text = ") " });
try stack.push(RenderState { .Expression = align_expr});
try stack.push(RenderState { .Text = "align(" });
}
try stack.push(RenderState { .Text = ") " });
var i = fn_proto.params.len;
while (i != 0) {
i -= 1;
const param_decl_node = *fn_proto.params.at(i);
try stack.push(RenderState { .ParamDecl = param_decl_node});
if (i != 0) {
try stack.push(RenderState { .Text = ", " });
}
}
try stack.push(RenderState { .Text = "(" });
if (fn_proto.name_token) |name_token| {
try stack.push(RenderState { .Text = tree.tokenSlice(name_token) });
try stack.push(RenderState { .Text = " " });
}
try stack.push(RenderState { .Text = "fn" });
if (fn_proto.async_attr) |async_attr| {
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Expression = &async_attr.base });
}
if (fn_proto.cc_token) |cc_token| {
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Text = tree.tokenSlice(cc_token) });
}
if (fn_proto.lib_name) |lib_name| {
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Expression = lib_name });
}
if (fn_proto.extern_export_inline_token) |extern_export_inline_token| {
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Text = tree.tokenSlice(extern_export_inline_token) });
}
if (fn_proto.visib_token) |visib_token_index| {
const visib_token = tree.tokens.at(visib_token_index);
assert(visib_token.id == Token.Id.Keyword_pub or visib_token.id == Token.Id.Keyword_export);
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Text = tree.tokenSlice(visib_token_index) });
}
},
ast.Node.Id.PromiseType => {
const promise_type = @fieldParentPtr(ast.Node.PromiseType, "base", base);
try stream.write(tree.tokenSlice(promise_type.promise_token));
if (promise_type.result) |result| {
try stream.write(tree.tokenSlice(result.arrow_token));
try stack.push(RenderState { .Expression = result.return_type});
}
},
ast.Node.Id.LineComment => {
const line_comment_node = @fieldParentPtr(ast.Node.LineComment, "base", base);
try stream.write(tree.tokenSlice(line_comment_node.token));
},
ast.Node.Id.DocComment => unreachable, // doc comments are attached to nodes
ast.Node.Id.Switch => {
const switch_node = @fieldParentPtr(ast.Node.Switch, "base", base);
try stream.print("{} (", tree.tokenSlice(switch_node.switch_token));
if (switch_node.cases.len == 0) {
try stack.push(RenderState { .Text = ") {}"});
try stack.push(RenderState { .Expression = switch_node.expr });
continue;
}
try stack.push(RenderState { .Text = "}"});
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Indent = indent });
try stack.push(RenderState { .Text = "\n"});
var i = switch_node.cases.len;
while (i != 0) {
i -= 1;
const node = *switch_node.cases.at(i);
try stack.push(RenderState { .Expression = node});
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState {
.Text = blk: {
if (i != 0) {
const prev_node = *switch_node.cases.at(i - 1);
const prev_node_last_token_end = tree.tokens.at(prev_node.lastToken()).end;
const loc = tree.tokenLocation(prev_node_last_token_end, node.firstToken());
if (loc.line >= 2) {
break :blk "\n\n";
}
}
break :blk "\n";
},
});
}
try stack.push(RenderState { .Indent = indent + indent_delta});
try stack.push(RenderState { .Text = ") {"});
try stack.push(RenderState { .Expression = switch_node.expr });
},
ast.Node.Id.SwitchCase => {
const switch_case = @fieldParentPtr(ast.Node.SwitchCase, "base", base);
try stack.push(RenderState { .Token = switch_case.lastToken() + 1 });
try stack.push(RenderState { .Expression = switch_case.expr });
if (switch_case.payload) |payload| {
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Expression = payload });
}
try stack.push(RenderState { .Text = " => "});
var i = switch_case.items.len;
while (i != 0) {
i -= 1;
try stack.push(RenderState { .Expression = *switch_case.items.at(i) });
if (i != 0) {
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Text = ",\n" });
}
}
},
ast.Node.Id.SwitchElse => {
const switch_else = @fieldParentPtr(ast.Node.SwitchElse, "base", base);
try stream.print("{}", tree.tokenSlice(switch_else.token));
},
ast.Node.Id.Else => {
const else_node = @fieldParentPtr(ast.Node.Else, "base", base);
try stream.print("{}", tree.tokenSlice(else_node.else_token));
switch (else_node.body.id) {
ast.Node.Id.Block, ast.Node.Id.If,
ast.Node.Id.For, ast.Node.Id.While,
ast.Node.Id.Switch => {
try stream.print(" ");
try stack.push(RenderState { .Expression = else_node.body });
},
else => {
try stack.push(RenderState { .Indent = indent });
try stack.push(RenderState { .Expression = else_node.body });
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Indent = indent + indent_delta });
try stack.push(RenderState { .Text = "\n" });
}
}
if (else_node.payload) |payload| {
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Expression = payload });
}
},
ast.Node.Id.While => {
const while_node = @fieldParentPtr(ast.Node.While, "base", base);
if (while_node.label) |label| {
try stream.print("{}: ", tree.tokenSlice(label));
}
if (while_node.inline_token) |inline_token| {
try stream.print("{} ", tree.tokenSlice(inline_token));
}
try stream.print("{} ", tree.tokenSlice(while_node.while_token));
if (while_node.@"else") |@"else"| {
try stack.push(RenderState { .Expression = &@"else".base });
if (while_node.body.id == ast.Node.Id.Block) {
try stack.push(RenderState { .Text = " " });
} else {
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Text = "\n" });
}
}
if (while_node.body.id == ast.Node.Id.Block) {
try stack.push(RenderState { .Expression = while_node.body });
try stack.push(RenderState { .Text = " " });
} else {
try stack.push(RenderState { .Indent = indent });
try stack.push(RenderState { .Expression = while_node.body });
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Indent = indent + indent_delta });
try stack.push(RenderState { .Text = "\n" });
}
if (while_node.continue_expr) |continue_expr| {
try stack.push(RenderState { .Text = ")" });
try stack.push(RenderState { .Expression = continue_expr });
try stack.push(RenderState { .Text = ": (" });
try stack.push(RenderState { .Text = " " });
}
if (while_node.payload) |payload| {
try stack.push(RenderState { .Expression = payload });
try stack.push(RenderState { .Text = " " });
}
try stack.push(RenderState { .Text = ")" });
try stack.push(RenderState { .Expression = while_node.condition });
try stack.push(RenderState { .Text = "(" });
},
ast.Node.Id.For => {
const for_node = @fieldParentPtr(ast.Node.For, "base", base);
if (for_node.label) |label| {
try stream.print("{}: ", tree.tokenSlice(label));
}
if (for_node.inline_token) |inline_token| {
try stream.print("{} ", tree.tokenSlice(inline_token));
}
try stream.print("{} ", tree.tokenSlice(for_node.for_token));
if (for_node.@"else") |@"else"| {
try stack.push(RenderState { .Expression = &@"else".base });
if (for_node.body.id == ast.Node.Id.Block) {
try stack.push(RenderState { .Text = " " });
} else {
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Text = "\n" });
}
}
if (for_node.body.id == ast.Node.Id.Block) {
try stack.push(RenderState { .Expression = for_node.body });
try stack.push(RenderState { .Text = " " });
} else {
try stack.push(RenderState { .Indent = indent });
try stack.push(RenderState { .Expression = for_node.body });
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Indent = indent + indent_delta });
try stack.push(RenderState { .Text = "\n" });
}
if (for_node.payload) |payload| {
try stack.push(RenderState { .Expression = payload });
try stack.push(RenderState { .Text = " " });
}
try stack.push(RenderState { .Text = ")" });
try stack.push(RenderState { .Expression = for_node.array_expr });
try stack.push(RenderState { .Text = "(" });
},
ast.Node.Id.If => {
const if_node = @fieldParentPtr(ast.Node.If, "base", base);
try stream.print("{} ", tree.tokenSlice(if_node.if_token));
switch (if_node.body.id) {
ast.Node.Id.Block, ast.Node.Id.If,
ast.Node.Id.For, ast.Node.Id.While,
ast.Node.Id.Switch => {
if (if_node.@"else") |@"else"| {
try stack.push(RenderState { .Expression = &@"else".base });
if (if_node.body.id == ast.Node.Id.Block) {
try stack.push(RenderState { .Text = " " });
} else {
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Text = "\n" });
}
}
},
else => {
if (if_node.@"else") |@"else"| {
try stack.push(RenderState { .Expression = @"else".body });
if (@"else".payload) |payload| {
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Expression = payload });
}
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Text = tree.tokenSlice(@"else".else_token) });
try stack.push(RenderState { .Text = " " });
}
}
}
try stack.push(RenderState { .Expression = if_node.body });
if (if_node.payload) |payload| {
try stack.push(RenderState { .Text = " " });
try stack.push(RenderState { .Expression = payload });
}
try stack.push(RenderState { .NonBreakToken = if_node.condition.lastToken() + 1 });
try stack.push(RenderState { .Expression = if_node.condition });
try stack.push(RenderState { .Text = "(" });
},
ast.Node.Id.Asm => {
const asm_node = @fieldParentPtr(ast.Node.Asm, "base", base);
try stream.print("{} ", tree.tokenSlice(asm_node.asm_token));
if (asm_node.volatile_token) |volatile_token| {
try stream.print("{} ", tree.tokenSlice(volatile_token));
}
try stack.push(RenderState { .Indent = indent });
try stack.push(RenderState { .Text = ")" });
{
var i = asm_node.clobbers.len;
while (i != 0) {
i -= 1;
try stack.push(RenderState { .Expression = *asm_node.clobbers.at(i) });
if (i != 0) {
try stack.push(RenderState { .Text = ", " });
}
}
}
try stack.push(RenderState { .Text = ": " });
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Indent = indent + indent_delta });
try stack.push(RenderState { .Text = "\n" });
{
var i = asm_node.inputs.len;
while (i != 0) {
i -= 1;
const node = *asm_node.inputs.at(i);
try stack.push(RenderState { .Expression = &node.base});
if (i != 0) {
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState {
.Text = blk: {
const prev_node = *asm_node.inputs.at(i - 1);
const prev_node_last_token_end = tree.tokens.at(prev_node.lastToken()).end;
const loc = tree.tokenLocation(prev_node_last_token_end, node.firstToken());
if (loc.line >= 2) {
break :blk "\n\n";
}
break :blk "\n";
},
});
try stack.push(RenderState { .Text = "," });
}
}
}
try stack.push(RenderState { .Indent = indent + indent_delta + 2});
try stack.push(RenderState { .Text = ": "});
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Indent = indent + indent_delta});
try stack.push(RenderState { .Text = "\n" });
{
var i = asm_node.outputs.len;
while (i != 0) {
i -= 1;
const node = *asm_node.outputs.at(i);
try stack.push(RenderState { .Expression = &node.base});
if (i != 0) {
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState {
.Text = blk: {
const prev_node = *asm_node.outputs.at(i - 1);
const prev_node_last_token_end = tree.tokens.at(prev_node.lastToken()).end;
const loc = tree.tokenLocation(prev_node_last_token_end, node.firstToken());
if (loc.line >= 2) {
break :blk "\n\n";
}
break :blk "\n";
},
});
try stack.push(RenderState { .Text = "," });
}
}
}
try stack.push(RenderState { .Indent = indent + indent_delta + 2});
try stack.push(RenderState { .Text = ": "});
try stack.push(RenderState.PrintIndent);
try stack.push(RenderState { .Indent = indent + indent_delta});
try stack.push(RenderState { .Text = "\n" });
try stack.push(RenderState { .Expression = asm_node.template });
try stack.push(RenderState { .Text = "(" });
},
ast.Node.Id.AsmInput => {
const asm_input = @fieldParentPtr(ast.Node.AsmInput, "base", base);
try stack.push(RenderState { .Text = ")"});
try stack.push(RenderState { .Expression = asm_input.expr});
try stack.push(RenderState { .Text = " ("});
try stack.push(RenderState { .Expression = asm_input.constraint });
try stack.push(RenderState { .Text = "] "});
try stack.push(RenderState { .Expression = asm_input.symbolic_name });
try stack.push(RenderState { .Text = "["});
},
ast.Node.Id.AsmOutput => {
const asm_output = @fieldParentPtr(ast.Node.AsmOutput, "base", base);
try stack.push(RenderState { .Text = ")"});
switch (asm_output.kind) {
ast.Node.AsmOutput.Kind.Variable => |variable_name| {
try stack.push(RenderState { .Expression = &variable_name.base});
},
ast.Node.AsmOutput.Kind.Return => |return_type| {
try stack.push(RenderState { .Expression = return_type});
try stack.push(RenderState { .Text = "-> "});
},
}
try stack.push(RenderState { .Text = " ("});
try stack.push(RenderState { .Expression = asm_output.constraint });
try stack.push(RenderState { .Text = "] "});
try stack.push(RenderState { .Expression = asm_output.symbolic_name });
try stack.push(RenderState { .Text = "["});
},
ast.Node.Id.StructField,
ast.Node.Id.UnionTag,
ast.Node.Id.EnumTag,
ast.Node.Id.ErrorTag,
ast.Node.Id.Root,
ast.Node.Id.VarDecl,
ast.Node.Id.Use,
ast.Node.Id.TestDecl,
ast.Node.Id.ParamDecl => unreachable,
},
RenderState.Statement => |base| {
switch (base.id) {
ast.Node.Id.VarDecl => {
const var_decl = @fieldParentPtr(ast.Node.VarDecl, "base", base);
try stack.push(RenderState { .VarDecl = var_decl});
},
else => {
try stack.push(RenderState { .MaybeSemiColon = base });
try stack.push(RenderState { .Expression = base });
},
}
},
RenderState.Indent => |new_indent| indent = new_indent,
RenderState.PrintIndent => try stream.writeByteNTimes(' ', indent),
RenderState.Token => |token_index| try renderToken(tree, stream, token_index, indent, true),
RenderState.NonBreakToken => |token_index| try renderToken(tree, stream, token_index, indent, false),
RenderState.MaybeSemiColon => |base| {
if (base.requireSemiColon()) {
const semicolon_index = base.lastToken() + 1;
assert(tree.tokens.at(semicolon_index).id == Token.Id.Semicolon);
try renderToken(tree, stream, semicolon_index, indent, true);
}
},
}
}
}
fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent: usize, line_break: bool) !void {
const token = tree.tokens.at(token_index);
try stream.write(tree.tokenSlicePtr(token));
const next_token = tree.tokens.at(token_index + 1);
if (next_token.id == Token.Id.LineComment) {
const loc = tree.tokenLocationPtr(token.end, next_token);
if (loc.line == 0) {
try stream.print(" {}", tree.tokenSlicePtr(next_token));
if (!line_break) {
try stream.write("\n");
try stream.writeByteNTimes(' ', indent + indent_delta);
return;
}
}
}
if (!line_break) {
try stream.writeByte(' ');
}
}
fn renderComments(tree: &ast.Tree, stream: var, node: var, indent: usize) !void {
const comment = node.doc_comments ?? return;
var it = comment.lines.iterator(0);
while (it.next()) |line_token_index| {
try stream.print("{}\n", tree.tokenSlice(*line_token_index));
try stream.writeByteNTimes(' ', indent);
}
}
|