Skip to content

Commit

Permalink
parser: don't quit on syntax errors caused by .s in struct inits an…
Browse files Browse the repository at this point in the history
…d switch blocks
  • Loading branch information
llogick committed Feb 21, 2024
1 parent 3a5b2e7 commit 42fb30b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
40 changes: 39 additions & 1 deletion src/stage2/Parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ fn parseGlobalVarDecl(p: *Parse) !Node.Index {

p.nodes.items(.data)[var_decl].rhs = init_node;

try p.expectSemicolon(.expected_semi_after_decl, false);
try p.expectSemicolon(.expected_semi_after_decl, true);
return var_decl;
}

Expand Down Expand Up @@ -2432,6 +2432,11 @@ fn parseCurlySuffixExpr(p: *Parse) !Node.Index {
if (lhs == 0) return null_node;
const lbrace = p.eatToken(.l_brace) orelse return lhs;

if (p.token_tags[p.tok_i] == .period and p.token_tags[p.tok_i + 1] == .period) { // zls
try p.warn(.expected_initializer);
p.tok_i += 1;
}

// If there are 0 or 1 items, we can use ArrayInitOne/StructInitOne;
// otherwise we use the full ArrayInit/StructInit.

Expand All @@ -2451,6 +2456,12 @@ fn parseCurlySuffixExpr(p: *Parse) !Node.Index {
// Likely just a missing comma; give error but continue parsing.
else => try p.warn(.expected_comma_after_initializer),
}
if (p.token_tags[p.tok_i] == .period) { // zls
if (p.token_tags[p.tok_i + 1] == .period or p.token_tags[p.tok_i + 1] == .r_brace) {
try p.warn(.expected_initializer);
p.tok_i += 1;
}
}
if (p.eatToken(.r_brace)) |_| break;
const next = try p.expectFieldInit();
try p.scratch.append(p.gpa, next);
Expand Down Expand Up @@ -2479,6 +2490,10 @@ fn parseCurlySuffixExpr(p: *Parse) !Node.Index {
}

while (true) {
if (p.token_tags[p.tok_i] == .period and p.token_tags[p.tok_i + 1] == .r_brace) { // zls
try p.warn(.expected_expr);
p.tok_i += 1;
}
if (p.eatToken(.r_brace)) |_| break;
const elem_init = try p.expectExpr();
try p.scratch.append(p.gpa, elem_init);
Expand Down Expand Up @@ -2851,6 +2866,11 @@ fn parsePrimaryTypeExpr(p: *Parse) !Node.Index {
const lbrace = p.tok_i + 1;
p.tok_i = lbrace + 1;

if (p.token_tags[p.tok_i] == .period and p.token_tags[p.tok_i + 1] == .period) { // zls
try p.warn(.expected_initializer);
p.tok_i += 1;
}

// If there are 0, 1, or 2 items, we can use ArrayInitDotTwo/StructInitDotTwo;
// otherwise we use the full ArrayInitDot/StructInitDot.

Expand All @@ -2870,6 +2890,12 @@ fn parsePrimaryTypeExpr(p: *Parse) !Node.Index {
// Likely just a missing comma; give error but continue parsing.
else => try p.warn(.expected_comma_after_initializer),
}
if (p.token_tags[p.tok_i] == .period) { // zls
if (p.token_tags[p.tok_i + 1] == .period or p.token_tags[p.tok_i + 1] == .r_brace) {
try p.warn(.expected_initializer);
p.tok_i += 1;
}
}
if (p.eatToken(.r_brace)) |_| break;
const next = try p.expectFieldInit();
try p.scratch.append(p.gpa, next);
Expand Down Expand Up @@ -2909,6 +2935,10 @@ fn parsePrimaryTypeExpr(p: *Parse) !Node.Index {
}

while (true) {
if (p.token_tags[p.tok_i] == .period and p.token_tags[p.tok_i + 1] == .r_brace) { // zls
try p.warn(.expected_expr);
p.tok_i += 1;
}
if (p.eatToken(.r_brace)) |_| break;
const elem_init = try p.expectExpr();
try p.scratch.append(p.gpa, elem_init);
Expand Down Expand Up @@ -3130,6 +3160,10 @@ fn expectSwitchExpr(p: *Parse) !Node.Index {
_ = try p.expectToken(.l_brace);
const cases = try p.parseSwitchProngList();
const trailing_comma = p.token_tags[p.tok_i - 1] == .comma;
if (p.token_tags[p.tok_i] == .period and p.token_tags[p.tok_i + 1] == .r_brace) { // zls
try p.warn(.expected_expr);
p.tok_i += 1;
}
_ = try p.expectToken(.r_brace);

return p.addNode(.{
Expand Down Expand Up @@ -3439,6 +3473,10 @@ fn parseSwitchProng(p: *Parse) !Node.Index {

if (p.eatToken(.keyword_else) == null) {
while (true) {
if (p.token_tags[p.tok_i] == .period and p.token_tags[p.tok_i + 1] == .period) { // zls
try p.warn(.expected_expr);
p.tok_i += 1;
}
const item = try p.parseSwitchItem();
if (item == 0) break;
try p.scratch.append(p.gpa, item);
Expand Down
22 changes: 14 additions & 8 deletions tests/lsp_features/completion.zig
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,16 @@ test "completion - generic function" {
try testCompletion(
\\const S = struct { alpha: u32 };
\\fn foo(comptime T: type) T {}
\\const s = foo(S);
\\const foo = s.<cursor>
\\const S1 = foo(S);
\\const S2 = S1.<cursor>
, &.{
.{ .label = "alpha", .kind = .Field, .detail = "u32" },
});
try testCompletion(
\\const S = struct { alpha: u32 };
\\fn foo(any: anytype, comptime T: type) T {}
\\const s = foo(null, S);
\\const foo = s.<cursor>
\\const S1 = foo(null, S);
\\const S2 = S1.<cursor>
, &.{
.{ .label = "alpha", .kind = .Field, .detail = "u32" },
});
Expand Down Expand Up @@ -1797,7 +1797,9 @@ test "completion - struct init" {
\\ brefa: A,
\\ this_is_b: []const u8,
\\};
\\ref(.{ .arefb = .{ .brefa = .{.<cursor>} } });
\\test {
\\ ref(.{ .arefb = .{ .brefa = .{.<cursor>} } });
\\}
, &.{
.{ .label = "arefb", .kind = .Field, .detail = "B = 8" },
.{ .label = "this_is_a", .kind = .Field, .detail = "u32 = 9" },
Expand Down Expand Up @@ -1836,7 +1838,9 @@ test "completion - struct init" {
\\ const Self = @This();
\\ pub fn s3(self: *Self, p0: es, p1: S2) void {}
\\};
\\S3.s3(null, .{ .mye = .{} }, .{ .ref1 = .{ .ref3 = .{ .ref2 = .{ .ref1 = .{.<cursor>} } } } });
\\test {
\\ S3.s3(null, .{ .mye = .{} }, .{ .ref1 = .{ .ref3 = .{ .ref2 = .{ .ref1 = .{.<cursor>} } } } });
\\}
, &.{
.{ .label = "s1f1", .kind = .Field, .detail = "u8" },
.{ .label = "s1f2", .kind = .Field, .detail = "u32 = 1" },
Expand All @@ -1862,8 +1866,10 @@ test "completion - struct init" {
\\ const Self = @This();
\\ pub fn s3(self: Self, p0: es, p1: S1) void {}
\\};
\\const iofs3 = S3{};
\\iofs3.s3(.{.<cursor>});
\\test {
\\ const iofs3 = S3{};
\\ iofs3.s3(.{.<cursor>});
\\}
, &.{
.{ .label = "s1f1", .kind = .Field, .detail = "u8" },
.{ .label = "s1f2", .kind = .Field, .detail = "u32 = 1" },
Expand Down

0 comments on commit 42fb30b

Please sign in to comment.