| author | |
| committer | |
| log | bcafc51e58fcd6a4b12e43a780c0e9eef43a8d28 |
| tree | 594d7d98319c6752e61d553cb20695112757be35 |
| parent | b1d8a0a5a6680383bad09b904dba231204a430bd |
3 files changed, 115 insertions(+), 48 deletions(-)
lib/std/zig/ast.zig+86-12| ... | @@ -250,6 +250,7 @@ pub const Tree = struct { | ... | @@ -250,6 +250,7 @@ pub const Tree = struct { |
| 250 | .FnProto, | 250 | .FnProto, |
| 251 | .ArrayType, | 251 | .ArrayType, |
| 252 | .ArrayTypeSentinel, | 252 | .ArrayTypeSentinel, |
| 253 | .ErrorValue, | ||
| 253 | => return main_tokens[n], | 254 | => return main_tokens[n], |
| 254 | 255 | ||
| 255 | .ArrayInitDot, | 256 | .ArrayInitDot, |
| ... | @@ -424,12 +425,18 @@ pub const Tree = struct { | ... | @@ -424,12 +425,18 @@ pub const Tree = struct { |
| 424 | return main_tokens[n] - 1; | 425 | return main_tokens[n] - 1; |
| 425 | }, | 426 | }, |
| 426 | 427 | ||
| 427 | .WhileSimple => unreachable, // TODO | 428 | .WhileSimple, |
| 428 | .WhileCont => unreachable, // TODO | 429 | .WhileCont, |
| 429 | .While => unreachable, // TODO | 430 | .While, |
| 430 | .ForSimple => unreachable, // TODO | 431 | .ForSimple, |
| 431 | .For => unreachable, // TODO | 432 | .For, |
| 432 | .ErrorValue => unreachable, // TODO | 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 | }, | ||
| 433 | }; | 440 | }; |
| 434 | } | 441 | } |
| 435 | 442 | ||
| ... | @@ -437,6 +444,7 @@ pub const Tree = struct { | ... | @@ -437,6 +444,7 @@ pub const Tree = struct { |
| 437 | const tags = tree.nodes.items(.tag); | 444 | const tags = tree.nodes.items(.tag); |
| 438 | const datas = tree.nodes.items(.data); | 445 | const datas = tree.nodes.items(.data); |
| 439 | const main_tokens = tree.nodes.items(.main_token); | 446 | const main_tokens = tree.nodes.items(.main_token); |
| 447 | const token_starts = tree.tokens.items(.start); | ||
| 440 | var n = node; | 448 | var n = node; |
| 441 | var end_offset: TokenIndex = 0; | 449 | var end_offset: TokenIndex = 0; |
| 442 | while (true) switch (tags[n]) { | 450 | while (true) switch (tags[n]) { |
| ... | @@ -521,6 +529,8 @@ pub const Tree = struct { | ... | @@ -521,6 +529,8 @@ pub const Tree = struct { |
| 521 | .AsmSimple, | 529 | .AsmSimple, |
| 522 | .AsmOutput, | 530 | .AsmOutput, |
| 523 | .AsmInput, | 531 | .AsmInput, |
| 532 | .FnProtoSimple, | ||
| 533 | .FnProtoMulti, | ||
| 524 | => return datas[n].rhs + end_offset, | 534 | => return datas[n].rhs + end_offset, |
| 525 | 535 | ||
| 526 | .AnyType, | 536 | .AnyType, |
| ... | @@ -759,6 +769,74 @@ pub const Tree = struct { | ... | @@ -759,6 +769,74 @@ pub const Tree = struct { |
| 759 | return main_tokens[n] + end_offset; | 769 | return main_tokens[n] + end_offset; |
| 760 | } | 770 | } |
| 761 | }, | 771 | }, |
| 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 | }, | ||
| 762 | 840 | ||
| 763 | // These are not supported by lastToken() because implementation would | 841 | // These are not supported by lastToken() because implementation would |
| 764 | // require recursion due to the optional comma followed by rbrace. | 842 | // require recursion due to the optional comma followed by rbrace. |
| ... | @@ -782,10 +860,6 @@ pub const Tree = struct { | ... | @@ -782,10 +860,6 @@ pub const Tree = struct { |
| 782 | .While => unreachable, // TODO | 860 | .While => unreachable, // TODO |
| 783 | .ForSimple => unreachable, // TODO | 861 | .ForSimple => unreachable, // TODO |
| 784 | .For => unreachable, // TODO | 862 | .For => unreachable, // TODO |
| 785 | .FnProtoSimple => unreachable, // TODO | ||
| 786 | .FnProtoMulti => unreachable, // TODO | ||
| 787 | .FnProtoOne => unreachable, // TODO | ||
| 788 | .FnProto => unreachable, // TODO | ||
| 789 | .ErrorValue => unreachable, // TODO | 863 | .ErrorValue => unreachable, // TODO |
| 790 | }; | 864 | }; |
| 791 | } | 865 | } |
| ... | @@ -2217,11 +2291,11 @@ pub const Node = struct { | ... | @@ -2217,11 +2291,11 @@ pub const Node = struct { |
| 2217 | /// `fn(a: b, c: d) rhs`. `sub_range_list[lhs]`. | 2291 | /// `fn(a: b, c: d) rhs`. `sub_range_list[lhs]`. |
| 2218 | /// anytype and ... parameters are omitted from the AST tree. | 2292 | /// anytype and ... parameters are omitted from the AST tree. |
| 2219 | FnProtoMulti, | 2293 | 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]`. |
| 2221 | /// zero or one parameters. | 2295 | /// zero or one parameters. |
| 2222 | /// anytype and ... parameters are omitted from the AST tree. | 2296 | /// anytype and ... parameters are omitted from the AST tree. |
| 2223 | FnProtoOne, | 2297 | 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]`. |
| 2225 | /// anytype and ... parameters are omitted from the AST tree. | 2299 | /// anytype and ... parameters are omitted from the AST tree. |
| 2226 | FnProto, | 2300 | FnProto, |
| 2227 | /// lhs is the FnProto, rhs is the function body block. | 2301 | /// 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" { | ... | @@ -337,15 +337,15 @@ test "zig fmt: asm expression with comptime content" { |
| 337 | ); | 337 | ); |
| 338 | } | 338 | } |
| 339 | 339 | ||
| 340 | //test "zig fmt: anytype struct field" { | 340 | test "zig fmt: anytype struct field" { |
| 341 | // try testCanonical( | 341 | try testCanonical( |
| 342 | // \\pub const Pointer = struct { | 342 | \\pub const Pointer = struct { |
| 343 | // \\ sentinel: anytype, | 343 | \\ sentinel: anytype, |
| 344 | // \\}; | 344 | \\}; |
| 345 | // \\ | 345 | \\ |
| 346 | // ); | 346 | ); |
| 347 | //} | 347 | } |
| 348 | // | 348 | |
| 349 | //test "zig fmt: sentinel-terminated array type" { | 349 | //test "zig fmt: sentinel-terminated array type" { |
| 350 | // try testCanonical( | 350 | // try testCanonical( |
| 351 | // \\pub fn cStrToPrefixedFileW(s: [*:0]const u8) ![PATH_MAX_WIDE:0]u16 { | 351 | // \\pub fn cStrToPrefixedFileW(s: [*:0]const u8) ![PATH_MAX_WIDE:0]u16 { |
| ... | @@ -691,18 +691,18 @@ test "zig fmt: block in slice expression" { | ... | @@ -691,18 +691,18 @@ test "zig fmt: block in slice expression" { |
| 691 | ); | 691 | ); |
| 692 | } | 692 | } |
| 693 | 693 | ||
| 694 | //test "zig fmt: async function" { | 694 | test "zig fmt: async function" { |
| 695 | // try testCanonical( | 695 | try testCanonical( |
| 696 | // \\pub const Server = struct { | 696 | \\pub const Server = struct { |
| 697 | // \\ handleRequestFn: fn (*Server, *const std.net.Address, File) callconv(.Async) void, | 697 | \\ handleRequestFn: fn (*Server, *const std.net.Address, File) callconv(.Async) void, |
| 698 | // \\}; | 698 | \\}; |
| 699 | // \\test "hi" { | 699 | \\test "hi" { |
| 700 | // \\ var ptr = @ptrCast(fn (i32) callconv(.Async) void, other); | 700 | \\ var ptr = @ptrCast(fn (i32) callconv(.Async) void, other); |
| 701 | // \\} | 701 | \\} |
| 702 | // \\ | 702 | \\ |
| 703 | // ); | 703 | ); |
| 704 | //} | 704 | } |
| 705 | // | 705 | |
| 706 | //test "zig fmt: whitespace fixes" { | 706 | //test "zig fmt: whitespace fixes" { |
| 707 | // try testTransform("test \"\" {\r\n\tconst hi = x;\r\n}\n// zig fmt: off\ntest \"\"{\r\n\tconst a = b;}\r\n", | 707 | // try testTransform("test \"\" {\r\n\tconst hi = x;\r\n}\n// zig fmt: off\ntest \"\"{\r\n\tconst a = b;}\r\n", |
| 708 | // \\test "" { | 708 | // \\test "" { |
lib/std/zig/render.zig+8-15| ... | @@ -22,7 +22,7 @@ pub const Error = error{ | ... | @@ -22,7 +22,7 @@ pub const Error = error{ |
| 22 | const Writer = std.ArrayList(u8).Writer; | 22 | const Writer = std.ArrayList(u8).Writer; |
| 23 | const Ais = std.io.AutoIndentingStream(Writer); | 23 | const Ais = std.io.AutoIndentingStream(Writer); |
| 24 | 24 | ||
| 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 |
| 26 | /// for allocating extra stack memory if needed, because this function utilizes recursion. | 26 | /// for allocating extra stack memory if needed, because this function utilizes recursion. |
| 27 | /// Note: that's not actually true yet, see https://github.com/ziglang/zig/issues/1006. | 27 | /// Note: that's not actually true yet, see https://github.com/ziglang/zig/issues/1006. |
| 28 | /// Caller owns the returned slice of bytes, allocated with `gpa`. | 28 | /// 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 | ... | @@ -191,17 +191,8 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac |
| 191 | 191 | ||
| 192 | .ErrorValue => unreachable, // TODO | 192 | .ErrorValue => unreachable, // TODO |
| 193 | 193 | ||
| 194 | .AnyType => unreachable, // TODO | 194 | .AnyType => return renderToken(ais, tree, main_tokens[node], space), |
| 195 | //.AnyType => { | 195 | |
| 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 | //}, | ||
| 205 | .BlockTwo, | 196 | .BlockTwo, |
| 206 | .BlockTwoSemicolon, | 197 | .BlockTwoSemicolon, |
| 207 | => { | 198 | => { |
| ... | @@ -1412,9 +1403,11 @@ fn renderFnProto(ais: *Ais, tree: ast.Tree, fn_proto: ast.full.FnProto, space: S | ... | @@ -1412,9 +1403,11 @@ fn renderFnProto(ais: *Ais, tree: ast.Tree, fn_proto: ast.full.FnProto, space: S |
| 1412 | try renderToken(ais, tree, last_param_token, .Space); // , | 1403 | try renderToken(ais, tree, last_param_token, .Space); // , |
| 1413 | last_param_token += 1; | 1404 | last_param_token += 1; |
| 1414 | }, | 1405 | }, |
| 1415 | else => unreachable, | 1406 | else => {}, // Parameter type without a name. |
| 1416 | } | 1407 | } |
| 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 | { | ||
| 1418 | try renderToken(ais, tree, last_param_token, .None); // name | 1411 | try renderToken(ais, tree, last_param_token, .None); // name |
| 1419 | last_param_token += 1; | 1412 | last_param_token += 1; |
| 1420 | try renderToken(ais, tree, last_param_token, .Space); // : | 1413 | 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 | ... | @@ -1427,7 +1420,7 @@ fn renderFnProto(ais: *Ais, tree: ast.Tree, fn_proto: ast.full.FnProto, space: S |
| 1427 | const param = fn_proto.ast.params[param_i]; | 1420 | const param = fn_proto.ast.params[param_i]; |
| 1428 | param_i += 1; | 1421 | param_i += 1; |
| 1429 | try renderExpression(ais, tree, param, .None); | 1422 | try renderExpression(ais, tree, param, .None); |
| 1430 | last_param_token = tree.lastToken(param) + 1; | 1423 | last_param_token = tree.lastToken(param); |
| 1431 | } | 1424 | } |
| 1432 | } else { | 1425 | } else { |
| 1433 | // One param per line. | 1426 | // One param per line. |