authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-23 18:32:47+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-23 18:32:47+01:00
log5306b1a9ab5cb0730fcc21da095db39b0197e99b
treebd7ba4faa7a0c25726d0ce19da12c8b33777873c
parent1f62e87031cbb85387934313a7a9e7091d424b84
signaturelock-open Commit is signed but in an unrecognized format.

zig fmt: container doc comments


5 files changed, 187 insertions(+), 164 deletions(-)

lib/std/zig/ast.zig+47-35
...@@ -525,17 +525,17 @@ pub const Tree = struct {...@@ -525,17 +525,17 @@ pub const Tree = struct {
525 },525 },
526526
527 .container_decl,527 .container_decl,
528 .container_decl_comma,528 .container_decl_trailing,
529 .container_decl_two,529 .container_decl_two,
530 .container_decl_two_comma,530 .container_decl_two_trailing,
531 .container_decl_arg,531 .container_decl_arg,
532 .container_decl_arg_comma,532 .container_decl_arg_trailing,
533 .tagged_union,533 .tagged_union,
534 .tagged_union_comma,534 .tagged_union_trailing,
535 .tagged_union_two,535 .tagged_union_two,
536 .tagged_union_two_comma,536 .tagged_union_two_trailing,
537 .tagged_union_enum_tag,537 .tagged_union_enum_tag,
538 .tagged_union_enum_tag_comma,538 .tagged_union_enum_tag_trailing,
539 => {539 => {
540 const main_token = main_tokens[n];540 const main_token = main_tokens[n];
541 switch (token_tags[main_token - 1]) {541 switch (token_tags[main_token - 1]) {
...@@ -606,6 +606,7 @@ pub const Tree = struct {...@@ -606,6 +606,7 @@ pub const Tree = struct {
606 const datas = tree.nodes.items(.data);606 const datas = tree.nodes.items(.data);
607 const main_tokens = tree.nodes.items(.main_token);607 const main_tokens = tree.nodes.items(.main_token);
608 const token_starts = tree.tokens.items(.start);608 const token_starts = tree.tokens.items(.start);
609 const token_tags = tree.tokens.items(.tag);
609 var n = node;610 var n = node;
610 var end_offset: TokenIndex = 0;611 var end_offset: TokenIndex = 0;
611 while (true) switch (tags[n]) {612 while (true) switch (tags[n]) {
...@@ -738,9 +739,9 @@ pub const Tree = struct {...@@ -738,9 +739,9 @@ pub const Tree = struct {
738 },739 },
739 .call_comma,740 .call_comma,
740 .async_call_comma,741 .async_call_comma,
741 .tagged_union_enum_tag_comma,742 .tagged_union_enum_tag_trailing,
742 => {743 => {
743 end_offset += 2; // for the comma + rparen/rbrace744 end_offset += 2; // for the comma/semicolon + rparen/rbrace
744 const params = tree.extraData(datas[n].rhs, Node.SubRange);745 const params = tree.extraData(datas[n].rhs, Node.SubRange);
745 assert(params.end > params.start);746 assert(params.end > params.start);
746 n = tree.extra_data[params.end - 1]; // last parameter747 n = tree.extra_data[params.end - 1]; // last parameter
...@@ -779,7 +780,7 @@ pub const Tree = struct {...@@ -779,7 +780,7 @@ pub const Tree = struct {
779 },780 },
780 .array_init_comma,781 .array_init_comma,
781 .struct_init_comma,782 .struct_init_comma,
782 .container_decl_arg_comma,783 .container_decl_arg_trailing,
783 .switch_comma,784 .switch_comma,
784 => {785 => {
785 const members = tree.extraData(datas[n].rhs, Node.SubRange);786 const members = tree.extraData(datas[n].rhs, Node.SubRange);
...@@ -801,8 +802,8 @@ pub const Tree = struct {...@@ -801,8 +802,8 @@ pub const Tree = struct {
801 .array_init_dot_comma,802 .array_init_dot_comma,
802 .struct_init_dot_comma,803 .struct_init_dot_comma,
803 .block_semicolon,804 .block_semicolon,
804 .container_decl_comma,805 .container_decl_trailing,
805 .tagged_union_comma,806 .tagged_union_trailing,
806 .builtin_call_comma,807 .builtin_call_comma,
807 => {808 => {
808 assert(datas[n].rhs - datas[n].lhs > 0);809 assert(datas[n].rhs - datas[n].lhs > 0);
...@@ -838,10 +839,17 @@ pub const Tree = struct {...@@ -838,10 +839,17 @@ pub const Tree = struct {
838 .block_two,839 .block_two,
839 .struct_init_dot_two,840 .struct_init_dot_two,
840 => end_offset += 1, // rbrace841 => end_offset += 1, // rbrace
841 .builtin_call_two,842 .builtin_call_two => end_offset += 2, // lparen/lbrace + rparen/rbrace
842 .container_decl_two,843 .container_decl_two => {
843 => end_offset += 2, // lparen/lbrace + rparen/rbrace844 var i: u32 = 2; // lbrace + rbrace
844 .tagged_union_two => end_offset += 5, // (enum) {}845 while (token_tags[main_tokens[n] + i] == .container_doc_comment) i += 1;
846 end_offset += i;
847 },
848 .tagged_union_two => {
849 var i: u32 = 5; // (enum) {}
850 while (token_tags[main_tokens[n] + i] == .container_doc_comment) i += 1;
851 end_offset += i;
852 },
845 else => unreachable,853 else => unreachable,
846 }854 }
847 return main_tokens[n] + end_offset;855 return main_tokens[n] + end_offset;
...@@ -851,8 +859,8 @@ pub const Tree = struct {...@@ -851,8 +859,8 @@ pub const Tree = struct {
851 .builtin_call_two_comma,859 .builtin_call_two_comma,
852 .block_two_semicolon,860 .block_two_semicolon,
853 .struct_init_dot_two_comma,861 .struct_init_dot_two_comma,
854 .container_decl_two_comma,862 .container_decl_two_trailing,
855 .tagged_union_two_comma,863 .tagged_union_two_trailing,
856 => {864 => {
857 end_offset += 2; // for the comma/semicolon + rbrace/rparen865 end_offset += 2; // for the comma/semicolon + rbrace/rparen
858 if (datas[n].rhs != 0) {866 if (datas[n].rhs != 0) {
...@@ -1531,7 +1539,7 @@ pub const Tree = struct {...@@ -1531,7 +1539,7 @@ pub const Tree = struct {
15311539
1532 pub fn containerDeclTwo(tree: Tree, buffer: *[2]Node.Index, node: Node.Index) full.ContainerDecl {1540 pub fn containerDeclTwo(tree: Tree, buffer: *[2]Node.Index, node: Node.Index) full.ContainerDecl {
1533 assert(tree.nodes.items(.tag)[node] == .container_decl_two or1541 assert(tree.nodes.items(.tag)[node] == .container_decl_two or
1534 tree.nodes.items(.tag)[node] == .container_decl_two_comma);1542 tree.nodes.items(.tag)[node] == .container_decl_two_trailing);
1535 const data = tree.nodes.items(.data)[node];1543 const data = tree.nodes.items(.data)[node];
1536 buffer.* = .{ data.lhs, data.rhs };1544 buffer.* = .{ data.lhs, data.rhs };
1537 const members = if (data.rhs != 0)1545 const members = if (data.rhs != 0)
...@@ -1550,7 +1558,7 @@ pub const Tree = struct {...@@ -1550,7 +1558,7 @@ pub const Tree = struct {
15501558
1551 pub fn containerDecl(tree: Tree, node: Node.Index) full.ContainerDecl {1559 pub fn containerDecl(tree: Tree, node: Node.Index) full.ContainerDecl {
1552 assert(tree.nodes.items(.tag)[node] == .container_decl or1560 assert(tree.nodes.items(.tag)[node] == .container_decl or
1553 tree.nodes.items(.tag)[node] == .container_decl_comma);1561 tree.nodes.items(.tag)[node] == .container_decl_trailing);
1554 const data = tree.nodes.items(.data)[node];1562 const data = tree.nodes.items(.data)[node];
1555 return tree.fullContainerDecl(.{1563 return tree.fullContainerDecl(.{
1556 .main_token = tree.nodes.items(.main_token)[node],1564 .main_token = tree.nodes.items(.main_token)[node],
...@@ -1562,7 +1570,7 @@ pub const Tree = struct {...@@ -1562,7 +1570,7 @@ pub const Tree = struct {
15621570
1563 pub fn containerDeclArg(tree: Tree, node: Node.Index) full.ContainerDecl {1571 pub fn containerDeclArg(tree: Tree, node: Node.Index) full.ContainerDecl {
1564 assert(tree.nodes.items(.tag)[node] == .container_decl_arg or1572 assert(tree.nodes.items(.tag)[node] == .container_decl_arg or
1565 tree.nodes.items(.tag)[node] == .container_decl_arg_comma);1573 tree.nodes.items(.tag)[node] == .container_decl_arg_trailing);
1566 const data = tree.nodes.items(.data)[node];1574 const data = tree.nodes.items(.data)[node];
1567 const members_range = tree.extraData(data.rhs, Node.SubRange);1575 const members_range = tree.extraData(data.rhs, Node.SubRange);
1568 return tree.fullContainerDecl(.{1576 return tree.fullContainerDecl(.{
...@@ -1575,7 +1583,7 @@ pub const Tree = struct {...@@ -1575,7 +1583,7 @@ pub const Tree = struct {
15751583
1576 pub fn taggedUnionTwo(tree: Tree, buffer: *[2]Node.Index, node: Node.Index) full.ContainerDecl {1584 pub fn taggedUnionTwo(tree: Tree, buffer: *[2]Node.Index, node: Node.Index) full.ContainerDecl {
1577 assert(tree.nodes.items(.tag)[node] == .tagged_union_two or1585 assert(tree.nodes.items(.tag)[node] == .tagged_union_two or
1578 tree.nodes.items(.tag)[node] == .tagged_union_two_comma);1586 tree.nodes.items(.tag)[node] == .tagged_union_two_trailing);
1579 const data = tree.nodes.items(.data)[node];1587 const data = tree.nodes.items(.data)[node];
1580 buffer.* = .{ data.lhs, data.rhs };1588 buffer.* = .{ data.lhs, data.rhs };
1581 const members = if (data.rhs != 0)1589 const members = if (data.rhs != 0)
...@@ -1595,7 +1603,7 @@ pub const Tree = struct {...@@ -1595,7 +1603,7 @@ pub const Tree = struct {
15951603
1596 pub fn taggedUnion(tree: Tree, node: Node.Index) full.ContainerDecl {1604 pub fn taggedUnion(tree: Tree, node: Node.Index) full.ContainerDecl {
1597 assert(tree.nodes.items(.tag)[node] == .tagged_union or1605 assert(tree.nodes.items(.tag)[node] == .tagged_union or
1598 tree.nodes.items(.tag)[node] == .tagged_union_comma);1606 tree.nodes.items(.tag)[node] == .tagged_union_trailing);
1599 const data = tree.nodes.items(.data)[node];1607 const data = tree.nodes.items(.data)[node];
1600 const main_token = tree.nodes.items(.main_token)[node];1608 const main_token = tree.nodes.items(.main_token)[node];
1601 return tree.fullContainerDecl(.{1609 return tree.fullContainerDecl(.{
...@@ -1608,7 +1616,7 @@ pub const Tree = struct {...@@ -1608,7 +1616,7 @@ pub const Tree = struct {
16081616
1609 pub fn taggedUnionEnumTag(tree: Tree, node: Node.Index) full.ContainerDecl {1617 pub fn taggedUnionEnumTag(tree: Tree, node: Node.Index) full.ContainerDecl {
1610 assert(tree.nodes.items(.tag)[node] == .tagged_union_enum_tag or1618 assert(tree.nodes.items(.tag)[node] == .tagged_union_enum_tag or
1611 tree.nodes.items(.tag)[node] == .tagged_union_enum_tag_comma);1619 tree.nodes.items(.tag)[node] == .tagged_union_enum_tag_trailing);
1612 const data = tree.nodes.items(.data)[node];1620 const data = tree.nodes.items(.data)[node];
1613 const members_range = tree.extraData(data.rhs, Node.SubRange);1621 const members_range = tree.extraData(data.rhs, Node.SubRange);
1614 const main_token = tree.nodes.items(.main_token)[node];1622 const main_token = tree.nodes.items(.main_token)[node];
...@@ -2762,36 +2770,40 @@ pub const Node = struct {...@@ -2762,36 +2770,40 @@ pub const Node = struct {
2762 /// `struct {}`, `union {}`, `opaque {}`, `enum {}`. `extra_data[lhs..rhs]`.2770 /// `struct {}`, `union {}`, `opaque {}`, `enum {}`. `extra_data[lhs..rhs]`.
2763 /// main_token is `struct`, `union`, `opaque`, `enum` keyword.2771 /// main_token is `struct`, `union`, `opaque`, `enum` keyword.
2764 container_decl,2772 container_decl,
2765 /// Same as ContainerDecl but there is known to be a trailing comma before the rbrace.2773 /// Same as ContainerDecl but there is known to be a trailing comma
2766 container_decl_comma,2774 /// or semicolon before the rbrace.
2775 container_decl_trailing,
2767 /// `struct {lhs, rhs}`, `union {lhs, rhs}`, `opaque {lhs, rhs}`, `enum {lhs, rhs}`.2776 /// `struct {lhs, rhs}`, `union {lhs, rhs}`, `opaque {lhs, rhs}`, `enum {lhs, rhs}`.
2768 /// lhs or rhs can be omitted.2777 /// lhs or rhs can be omitted.
2769 /// main_token is `struct`, `union`, `opaque`, `enum` keyword.2778 /// main_token is `struct`, `union`, `opaque`, `enum` keyword.
2770 container_decl_two,2779 container_decl_two,
2771 /// Same as ContainerDeclTwo except there is known to be a trailing comma2780 /// Same as ContainerDeclTwo except there is known to be a trailing comma
2772 /// before the rbrace.2781 /// or semicolon before the rbrace.
2773 container_decl_two_comma,2782 container_decl_two_trailing,
2774 /// `union(lhs)` / `enum(lhs)`. `SubRange[rhs]`.2783 /// `union(lhs)` / `enum(lhs)`. `SubRange[rhs]`.
2775 container_decl_arg,2784 container_decl_arg,
2776 /// Same as container_decl_arg but there is known to be a trailing comma before the rbrace.2785 /// Same as container_decl_arg but there is known to be a trailing
2777 container_decl_arg_comma,2786 /// comma or semicolon before the rbrace.
2787 container_decl_arg_trailing,
2778 /// `union(enum) {}`. `sub_list[lhs..rhs]`.2788 /// `union(enum) {}`. `sub_list[lhs..rhs]`.
2779 /// Note that tagged unions with explicitly provided enums are represented2789 /// Note that tagged unions with explicitly provided enums are represented
2780 /// by `container_decl_arg`.2790 /// by `container_decl_arg`.
2781 tagged_union,2791 tagged_union,
2782 /// Same as tagged_union but there is known to be a trailing comma before the rbrace.2792 /// Same as tagged_union but there is known to be a trailing comma
2783 tagged_union_comma,2793 /// or semicolon before the rbrace.
2794 tagged_union_trailing,
2784 /// `union(enum) {lhs, rhs}`. lhs or rhs may be omitted.2795 /// `union(enum) {lhs, rhs}`. lhs or rhs may be omitted.
2785 /// Note that tagged unions with explicitly provided enums are represented2796 /// Note that tagged unions with explicitly provided enums are represented
2786 /// by `container_decl_arg`.2797 /// by `container_decl_arg`.
2787 tagged_union_two,2798 tagged_union_two,
2788 /// Same as tagged_union_two but there is known to be a trailing comma before the rbrace.2799 /// Same as tagged_union_two but there is known to be a trailing comma
2789 tagged_union_two_comma,2800 /// or semicolon before the rbrace.
2801 tagged_union_two_trailing,
2790 /// `union(enum(lhs)) {}`. `SubRange[rhs]`.2802 /// `union(enum(lhs)) {}`. `SubRange[rhs]`.
2791 tagged_union_enum_tag,2803 tagged_union_enum_tag,
2792 /// Same as tagged_union_enum_tag but there is known to be a trailing comma2804 /// Same as tagged_union_enum_tag but there is known to be a trailing comma
2793 /// before the rbrace.2805 /// or semicolon before the rbrace.
2794 tagged_union_enum_tag_comma,2806 tagged_union_enum_tag_trailing,
2795 /// `a: lhs = rhs,`. lhs and rhs can be omitted.2807 /// `a: lhs = rhs,`. lhs and rhs can be omitted.
2796 /// main_token is the field name identifier.2808 /// main_token is the field name identifier.
2797 /// lastToken() does not include the possible trailing comma.2809 /// lastToken() does not include the possible trailing comma.
lib/std/zig/parse.zig+27-27
...@@ -113,7 +113,7 @@ const Parser = struct {...@@ -113,7 +113,7 @@ const Parser = struct {
113 len: usize,113 len: usize,
114 lhs: Node.Index,114 lhs: Node.Index,
115 rhs: Node.Index,115 rhs: Node.Index,
116 trailing_comma: bool,116 trailing: bool,
117117
118 fn toSpan(self: Members, p: *Parser) !Node.SubRange {118 fn toSpan(self: Members, p: *Parser) !Node.SubRange {
119 if (self.len <= 2) {119 if (self.len <= 2) {
...@@ -215,7 +215,7 @@ const Parser = struct {...@@ -215,7 +215,7 @@ const Parser = struct {
215 // Skip container doc comments.215 // Skip container doc comments.
216 while (p.eatToken(.container_doc_comment)) |_| {}216 while (p.eatToken(.container_doc_comment)) |_| {}
217217
218 var trailing_comma = false;218 var trailing = false;
219 while (true) {219 while (true) {
220 const doc_comment = try p.eatDocComments();220 const doc_comment = try p.eatDocComments();
221221
...@@ -228,7 +228,7 @@ const Parser = struct {...@@ -228,7 +228,7 @@ const Parser = struct {
228 }228 }
229 try list.append(test_decl_node);229 try list.append(test_decl_node);
230 }230 }
231 trailing_comma = false;231 trailing = false;
232 },232 },
233 .keyword_comptime => switch (p.token_tags[p.tok_i + 1]) {233 .keyword_comptime => switch (p.token_tags[p.tok_i + 1]) {
234 .identifier => {234 .identifier => {
...@@ -251,11 +251,11 @@ const Parser = struct {...@@ -251,11 +251,11 @@ const Parser = struct {
251 switch (p.token_tags[p.tok_i]) {251 switch (p.token_tags[p.tok_i]) {
252 .comma => {252 .comma => {
253 p.tok_i += 1;253 p.tok_i += 1;
254 trailing_comma = true;254 trailing = true;
255 continue;255 continue;
256 },256 },
257 .r_brace, .eof => {257 .r_brace, .eof => {
258 trailing_comma = false;258 trailing = false;
259 break;259 break;
260 },260 },
261 else => {},261 else => {},
...@@ -289,7 +289,7 @@ const Parser = struct {...@@ -289,7 +289,7 @@ const Parser = struct {
289 }289 }
290 try list.append(comptime_node);290 try list.append(comptime_node);
291 }291 }
292 trailing_comma = false;292 trailing = false;
293 },293 },
294 else => {294 else => {
295 p.tok_i += 1;295 p.tok_i += 1;
...@@ -305,7 +305,7 @@ const Parser = struct {...@@ -305,7 +305,7 @@ const Parser = struct {
305 }305 }
306 try list.append(top_level_decl);306 try list.append(top_level_decl);
307 }307 }
308 trailing_comma = false;308 trailing = p.token_tags[p.tok_i - 1] == .semicolon;
309 },309 },
310 .keyword_usingnamespace => {310 .keyword_usingnamespace => {
311 const node = try p.expectUsingNamespaceRecoverable();311 const node = try p.expectUsingNamespaceRecoverable();
...@@ -315,7 +315,7 @@ const Parser = struct {...@@ -315,7 +315,7 @@ const Parser = struct {
315 }315 }
316 try list.append(node);316 try list.append(node);
317 }317 }
318 trailing_comma = false;318 trailing = p.token_tags[p.tok_i - 1] == .semicolon;
319 },319 },
320 .keyword_const,320 .keyword_const,
321 .keyword_var,321 .keyword_var,
...@@ -333,7 +333,7 @@ const Parser = struct {...@@ -333,7 +333,7 @@ const Parser = struct {
333 }333 }
334 try list.append(top_level_decl);334 try list.append(top_level_decl);
335 }335 }
336 trailing_comma = false;336 trailing = p.token_tags[p.tok_i - 1] == .semicolon;
337 },337 },
338 .identifier => {338 .identifier => {
339 const container_field = try p.expectContainerFieldRecoverable();339 const container_field = try p.expectContainerFieldRecoverable();
...@@ -354,11 +354,11 @@ const Parser = struct {...@@ -354,11 +354,11 @@ const Parser = struct {
354 switch (p.token_tags[p.tok_i]) {354 switch (p.token_tags[p.tok_i]) {
355 .comma => {355 .comma => {
356 p.tok_i += 1;356 p.tok_i += 1;
357 trailing_comma = true;357 trailing = true;
358 continue;358 continue;
359 },359 },
360 .r_brace, .eof => {360 .r_brace, .eof => {
361 trailing_comma = false;361 trailing = false;
362 break;362 break;
363 },363 },
364 else => {},364 else => {},
...@@ -391,19 +391,19 @@ const Parser = struct {...@@ -391,19 +391,19 @@ const Parser = struct {
391 .len = 0,391 .len = 0,
392 .lhs = 0,392 .lhs = 0,
393 .rhs = 0,393 .rhs = 0,
394 .trailing_comma = trailing_comma,394 .trailing = trailing,
395 },395 },
396 1 => return Members{396 1 => return Members{
397 .len = 1,397 .len = 1,
398 .lhs = list.items[0],398 .lhs = list.items[0],
399 .rhs = 0,399 .rhs = 0,
400 .trailing_comma = trailing_comma,400 .trailing = trailing,
401 },401 },
402 2 => return Members{402 2 => return Members{
403 .len = 2,403 .len = 2,
404 .lhs = list.items[0],404 .lhs = list.items[0],
405 .rhs = list.items[1],405 .rhs = list.items[1],
406 .trailing_comma = trailing_comma,406 .trailing = trailing,
407 },407 },
408 else => {408 else => {
409 const span = try p.listToSpan(list.items);409 const span = try p.listToSpan(list.items);
...@@ -411,7 +411,7 @@ const Parser = struct {...@@ -411,7 +411,7 @@ const Parser = struct {
411 .len = list.items.len,411 .len = list.items.len,
412 .lhs = span.start,412 .lhs = span.start,
413 .rhs = span.end,413 .rhs = span.end,
414 .trailing_comma = trailing_comma,414 .trailing = trailing,
415 };415 };
416 },416 },
417 }417 }
...@@ -3575,8 +3575,8 @@ const Parser = struct {...@@ -3575,8 +3575,8 @@ const Parser = struct {
3575 const members_span = try members.toSpan(p);3575 const members_span = try members.toSpan(p);
3576 _ = try p.expectToken(.r_brace);3576 _ = try p.expectToken(.r_brace);
3577 return p.addNode(.{3577 return p.addNode(.{
3578 .tag = switch (members.trailing_comma) {3578 .tag = switch (members.trailing) {
3579 true => .tagged_union_enum_tag_comma,3579 true => .tagged_union_enum_tag_trailing,
3580 false => .tagged_union_enum_tag,3580 false => .tagged_union_enum_tag,
3581 },3581 },
3582 .main_token = main_token,3582 .main_token = main_token,
...@@ -3593,8 +3593,8 @@ const Parser = struct {...@@ -3593,8 +3593,8 @@ const Parser = struct {
3593 _ = try p.expectToken(.r_brace);3593 _ = try p.expectToken(.r_brace);
3594 if (members.len <= 2) {3594 if (members.len <= 2) {
3595 return p.addNode(.{3595 return p.addNode(.{
3596 .tag = switch (members.trailing_comma) {3596 .tag = switch (members.trailing) {
3597 true => .tagged_union_two_comma,3597 true => .tagged_union_two_trailing,
3598 false => .tagged_union_two,3598 false => .tagged_union_two,
3599 },3599 },
3600 .main_token = main_token,3600 .main_token = main_token,
...@@ -3606,8 +3606,8 @@ const Parser = struct {...@@ -3606,8 +3606,8 @@ const Parser = struct {
3606 } else {3606 } else {
3607 const span = try members.toSpan(p);3607 const span = try members.toSpan(p);
3608 return p.addNode(.{3608 return p.addNode(.{
3609 .tag = switch (members.trailing_comma) {3609 .tag = switch (members.trailing) {
3610 true => .tagged_union_comma,3610 true => .tagged_union_trailing,
3611 false => .tagged_union,3611 false => .tagged_union,
3612 },3612 },
3613 .main_token = main_token,3613 .main_token = main_token,
...@@ -3638,8 +3638,8 @@ const Parser = struct {...@@ -3638,8 +3638,8 @@ const Parser = struct {
3638 if (arg_expr == 0) {3638 if (arg_expr == 0) {
3639 if (members.len <= 2) {3639 if (members.len <= 2) {
3640 return p.addNode(.{3640 return p.addNode(.{
3641 .tag = switch (members.trailing_comma) {3641 .tag = switch (members.trailing) {
3642 true => .container_decl_two_comma,3642 true => .container_decl_two_trailing,
3643 false => .container_decl_two,3643 false => .container_decl_two,
3644 },3644 },
3645 .main_token = main_token,3645 .main_token = main_token,
...@@ -3651,8 +3651,8 @@ const Parser = struct {...@@ -3651,8 +3651,8 @@ const Parser = struct {
3651 } else {3651 } else {
3652 const span = try members.toSpan(p);3652 const span = try members.toSpan(p);
3653 return p.addNode(.{3653 return p.addNode(.{
3654 .tag = switch (members.trailing_comma) {3654 .tag = switch (members.trailing) {
3655 true => .container_decl_comma,3655 true => .container_decl_trailing,
3656 false => .container_decl,3656 false => .container_decl,
3657 },3657 },
3658 .main_token = main_token,3658 .main_token = main_token,
...@@ -3665,8 +3665,8 @@ const Parser = struct {...@@ -3665,8 +3665,8 @@ const Parser = struct {
3665 } else {3665 } else {
3666 const span = try members.toSpan(p);3666 const span = try members.toSpan(p);
3667 return p.addNode(.{3667 return p.addNode(.{
3668 .tag = switch (members.trailing_comma) {3668 .tag = switch (members.trailing) {
3669 true => .container_decl_arg_comma,3669 true => .container_decl_arg_trailing,
3670 false => .container_decl_arg,3670 false => .container_decl_arg,
3671 },3671 },
3672 .main_token = main_token,3672 .main_token = main_token,
lib/std/zig/parser_test.zig+55-55
...@@ -3577,61 +3577,61 @@ test "zig fmt: file ends with struct field" {...@@ -3577,61 +3577,61 @@ test "zig fmt: file ends with struct field" {
3577// );3577// );
3578//}3578//}
35793579
3580//test "zig fmt: top level doc comments" {3580test "zig fmt: container doc comments" {
3581// try testCanonical(3581 try testCanonical(
3582// \\//! tld 13582 \\//! tld 1
3583// \\//! tld 23583 \\//! tld 2
3584// \\//! tld 33584 \\//! tld 3
3585// \\3585 \\
3586// \\// comment3586 \\// comment
3587// \\3587 \\
3588// \\/// A doc3588 \\/// A doc
3589// \\const A = struct {3589 \\const A = struct {
3590// \\ //! A tld 13590 \\ //! A tld 1
3591// \\ //! A tld 23591 \\ //! A tld 2
3592// \\ //! A tld 33592 \\ //! A tld 3
3593// \\};3593 \\};
3594// \\3594 \\
3595// \\/// B doc3595 \\/// B doc
3596// \\const B = struct {3596 \\const B = struct {
3597// \\ //! B tld 13597 \\ //! B tld 1
3598// \\ //! B tld 23598 \\ //! B tld 2
3599// \\ //! B tld 33599 \\ //! B tld 3
3600// \\3600 \\
3601// \\ /// b doc3601 \\ /// B doc
3602// \\ b: u32,3602 \\ b: u32,
3603// \\};3603 \\};
3604// \\3604 \\
3605// \\/// C doc3605 \\/// C doc
3606// \\const C = struct {3606 \\const C = union(enum) { // comment
3607// \\ //! C tld 13607 \\ //! C tld 1
3608// \\ //! C tld 23608 \\ //! C tld 2
3609// \\ //! C tld 33609 \\ //! C tld 3
3610// \\3610 \\};
3611// \\ /// c1 doc3611 \\
3612// \\ c1: u32,3612 \\/// D doc
3613// \\3613 \\const D = union(Foo) {
3614// \\ //! C tld 43614 \\ //! D tld 1
3615// \\ //! C tld 53615 \\ //! D tld 2
3616// \\ //! C tld 63616 \\ //! D tld 3
3617// \\3617 \\
3618// \\ /// c2 doc3618 \\ /// D doc
3619// \\ c2: u32,3619 \\ b: u32,
3620// \\};3620 \\};
3621// \\3621 \\
3622// );3622 );
3623// try testCanonical(3623 try testCanonical(
3624// \\//! Top-level documentation.3624 \\//! Top-level documentation.
3625// \\3625 \\
3626// \\/// This is A3626 \\/// This is A
3627// \\pub const A = usize;3627 \\pub const A = usize;
3628// \\3628 \\
3629// );3629 );
3630// try testCanonical(3630 try testCanonical(
3631// \\//! Nothing here3631 \\//! Nothing here
3632// \\3632 \\
3633// );3633 );
3634//}3634}
36353635
3636test "zig fmt: extern without container keyword returns error" {3636test "zig fmt: extern without container keyword returns error" {
3637 try testError(3637 try testError(
lib/std/zig/render.zig+40-29
...@@ -30,6 +30,10 @@ pub fn renderTree(buffer: *std.ArrayList(u8), tree: ast.Tree) Error!void {...@@ -30,6 +30,10 @@ pub fn renderTree(buffer: *std.ArrayList(u8), tree: ast.Tree) Error!void {
30 const comment_end_loc = tree.tokens.items(.start)[0];30 const comment_end_loc = tree.tokens.items(.start)[0];
31 _ = try renderComments(ais, tree, 0, comment_end_loc);31 _ = try renderComments(ais, tree, 0, comment_end_loc);
3232
33 if (tree.tokens.items(.tag)[0] == .container_doc_comment) {
34 try renderContainerDocComments(ais, tree, 0);
35 }
36
33 try renderMembers(buffer.allocator, ais, tree, tree.rootDecls());37 try renderMembers(buffer.allocator, ais, tree, tree.rootDecls());
3438
35 if (ais.disabled_offset) |disabled_offset| {39 if (ais.disabled_offset) |disabled_offset| {
...@@ -506,28 +510,28 @@ fn renderExpression(gpa: *Allocator, ais: *Ais, tree: ast.Tree, node: ast.Node.I...@@ -506,28 +510,28 @@ fn renderExpression(gpa: *Allocator, ais: *Ais, tree: ast.Tree, node: ast.Node.I
506 },510 },
507511
508 .container_decl,512 .container_decl,
509 .container_decl_comma,513 .container_decl_trailing,
510 => return renderContainerDecl(gpa, ais, tree, tree.containerDecl(node), space),514 => return renderContainerDecl(gpa, ais, tree, node, tree.containerDecl(node), space),
511515
512 .container_decl_two, .container_decl_two_comma => {516 .container_decl_two, .container_decl_two_trailing => {
513 var buffer: [2]ast.Node.Index = undefined;517 var buffer: [2]ast.Node.Index = undefined;
514 return renderContainerDecl(gpa, ais, tree, tree.containerDeclTwo(&buffer, node), space);518 return renderContainerDecl(gpa, ais, tree, node, tree.containerDeclTwo(&buffer, node), space);
515 },519 },
516 .container_decl_arg,520 .container_decl_arg,
517 .container_decl_arg_comma,521 .container_decl_arg_trailing,
518 => return renderContainerDecl(gpa, ais, tree, tree.containerDeclArg(node), space),522 => return renderContainerDecl(gpa, ais, tree, node, tree.containerDeclArg(node), space),
519523
520 .tagged_union,524 .tagged_union,
521 .tagged_union_comma,525 .tagged_union_trailing,
522 => return renderContainerDecl(gpa, ais, tree, tree.taggedUnion(node), space),526 => return renderContainerDecl(gpa, ais, tree, node, tree.taggedUnion(node), space),
523527
524 .tagged_union_two, .tagged_union_two_comma => {528 .tagged_union_two, .tagged_union_two_trailing => {
525 var buffer: [2]ast.Node.Index = undefined;529 var buffer: [2]ast.Node.Index = undefined;
526 return renderContainerDecl(gpa, ais, tree, tree.taggedUnionTwo(&buffer, node), space);530 return renderContainerDecl(gpa, ais, tree, node, tree.taggedUnionTwo(&buffer, node), space);
527 },531 },
528 .tagged_union_enum_tag,532 .tagged_union_enum_tag,
529 .tagged_union_enum_tag_comma,533 .tagged_union_enum_tag_trailing,
530 => return renderContainerDecl(gpa, ais, tree, tree.taggedUnionEnumTag(node), space),534 => return renderContainerDecl(gpa, ais, tree, node, tree.taggedUnionEnumTag(node), space),
531535
532 .error_set_decl => {536 .error_set_decl => {
533 const error_token = main_tokens[node];537 const error_token = main_tokens[node];
...@@ -1662,7 +1666,6 @@ fn renderArrayInit(...@@ -1662,7 +1666,6 @@ fn renderArrayInit(
1662 ais.pushIndentNextLine();1666 ais.pushIndentNextLine();
1663 try renderToken(ais, tree, array_init.ast.lbrace, .newline);1667 try renderToken(ais, tree, array_init.ast.lbrace, .newline);
16641668
1665
1666 var expr_index: usize = 0;1669 var expr_index: usize = 0;
1667 while (rowSize(tree, array_init.ast.elements[expr_index..], rbrace)) |row_size| {1670 while (rowSize(tree, array_init.ast.elements[expr_index..], rbrace)) |row_size| {
1668 const row_exprs = array_init.ast.elements[expr_index..];1671 const row_exprs = array_init.ast.elements[expr_index..];
...@@ -1806,6 +1809,7 @@ fn renderContainerDecl(...@@ -1806,6 +1809,7 @@ fn renderContainerDecl(
1806 gpa: *Allocator,1809 gpa: *Allocator,
1807 ais: *Ais,1810 ais: *Ais,
1808 tree: ast.Tree,1811 tree: ast.Tree,
1812 container_decl_node: ast.Node.Index,
1809 container_decl: ast.full.ContainerDecl,1813 container_decl: ast.full.ContainerDecl,
1810 space: Space,1814 space: Space,
1811) Error!void {1815) Error!void {
...@@ -1844,25 +1848,20 @@ fn renderContainerDecl(...@@ -1844,25 +1848,20 @@ fn renderContainerDecl(
1844 lbrace = container_decl.ast.main_token + 1;1848 lbrace = container_decl.ast.main_token + 1;
1845 }1849 }
18461850
1851 const rbrace = tree.lastToken(container_decl_node);
1847 if (container_decl.ast.members.len == 0) {1852 if (container_decl.ast.members.len == 0) {
1848 try renderToken(ais, tree, lbrace, Space.none); // lbrace1853 ais.pushIndentNextLine();
1849 return renderToken(ais, tree, lbrace + 1, space); // rbrace1854 if (token_tags[lbrace + 1] == .container_doc_comment) {
1855 try renderToken(ais, tree, lbrace, .newline); // lbrace
1856 try renderContainerDocComments(ais, tree, lbrace + 1);
1857 } else {
1858 try renderToken(ais, tree, lbrace, .none); // lbrace
1859 }
1860 ais.popIndent();
1861 return renderToken(ais, tree, rbrace, space); // rbrace
1850 }1862 }
18511863
1852 const last_member = container_decl.ast.members[container_decl.ast.members.len - 1];1864 const src_has_trailing_comma = token_tags[rbrace - 1] == .comma;
1853 const last_member_token = tree.lastToken(last_member);
1854 const rbrace = switch (token_tags[last_member_token + 1]) {
1855 .doc_comment => last_member_token + 2,
1856 .comma, .semicolon => switch (token_tags[last_member_token + 2]) {
1857 .doc_comment => last_member_token + 3,
1858 .r_brace => last_member_token + 2,
1859 else => unreachable,
1860 },
1861 .r_brace => last_member_token + 1,
1862 else => unreachable,
1863 };
1864 const src_has_trailing_comma = token_tags[last_member_token + 1] == .comma;
1865
1866 if (!src_has_trailing_comma) one_line: {1865 if (!src_has_trailing_comma) one_line: {
1867 // We can only print all the members in-line if all the members are fields.1866 // We can only print all the members in-line if all the members are fields.
1868 for (container_decl.ast.members) |member| {1867 for (container_decl.ast.members) |member| {
...@@ -1879,6 +1878,9 @@ fn renderContainerDecl(...@@ -1879,6 +1878,9 @@ fn renderContainerDecl(
1879 // One member per line.1878 // One member per line.
1880 ais.pushIndentNextLine();1879 ais.pushIndentNextLine();
1881 try renderToken(ais, tree, lbrace, .newline); // lbrace1880 try renderToken(ais, tree, lbrace, .newline); // lbrace
1881 if (token_tags[lbrace + 1] == .container_doc_comment) {
1882 try renderContainerDocComments(ais, tree, lbrace + 1);
1883 }
1882 try renderMembers(gpa, ais, tree, container_decl.ast.members);1884 try renderMembers(gpa, ais, tree, container_decl.ast.members);
1883 ais.popIndent();1885 ais.popIndent();
18841886
...@@ -2272,6 +2274,15 @@ fn renderDocComments(ais: *Ais, tree: ast.Tree, end_token: ast.TokenIndex) Error...@@ -2272,6 +2274,15 @@ fn renderDocComments(ais: *Ais, tree: ast.Tree, end_token: ast.TokenIndex) Error
2272 }2274 }
2273}2275}
22742276
2277/// start_token is first container doc comment token.
2278fn renderContainerDocComments(ais: *Ais, tree: ast.Tree, start_token: ast.TokenIndex) Error!void {
2279 const token_tags = tree.tokens.items(.tag);
2280 var tok = start_token;
2281 while (token_tags[tok] == .container_doc_comment) : (tok += 1) {
2282 try renderToken(ais, tree, tok, .newline);
2283 }
2284}
2285
2275fn tokenSliceForRender(tree: ast.Tree, token_index: ast.TokenIndex) []const u8 {2286fn tokenSliceForRender(tree: ast.Tree, token_index: ast.TokenIndex) []const u8 {
2276 var ret = tree.tokenSlice(token_index);2287 var ret = tree.tokenSlice(token_index);
2277 if (tree.tokens.items(.tag)[token_index] == .multiline_string_literal_line) {2288 if (tree.tokens.items(.tag)[token_index] == .multiline_string_literal_line) {
src/astgen.zig+18-18
...@@ -215,17 +215,17 @@ fn lvalExpr(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zir.I...@@ -215,17 +215,17 @@ fn lvalExpr(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!*zir.I
215 .anyframe_literal,215 .anyframe_literal,
216 .error_set_decl,216 .error_set_decl,
217 .container_decl,217 .container_decl,
218 .container_decl_comma,218 .container_decl_trailing,
219 .container_decl_two,219 .container_decl_two,
220 .container_decl_two_comma,220 .container_decl_two_trailing,
221 .container_decl_arg,221 .container_decl_arg,
222 .container_decl_arg_comma,222 .container_decl_arg_trailing,
223 .tagged_union,223 .tagged_union,
224 .tagged_union_comma,224 .tagged_union_trailing,
225 .tagged_union_two,225 .tagged_union_two,
226 .tagged_union_two_comma,226 .tagged_union_two_trailing,
227 .tagged_union_enum_tag,227 .tagged_union_enum_tag,
228 .tagged_union_enum_tag_comma,228 .tagged_union_enum_tag_trailing,
229 .@"comptime",229 .@"comptime",
230 .@"nosuspend",230 .@"nosuspend",
231 .error_value,231 .error_value,
...@@ -577,25 +577,25 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -577,25 +577,25 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
577 .ptr_type_bit_range => return ptrType(mod, scope, rl, tree.ptrTypeBitRange(node)),577 .ptr_type_bit_range => return ptrType(mod, scope, rl, tree.ptrTypeBitRange(node)),
578578
579 .container_decl,579 .container_decl,
580 .container_decl_comma,580 .container_decl_trailing,
581 => return containerDecl(mod, scope, rl, tree.containerDecl(node)),581 => return containerDecl(mod, scope, rl, tree.containerDecl(node)),
582 .container_decl_two, .container_decl_two_comma => {582 .container_decl_two, .container_decl_two_trailing => {
583 var buffer: [2]ast.Node.Index = undefined;583 var buffer: [2]ast.Node.Index = undefined;
584 return containerDecl(mod, scope, rl, tree.containerDeclTwo(&buffer, node));584 return containerDecl(mod, scope, rl, tree.containerDeclTwo(&buffer, node));
585 },585 },
586 .container_decl_arg,586 .container_decl_arg,
587 .container_decl_arg_comma,587 .container_decl_arg_trailing,
588 => return containerDecl(mod, scope, rl, tree.containerDeclArg(node)),588 => return containerDecl(mod, scope, rl, tree.containerDeclArg(node)),
589589
590 .tagged_union,590 .tagged_union,
591 .tagged_union_comma,591 .tagged_union_trailing,
592 => return containerDecl(mod, scope, rl, tree.taggedUnion(node)),592 => return containerDecl(mod, scope, rl, tree.taggedUnion(node)),
593 .tagged_union_two, .tagged_union_two_comma => {593 .tagged_union_two, .tagged_union_two_trailing => {
594 var buffer: [2]ast.Node.Index = undefined;594 var buffer: [2]ast.Node.Index = undefined;
595 return containerDecl(mod, scope, rl, tree.taggedUnionTwo(&buffer, node));595 return containerDecl(mod, scope, rl, tree.taggedUnionTwo(&buffer, node));
596 },596 },
597 .tagged_union_enum_tag,597 .tagged_union_enum_tag,
598 .tagged_union_enum_tag_comma,598 .tagged_union_enum_tag_trailing,
599 => return containerDecl(mod, scope, rl, tree.taggedUnionEnumTag(node)),599 => return containerDecl(mod, scope, rl, tree.taggedUnionEnumTag(node)),
600600
601 .@"break" => return breakExpr(mod, scope, rl, node),601 .@"break" => return breakExpr(mod, scope, rl, node),
...@@ -3715,17 +3715,17 @@ fn nodeMayNeedMemoryLocation(scope: *Scope, start_node: ast.Node.Index) bool {...@@ -3715,17 +3715,17 @@ fn nodeMayNeedMemoryLocation(scope: *Scope, start_node: ast.Node.Index) bool {
3715 .identifier,3715 .identifier,
3716 .error_set_decl,3716 .error_set_decl,
3717 .container_decl,3717 .container_decl,
3718 .container_decl_comma,3718 .container_decl_trailing,
3719 .container_decl_two,3719 .container_decl_two,
3720 .container_decl_two_comma,3720 .container_decl_two_trailing,
3721 .container_decl_arg,3721 .container_decl_arg,
3722 .container_decl_arg_comma,3722 .container_decl_arg_trailing,
3723 .tagged_union,3723 .tagged_union,
3724 .tagged_union_comma,3724 .tagged_union_trailing,
3725 .tagged_union_two,3725 .tagged_union_two,
3726 .tagged_union_two_comma,3726 .tagged_union_two_trailing,
3727 .tagged_union_enum_tag,3727 .tagged_union_enum_tag,
3728 .tagged_union_enum_tag_comma,3728 .tagged_union_enum_tag_trailing,
3729 .@"asm",3729 .@"asm",
3730 .asm_simple,3730 .asm_simple,
3731 .add,3731 .add,