parse.zig: better c pointer prefix parsing, don't index out of bounds on eof

This commit is contained in:
Matthew Borkowski 2021-09-30 01:55:21 -04:00 committed by Isaac Freund
parent ba7f40c430
commit 2ed9288246
2 changed files with 14 additions and 11 deletions

View File

@ -1584,18 +1584,13 @@ const Parser = struct {
_ = p.nextToken();
const asterisk = p.nextToken();
var sentinel: Node.Index = 0;
prefix: {
if (p.eatToken(.identifier)) |ident| {
const token_slice = p.source[p.token_starts[ident]..][0..2];
if (!std.mem.eql(u8, token_slice, "c]")) {
p.tok_i -= 1;
} else {
break :prefix;
}
}
if (p.eatToken(.colon)) |_| {
sentinel = try p.expectExpr();
if (p.eatToken(.identifier)) |ident| {
const ident_slice = p.source[p.token_starts[ident]..p.token_starts[ident + 1]];
if (!std.mem.eql(u8, std.mem.trimRight(u8, ident_slice, &std.ascii.spaces), "c")) {
p.tok_i -= 1;
}
} else if (p.eatToken(.colon)) |_| {
sentinel = try p.expectExpr();
}
_ = try p.expectToken(.r_bracket);
const mods = try p.parsePtrModifiers();

View File

@ -5259,6 +5259,14 @@ test "recovery: nonfinal varargs" {
});
}
test "recovery: eof in c pointer" {
try testError(
\\const Ptr = [*c
, &[_]Error{
.expected_token,
});
}
const std = @import("std");
const mem = std.mem;
const print = std.debug.print;