| author | |
| committer | |
| log | aaf13a2bb369aa5b35d4b5ee36ddd8c6029f7473 |
| tree | 945a9ef8e33f5ce42fde6a04d857e82ec0edcf9a |
| parent | 57cec38e6144754fcd15266100974a7cf0059570 |
3 files changed, 50 insertions(+), 24 deletions(-)
lib/std/zig/ast.zig+12-2| ... | @@ -438,7 +438,6 @@ pub const Tree = struct { | ... | @@ -438,7 +438,6 @@ pub const Tree = struct { |
| 438 | .OptionalType, | 438 | .OptionalType, |
| 439 | .Suspend, | 439 | .Suspend, |
| 440 | .Resume, | 440 | .Resume, |
| 441 | .Break, | ||
| 442 | .Nosuspend, | 441 | .Nosuspend, |
| 443 | .Comptime, | 442 | .Comptime, |
| 444 | => n = datas[n].lhs, | 443 | => n = datas[n].lhs, |
| ... | @@ -715,6 +714,16 @@ pub const Tree = struct { | ... | @@ -715,6 +714,16 @@ pub const Tree = struct { |
| 715 | n = extra.sentinel; | 714 | n = extra.sentinel; |
| 716 | }, | 715 | }, |
| 717 | 716 | ||
| 717 | .Break => { | ||
| 718 | if (datas[n].rhs != 0) { | ||
| 719 | n = datas[n].rhs; | ||
| 720 | } else if (datas[n].lhs != 0) { | ||
| 721 | return datas[n].lhs + end_offset; | ||
| 722 | } else { | ||
| 723 | return main_tokens[n] + end_offset; | ||
| 724 | } | ||
| 725 | }, | ||
| 726 | |||
| 718 | // These are not supported by lastToken() because implementation would | 727 | // These are not supported by lastToken() because implementation would |
| 719 | // require recursion due to the optional comma followed by rbrace. | 728 | // require recursion due to the optional comma followed by rbrace. |
| 720 | // TODO follow the pattern set by StructInitDotTwoComma which will allow | 729 | // TODO follow the pattern set by StructInitDotTwoComma which will allow |
| ... | @@ -2023,7 +2032,8 @@ pub const Node = struct { | ... | @@ -2023,7 +2032,8 @@ pub const Node = struct { |
| 2023 | Resume, | 2032 | Resume, |
| 2024 | /// `continue`. lhs is token index of label if any. rhs is unused. | 2033 | /// `continue`. lhs is token index of label if any. rhs is unused. |
| 2025 | Continue, | 2034 | Continue, |
| 2026 | /// `break rhs`. rhs can be omitted. lhs is label token index, if any. | 2035 | /// `break :lhs rhs` |
| 2036 | /// both lhs and rhs may be omitted. | ||
| 2027 | Break, | 2037 | Break, |
| 2028 | /// `return lhs`. lhs can be omitted. rhs is unused. | 2038 | /// `return lhs`. lhs can be omitted. rhs is unused. |
| 2029 | Return, | 2039 | Return, |
lib/std/zig/parser_test.zig+18| ... | @@ -273,6 +273,24 @@ test "zig fmt: comptime struct field" { | ... | @@ -273,6 +273,24 @@ test "zig fmt: comptime struct field" { |
| 273 | ); | 273 | ); |
| 274 | } | 274 | } |
| 275 | 275 | ||
| 276 | test "zig fmt: break from block" { | ||
| 277 | try testCanonical( | ||
| 278 | \\const a = blk: { | ||
| 279 | \\ break :blk 42; | ||
| 280 | \\}; | ||
| 281 | \\const b = blk: { | ||
| 282 | \\ break :blk; | ||
| 283 | \\}; | ||
| 284 | \\const c = { | ||
| 285 | \\ break 42; | ||
| 286 | \\}; | ||
| 287 | \\const d = { | ||
| 288 | \\ break; | ||
| 289 | \\}; | ||
| 290 | \\ | ||
| 291 | ); | ||
| 292 | } | ||
| 293 | |||
| 276 | //test "zig fmt: c pointer type" { | 294 | //test "zig fmt: c pointer type" { |
| 277 | // try testCanonical( | 295 | // try testCanonical( |
| 278 | // \\pub extern fn repro() [*c]const u8; | 296 | // \\pub extern fn repro() [*c]const u8; |
lib/std/zig/render.zig+20-22| ... | @@ -487,28 +487,26 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac | ... | @@ -487,28 +487,26 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac |
| 487 | return renderToken(ais, tree, datas[node].rhs, space); | 487 | return renderToken(ais, tree, datas[node].rhs, space); |
| 488 | }, | 488 | }, |
| 489 | 489 | ||
| 490 | .Break => unreachable, // TODO | 490 | .Break => { |
| 491 | //.Break => { | 491 | const main_token = main_tokens[node]; |
| 492 | // const flow_expr = base.castTag(.Break).?; | 492 | const label_token = datas[node].lhs; |
| 493 | // const maybe_rhs = flow_expr.getRHS(); | 493 | const target = datas[node].rhs; |
| 494 | // const maybe_label = flow_expr.getLabel(); | 494 | if (label_token == 0 and target == 0) { |
| 495 | 495 | try renderToken(ais, tree, main_token, space); // break keyword | |
| 496 | // if (maybe_label == null and maybe_rhs == null) { | 496 | } else if (label_token == 0 and target != 0) { |
| 497 | // return renderToken(ais, tree, flow_expr.ltoken, space); // break | 497 | try renderToken(ais, tree, main_token, .Space); // break keyword |
| 498 | // } | 498 | try renderExpression(ais, tree, target, space); |
| 499 | 499 | } else if (label_token != 0 and target == 0) { | |
| 500 | // try renderToken(ais, tree, flow_expr.ltoken, Space.Space); // break | 500 | try renderToken(ais, tree, main_token, .Space); // break keyword |
| 501 | // if (maybe_label) |label| { | 501 | try renderToken(ais, tree, label_token - 1, .None); // colon |
| 502 | // const colon = tree.nextToken(flow_expr.ltoken); | 502 | try renderToken(ais, tree, label_token, space); // identifier |
| 503 | // try renderToken(ais, tree, colon, Space.None); // : | 503 | } else if (label_token != 0 and target != 0) { |
| 504 | 504 | try renderToken(ais, tree, main_token, .Space); // break keyword | |
| 505 | // if (maybe_rhs == null) { | 505 | try renderToken(ais, tree, label_token - 1, .None); // colon |
| 506 | // return renderToken(ais, tree, label, space); // label | 506 | try renderToken(ais, tree, label_token, .Space); // identifier |
| 507 | // } | 507 | try renderExpression(ais, tree, target, space); |
| 508 | // try renderToken(ais, tree, label, Space.Space); // label | 508 | } |
| 509 | // } | 509 | }, |
| 510 | // return renderExpression(ais, tree, maybe_rhs.?, space); | ||
| 511 | //}, | ||
| 512 | 510 | ||
| 513 | .Continue => unreachable, // TODO | 511 | .Continue => unreachable, // TODO |
| 514 | //.Continue => { | 512 | //.Continue => { |