authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-22 17:39:41+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-22 17:39:41+02:00
log928790364ab486de240b8c14ef5ef312079377d7
treef87cda11eb9755e9c5a8477b7ccb1f58b886fb13
parent69d5a106da3fd339c7d1ed177b706aa30d8cb1a9
signaturelock-open Commit is signed but in an unrecognized format.

zig fmt: correct Node.firstToken for .fn_decl, add error for missing container


4 files changed, 99 insertions(+), 74 deletions(-)

lib/std/zig/ast.zig+24-1
......@@ -258,6 +258,11 @@ pub const Tree = struct {
258258 token_tags[parse_error.token].symbol(),
259259 });
260260 },
261 .expected_container => {
262 return stream.print("expected a struct, enum or union, found '{s}'", .{
263 token_tags[parse_error.token].symbol(),
264 });
265 },
261266 .extra_align_qualifier => {
262267 return stream.writeAll("extra align qualifier");
263268 },
......@@ -441,10 +446,27 @@ pub const Tree = struct {
441446 .call,
442447 .call_comma,
443448 .switch_range,
444 .fn_decl,
445449 .error_union,
446450 => n = datas[n].lhs,
447451
452 .fn_decl => {
453 var i = main_tokens[n]; // fn token
454 while (i > 0) {
455 i -= 1;
456 switch (token_tags[i]) {
457 .keyword_extern,
458 .keyword_export,
459 .keyword_pub,
460 .keyword_threadlocal,
461 .string_literal,
462 => continue,
463
464 else => return i + 1 - end_offset,
465 }
466 }
467 return i - end_offset;
468 },
469
448470 .async_call_one,
449471 .async_call_one_comma,
450472 .async_call,
......@@ -2338,6 +2360,7 @@ pub const Error = struct {
23382360 expected_var_decl,
23392361 expected_var_decl_or_fn,
23402362 expected_loop_payload,
2363 expected_container,
23412364 extra_align_qualifier,
23422365 extra_allowzero_qualifier,
23432366 extra_const_qualifier,
lib/std/zig/parse.zig+4-1
......@@ -3627,7 +3627,10 @@ const Parser = struct {
36273627 break :blk null_node;
36283628 }
36293629 },
3630 else => unreachable,
3630 else => {
3631 p.tok_i -= 1;
3632 return p.fail(.expected_container);
3633 },
36313634 };
36323635 _ = try p.expectToken(.l_brace);
36333636 const members = try p.parseContainerMembers();
lib/std/zig/parser_test.zig+70-71
......@@ -2399,20 +2399,20 @@ test "zig fmt: comments before test decl" {
23992399 );
24002400}
24012401
2402//test "zig fmt: preserve spacing" {
2403// try testCanonical(
2404// \\const std = @import("std");
2405// \\
2406// \\pub fn main() !void {
2407// \\ var stdout_file = std.io.getStdOut;
2408// \\ var stdout_file = std.io.getStdOut;
2409// \\
2410// \\ var stdout_file = std.io.getStdOut;
2411// \\ var stdout_file = std.io.getStdOut;
2412// \\}
2413// \\
2414// );
2415//}
2402test "zig fmt: preserve spacing" {
2403 try testCanonical(
2404 \\const std = @import("std");
2405 \\
2406 \\pub fn main() !void {
2407 \\ var stdout_file = std.io.getStdOut;
2408 \\ var stdout_file = std.io.getStdOut;
2409 \\
2410 \\ var stdout_file = std.io.getStdOut;
2411 \\ var stdout_file = std.io.getStdOut;
2412 \\}
2413 \\
2414 );
2415}
24162416
24172417//test "zig fmt: return types" {
24182418// try testCanonical(
......@@ -2431,27 +2431,27 @@ test "zig fmt: imports" {
24312431 );
24322432}
24332433
2434//test "zig fmt: global declarations" {
2435// try testCanonical(
2436// \\const a = b;
2437// \\pub const a = b;
2438// \\var a = b;
2439// \\pub var a = b;
2440// \\const a: i32 = b;
2441// \\pub const a: i32 = b;
2442// \\var a: i32 = b;
2443// \\pub var a: i32 = b;
2444// \\extern const a: i32 = b;
2445// \\pub extern const a: i32 = b;
2446// \\extern var a: i32 = b;
2447// \\pub extern var a: i32 = b;
2448// \\extern "a" const a: i32 = b;
2449// \\pub extern "a" const a: i32 = b;
2450// \\extern "a" var a: i32 = b;
2451// \\pub extern "a" var a: i32 = b;
2452// \\
2453// );
2454//}
2434test "zig fmt: global declarations" {
2435 try testCanonical(
2436 \\const a = b;
2437 \\pub const a = b;
2438 \\var a = b;
2439 \\pub var a = b;
2440 \\const a: i32 = b;
2441 \\pub const a: i32 = b;
2442 \\var a: i32 = b;
2443 \\pub var a: i32 = b;
2444 \\extern const a: i32 = b;
2445 \\pub extern const a: i32 = b;
2446 \\extern var a: i32 = b;
2447 \\pub extern var a: i32 = b;
2448 \\extern "a" const a: i32 = b;
2449 \\pub extern "a" const a: i32 = b;
2450 \\extern "a" var a: i32 = b;
2451 \\pub extern "a" var a: i32 = b;
2452 \\
2453 );
2454}
24552455
24562456test "zig fmt: extern declaration" {
24572457 try testCanonical(
......@@ -2680,23 +2680,23 @@ test "zig fmt: functions" {
26802680 );
26812681}
26822682
2683//test "zig fmt: multiline string" {
2684// try testCanonical(
2685// \\test "" {
2686// \\ const s1 =
2687// \\ \\one
2688// \\ \\two)
2689// \\ \\three
2690// \\ ;
2691// \\ const s3 = // hi
2692// \\ \\one
2693// \\ \\two)
2694// \\ \\three
2695// \\ ;
2696// \\}
2697// \\
2698// );
2699//}
2683test "zig fmt: multiline string" {
2684 try testCanonical(
2685 \\test "" {
2686 \\ const s1 =
2687 \\ \\one
2688 \\ \\two)
2689 \\ \\three
2690 \\ ;
2691 \\ const s3 = // hi
2692 \\ \\one
2693 \\ \\two)
2694 \\ \\three
2695 \\ ;
2696 \\}
2697 \\
2698 );
2699}
27002700
27012701test "zig fmt: values" {
27022702 try testCanonical(
......@@ -3578,15 +3578,14 @@ test "zig fmt: comment after empty comment" {
35783578// );
35793579//}
35803580
3581//test "zig fmt: extern without container keyword returns error" {
3582// try testError(
3583// \\const container = extern {};
3584// \\
3585// , &[_]Error{
3586// .expected_expr,
3587// .expected_var_decl_or_fn,
3588// });
3589//}
3581test "zig fmt: extern without container keyword returns error" {
3582 try testError(
3583 \\const container = extern {};
3584 \\
3585 , &[_]Error{
3586 .expected_container,
3587 });
3588}
35903589
35913590test "zig fmt: same line doc comment returns error" {
35923591 try testError(
......@@ -3706,16 +3705,16 @@ test "zig fmt: C var args" {
37063705// );
37073706//}
37083707
3709//test "zig fmt: Don't add extra newline after if" {
3710// try testCanonical(
3711// \\pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path: []const u8) !void {
3712// \\ if (cwd().symLink(existing_path, new_path, .{})) {
3713// \\ return;
3714// \\ }
3715// \\}
3716// \\
3717// );
3718//}
3708test "zig fmt: Don't add extra newline after if" {
3709 try testCanonical(
3710 \\pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path: []const u8) !void {
3711 \\ if (cwd().symLink(existing_path, new_path, .{})) {
3712 \\ return;
3713 \\ }
3714 \\}
3715 \\
3716 );
3717}
37193718
37203719//test "zig fmt: comments in ternary ifs" {
37213720// try testCanonical(
lib/std/zig/render.zig+1-1
......@@ -873,7 +873,7 @@ fn renderVarDecl(ais: *Ais, tree: ast.Tree, var_decl: ast.full.VarDecl) Error!vo
873873 try renderToken(ais, tree, extern_export_token, Space.space); // extern
874874
875875 if (var_decl.lib_name) |lib_name| {
876 try renderExpression(ais, tree, lib_name, Space.space); // "lib"
876 try renderToken(ais, tree, lib_name, Space.space); // "lib"
877877 }
878878 }
879879