From 3692fb039d0f8d16ffce35e0062e51ab79192c9d Mon Sep 17 00:00:00 2001 From: Matthew Borkowski Date: Fri, 11 Jun 2021 17:59:41 -0400 Subject: [PATCH] parse.zig: simplify parseSwitchProng and make one item cases with trailing commas produce .switch_case_one nodes --- lib/std/zig/parse.zig | 67 ++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/lib/std/zig/parse.zig b/lib/std/zig/parse.zig index 2bdfc348bc..044766c5cf 100644 --- a/lib/std/zig/parse.zig +++ b/lib/std/zig/parse.zig @@ -2976,53 +2976,48 @@ const Parser = struct { /// <- SwitchItem (COMMA SwitchItem)* COMMA? /// / KEYWORD_else fn parseSwitchProng(p: *Parser) !Node.Index { - if (p.eatToken(.keyword_else)) |_| { - const arrow_token = try p.expectToken(.equal_angle_bracket_right); - _ = try p.parsePtrPayload(); - return p.addNode(.{ + const scratch_top = p.scratch.items.len; + defer p.scratch.shrinkRetainingCapacity(scratch_top); + + if (p.eatToken(.keyword_else) == null) { + while (true) { + const item = try p.parseSwitchItem(); + if (item == 0) break; + try p.scratch.append(p.gpa, item); + if (p.eatToken(.comma) == null) break; + } + if (scratch_top == p.scratch.items.len) return null_node; + } + const arrow_token = try p.expectToken(.equal_angle_bracket_right); + _ = try p.parsePtrPayload(); + + const items = p.scratch.items[scratch_top..]; + switch(items.len) { + 0 => return p.addNode(.{ .tag = .switch_case_one, .main_token = arrow_token, .data = .{ .lhs = 0, .rhs = try p.expectAssignExpr(), - }, - }); - } - const first_item = try p.parseSwitchItem(); - if (first_item == 0) return null_node; - - if (p.eatToken(.equal_angle_bracket_right)) |arrow_token| { - _ = try p.parsePtrPayload(); - return p.addNode(.{ + } + }), + 1 => return p.addNode(.{ .tag = .switch_case_one, .main_token = arrow_token, .data = .{ - .lhs = first_item, + .lhs = items[0], + .rhs = try p.expectAssignExpr(), + } + }), + else => return p.addNode(.{ + .tag = .switch_case, + .main_token = arrow_token, + .data = .{ + .lhs = try p.addExtra(try p.listToSpan(items)), .rhs = try p.expectAssignExpr(), }, - }); + }), } - - const scratch_top = p.scratch.items.len; - defer p.scratch.shrinkRetainingCapacity(scratch_top); - - try p.scratch.append(p.gpa, first_item); - while (p.eatToken(.comma)) |_| { - const next_item = try p.parseSwitchItem(); - if (next_item == 0) break; - try p.scratch.append(p.gpa, next_item); - } - const span = try p.listToSpan(p.scratch.items[scratch_top..]); - const arrow_token = try p.expectToken(.equal_angle_bracket_right); - _ = try p.parsePtrPayload(); - return p.addNode(.{ - .tag = .switch_case, - .main_token = arrow_token, - .data = .{ - .lhs = try p.addExtra(span), - .rhs = try p.expectAssignExpr(), - }, - }); } /// SwitchItem <- Expr (DOT3 Expr)?