authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-30 18:20:27-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-30 18:20:27-04:00
log1d06915f275ac59918c32206ad944fd10cc997d4
treee64c2f4b132f0fa7de9bddc603420f9c6be573e0
parent7dc8d433abbf697f05eb1ad2003b6335f750557b

zig fmt: support union(enum(tag)) and enum init values


3 files changed, 60 insertions(+), 3 deletions(-)

std/zig/ast.zig+10-1
...@@ -261,7 +261,7 @@ pub const Node = struct {...@@ -261,7 +261,7 @@ pub const Node = struct {
261261
262 const InitArg = union(enum) {262 const InitArg = union(enum) {
263 None,263 None,
264 Enum,264 Enum: ?&Node,
265 Type: &Node,265 Type: &Node,
266 };266 };
267267
...@@ -321,6 +321,7 @@ pub const Node = struct {...@@ -321,6 +321,7 @@ pub const Node = struct {
321 base: Node,321 base: Node,
322 name_token: Token,322 name_token: Token,
323 type_expr: ?&Node,323 type_expr: ?&Node,
324 value_expr: ?&Node,
324325
325 pub fn iterate(self: &UnionTag, index: usize) ?&Node {326 pub fn iterate(self: &UnionTag, index: usize) ?&Node {
326 var i = index;327 var i = index;
...@@ -330,6 +331,11 @@ pub const Node = struct {...@@ -330,6 +331,11 @@ pub const Node = struct {
330 i -= 1;331 i -= 1;
331 }332 }
332333
334 if (self.value_expr) |value_expr| {
335 if (i < 1) return value_expr;
336 i -= 1;
337 }
338
333 return null;339 return null;
334 }340 }
335341
...@@ -338,6 +344,9 @@ pub const Node = struct {...@@ -338,6 +344,9 @@ pub const Node = struct {
338 }344 }
339345
340 pub fn lastToken(self: &UnionTag) Token {346 pub fn lastToken(self: &UnionTag) Token {
347 if (self.value_expr) |value_expr| {
348 return value_expr.lastToken();
349 }
341 if (self.type_expr) |type_expr| {350 if (self.type_expr) |type_expr| {
342 return type_expr.lastToken();351 return type_expr.lastToken();
343 }352 }
std/zig/parser.zig+38-2
...@@ -242,6 +242,7 @@ pub const Parser = struct {...@@ -242,6 +242,7 @@ pub const Parser = struct {
242 FieldInitListItemOrEnd: ListSave(&ast.Node.FieldInitializer),242 FieldInitListItemOrEnd: ListSave(&ast.Node.FieldInitializer),
243 FieldInitListCommaOrEnd: ListSave(&ast.Node.FieldInitializer),243 FieldInitListCommaOrEnd: ListSave(&ast.Node.FieldInitializer),
244 FieldListCommaOrEnd: &ast.Node.ContainerDecl,244 FieldListCommaOrEnd: &ast.Node.ContainerDecl,
245 FieldInitValue: OptionalCtx,
245 IdentifierListItemOrEnd: ListSave(&ast.Node),246 IdentifierListItemOrEnd: ListSave(&ast.Node),
246 IdentifierListCommaOrEnd: ListSave(&ast.Node),247 IdentifierListCommaOrEnd: ListSave(&ast.Node),
247 SwitchCaseOrEnd: ListSave(&ast.Node),248 SwitchCaseOrEnd: ListSave(&ast.Node),
...@@ -653,6 +654,15 @@ pub const Parser = struct {...@@ -653,6 +654,15 @@ pub const Parser = struct {
653 continue;654 continue;
654 },655 },
655656
657 State.FieldInitValue => |ctx| {
658 const eq_tok = self.getNextToken();
659 if (eq_tok.id != Token.Id.Equal) {
660 self.putBackToken(eq_tok);
661 continue;
662 }
663 stack.append(State { .Expression = ctx }) catch unreachable;
664 continue;
665 },
656666
657 State.ContainerKind => |ctx| {667 State.ContainerKind => |ctx| {
658 const token = self.getNextToken();668 const token = self.getNextToken();
...@@ -699,7 +709,16 @@ pub const Parser = struct {...@@ -699,7 +709,16 @@ pub const Parser = struct {
699 const init_arg_token = self.getNextToken();709 const init_arg_token = self.getNextToken();
700 switch (init_arg_token.id) {710 switch (init_arg_token.id) {
701 Token.Id.Keyword_enum => {711 Token.Id.Keyword_enum => {
702 container_decl.init_arg_expr = ast.Node.ContainerDecl.InitArg.Enum;712 container_decl.init_arg_expr = ast.Node.ContainerDecl.InitArg {.Enum = null};
713 const lparen_tok = self.getNextToken();
714 if (lparen_tok.id == Token.Id.LParen) {
715 try stack.append(State { .ExpectToken = Token.Id.RParen } );
716 try stack.append(State { .Expression = OptionalCtx {
717 .RequiredNull = &container_decl.init_arg_expr.Enum,
718 } });
719 } else {
720 self.putBackToken(lparen_tok);
721 }
703 },722 },
704 else => {723 else => {
705 self.putBackToken(init_arg_token);724 self.putBackToken(init_arg_token);
...@@ -709,6 +728,7 @@ pub const Parser = struct {...@@ -709,6 +728,7 @@ pub const Parser = struct {
709 }728 }
710 continue;729 continue;
711 },730 },
731
712 State.ContainerDecl => |container_decl| {732 State.ContainerDecl => |container_decl| {
713 while (try self.eatLineComment(arena)) |line_comment| {733 while (try self.eatLineComment(arena)) |line_comment| {
714 try container_decl.fields_and_decls.append(&line_comment.base);734 try container_decl.fields_and_decls.append(&line_comment.base);
...@@ -744,10 +764,12 @@ pub const Parser = struct {...@@ -744,10 +764,12 @@ pub const Parser = struct {
744 .base = undefined,764 .base = undefined,
745 .name_token = token,765 .name_token = token,
746 .type_expr = null,766 .type_expr = null,
767 .value_expr = null,
747 }768 }
748 );769 );
749770
750 stack.append(State { .FieldListCommaOrEnd = container_decl }) catch unreachable;771 stack.append(State { .FieldListCommaOrEnd = container_decl }) catch unreachable;
772 try stack.append(State { .FieldInitValue = OptionalCtx { .RequiredNull = &node.value_expr } });
751 try stack.append(State { .TypeExprBegin = OptionalCtx { .RequiredNull = &node.type_expr } });773 try stack.append(State { .TypeExprBegin = OptionalCtx { .RequiredNull = &node.type_expr } });
752 try stack.append(State { .IfToken = Token.Id.Colon });774 try stack.append(State { .IfToken = Token.Id.Colon });
753 continue;775 continue;
...@@ -3515,6 +3537,12 @@ pub const Parser = struct {...@@ -3515,6 +3537,12 @@ pub const Parser = struct {
3515 try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token));3537 try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token));
35163538
3517 try stack.append(RenderState { .Text = "," });3539 try stack.append(RenderState { .Text = "," });
3540
3541 if (tag.value_expr) |value_expr| {
3542 try stack.append(RenderState { .Expression = value_expr });
3543 try stack.append(RenderState { .Text = " = " });
3544 }
3545
3518 if (tag.type_expr) |type_expr| {3546 if (tag.type_expr) |type_expr| {
3519 try stream.print(": ");3547 try stream.print(": ");
3520 try stack.append(RenderState { .Expression = type_expr});3548 try stack.append(RenderState { .Expression = type_expr});
...@@ -4055,7 +4083,15 @@ pub const Parser = struct {...@@ -4055,7 +4083,15 @@ pub const Parser = struct {
40554083
4056 switch (container_decl.init_arg_expr) {4084 switch (container_decl.init_arg_expr) {
4057 ast.Node.ContainerDecl.InitArg.None => try stack.append(RenderState { .Text = " "}),4085 ast.Node.ContainerDecl.InitArg.None => try stack.append(RenderState { .Text = " "}),
4058 ast.Node.ContainerDecl.InitArg.Enum => try stack.append(RenderState { .Text = "(enum) "}),4086 ast.Node.ContainerDecl.InitArg.Enum => |enum_tag_type| {
4087 if (enum_tag_type) |expr| {
4088 try stack.append(RenderState { .Text = ")) "});
4089 try stack.append(RenderState { .Expression = expr});
4090 try stack.append(RenderState { .Text = "(enum("});
4091 } else {
4092 try stack.append(RenderState { .Text = "(enum) "});
4093 }
4094 },
4059 ast.Node.ContainerDecl.InitArg.Type => |type_expr| {4095 ast.Node.ContainerDecl.InitArg.Type => |type_expr| {
4060 try stack.append(RenderState { .Text = ") "});4096 try stack.append(RenderState { .Text = ") "});
4061 try stack.append(RenderState { .Expression = type_expr});4097 try stack.append(RenderState { .Expression = type_expr});
std/zig/parser_test.zig+12
...@@ -1,3 +1,15 @@...@@ -1,3 +1,15 @@
1test "zig fmt: union(enum(u32)) with assigned enum values" {
2 try testCanonical(
3 \\const MultipleChoice = union(enum(u32)) {
4 \\ A = 20,
5 \\ B = 40,
6 \\ C = 60,
7 \\ D = 1000,
8 \\};
9 \\
10 );
11}
12
1test "zig fmt: labeled suspend" {13test "zig fmt: labeled suspend" {
2 try testCanonical(14 try testCanonical(
3 \\fn foo() void {15 \\fn foo() void {