| author | |
| committer | |
| log | 1d71b19c0d025aeeede229e714679f4b4fb7880d |
| tree | 516c0f0ae27457897680746b24df50ac04930d88 |
| parent | bb7b5ee2acb81a69290b2eaafecd6095f3adfe6a |
4 files changed, 84 insertions(+), 7 deletions(-)
lib/std/zig/ast.zig+2-2| ... | @@ -498,6 +498,7 @@ pub const Tree = struct { | ... | @@ -498,6 +498,7 @@ pub const Tree = struct { |
| 498 | .UnwrapOptional, | 498 | .UnwrapOptional, |
| 499 | .GroupedExpression, | 499 | .GroupedExpression, |
| 500 | .StringLiteral, | 500 | .StringLiteral, |
| 501 | .ErrorSetDecl, | ||
| 501 | => return datas[n].rhs + end_offset, | 502 | => return datas[n].rhs + end_offset, |
| 502 | 503 | ||
| 503 | .AnyType, | 504 | .AnyType, |
| ... | @@ -724,7 +725,6 @@ pub const Tree = struct { | ... | @@ -724,7 +725,6 @@ pub const Tree = struct { |
| 724 | .Switch => unreachable, // TODO | 725 | .Switch => unreachable, // TODO |
| 725 | .If => unreachable, // TODO | 726 | .If => unreachable, // TODO |
| 726 | .Continue => unreachable, // TODO | 727 | .Continue => unreachable, // TODO |
| 727 | .ErrorSetDecl => unreachable, // TODO | ||
| 728 | .AsmSimple => unreachable, // TODO | 728 | .AsmSimple => unreachable, // TODO |
| 729 | .Asm => unreachable, // TODO | 729 | .Asm => unreachable, // TODO |
| 730 | .SwitchCaseOne => unreachable, // TODO | 730 | .SwitchCaseOne => unreachable, // TODO |
| ... | @@ -2061,7 +2061,7 @@ pub const Node = struct { | ... | @@ -2061,7 +2061,7 @@ pub const Node = struct { |
| 2061 | /// Same as BuiltinCall but there is known to be a trailing comma before the rparen. | 2061 | /// Same as BuiltinCall but there is known to be a trailing comma before the rparen. |
| 2062 | BuiltinCallComma, | 2062 | BuiltinCallComma, |
| 2063 | /// `error{a, b}`. | 2063 | /// `error{a, b}`. |
| 2064 | /// lhs and rhs both unused. | 2064 | /// rhs is the rbrace, lhs is unused. |
| 2065 | ErrorSetDecl, | 2065 | ErrorSetDecl, |
| 2066 | /// `struct {}`, `union {}`, `opaque {}`, `enum {}`. `extra_data[lhs..rhs]`. | 2066 | /// `struct {}`, `union {}`, `opaque {}`, `enum {}`. `extra_data[lhs..rhs]`. |
| 2067 | /// main_token is `struct`, `union`, `opaque`, `enum` keyword. | 2067 | /// main_token is `struct`, `union`, `opaque`, `enum` keyword. |
lib/std/zig/parse.zig+3-3| ... | @@ -2714,13 +2714,13 @@ const Parser = struct { | ... | @@ -2714,13 +2714,13 @@ const Parser = struct { |
| 2714 | const error_token = p.tok_i; | 2714 | const error_token = p.tok_i; |
| 2715 | p.tok_i += 2; | 2715 | p.tok_i += 2; |
| 2716 | 2716 | ||
| 2717 | if (p.eatToken(.RBrace)) |_| { | 2717 | if (p.eatToken(.RBrace)) |rbrace| { |
| 2718 | return p.addNode(.{ | 2718 | return p.addNode(.{ |
| 2719 | .tag = .ErrorSetDecl, | 2719 | .tag = .ErrorSetDecl, |
| 2720 | .main_token = error_token, | 2720 | .main_token = error_token, |
| 2721 | .data = .{ | 2721 | .data = .{ |
| 2722 | .lhs = undefined, | 2722 | .lhs = undefined, |
| 2723 | .rhs = undefined, | 2723 | .rhs = rbrace, |
| 2724 | }, | 2724 | }, |
| 2725 | }); | 2725 | }); |
| 2726 | } | 2726 | } |
| ... | @@ -2758,7 +2758,7 @@ const Parser = struct { | ... | @@ -2758,7 +2758,7 @@ const Parser = struct { |
| 2758 | .main_token = error_token, | 2758 | .main_token = error_token, |
| 2759 | .data = .{ | 2759 | .data = .{ |
| 2760 | .lhs = undefined, | 2760 | .lhs = undefined, |
| 2761 | .rhs = undefined, | 2761 | .rhs = p.tok_i - 1, // rbrace |
| 2762 | }, | 2762 | }, |
| 2763 | }); | 2763 | }); |
| 2764 | }, | 2764 | }, |
lib/std/zig/parser_test.zig+30-1| ... | @@ -2015,7 +2015,36 @@ test "zig fmt: ptr deref operator and unwrap optional operator" { | ... | @@ -2015,7 +2015,36 @@ test "zig fmt: ptr deref operator and unwrap optional operator" { |
| 2015 | // \\ | 2015 | // \\ |
| 2016 | // ); | 2016 | // ); |
| 2017 | //} | 2017 | //} |
| 2018 | // | 2018 | |
| 2019 | // TODO: replace this with the next test case when possible | ||
| 2020 | test "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 | |||
| 2019 | //test "zig fmt: error set declaration" { | 2048 | //test "zig fmt: error set declaration" { |
| 2020 | // try testCanonical( | 2049 | // try testCanonical( |
| 2021 | // \\const E = error{ | 2050 | // \\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 | ... | @@ -563,7 +563,55 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac |
| 563 | .TaggedUnionEnumTagComma, | 563 | .TaggedUnionEnumTagComma, |
| 564 | => return renderContainerDecl(ais, tree, tree.taggedUnionEnumTag(node), space), | 564 | => return renderContainerDecl(ais, tree, tree.taggedUnionEnumTag(node), space), |
| 565 | 565 | ||
| 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 | }, | ||
| 567 | //.ErrorSetDecl => { | 615 | //.ErrorSetDecl => { |
| 568 | // const err_set_decl = @fieldParentPtr(ast.Node.ErrorSetDecl, "base", base); | 616 | // const err_set_decl = @fieldParentPtr(ast.Node.ErrorSetDecl, "base", base); |
| 569 | 617 |