authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-07 22:16:23+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-07 14:51:37-08:00
log1d71b19c0d025aeeede229e714679f4b4fb7880d
tree516c0f0ae27457897680746b24df50ac04930d88
parentbb7b5ee2acb81a69290b2eaafecd6095f3adfe6a

zig fmt: implement error set decls


4 files changed, 84 insertions(+), 7 deletions(-)

lib/std/zig/ast.zig+2-2
......@@ -498,6 +498,7 @@ pub const Tree = struct {
498498 .UnwrapOptional,
499499 .GroupedExpression,
500500 .StringLiteral,
501 .ErrorSetDecl,
501502 => return datas[n].rhs + end_offset,
502503
503504 .AnyType,
......@@ -724,7 +725,6 @@ pub const Tree = struct {
724725 .Switch => unreachable, // TODO
725726 .If => unreachable, // TODO
726727 .Continue => unreachable, // TODO
727 .ErrorSetDecl => unreachable, // TODO
728728 .AsmSimple => unreachable, // TODO
729729 .Asm => unreachable, // TODO
730730 .SwitchCaseOne => unreachable, // TODO
......@@ -2061,7 +2061,7 @@ pub const Node = struct {
20612061 /// Same as BuiltinCall but there is known to be a trailing comma before the rparen.
20622062 BuiltinCallComma,
20632063 /// `error{a, b}`.
2064 /// lhs and rhs both unused.
2064 /// rhs is the rbrace, lhs is unused.
20652065 ErrorSetDecl,
20662066 /// `struct {}`, `union {}`, `opaque {}`, `enum {}`. `extra_data[lhs..rhs]`.
20672067 /// main_token is `struct`, `union`, `opaque`, `enum` keyword.
lib/std/zig/parse.zig+3-3
......@@ -2714,13 +2714,13 @@ const Parser = struct {
27142714 const error_token = p.tok_i;
27152715 p.tok_i += 2;
27162716
2717 if (p.eatToken(.RBrace)) |_| {
2717 if (p.eatToken(.RBrace)) |rbrace| {
27182718 return p.addNode(.{
27192719 .tag = .ErrorSetDecl,
27202720 .main_token = error_token,
27212721 .data = .{
27222722 .lhs = undefined,
2723 .rhs = undefined,
2723 .rhs = rbrace,
27242724 },
27252725 });
27262726 }
......@@ -2758,7 +2758,7 @@ const Parser = struct {
27582758 .main_token = error_token,
27592759 .data = .{
27602760 .lhs = undefined,
2761 .rhs = undefined,
2761 .rhs = p.tok_i - 1, // rbrace
27622762 },
27632763 });
27642764 },
lib/std/zig/parser_test.zig+30-1
......@@ -2015,7 +2015,36 @@ test "zig fmt: ptr deref operator and unwrap optional operator" {
20152015// \\
20162016// );
20172017//}
2018//
2018
2019// TODO: replace this with the next test case when possible
2020test "zig fmt: error set declaration" {
2021 try testCanonical(
2022 \\const E = error{
2023 \\ A,
2024 \\ B,
2025 \\
2026 \\ C,
2027 \\};
2028 \\const Error = error{
2029 \\ /// no more memory
2030 \\ OutOfMemory,
2031 \\};
2032 \\const Error = error{
2033 \\ /// no more memory
2034 \\ OutOfMemory,
2035 \\
2036 \\ /// another
2037 \\ Another,
2038 \\ /// and one more
2039 \\ Another,
2040 \\};
2041 \\const Error = error{OutOfMemory};
2042 \\const Error = error{};
2043 \\const Error = error{ OutOfMemory, OutOfTime };
2044 \\
2045 );
2046}
2047
20192048//test "zig fmt: error set declaration" {
20202049// try testCanonical(
20212050// \\const E = error{
lib/std/zig/render.zig+49-1
......@@ -563,7 +563,55 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
563563 .TaggedUnionEnumTagComma,
564564 => return renderContainerDecl(ais, tree, tree.taggedUnionEnumTag(node), space),
565565
566 .ErrorSetDecl => unreachable, // TODO
566 // TODO: handle comments properly
567 .ErrorSetDecl => {
568 const error_token = main_tokens[node];
569 const lbrace = error_token + 1;
570 const rbrace = datas[node].rhs;
571
572 try renderToken(ais, tree, error_token, .None);
573
574 if (lbrace + 1 == rbrace) {
575 // There is nothing between the braces so render condensed: `error{}`
576 try renderToken(ais, tree, lbrace, .None);
577 try renderToken(ais, tree, rbrace, space);
578 } else if (lbrace + 2 == rbrace and token_tags[lbrace + 1] == .Identifier) {
579 // There is exactly one member and no trailing comma or
580 // comments, so render without surrounding spaces: `error{Foo}`
581 try renderToken(ais, tree, lbrace, .None);
582 try renderToken(ais, tree, lbrace + 1, .None); // identifier
583 try renderToken(ais, tree, rbrace, space);
584 } else if (token_tags[rbrace - 1] == .Comma) {
585 // There is a trailing comma so render each member on a new line.
586 try renderToken(ais, tree, lbrace, .Newline);
587 ais.pushIndent();
588 var i = lbrace + 1;
589 while (i < rbrace) : (i += 1) {
590 try renderExtraNewlineToken(ais, tree, i);
591 switch (token_tags[i]) {
592 .DocComment => try renderToken(ais, tree, i, .Newline),
593 .Identifier => try renderToken(ais, tree, i, .Comma),
594 .Comma => {},
595 else => unreachable,
596 }
597 }
598 ais.popIndent();
599 try renderToken(ais, tree, rbrace, space);
600 } else {
601 // There is no trailing comma so render everything on one line.
602 try renderToken(ais, tree, lbrace, .Space);
603 var i = lbrace + 1;
604 while (i < rbrace) : (i += 1) {
605 switch (token_tags[i]) {
606 .DocComment => unreachable, // TODO
607 .Identifier => try renderToken(ais, tree, i, .CommaSpace),
608 .Comma => {},
609 else => unreachable,
610 }
611 }
612 try renderToken(ais, tree, rbrace, space);
613 }
614 },
567615 //.ErrorSetDecl => {
568616 // const err_set_decl = @fieldParentPtr(ast.Node.ErrorSetDecl, "base", base);
569617