authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-09 14:41:50-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-09 14:41:50-07:00
logbcafc51e58fcd6a4b12e43a780c0e9eef43a8d28
tree594d7d98319c6752e61d553cb20695112757be35
parentb1d8a0a5a6680383bad09b904dba231204a430bd

zig fmt: fn protos and anytype


3 files changed, 115 insertions(+), 48 deletions(-)

lib/std/zig/ast.zig+86-12
......@@ -250,6 +250,7 @@ pub const Tree = struct {
250250 .FnProto,
251251 .ArrayType,
252252 .ArrayTypeSentinel,
253 .ErrorValue,
253254 => return main_tokens[n],
254255
255256 .ArrayInitDot,
......@@ -424,12 +425,18 @@ pub const Tree = struct {
424425 return main_tokens[n] - 1;
425426 },
426427
427 .WhileSimple => unreachable, // TODO
428 .WhileCont => unreachable, // TODO
429 .While => unreachable, // TODO
430 .ForSimple => unreachable, // TODO
431 .For => unreachable, // TODO
432 .ErrorValue => unreachable, // TODO
428 .WhileSimple,
429 .WhileCont,
430 .While,
431 .ForSimple,
432 .For,
433 => {
434 const main_token = main_tokens[n];
435 return switch (token_tags[main_token - 1]) {
436 .Keyword_inline => main_token - 1,
437 else => main_token,
438 };
439 },
433440 };
434441 }
435442
......@@ -437,6 +444,7 @@ pub const Tree = struct {
437444 const tags = tree.nodes.items(.tag);
438445 const datas = tree.nodes.items(.data);
439446 const main_tokens = tree.nodes.items(.main_token);
447 const token_starts = tree.tokens.items(.start);
440448 var n = node;
441449 var end_offset: TokenIndex = 0;
442450 while (true) switch (tags[n]) {
......@@ -521,6 +529,8 @@ pub const Tree = struct {
521529 .AsmSimple,
522530 .AsmOutput,
523531 .AsmInput,
532 .FnProtoSimple,
533 .FnProtoMulti,
524534 => return datas[n].rhs + end_offset,
525535
526536 .AnyType,
......@@ -759,6 +769,74 @@ pub const Tree = struct {
759769 return main_tokens[n] + end_offset;
760770 }
761771 },
772 .FnProtoOne => {
773 const extra = tree.extraData(datas[n].lhs, Node.FnProtoOne);
774 // linksection, callconv, align can appear in any order, so we
775 // find the last one here.
776 var max_node: Node.Index = datas[n].rhs;
777 var max_start = token_starts[main_tokens[max_node]];
778 var max_offset: TokenIndex = 0;
779 if (extra.align_expr != 0) {
780 const start = token_starts[main_tokens[extra.align_expr]];
781 if (start > max_start) {
782 max_node = extra.align_expr;
783 max_start = start;
784 max_offset = 1; // for the rparen
785 }
786 }
787 if (extra.section_expr != 0) {
788 const start = token_starts[main_tokens[extra.section_expr]];
789 if (start > max_start) {
790 max_node = extra.section_expr;
791 max_start = start;
792 max_offset = 1; // for the rparen
793 }
794 }
795 if (extra.callconv_expr != 0) {
796 const start = token_starts[main_tokens[extra.callconv_expr]];
797 if (start > max_start) {
798 max_node = extra.callconv_expr;
799 max_start = start;
800 max_offset = 1; // for the rparen
801 }
802 }
803 n = max_node;
804 end_offset += max_offset;
805 },
806 .FnProto => {
807 const extra = tree.extraData(datas[n].lhs, Node.FnProto);
808 // linksection, callconv, align can appear in any order, so we
809 // find the last one here.
810 var max_node: Node.Index = datas[n].rhs;
811 var max_start = token_starts[main_tokens[max_node]];
812 var max_offset: TokenIndex = 0;
813 if (extra.align_expr != 0) {
814 const start = token_starts[main_tokens[extra.align_expr]];
815 if (start > max_start) {
816 max_node = extra.align_expr;
817 max_start = start;
818 max_offset = 1; // for the rparen
819 }
820 }
821 if (extra.section_expr != 0) {
822 const start = token_starts[main_tokens[extra.section_expr]];
823 if (start > max_start) {
824 max_node = extra.section_expr;
825 max_start = start;
826 max_offset = 1; // for the rparen
827 }
828 }
829 if (extra.callconv_expr != 0) {
830 const start = token_starts[main_tokens[extra.callconv_expr]];
831 if (start > max_start) {
832 max_node = extra.callconv_expr;
833 max_start = start;
834 max_offset = 1; // for the rparen
835 }
836 }
837 n = max_node;
838 end_offset += max_offset;
839 },
762840
763841 // These are not supported by lastToken() because implementation would
764842 // require recursion due to the optional comma followed by rbrace.
......@@ -782,10 +860,6 @@ pub const Tree = struct {
782860 .While => unreachable, // TODO
783861 .ForSimple => unreachable, // TODO
784862 .For => unreachable, // TODO
785 .FnProtoSimple => unreachable, // TODO
786 .FnProtoMulti => unreachable, // TODO
787 .FnProtoOne => unreachable, // TODO
788 .FnProto => unreachable, // TODO
789863 .ErrorValue => unreachable, // TODO
790864 };
791865 }
......@@ -2217,11 +2291,11 @@ pub const Node = struct {
22172291 /// `fn(a: b, c: d) rhs`. `sub_range_list[lhs]`.
22182292 /// anytype and ... parameters are omitted from the AST tree.
22192293 FnProtoMulti,
2220 /// `fn(a: b) rhs linksection(e) callconv(f)`. lhs is index into extra_data.
2294 /// `fn(a: b) rhs linksection(e) callconv(f)`. `FnProtoOne[lhs]`.
22212295 /// zero or one parameters.
22222296 /// anytype and ... parameters are omitted from the AST tree.
22232297 FnProtoOne,
2224 /// `fn(a: b, c: d) rhs linksection(e) callconv(f)`. `fn_proto_list[lhs]`.
2298 /// `fn(a: b, c: d) rhs linksection(e) callconv(f)`. `FnProto[lhs]`.
22252299 /// anytype and ... parameters are omitted from the AST tree.
22262300 FnProto,
22272301 /// lhs is the FnProto, rhs is the function body block.
lib/std/zig/parser_test.zig+21-21
......@@ -337,15 +337,15 @@ test "zig fmt: asm expression with comptime content" {
337337 );
338338}
339339
340//test "zig fmt: anytype struct field" {
341// try testCanonical(
342// \\pub const Pointer = struct {
343// \\ sentinel: anytype,
344// \\};
345// \\
346// );
347//}
348//
340test "zig fmt: anytype struct field" {
341 try testCanonical(
342 \\pub const Pointer = struct {
343 \\ sentinel: anytype,
344 \\};
345 \\
346 );
347}
348
349349//test "zig fmt: sentinel-terminated array type" {
350350// try testCanonical(
351351// \\pub fn cStrToPrefixedFileW(s: [*:0]const u8) ![PATH_MAX_WIDE:0]u16 {
......@@ -691,18 +691,18 @@ test "zig fmt: block in slice expression" {
691691 );
692692}
693693
694//test "zig fmt: async function" {
695// try testCanonical(
696// \\pub const Server = struct {
697// \\ handleRequestFn: fn (*Server, *const std.net.Address, File) callconv(.Async) void,
698// \\};
699// \\test "hi" {
700// \\ var ptr = @ptrCast(fn (i32) callconv(.Async) void, other);
701// \\}
702// \\
703// );
704//}
705//
694test "zig fmt: async function" {
695 try testCanonical(
696 \\pub const Server = struct {
697 \\ handleRequestFn: fn (*Server, *const std.net.Address, File) callconv(.Async) void,
698 \\};
699 \\test "hi" {
700 \\ var ptr = @ptrCast(fn (i32) callconv(.Async) void, other);
701 \\}
702 \\
703 );
704}
705
706706//test "zig fmt: whitespace fixes" {
707707// try testTransform("test \"\" {\r\n\tconst hi = x;\r\n}\n// zig fmt: off\ntest \"\"{\r\n\tconst a = b;}\r\n",
708708// \\test "" {
lib/std/zig/render.zig+8-15
......@@ -22,7 +22,7 @@ pub const Error = error{
2222const Writer = std.ArrayList(u8).Writer;
2323const Ais = std.io.AutoIndentingStream(Writer);
2424
25/// `gpa` is used both for allocating the resulting formatted source code, but also
25/// `gpa` is used for allocating the resulting formatted source code, as well as
2626/// for allocating extra stack memory if needed, because this function utilizes recursion.
2727/// Note: that's not actually true yet, see https://github.com/ziglang/zig/issues/1006.
2828/// Caller owns the returned slice of bytes, allocated with `gpa`.
......@@ -191,17 +191,8 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
191191
192192 .ErrorValue => unreachable, // TODO
193193
194 .AnyType => unreachable, // TODO
195 //.AnyType => {
196 // const any_type = base.castTag(.AnyType).?;
197 // if (mem.eql(u8, tree.tokenSlice(any_type.token), "var")) {
198 // // TODO remove in next release cycle
199 // try ais.writer().writeAll("anytype");
200 // if (space == .Comma) try ais.writer().writeAll(",\n");
201 // return;
202 // }
203 // return renderToken(ais, tree, any_type.token, space);
204 //},
194 .AnyType => return renderToken(ais, tree, main_tokens[node], space),
195
205196 .BlockTwo,
206197 .BlockTwoSemicolon,
207198 => {
......@@ -1412,9 +1403,11 @@ fn renderFnProto(ais: *Ais, tree: ast.Tree, fn_proto: ast.full.FnProto, space: S
14121403 try renderToken(ais, tree, last_param_token, .Space); // ,
14131404 last_param_token += 1;
14141405 },
1415 else => unreachable,
1406 else => {}, // Parameter type without a name.
14161407 }
1417 if (token_tags[last_param_token] == .Identifier) {
1408 if (token_tags[last_param_token] == .Identifier and
1409 token_tags[last_param_token + 1] == .Colon)
1410 {
14181411 try renderToken(ais, tree, last_param_token, .None); // name
14191412 last_param_token += 1;
14201413 try renderToken(ais, tree, last_param_token, .Space); // :
......@@ -1427,7 +1420,7 @@ fn renderFnProto(ais: *Ais, tree: ast.Tree, fn_proto: ast.full.FnProto, space: S
14271420 const param = fn_proto.ast.params[param_i];
14281421 param_i += 1;
14291422 try renderExpression(ais, tree, param, .None);
1430 last_param_token = tree.lastToken(param) + 1;
1423 last_param_token = tree.lastToken(param);
14311424 }
14321425 } else {
14331426 // One param per line.