authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-10 18:17:50+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-10 11:53:53-08:00
log928f6f48a62b4b356a0031ba54e247dbbcde4256
tree7e9878fd8f8e1f9c65afbce8013240e79e0b31e7
parent3110a73486223aba9152946fa0184536fc3c4b76

zig fmt: implement Tree.lastToken() for array init


4 files changed, 117 insertions(+), 48 deletions(-)

lib/std/zig/ast.zig+36-7
......@@ -255,6 +255,7 @@ pub const Tree = struct {
255255 => return main_tokens[n] - end_offset,
256256
257257 .ArrayInitDot,
258 .ArrayInitDotComma,
258259 .ArrayInitDotTwo,
259260 .ArrayInitDotTwoComma,
260261 .StructInitDot,
......@@ -311,7 +312,9 @@ pub const Tree = struct {
311312 .Deref,
312313 .ArrayAccess,
313314 .ArrayInitOne,
315 .ArrayInitOneComma,
314316 .ArrayInit,
317 .ArrayInitComma,
315318 .StructInitOne,
316319 .StructInit,
317320 .CallOne,
......@@ -604,6 +607,13 @@ pub const Tree = struct {
604607 const extra = tree.extraData(datas[n].rhs, Node.Asm);
605608 return extra.rparen + end_offset;
606609 },
610 .ArrayInit => {
611 const elements = tree.extraData(datas[n].rhs, Node.SubRange);
612 assert(elements.end - elements.start > 0);
613 end_offset += 1; // for the rbrace
614 n = tree.extra_data[elements.end - 1]; // last element
615 },
616 .ArrayInitComma,
607617 .ContainerDeclArgComma,
608618 .SwitchComma,
609619 => {
......@@ -612,6 +622,7 @@ pub const Tree = struct {
612622 end_offset += 2; // for the comma + rbrace
613623 n = tree.extra_data[members.end - 1]; // last parameter
614624 },
625 .ArrayInitDot,
615626 .Block,
616627 .ContainerDecl,
617628 .TaggedUnion,
......@@ -621,6 +632,7 @@ pub const Tree = struct {
621632 end_offset += 1; // for the rbrace
622633 n = tree.extra_data[datas[n].rhs - 1]; // last statement
623634 },
635 .ArrayInitDotComma,
624636 .BlockSemicolon,
625637 .ContainerDeclComma,
626638 .TaggedUnionComma,
......@@ -772,7 +784,16 @@ pub const Tree = struct {
772784 }
773785 },
774786
775 .SliceOpen, .CallOneComma, .AsyncCallOneComma => {
787 .ArrayInitOne => {
788 end_offset += 1; // rbrace
789 n = datas[n].rhs;
790 assert(n != 0);
791 },
792 .SliceOpen,
793 .CallOneComma,
794 .AsyncCallOneComma,
795 .ArrayInitOneComma,
796 => {
776797 end_offset += 2; // ellipsis2 + rbracket, or comma + rparen
777798 n = datas[n].rhs;
778799 assert(n != 0);
......@@ -912,9 +933,6 @@ pub const Tree = struct {
912933 // require recursion due to the optional comma followed by rbrace.
913934 // TODO follow the pattern set by StructInitDotTwoComma which will allow
914935 // lastToken to work for all of these.
915 .ArrayInit => unreachable, // TODO
916 .ArrayInitOne => unreachable, // TODO
917 .ArrayInitDot => unreachable, // TODO
918936 .StructInit => unreachable, // TODO
919937 .StructInitOne => unreachable, // TODO
920938 .StructInitDot => unreachable, // TODO
......@@ -1151,7 +1169,8 @@ pub const Tree = struct {
11511169 }
11521170
11531171 pub fn arrayInitOne(tree: Tree, buffer: *[1]Node.Index, node: Node.Index) full.ArrayInit {
1154 assert(tree.nodes.items(.tag)[node] == .ArrayInitOne);
1172 assert(tree.nodes.items(.tag)[node] == .ArrayInitOne or
1173 tree.nodes.items(.tag)[node] == .ArrayInitOneComma);
11551174 const data = tree.nodes.items(.data)[node];
11561175 buffer[0] = data.rhs;
11571176 const elements = if (data.rhs == 0) buffer[0..0] else buffer[0..1];
......@@ -1185,7 +1204,8 @@ pub const Tree = struct {
11851204 }
11861205
11871206 pub fn arrayInitDot(tree: Tree, node: Node.Index) full.ArrayInit {
1188 assert(tree.nodes.items(.tag)[node] == .ArrayInitDot);
1207 assert(tree.nodes.items(.tag)[node] == .ArrayInitDot or
1208 tree.nodes.items(.tag)[node] == .ArrayInitDotComma);
11891209 const data = tree.nodes.items(.data)[node];
11901210 return .{
11911211 .ast = .{
......@@ -1197,7 +1217,8 @@ pub const Tree = struct {
11971217 }
11981218
11991219 pub fn arrayInit(tree: Tree, node: Node.Index) full.ArrayInit {
1200 assert(tree.nodes.items(.tag)[node] == .ArrayInit);
1220 assert(tree.nodes.items(.tag)[node] == .ArrayInit or
1221 tree.nodes.items(.tag)[node] == .ArrayInitComma);
12011222 const data = tree.nodes.items(.data)[node];
12021223 const elem_range = tree.extraData(data.rhs, Node.SubRange);
12031224 return .{
......@@ -2436,6 +2457,8 @@ pub const Node = struct {
24362457 ArrayAccess,
24372458 /// `lhs{rhs}`. rhs can be omitted.
24382459 ArrayInitOne,
2460 /// `lhs{rhs,}`. rhs can *not* be omitted
2461 ArrayInitOneComma,
24392462 /// `.{lhs, rhs}`. lhs and rhs can be omitted.
24402463 ArrayInitDotTwo,
24412464 /// Same as `ArrayInitDotTwo` except there is known to be a trailing comma
......@@ -2443,8 +2466,14 @@ pub const Node = struct {
24432466 ArrayInitDotTwoComma,
24442467 /// `.{a, b}`. `sub_list[lhs..rhs]`.
24452468 ArrayInitDot,
2469 /// Same as `ArrayInitDot` except there is known to be a trailing comma
2470 /// before the final rbrace.
2471 ArrayInitDotComma,
24462472 /// `lhs{a, b}`. `sub_range_list[rhs]`. lhs can be omitted which means `.{a, b}`.
24472473 ArrayInit,
2474 /// Same as `ArrayInit` except there is known to be a trailing comma
2475 /// before the final rbrace.
2476 ArrayInitComma,
24482477 /// `lhs{.a = rhs}`. rhs can be omitted making it empty.
24492478 /// main_token is the lbrace.
24502479 StructInitOne,
lib/std/zig/parse.zig+17-7
......@@ -2205,9 +2205,10 @@ const Parser = struct {
22052205 }
22062206
22072207 const elem_init = try p.expectExpr();
2208 const comma_one = p.eatToken(.Comma);
22082209 if (p.eatToken(.RBrace)) |_| {
22092210 return p.addNode(.{
2210 .tag = .ArrayInitOne,
2211 .tag = if (comma_one != null) .ArrayInitOneComma else .ArrayInitOne,
22112212 .main_token = lbrace,
22122213 .data = .{
22132214 .lhs = lhs,
......@@ -2215,21 +2216,30 @@ const Parser = struct {
22152216 },
22162217 });
22172218 }
2219 if (comma_one == null) {
2220 try p.warn(.{
2221 .ExpectedToken = .{ .token = p.tok_i, .expected_id = .Comma },
2222 });
2223 }
22182224
22192225 var init_list = std.ArrayList(Node.Index).init(p.gpa);
22202226 defer init_list.deinit();
22212227
22222228 try init_list.append(elem_init);
22232229
2224 while (p.eatToken(.Comma)) |_| {
2225 const next = try p.parseExpr();
2226 if (next == 0) break;
2230 var trailing_comma = true;
2231 var next = try p.parseExpr();
2232 while (next != 0) : (next = try p.parseExpr()) {
22272233 try init_list.append(next);
2234 if (p.eatToken(.Comma) == null) {
2235 trailing_comma = false;
2236 break;
2237 }
22282238 }
22292239 _ = try p.expectToken(.RBrace);
22302240 const span = try p.listToSpan(init_list.items);
22312241 return p.addNode(.{
2232 .tag = .ArrayInit,
2242 .tag = if (trailing_comma) .ArrayInitComma else .ArrayInit,
22332243 .main_token = lbrace,
22342244 .data = .{
22352245 .lhs = lhs,
......@@ -2805,7 +2815,7 @@ const Parser = struct {
28052815 const comma_two = p.eatToken(.Comma);
28062816 if (p.eatToken(.RBrace)) |_| {
28072817 return p.addNode(.{
2808 .tag = if (comma_one != null) .ArrayInitDotTwoComma else .ArrayInitDotTwo,
2818 .tag = if (comma_two != null) .ArrayInitDotTwoComma else .ArrayInitDotTwo,
28092819 .main_token = lbrace,
28102820 .data = .{
28112821 .lhs = elem_init_one,
......@@ -2855,7 +2865,7 @@ const Parser = struct {
28552865 }
28562866 const span = try p.listToSpan(init_list.items);
28572867 return p.addNode(.{
2858 .tag = .ArrayInitDot,
2868 .tag = if (p.token_tags[p.tok_i - 2] == .Comma) .ArrayInitDotComma else .ArrayInitDot,
28592869 .main_token = lbrace,
28602870 .data = .{
28612871 .lhs = span.start,
lib/std/zig/parser_test.zig+57-31
......@@ -568,109 +568,135 @@ test "zig fmt: struct literal 3 element comma" {
568568
569569test "zig fmt: anon list literal 1 element" {
570570 try testCanonical(
571 \\const x = .{a};
571 \\test {
572 \\ const x = .{a};
573 \\}
572574 \\
573575 );
574576}
575577
576578test "zig fmt: anon list literal 1 element comma" {
577579 try testCanonical(
578 \\const x = .{
579 \\ a,
580 \\};
580 \\test {
581 \\ const x = .{
582 \\ a,
583 \\ };
584 \\}
581585 \\
582586 );
583587}
584588
585589test "zig fmt: anon list literal 2 element" {
586590 try testCanonical(
587 \\const x = .{ a, b };
591 \\test {
592 \\ const x = .{ a, b };
593 \\}
588594 \\
589595 );
590596}
591597
592598test "zig fmt: anon list literal 2 element comma" {
593599 try testCanonical(
594 \\const x = .{
595 \\ a,
596 \\ b,
597 \\};
600 \\test {
601 \\ const x = .{
602 \\ a,
603 \\ b,
604 \\ };
605 \\}
598606 \\
599607 );
600608}
601609
602610test "zig fmt: anon list literal 3 element" {
603611 try testCanonical(
604 \\const x = .{ a, b, c };
612 \\test {
613 \\ const x = .{ a, b, c };
614 \\}
605615 \\
606616 );
607617}
608618
609619test "zig fmt: anon list literal 3 element comma" {
610620 try testCanonical(
611 \\const x = .{
612 \\ a,
613 \\ b,
614 \\ c,
615 \\};
621 \\test {
622 \\ const x = .{
623 \\ a,
624 \\ b,
625 \\ c,
626 \\ };
627 \\}
616628 \\
617629 );
618630}
619631
620632test "zig fmt: array literal 1 element" {
621633 try testCanonical(
622 \\const x = [_]u32{a};
634 \\test {
635 \\ const x = [_]u32{a};
636 \\}
623637 \\
624638 );
625639}
626640
627641test "zig fmt: array literal 1 element comma" {
628642 try testCanonical(
629 \\const x = [1]u32{
630 \\ a,
631 \\};
643 \\test {
644 \\ const x = [1]u32{
645 \\ a,
646 \\ };
647 \\}
632648 \\
633649 );
634650}
635651
636652test "zig fmt: array literal 2 element" {
637653 try testCanonical(
638 \\const x = [_]u32{ a, b };
654 \\test {
655 \\ const x = [_]u32{ a, b };
656 \\}
639657 \\
640658 );
641659}
642660
643661test "zig fmt: array literal 2 element comma" {
644662 try testCanonical(
645 \\const x = [2]u32{
646 \\ a,
647 \\ b,
648 \\};
663 \\test {
664 \\ const x = [2]u32{
665 \\ a,
666 \\ b,
667 \\ };
668 \\}
649669 \\
650670 );
651671}
652672
653673test "zig fmt: array literal 3 element" {
654674 try testCanonical(
655 \\const x = [_]u32{ a, b, c };
675 \\test {
676 \\ const x = [_]u32{ a, b, c };
677 \\}
656678 \\
657679 );
658680}
659681
660682test "zig fmt: array literal 3 element comma" {
661683 try testCanonical(
662 \\const x = [3]u32{
663 \\ a,
664 \\ b,
665 \\ c,
666 \\};
684 \\test {
685 \\ const x = [3]u32{
686 \\ a,
687 \\ b,
688 \\ c,
689 \\ };
690 \\}
667691 \\
668692 );
669693}
670694
671695test "zig fmt: sentinel array literal 1 element" {
672696 try testCanonical(
673 \\const x = [_:9000]u32{a};
697 \\test {
698 \\ const x = [_:9000]u32{a};
699 \\}
674700 \\
675701 );
676702}
lib/std/zig/render.zig+7-3
......@@ -390,7 +390,7 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
390390 .PtrType => return renderPtrType(ais, tree, tree.ptrType(node), space),
391391 .PtrTypeBitRange => return renderPtrType(ais, tree, tree.ptrTypeBitRange(node), space),
392392
393 .ArrayInitOne => {
393 .ArrayInitOne, .ArrayInitOneComma => {
394394 var elements: [1]ast.Node.Index = undefined;
395395 return renderArrayInit(ais, tree, tree.arrayInitOne(&elements, node), space);
396396 },
......@@ -398,8 +398,12 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
398398 var elements: [2]ast.Node.Index = undefined;
399399 return renderArrayInit(ais, tree, tree.arrayInitDotTwo(&elements, node), space);
400400 },
401 .ArrayInitDot => return renderArrayInit(ais, tree, tree.arrayInitDot(node), space),
402 .ArrayInit => return renderArrayInit(ais, tree, tree.arrayInit(node), space),
401 .ArrayInitDot,
402 .ArrayInitDotComma,
403 => return renderArrayInit(ais, tree, tree.arrayInitDot(node), space),
404 .ArrayInit,
405 .ArrayInitComma,
406 => return renderArrayInit(ais, tree, tree.arrayInit(node), space),
403407
404408 .StructInitOne => {
405409 var fields: [1]ast.Node.Index = undefined;