authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2019-11-17 17:07:48+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-17 22:24:21+00:00
log6cddf9d7238a58b88064c3f3ce6e3948143598d8
tree4b0f91b91e49d7bdd005ad8138ef6ff77fc7d470
parent8c4784f9c181a13eae36d7a1ac57f278cf5fcd72

properly parse anon literal in array


4 files changed, 42 insertions(+), 2 deletions(-)

lib/std/zig/parse.zig+5-1
......@@ -1630,7 +1630,11 @@ fn parseBlockLabel(arena: *Allocator, it: *TokenIterator, tree: *Tree) ?TokenInd
16301630/// FieldInit <- DOT IDENTIFIER EQUAL Expr
16311631fn parseFieldInit(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
16321632 const period_token = eatToken(it, .Period) orelse return null;
1633 const name_token = try expectToken(it, tree, .Identifier);
1633 const name_token = eatToken(it, .Identifier) orelse {
1634 // Because of anon literals `.{` is also valid.
1635 putBackToken(it, period_token);
1636 return null;
1637 };
16341638 const eq_token = eatToken(it, .Equal) orelse {
16351639 // `.Name` may also be an enum literal, which is a later rule.
16361640 putBackToken(it, name_token);
lib/std/zig/parser_test.zig+10
......@@ -1,3 +1,13 @@
1test "zig fmt: anon literal in array" {
2 try testCanonical(
3 \\var arr: [2]Foo = .{
4 \\ .{ .a = 2 },
5 \\ .{ .b = 3 },
6 \\};
7 \\
8 );
9}
10
111test "zig fmt: anon struct literal syntax" {
212 try testCanonical(
313 \\const x = .{
src/parser.cpp+6-1
......@@ -2025,7 +2025,12 @@ static AstNode *ast_parse_field_init(ParseContext *pc) {
20252025 if (first == nullptr)
20262026 return nullptr;
20272027
2028 Token *name = expect_token(pc, TokenIdSymbol);
2028 Token *name = eat_token_if(pc, TokenIdSymbol);
2029 if (name == nullptr) {
2030 // Because of anon literals ".{" is also valid.
2031 put_back_token(pc);
2032 return nullptr;
2033 }
20292034 if (eat_token_if(pc, TokenIdEq) == nullptr) {
20302035 // Because ".Name" can also be intepreted as an enum literal, we should put back
20312036 // those two tokens again so that the parser can try to parse them as the enum
test/stage1/behavior/array.zig+21
......@@ -312,3 +312,24 @@ test "anonymous list literal syntax" {
312312 S.doTheTest();
313313 comptime S.doTheTest();
314314}
315
316test "anonymous literal in array" {
317 const S = struct {
318 const Foo = struct {
319 a: usize = 2,
320 b: usize = 4,
321 };
322 fn doTheTest() void {
323 var array: [2]Foo = .{
324 .{.a = 3},
325 .{.b = 3},
326 };
327 expect(array[0].a == 3);
328 expect(array[0].b == 4);
329 expect(array[1].a == 2);
330 expect(array[1].b == 3);
331 }
332 };
333 S.doTheTest();
334 comptime S.doTheTest();
335}