authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-30 17:30:57-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-30 17:30:57-04:00
log7dc8d433abbf697f05eb1ad2003b6335f750557b
tree22ddf5420466c8c3a5f6ae4e8b02490ce92c5404
parent37d3ef28351027a83249080d3238d61d9346f6db

zig fmt: support labeled suspend


3 files changed, 34 insertions(+), 0 deletions(-)

std/zig/ast.zig+2
...@@ -1371,6 +1371,7 @@ pub const Node = struct {...@@ -1371,6 +1371,7 @@ pub const Node = struct {
13711371
1372 pub const Suspend = struct {1372 pub const Suspend = struct {
1373 base: Node,1373 base: Node,
1374 label: ?Token,
1374 suspend_token: Token,1375 suspend_token: Token,
1375 payload: ?&Node,1376 payload: ?&Node,
1376 body: ?&Node,1377 body: ?&Node,
...@@ -1392,6 +1393,7 @@ pub const Node = struct {...@@ -1392,6 +1393,7 @@ pub const Node = struct {
1392 }1393 }
13931394
1394 pub fn firstToken(self: &Suspend) Token {1395 pub fn firstToken(self: &Suspend) Token {
1396 if (self.label) |label| return label;
1395 return self.suspend_token;1397 return self.suspend_token;
1396 }1398 }
13971399
std/zig/parser.zig+21
...@@ -1093,6 +1093,23 @@ pub const Parser = struct {...@@ -1093,6 +1093,23 @@ pub const Parser = struct {
1093 }) catch unreachable;1093 }) catch unreachable;
1094 continue;1094 continue;
1095 },1095 },
1096 Token.Id.Keyword_suspend => {
1097 const node = try arena.construct(ast.Node.Suspend {
1098 .base = ast.Node {
1099 .id = ast.Node.Id.Suspend,
1100 .doc_comments = null,
1101 .same_line_comment = null,
1102 },
1103 .label = ctx.label,
1104 .suspend_token = token,
1105 .payload = null,
1106 .body = null,
1107 });
1108 ctx.opt_ctx.store(&node.base);
1109 stack.append(State { .SuspendBody = node }) catch unreachable;
1110 try stack.append(State { .Payload = OptionalCtx { .Optional = &node.payload } });
1111 continue;
1112 },
1096 Token.Id.Keyword_inline => {1113 Token.Id.Keyword_inline => {
1097 stack.append(State {1114 stack.append(State {
1098 .Inline = InlineCtx {1115 .Inline = InlineCtx {
...@@ -3046,6 +3063,7 @@ pub const Parser = struct {...@@ -3046,6 +3063,7 @@ pub const Parser = struct {
3046 const node = try self.createToCtxNode(arena, ctx, ast.Node.Suspend,3063 const node = try self.createToCtxNode(arena, ctx, ast.Node.Suspend,
3047 ast.Node.Suspend {3064 ast.Node.Suspend {
3048 .base = undefined,3065 .base = undefined,
3066 .label = null,
3049 .suspend_token = *token,3067 .suspend_token = *token,
3050 .payload = null,3068 .payload = null,
3051 .body = null,3069 .body = null,
...@@ -3655,6 +3673,9 @@ pub const Parser = struct {...@@ -3655,6 +3673,9 @@ pub const Parser = struct {
3655 },3673 },
3656 ast.Node.Id.Suspend => {3674 ast.Node.Id.Suspend => {
3657 const suspend_node = @fieldParentPtr(ast.Node.Suspend, "base", base);3675 const suspend_node = @fieldParentPtr(ast.Node.Suspend, "base", base);
3676 if (suspend_node.label) |label| {
3677 try stream.print("{}: ", self.tokenizer.getTokenSlice(label));
3678 }
3658 try stream.print("{}", self.tokenizer.getTokenSlice(suspend_node.suspend_token));3679 try stream.print("{}", self.tokenizer.getTokenSlice(suspend_node.suspend_token));
36593680
3660 if (suspend_node.body) |body| {3681 if (suspend_node.body) |body| {
std/zig/parser_test.zig+11
...@@ -1,3 +1,14 @@...@@ -1,3 +1,14 @@
1test "zig fmt: labeled suspend" {
2 try testCanonical(
3 \\fn foo() void {
4 \\ s: suspend |p| {
5 \\ break :s;
6 \\ }
7 \\}
8 \\
9 );
10}
11
1test "zig fmt: comments before error set decl" {12test "zig fmt: comments before error set decl" {
2 try testCanonical(13 try testCanonical(
3 \\const UnexpectedError = error {14 \\const UnexpectedError = error {