Commit 79f1876367

Andrew Kelley <andrew@ziglang.org>
2021-02-22 01:57:04
parser: remove support for recovering from extra top level end curlies
After #35 is implemented, we should be able to recover from this *at any indentation level*, reporting a parse error and yet also parsing all the decls even inside structs. Until then, I don't want to add any hacks to make this work.
1 parent 2da2123
Changed files (2)
lib/std/zig/parse.zig
@@ -66,9 +66,9 @@ pub fn parse(gpa: *Allocator, source: []const u8) Allocator.Error!Tree {
     });
     const root_members = try parser.parseContainerMembers();
     const root_decls = try root_members.toSpan(&parser);
-    // parseContainerMembers will try to skip as much invalid tokens as
-    // it can, so we are now at EOF.
-    assert(parser.token_tags[parser.tok_i] == .eof);
+    if (parser.token_tags[parser.tok_i] != .eof) {
+        try parser.warnExpected(.eof);
+    }
     parser.nodes.items(.data)[0] = .{
         .lhs = root_decls.start,
         .rhs = root_decls.end,
lib/std/zig/parser_test.zig
@@ -4187,20 +4187,21 @@ test "recovery: invalid container members" {
     });
 }
 
-//test "recovery: extra '}' at top level" {
-//    try testError(
-//        \\}}}
-//        \\test "" {
-//        \\    a && b;
-//        \\}
-//    , &[_]Error{
-//        .expected_container_members,
-//        .expected_container_members,
-//        .expected_container_members,
-//        .invalid_and,
-//    });
-//}
-//
+// TODO after https://github.com/ziglang/zig/issues/35 is implemented,
+// we should be able to recover from this *at any indentation level*,
+// reporting a parse error and yet also parsing all the decls even
+// inside structs.
+test "recovery: extra '}' at top level" {
+    try testError(
+        \\}}}
+        \\test "" {
+        \\    a && b;
+        \\}
+    , &[_]Error{
+        .expected_token,
+    });
+}
+
 test "recovery: mismatched bracket at top level" {
     try testError(
         \\const S = struct {