authorgravatar for matthew.h.borkowski@gmail.comMatthew Borkowski <matthew.h.borkowski@gmail.com> 2021-05-30 02:20:20-04:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-06-08 20:50:55+03:00
log8bf04c3a6955bc66e8d6fbbf00f12a44bb7079d5
treee4de188c3abb846c557fbcdd881987634f0fdfd3
parentfc956fc1107e7f7fecec75d178ed315e5f338e12

parse.zig: simplify `parseParamDeclList` by always using scratch buffer


1 files changed, 12 insertions(+), 52 deletions(-)

lib/std/zig/parse.zig+12-52
......@@ -3551,58 +3551,17 @@ const Parser = struct {
35513551 /// ParamDeclList <- (ParamDecl COMMA)* ParamDecl?
35523552 fn parseParamDeclList(p: *Parser) !SmallSpan {
35533553 _ = try p.expectToken(.l_paren);
3554 if (p.eatToken(.r_paren)) |_| {
3555 return SmallSpan{ .zero_or_one = 0 };
3556 }
3557 const param_one = while (true) {
3558 const param = try p.expectParamDecl();
3559 if (param != 0) break param;
3560 switch (p.token_tags[p.nextToken()]) {
3561 .comma => {
3562 if (p.eatToken(.r_paren)) |_| {
3563 return SmallSpan{ .zero_or_one = 0 };
3564 }
3565 },
3566 .r_paren => return SmallSpan{ .zero_or_one = 0 },
3567 else => {
3568 // This is likely just a missing comma;
3569 // give an error but continue parsing this list.
3570 p.tok_i -= 1;
3571 try p.warnExpected(.comma);
3572 },
3573 }
3574 } else unreachable;
3575
3576 const param_two = while (true) {
3577 switch (p.token_tags[p.nextToken()]) {
3578 .comma => {},
3579 .r_paren => return SmallSpan{ .zero_or_one = param_one },
3580 .colon, .r_brace, .r_bracket => {
3581 p.tok_i -= 1;
3582 return p.failExpected(.r_paren);
3583 },
3584 else => {
3585 // This is likely just a missing comma;
3586 // give an error but continue parsing this list.
3587 p.tok_i -= 1;
3588 try p.warnExpected(.comma);
3589 },
3590 }
3591 if (p.eatToken(.r_paren)) |_| {
3592 return SmallSpan{ .zero_or_one = param_one };
3593 }
3594 const param = try p.expectParamDecl();
3595 if (param != 0) break param;
3596 } else unreachable;
3597
35983554 const scratch_top = p.scratch.items.len;
35993555 defer p.scratch.shrinkRetainingCapacity(scratch_top);
3600 try p.scratch.appendSlice(p.gpa, &.{ param_one, param_two });
3601
36023556 while (true) {
3557 if (p.eatToken(.r_paren)) |_| break;
3558 const param = try p.expectParamDecl();
3559 if (param != 0) {
3560 try p.scratch.append(p.gpa, param);
3561 }
36033562 switch (p.token_tags[p.nextToken()]) {
36043563 .comma => {},
3605 .r_paren => return SmallSpan{ .multi = try p.listToSpan(p.scratch.items[scratch_top..]) },
3564 .r_paren => break,
36063565 .colon, .r_brace, .r_bracket => {
36073566 p.tok_i -= 1;
36083567 return p.failExpected(.r_paren);
......@@ -3614,12 +3573,13 @@ const Parser = struct {
36143573 try p.warnExpected(.comma);
36153574 },
36163575 }
3617 if (p.eatToken(.r_paren)) |_| {
3618 return SmallSpan{ .multi = try p.listToSpan(p.scratch.items[scratch_top..]) };
3619 }
3620 const param = try p.expectParamDecl();
3621 if (param != 0) try p.scratch.append(p.gpa, param);
36223576 }
3577 const params = p.scratch.items[scratch_top..];
3578 return switch (params.len) {
3579 0 => SmallSpan { .zero_or_one = 0 },
3580 1 => SmallSpan { .zero_or_one = params[0] },
3581 else => SmallSpan { .multi = try p.listToSpan(params) },
3582 };
36233583 }
36243584
36253585 const NodeParseFn = fn (p: *Parser) Error!Node.Index;