| author | |
| committer | |
| log | 928f6f48a62b4b356a0031ba54e247dbbcde4256 |
| tree | 7e9878fd8f8e1f9c65afbce8013240e79e0b31e7 |
| parent | 3110a73486223aba9152946fa0184536fc3c4b76 |
4 files changed, 117 insertions(+), 48 deletions(-)
lib/std/zig/ast.zig+36-7| ... | ... | @@ -255,6 +255,7 @@ pub const Tree = struct { |
| 255 | 255 | => return main_tokens[n] - end_offset, |
| 256 | 256 | |
| 257 | 257 | .ArrayInitDot, |
| 258 | .ArrayInitDotComma, | |
| 258 | 259 | .ArrayInitDotTwo, |
| 259 | 260 | .ArrayInitDotTwoComma, |
| 260 | 261 | .StructInitDot, |
| ... | ... | @@ -311,7 +312,9 @@ pub const Tree = struct { |
| 311 | 312 | .Deref, |
| 312 | 313 | .ArrayAccess, |
| 313 | 314 | .ArrayInitOne, |
| 315 | .ArrayInitOneComma, | |
| 314 | 316 | .ArrayInit, |
| 317 | .ArrayInitComma, | |
| 315 | 318 | .StructInitOne, |
| 316 | 319 | .StructInit, |
| 317 | 320 | .CallOne, |
| ... | ... | @@ -604,6 +607,13 @@ pub const Tree = struct { |
| 604 | 607 | const extra = tree.extraData(datas[n].rhs, Node.Asm); |
| 605 | 608 | return extra.rparen + end_offset; |
| 606 | 609 | }, |
| 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, | |
| 607 | 617 | .ContainerDeclArgComma, |
| 608 | 618 | .SwitchComma, |
| 609 | 619 | => { |
| ... | ... | @@ -612,6 +622,7 @@ pub const Tree = struct { |
| 612 | 622 | end_offset += 2; // for the comma + rbrace |
| 613 | 623 | n = tree.extra_data[members.end - 1]; // last parameter |
| 614 | 624 | }, |
| 625 | .ArrayInitDot, | |
| 615 | 626 | .Block, |
| 616 | 627 | .ContainerDecl, |
| 617 | 628 | .TaggedUnion, |
| ... | ... | @@ -621,6 +632,7 @@ pub const Tree = struct { |
| 621 | 632 | end_offset += 1; // for the rbrace |
| 622 | 633 | n = tree.extra_data[datas[n].rhs - 1]; // last statement |
| 623 | 634 | }, |
| 635 | .ArrayInitDotComma, | |
| 624 | 636 | .BlockSemicolon, |
| 625 | 637 | .ContainerDeclComma, |
| 626 | 638 | .TaggedUnionComma, |
| ... | ... | @@ -772,7 +784,16 @@ pub const Tree = struct { |
| 772 | 784 | } |
| 773 | 785 | }, |
| 774 | 786 | |
| 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 | => { | |
| 776 | 797 | end_offset += 2; // ellipsis2 + rbracket, or comma + rparen |
| 777 | 798 | n = datas[n].rhs; |
| 778 | 799 | assert(n != 0); |
| ... | ... | @@ -912,9 +933,6 @@ pub const Tree = struct { |
| 912 | 933 | // require recursion due to the optional comma followed by rbrace. |
| 913 | 934 | // TODO follow the pattern set by StructInitDotTwoComma which will allow |
| 914 | 935 | // lastToken to work for all of these. |
| 915 | .ArrayInit => unreachable, // TODO | |
| 916 | .ArrayInitOne => unreachable, // TODO | |
| 917 | .ArrayInitDot => unreachable, // TODO | |
| 918 | 936 | .StructInit => unreachable, // TODO |
| 919 | 937 | .StructInitOne => unreachable, // TODO |
| 920 | 938 | .StructInitDot => unreachable, // TODO |
| ... | ... | @@ -1151,7 +1169,8 @@ pub const Tree = struct { |
| 1151 | 1169 | } |
| 1152 | 1170 | |
| 1153 | 1171 | 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); | |
| 1155 | 1174 | const data = tree.nodes.items(.data)[node]; |
| 1156 | 1175 | buffer[0] = data.rhs; |
| 1157 | 1176 | const elements = if (data.rhs == 0) buffer[0..0] else buffer[0..1]; |
| ... | ... | @@ -1185,7 +1204,8 @@ pub const Tree = struct { |
| 1185 | 1204 | } |
| 1186 | 1205 | |
| 1187 | 1206 | 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); | |
| 1189 | 1209 | const data = tree.nodes.items(.data)[node]; |
| 1190 | 1210 | return .{ |
| 1191 | 1211 | .ast = .{ |
| ... | ... | @@ -1197,7 +1217,8 @@ pub const Tree = struct { |
| 1197 | 1217 | } |
| 1198 | 1218 | |
| 1199 | 1219 | 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); | |
| 1201 | 1222 | const data = tree.nodes.items(.data)[node]; |
| 1202 | 1223 | const elem_range = tree.extraData(data.rhs, Node.SubRange); |
| 1203 | 1224 | return .{ |
| ... | ... | @@ -2436,6 +2457,8 @@ pub const Node = struct { |
| 2436 | 2457 | ArrayAccess, |
| 2437 | 2458 | /// `lhs{rhs}`. rhs can be omitted. |
| 2438 | 2459 | ArrayInitOne, |
| 2460 | /// `lhs{rhs,}`. rhs can *not* be omitted | |
| 2461 | ArrayInitOneComma, | |
| 2439 | 2462 | /// `.{lhs, rhs}`. lhs and rhs can be omitted. |
| 2440 | 2463 | ArrayInitDotTwo, |
| 2441 | 2464 | /// Same as `ArrayInitDotTwo` except there is known to be a trailing comma |
| ... | ... | @@ -2443,8 +2466,14 @@ pub const Node = struct { |
| 2443 | 2466 | ArrayInitDotTwoComma, |
| 2444 | 2467 | /// `.{a, b}`. `sub_list[lhs..rhs]`. |
| 2445 | 2468 | ArrayInitDot, |
| 2469 | /// Same as `ArrayInitDot` except there is known to be a trailing comma | |
| 2470 | /// before the final rbrace. | |
| 2471 | ArrayInitDotComma, | |
| 2446 | 2472 | /// `lhs{a, b}`. `sub_range_list[rhs]`. lhs can be omitted which means `.{a, b}`. |
| 2447 | 2473 | ArrayInit, |
| 2474 | /// Same as `ArrayInit` except there is known to be a trailing comma | |
| 2475 | /// before the final rbrace. | |
| 2476 | ArrayInitComma, | |
| 2448 | 2477 | /// `lhs{.a = rhs}`. rhs can be omitted making it empty. |
| 2449 | 2478 | /// main_token is the lbrace. |
| 2450 | 2479 | StructInitOne, |
lib/std/zig/parse.zig+17-7| ... | ... | @@ -2205,9 +2205,10 @@ const Parser = struct { |
| 2205 | 2205 | } |
| 2206 | 2206 | |
| 2207 | 2207 | const elem_init = try p.expectExpr(); |
| 2208 | const comma_one = p.eatToken(.Comma); | |
| 2208 | 2209 | if (p.eatToken(.RBrace)) |_| { |
| 2209 | 2210 | return p.addNode(.{ |
| 2210 | .tag = .ArrayInitOne, | |
| 2211 | .tag = if (comma_one != null) .ArrayInitOneComma else .ArrayInitOne, | |
| 2211 | 2212 | .main_token = lbrace, |
| 2212 | 2213 | .data = .{ |
| 2213 | 2214 | .lhs = lhs, |
| ... | ... | @@ -2215,21 +2216,30 @@ const Parser = struct { |
| 2215 | 2216 | }, |
| 2216 | 2217 | }); |
| 2217 | 2218 | } |
| 2219 | if (comma_one == null) { | |
| 2220 | try p.warn(.{ | |
| 2221 | .ExpectedToken = .{ .token = p.tok_i, .expected_id = .Comma }, | |
| 2222 | }); | |
| 2223 | } | |
| 2218 | 2224 | |
| 2219 | 2225 | var init_list = std.ArrayList(Node.Index).init(p.gpa); |
| 2220 | 2226 | defer init_list.deinit(); |
| 2221 | 2227 | |
| 2222 | 2228 | try init_list.append(elem_init); |
| 2223 | 2229 | |
| 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()) { | |
| 2227 | 2233 | try init_list.append(next); |
| 2234 | if (p.eatToken(.Comma) == null) { | |
| 2235 | trailing_comma = false; | |
| 2236 | break; | |
| 2237 | } | |
| 2228 | 2238 | } |
| 2229 | 2239 | _ = try p.expectToken(.RBrace); |
| 2230 | 2240 | const span = try p.listToSpan(init_list.items); |
| 2231 | 2241 | return p.addNode(.{ |
| 2232 | .tag = .ArrayInit, | |
| 2242 | .tag = if (trailing_comma) .ArrayInitComma else .ArrayInit, | |
| 2233 | 2243 | .main_token = lbrace, |
| 2234 | 2244 | .data = .{ |
| 2235 | 2245 | .lhs = lhs, |
| ... | ... | @@ -2805,7 +2815,7 @@ const Parser = struct { |
| 2805 | 2815 | const comma_two = p.eatToken(.Comma); |
| 2806 | 2816 | if (p.eatToken(.RBrace)) |_| { |
| 2807 | 2817 | return p.addNode(.{ |
| 2808 | .tag = if (comma_one != null) .ArrayInitDotTwoComma else .ArrayInitDotTwo, | |
| 2818 | .tag = if (comma_two != null) .ArrayInitDotTwoComma else .ArrayInitDotTwo, | |
| 2809 | 2819 | .main_token = lbrace, |
| 2810 | 2820 | .data = .{ |
| 2811 | 2821 | .lhs = elem_init_one, |
| ... | ... | @@ -2855,7 +2865,7 @@ const Parser = struct { |
| 2855 | 2865 | } |
| 2856 | 2866 | const span = try p.listToSpan(init_list.items); |
| 2857 | 2867 | return p.addNode(.{ |
| 2858 | .tag = .ArrayInitDot, | |
| 2868 | .tag = if (p.token_tags[p.tok_i - 2] == .Comma) .ArrayInitDotComma else .ArrayInitDot, | |
| 2859 | 2869 | .main_token = lbrace, |
| 2860 | 2870 | .data = .{ |
| 2861 | 2871 | .lhs = span.start, |
lib/std/zig/parser_test.zig+57-31| ... | ... | @@ -568,109 +568,135 @@ test "zig fmt: struct literal 3 element comma" { |
| 568 | 568 | |
| 569 | 569 | test "zig fmt: anon list literal 1 element" { |
| 570 | 570 | try testCanonical( |
| 571 | \\const x = .{a}; | |
| 571 | \\test { | |
| 572 | \\ const x = .{a}; | |
| 573 | \\} | |
| 572 | 574 | \\ |
| 573 | 575 | ); |
| 574 | 576 | } |
| 575 | 577 | |
| 576 | 578 | test "zig fmt: anon list literal 1 element comma" { |
| 577 | 579 | try testCanonical( |
| 578 | \\const x = .{ | |
| 579 | \\ a, | |
| 580 | \\}; | |
| 580 | \\test { | |
| 581 | \\ const x = .{ | |
| 582 | \\ a, | |
| 583 | \\ }; | |
| 584 | \\} | |
| 581 | 585 | \\ |
| 582 | 586 | ); |
| 583 | 587 | } |
| 584 | 588 | |
| 585 | 589 | test "zig fmt: anon list literal 2 element" { |
| 586 | 590 | try testCanonical( |
| 587 | \\const x = .{ a, b }; | |
| 591 | \\test { | |
| 592 | \\ const x = .{ a, b }; | |
| 593 | \\} | |
| 588 | 594 | \\ |
| 589 | 595 | ); |
| 590 | 596 | } |
| 591 | 597 | |
| 592 | 598 | test "zig fmt: anon list literal 2 element comma" { |
| 593 | 599 | try testCanonical( |
| 594 | \\const x = .{ | |
| 595 | \\ a, | |
| 596 | \\ b, | |
| 597 | \\}; | |
| 600 | \\test { | |
| 601 | \\ const x = .{ | |
| 602 | \\ a, | |
| 603 | \\ b, | |
| 604 | \\ }; | |
| 605 | \\} | |
| 598 | 606 | \\ |
| 599 | 607 | ); |
| 600 | 608 | } |
| 601 | 609 | |
| 602 | 610 | test "zig fmt: anon list literal 3 element" { |
| 603 | 611 | try testCanonical( |
| 604 | \\const x = .{ a, b, c }; | |
| 612 | \\test { | |
| 613 | \\ const x = .{ a, b, c }; | |
| 614 | \\} | |
| 605 | 615 | \\ |
| 606 | 616 | ); |
| 607 | 617 | } |
| 608 | 618 | |
| 609 | 619 | test "zig fmt: anon list literal 3 element comma" { |
| 610 | 620 | 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 | \\} | |
| 616 | 628 | \\ |
| 617 | 629 | ); |
| 618 | 630 | } |
| 619 | 631 | |
| 620 | 632 | test "zig fmt: array literal 1 element" { |
| 621 | 633 | try testCanonical( |
| 622 | \\const x = [_]u32{a}; | |
| 634 | \\test { | |
| 635 | \\ const x = [_]u32{a}; | |
| 636 | \\} | |
| 623 | 637 | \\ |
| 624 | 638 | ); |
| 625 | 639 | } |
| 626 | 640 | |
| 627 | 641 | test "zig fmt: array literal 1 element comma" { |
| 628 | 642 | try testCanonical( |
| 629 | \\const x = [1]u32{ | |
| 630 | \\ a, | |
| 631 | \\}; | |
| 643 | \\test { | |
| 644 | \\ const x = [1]u32{ | |
| 645 | \\ a, | |
| 646 | \\ }; | |
| 647 | \\} | |
| 632 | 648 | \\ |
| 633 | 649 | ); |
| 634 | 650 | } |
| 635 | 651 | |
| 636 | 652 | test "zig fmt: array literal 2 element" { |
| 637 | 653 | try testCanonical( |
| 638 | \\const x = [_]u32{ a, b }; | |
| 654 | \\test { | |
| 655 | \\ const x = [_]u32{ a, b }; | |
| 656 | \\} | |
| 639 | 657 | \\ |
| 640 | 658 | ); |
| 641 | 659 | } |
| 642 | 660 | |
| 643 | 661 | test "zig fmt: array literal 2 element comma" { |
| 644 | 662 | 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 | \\} | |
| 649 | 669 | \\ |
| 650 | 670 | ); |
| 651 | 671 | } |
| 652 | 672 | |
| 653 | 673 | test "zig fmt: array literal 3 element" { |
| 654 | 674 | try testCanonical( |
| 655 | \\const x = [_]u32{ a, b, c }; | |
| 675 | \\test { | |
| 676 | \\ const x = [_]u32{ a, b, c }; | |
| 677 | \\} | |
| 656 | 678 | \\ |
| 657 | 679 | ); |
| 658 | 680 | } |
| 659 | 681 | |
| 660 | 682 | test "zig fmt: array literal 3 element comma" { |
| 661 | 683 | 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 | \\} | |
| 667 | 691 | \\ |
| 668 | 692 | ); |
| 669 | 693 | } |
| 670 | 694 | |
| 671 | 695 | test "zig fmt: sentinel array literal 1 element" { |
| 672 | 696 | try testCanonical( |
| 673 | \\const x = [_:9000]u32{a}; | |
| 697 | \\test { | |
| 698 | \\ const x = [_:9000]u32{a}; | |
| 699 | \\} | |
| 674 | 700 | \\ |
| 675 | 701 | ); |
| 676 | 702 | } |
lib/std/zig/render.zig+7-3| ... | ... | @@ -390,7 +390,7 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac |
| 390 | 390 | .PtrType => return renderPtrType(ais, tree, tree.ptrType(node), space), |
| 391 | 391 | .PtrTypeBitRange => return renderPtrType(ais, tree, tree.ptrTypeBitRange(node), space), |
| 392 | 392 | |
| 393 | .ArrayInitOne => { | |
| 393 | .ArrayInitOne, .ArrayInitOneComma => { | |
| 394 | 394 | var elements: [1]ast.Node.Index = undefined; |
| 395 | 395 | return renderArrayInit(ais, tree, tree.arrayInitOne(&elements, node), space); |
| 396 | 396 | }, |
| ... | ... | @@ -398,8 +398,12 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac |
| 398 | 398 | var elements: [2]ast.Node.Index = undefined; |
| 399 | 399 | return renderArrayInit(ais, tree, tree.arrayInitDotTwo(&elements, node), space); |
| 400 | 400 | }, |
| 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), | |
| 403 | 407 | |
| 404 | 408 | .StructInitOne => { |
| 405 | 409 | var fields: [1]ast.Node.Index = undefined; |