diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-04-30 17:30:57 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-04-30 17:30:57 -0400 |
| commit | 7dc8d433abbf697f05eb1ad2003b6335f750557b (patch) | |
| tree | 22ddf5420466c8c3a5f6ae4e8b02490ce92c5404 /std | |
| parent | 37d3ef28351027a83249080d3238d61d9346f6db (diff) | |
| download | zig-7dc8d433abbf697f05eb1ad2003b6335f750557b.tar.gz zig-7dc8d433abbf697f05eb1ad2003b6335f750557b.zip | |
zig fmt: support labeled suspend
Diffstat (limited to 'std')
| -rw-r--r-- | std/zig/ast.zig | 2 | ||||
| -rw-r--r-- | std/zig/parser.zig | 21 | ||||
| -rw-r--r-- | std/zig/parser_test.zig | 11 |
3 files changed, 34 insertions, 0 deletions
diff --git a/std/zig/ast.zig b/std/zig/ast.zig index b8b399afc6..70758ece58 100644 --- a/std/zig/ast.zig +++ b/std/zig/ast.zig @@ -1371,6 +1371,7 @@ pub const Node = struct { pub const Suspend = struct { base: Node, + label: ?Token, suspend_token: Token, payload: ?&Node, body: ?&Node, @@ -1392,6 +1393,7 @@ pub const Node = struct { } pub fn firstToken(self: &Suspend) Token { + if (self.label) |label| return label; return self.suspend_token; } diff --git a/std/zig/parser.zig b/std/zig/parser.zig index afef6704cf..ebc0d5b06e 100644 --- a/std/zig/parser.zig +++ b/std/zig/parser.zig @@ -1093,6 +1093,23 @@ pub const Parser = struct { }) catch unreachable; continue; }, + Token.Id.Keyword_suspend => { + const node = try arena.construct(ast.Node.Suspend { + .base = ast.Node { + .id = ast.Node.Id.Suspend, + .doc_comments = null, + .same_line_comment = null, + }, + .label = ctx.label, + .suspend_token = token, + .payload = null, + .body = null, + }); + ctx.opt_ctx.store(&node.base); + stack.append(State { .SuspendBody = node }) catch unreachable; + try stack.append(State { .Payload = OptionalCtx { .Optional = &node.payload } }); + continue; + }, Token.Id.Keyword_inline => { stack.append(State { .Inline = InlineCtx { @@ -3046,6 +3063,7 @@ pub const Parser = struct { const node = try self.createToCtxNode(arena, ctx, ast.Node.Suspend, ast.Node.Suspend { .base = undefined, + .label = null, .suspend_token = *token, .payload = null, .body = null, @@ -3655,6 +3673,9 @@ pub const Parser = struct { }, ast.Node.Id.Suspend => { const suspend_node = @fieldParentPtr(ast.Node.Suspend, "base", base); + if (suspend_node.label) |label| { + try stream.print("{}: ", self.tokenizer.getTokenSlice(label)); + } try stream.print("{}", self.tokenizer.getTokenSlice(suspend_node.suspend_token)); if (suspend_node.body) |body| { diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig index 85fd6c807e..c82030d22b 100644 --- a/std/zig/parser_test.zig +++ b/std/zig/parser_test.zig @@ -1,3 +1,14 @@ +test "zig fmt: labeled suspend" { + try testCanonical( + \\fn foo() void { + \\ s: suspend |p| { + \\ break :s; + \\ } + \\} + \\ + ); +} + test "zig fmt: comments before error set decl" { try testCanonical( \\const UnexpectedError = error { |
