authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-06 08:56:28+02:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-06 08:56:28+02:00
loge45de607d6437428d82f69711a8cc8f338d019c8
treea68bbc0aa4cf7f4a764de858314d490c48e70798
parent779247ba114492a28287bc2026719091b7022e1d

std.zig.parser: Initializers are now parsed and fmt correctly


1 files changed, 82 insertions(+), 45 deletions(-)

std/zig/parser.zig+82-45
...@@ -249,7 +249,7 @@ pub const Parser = struct {...@@ -249,7 +249,7 @@ pub const Parser = struct {
249 const name = try self.createStringLiteral(arena, name_token);249 const name = try self.createStringLiteral(arena, name_token);
250 const block = try self.createBlock(arena, token);250 const block = try self.createBlock(arena, token);
251 const test_decl = try self.createAttachTestDecl(arena, &root_node.decls, token, &name.base, block);251 const test_decl = try self.createAttachTestDecl(arena, &root_node.decls, token, &name.base, block);
252 try stack.append(State { .Block = block });252 stack.append(State { .Block = block }) catch unreachable;
253 continue;253 continue;
254 },254 },
255 Token.Id.Eof => {255 Token.Id.Eof => {
...@@ -318,14 +318,14 @@ pub const Parser = struct {...@@ -318,14 +318,14 @@ pub const Parser = struct {
318 // TODO shouldn't need these casts318 // TODO shouldn't need these casts
319 const var_decl_node = try self.createAttachVarDecl(arena, ctx.decls, ctx.visib_token,319 const var_decl_node = try self.createAttachVarDecl(arena, ctx.decls, ctx.visib_token,
320 token, (?Token)(null), ctx.extern_token, ctx.lib_name);320 token, (?Token)(null), ctx.extern_token, ctx.lib_name);
321 try stack.append(State { .VarDecl = var_decl_node });321 stack.append(State { .VarDecl = var_decl_node }) catch unreachable;
322 continue;322 continue;
323 },323 },
324 Token.Id.Keyword_fn => {324 Token.Id.Keyword_fn => {
325 // TODO shouldn't need these casts325 // TODO shouldn't need these casts
326 const fn_proto = try self.createAttachFnProto(arena, ctx.decls, token,326 const fn_proto = try self.createAttachFnProto(arena, ctx.decls, token,
327 ctx.extern_token, ctx.lib_name, (?Token)(null), ctx.visib_token, (?Token)(null));327 ctx.extern_token, ctx.lib_name, (?Token)(null), ctx.visib_token, (?Token)(null));
328 try stack.append(State { .FnDef = fn_proto });328 stack.append(State { .FnDef = fn_proto }) catch unreachable;
329 try stack.append(State { .FnProto = fn_proto });329 try stack.append(State { .FnProto = fn_proto });
330 continue;330 continue;
331 },331 },
...@@ -333,7 +333,7 @@ pub const Parser = struct {...@@ -333,7 +333,7 @@ pub const Parser = struct {
333 // TODO shouldn't need this cast333 // TODO shouldn't need this cast
334 const fn_proto = try self.createAttachFnProto(arena, ctx.decls, undefined,334 const fn_proto = try self.createAttachFnProto(arena, ctx.decls, undefined,
335 ctx.extern_token, ctx.lib_name, (?Token)(token), (?Token)(null), (?Token)(null));335 ctx.extern_token, ctx.lib_name, (?Token)(token), (?Token)(null), (?Token)(null));
336 try stack.append(State { .FnDef = fn_proto });336 stack.append(State { .FnDef = fn_proto }) catch unreachable;
337 try stack.append(State { .FnProto = fn_proto });337 try stack.append(State { .FnProto = fn_proto });
338 try stack.append(State {338 try stack.append(State {
339 .ExpectTokenSave = ExpectTokenSave {339 .ExpectTokenSave = ExpectTokenSave {
...@@ -425,7 +425,7 @@ pub const Parser = struct {...@@ -425,7 +425,7 @@ pub const Parser = struct {
425 };425 };
426 ctx.dest_ptr.store(&node.base);426 ctx.dest_ptr.store(&node.base);
427427
428 try stack.append(State { .ContainerDecl = node });428 stack.append(State { .ContainerDecl = node }) catch unreachable;
429 try stack.append(State { .ExpectToken = Token.Id.LBrace });429 try stack.append(State { .ExpectToken = Token.Id.LBrace });
430430
431 const lparen = self.getNextToken();431 const lparen = self.getNextToken();
...@@ -470,7 +470,7 @@ pub const Parser = struct {...@@ -470,7 +470,7 @@ pub const Parser = struct {
470 };470 };
471 try container_decl.fields_and_decls.append(&node.base);471 try container_decl.fields_and_decls.append(&node.base);
472472
473 try stack.append(State { .FieldListCommaOrEnd = container_decl });473 stack.append(State { .FieldListCommaOrEnd = container_decl }) catch unreachable;
474 try stack.append(State { .Expression = DestPtr { .Field = &node.type_expr } });474 try stack.append(State { .Expression = DestPtr { .Field = &node.type_expr } });
475 try stack.append(State { .ExpectToken = Token.Id.Colon });475 try stack.append(State { .ExpectToken = Token.Id.Colon });
476 continue;476 continue;
...@@ -484,7 +484,7 @@ pub const Parser = struct {...@@ -484,7 +484,7 @@ pub const Parser = struct {
484 };484 };
485 try container_decl.fields_and_decls.append(&node.base);485 try container_decl.fields_and_decls.append(&node.base);
486486
487 try stack.append(State { .FieldListCommaOrEnd = container_decl });487 stack.append(State { .FieldListCommaOrEnd = container_decl }) catch unreachable;
488488
489 const next = self.getNextToken();489 const next = self.getNextToken();
490 if (next.id != Token.Id.Colon) {490 if (next.id != Token.Id.Colon) {
...@@ -504,7 +504,7 @@ pub const Parser = struct {...@@ -504,7 +504,7 @@ pub const Parser = struct {
504 };504 };
505 try container_decl.fields_and_decls.append(&node.base);505 try container_decl.fields_and_decls.append(&node.base);
506506
507 try stack.append(State { .FieldListCommaOrEnd = container_decl });507 stack.append(State { .FieldListCommaOrEnd = container_decl }) catch unreachable;
508508
509 const next = self.getNextToken();509 const next = self.getNextToken();
510 if (next.id != Token.Id.Equal) {510 if (next.id != Token.Id.Equal) {
...@@ -629,8 +629,8 @@ pub const Parser = struct {...@@ -629,8 +629,8 @@ pub const Parser = struct {
629 },629 },
630630
631 State.AssignmentExpressionBegin => |dest_ptr| {631 State.AssignmentExpressionBegin => |dest_ptr| {
632 try stack.append(State { .AssignmentExpressionEnd = dest_ptr });632 stack.append(State { .AssignmentExpressionEnd = dest_ptr }) catch unreachable;
633 stack.append(State { .UnwrapExpressionBegin = dest_ptr }) catch unreachable;633 try stack.append(State { .UnwrapExpressionBegin = dest_ptr });
634 continue;634 continue;
635 },635 },
636636
...@@ -926,7 +926,6 @@ pub const Parser = struct {...@@ -926,7 +926,6 @@ pub const Parser = struct {
926 }926 }
927927
928 const next = self.getNextToken();928 const next = self.getNextToken();
929 self.putBackToken(token);
930 switch (next.id) {929 switch (next.id) {
931 Token.Id.Period => {930 Token.Id.Period => {
932 const node = try self.createSuffixOp(arena, ast.NodeSuffixOp.SuffixOp {931 const node = try self.createSuffixOp(arena, ast.NodeSuffixOp.SuffixOp {
...@@ -943,6 +942,7 @@ pub const Parser = struct {...@@ -943,6 +942,7 @@ pub const Parser = struct {
943 .ptr = &node.rtoken,942 .ptr = &node.rtoken,
944 }943 }
945 });944 });
945 self.putBackToken(next);
946 continue;946 continue;
947 },947 },
948 else => {948 else => {
...@@ -960,6 +960,7 @@ pub const Parser = struct {...@@ -960,6 +960,7 @@ pub const Parser = struct {
960 .ptr = &node.rtoken,960 .ptr = &node.rtoken,
961 }961 }
962 });962 });
963 self.putBackToken(next);
963 continue;964 continue;
964 },965 },
965 }966 }
...@@ -1180,12 +1181,12 @@ pub const Parser = struct {...@@ -1180,12 +1181,12 @@ pub const Parser = struct {
1180 .rparen = undefined,1181 .rparen = undefined,
1181 };1182 };
1182 dest_ptr.store(&node.base);1183 dest_ptr.store(&node.base);
1183 try stack.append(State {1184 stack.append(State {
1184 .ExpectTokenSave = ExpectTokenSave {1185 .ExpectTokenSave = ExpectTokenSave {
1185 .id = Token.Id.RParen,1186 .id = Token.Id.RParen,
1186 .ptr = &node.rparen,1187 .ptr = &node.rparen,
1187 }1188 }
1188 });1189 }) catch unreachable;
1189 try stack.append(State { .Expression = DestPtr { .Field = &node.expr } });1190 try stack.append(State { .Expression = DestPtr { .Field = &node.expr } });
1190 continue;1191 continue;
1191 },1192 },
...@@ -1202,17 +1203,18 @@ pub const Parser = struct {...@@ -1202,17 +1203,18 @@ pub const Parser = struct {
1202 .rparen_token = undefined,1203 .rparen_token = undefined,
1203 };1204 };
1204 dest_ptr.store(&node.base);1205 dest_ptr.store(&node.base);
1205 try stack.append(State {1206 stack.append(State {
1206 .ExprListItemOrEnd = ListState(&ast.Node) {1207 .ExprListItemOrEnd = ListState(&ast.Node) {
1207 .list = &node.params,1208 .list = &node.params,
1208 .end = Token.Id.RParen,1209 .end = Token.Id.RParen,
1209 .ptr = &node.rparen_token,1210 .ptr = &node.rparen_token,
1210 }1211 }
1211 });1212 }) catch unreachable;
1212 try stack.append(State { .ExpectToken = Token.Id.LParen, });1213 try stack.append(State { .ExpectToken = Token.Id.LParen, });
1213 continue;1214 continue;
1214 },1215 },
1215 Token.Id.LBracket => {1216 Token.Id.LBracket => {
1217 // TODO: option("align" "(" Expression option(":" Integer ":" Integer) ")")) option("const") option("volatile")
1216 const rbracket_token = self.getNextToken();1218 const rbracket_token = self.getNextToken();
1217 if (rbracket_token.id == Token.Id.RBracket) {1219 if (rbracket_token.id == Token.Id.RBracket) {
1218 const node = try self.createPrefixOp(arena, token, ast.NodePrefixOp.PrefixOp{1220 const node = try self.createPrefixOp(arena, token, ast.NodePrefixOp.PrefixOp{
...@@ -1225,7 +1227,8 @@ pub const Parser = struct {...@@ -1225,7 +1227,8 @@ pub const Parser = struct {
1225 }1227 }
1226 });1228 });
1227 dest_ptr.store(&node.base);1229 dest_ptr.store(&node.base);
1228 try stack.append(State { .AddrOfModifiers = &node.op.AddrOf });1230 stack.append(State { .Expression = DestPtr { .Field = &node.rhs } }) catch unreachable;
1231 try stack.append(State { .AddrOfModifiers = &node.op.SliceType });
1229 continue;1232 continue;
1230 }1233 }
12311234
...@@ -1235,6 +1238,7 @@ pub const Parser = struct {...@@ -1235,6 +1238,7 @@ pub const Parser = struct {
1235 .ArrayType = undefined,1238 .ArrayType = undefined,
1236 });1239 });
1237 dest_ptr.store(&node.base);1240 dest_ptr.store(&node.base);
1241 stack.append(State { .Expression = DestPtr { .Field = &node.rhs } }) catch unreachable;
1238 try stack.append(State { .ExpectToken = Token.Id.RBracket });1242 try stack.append(State { .ExpectToken = Token.Id.RBracket });
1239 try stack.append(State { .Expression = DestPtr { .Field = &node.op.ArrayType } });1243 try stack.append(State { .Expression = DestPtr { .Field = &node.op.ArrayType } });
12401244
...@@ -1302,32 +1306,32 @@ pub const Parser = struct {...@@ -1302,32 +1306,32 @@ pub const Parser = struct {
1302 continue;1306 continue;
1303 },1307 },
1304 Token.Id.Keyword_packed => {1308 Token.Id.Keyword_packed => {
1305 try stack.append(State {1309 stack.append(State {
1306 .ContainerExtern = ContainerExternCtx {1310 .ContainerExtern = ContainerExternCtx {
1307 .dest_ptr = dest_ptr,1311 .dest_ptr = dest_ptr,
1308 .ltoken = token,1312 .ltoken = token,
1309 .layout = ast.NodeContainerDecl.Layout.Packed,1313 .layout = ast.NodeContainerDecl.Layout.Packed,
1310 },1314 },
1311 });1315 }) catch unreachable;
1312 },1316 },
1313 Token.Id.Keyword_extern => {1317 Token.Id.Keyword_extern => {
1314 try stack.append(State {1318 stack.append(State {
1315 .ContainerExtern = ContainerExternCtx {1319 .ContainerExtern = ContainerExternCtx {
1316 .dest_ptr = dest_ptr,1320 .dest_ptr = dest_ptr,
1317 .ltoken = token,1321 .ltoken = token,
1318 .layout = ast.NodeContainerDecl.Layout.Extern,1322 .layout = ast.NodeContainerDecl.Layout.Extern,
1319 },1323 },
1320 });1324 }) catch unreachable;
1321 },1325 },
1322 Token.Id.Keyword_struct, Token.Id.Keyword_union, Token.Id.Keyword_enum => {1326 Token.Id.Keyword_struct, Token.Id.Keyword_union, Token.Id.Keyword_enum => {
1323 self.putBackToken(token);1327 self.putBackToken(token);
1324 try stack.append(State {1328 stack.append(State {
1325 .ContainerExtern = ContainerExternCtx {1329 .ContainerExtern = ContainerExternCtx {
1326 .dest_ptr = dest_ptr,1330 .dest_ptr = dest_ptr,
1327 .ltoken = token,1331 .ltoken = token,
1328 .layout = ast.NodeContainerDecl.Layout.Auto,1332 .layout = ast.NodeContainerDecl.Layout.Auto,
1329 },1333 },
1330 });1334 }) catch unreachable;
1331 },1335 },
1332 Token.Id.LBrace => {1336 Token.Id.LBrace => {
1333 @panic("TODO: Block expr");1337 @panic("TODO: Block expr");
...@@ -1361,12 +1365,12 @@ pub const Parser = struct {...@@ -1361,12 +1365,12 @@ pub const Parser = struct {
1361 const rbracket_token = self.getNextToken();1365 const rbracket_token = self.getNextToken();
1362 if (rbracket_token.id != Token.Id.RBracket) {1366 if (rbracket_token.id != Token.Id.RBracket) {
1363 self.putBackToken(rbracket_token);1367 self.putBackToken(rbracket_token);
1364 try stack.append(State {1368 stack.append(State {
1365 .ExpectTokenSave = ExpectTokenSave {1369 .ExpectTokenSave = ExpectTokenSave {
1366 .id = Token.Id.RBracket,1370 .id = Token.Id.RBracket,
1367 .ptr = &node.rtoken,1371 .ptr = &node.rtoken,
1368 }1372 }
1369 });1373 }) catch unreachable;
1370 try stack.append(State { .Expression = DestPtr { .NullableField = &node.op.Slice.end } });1374 try stack.append(State { .Expression = DestPtr { .NullableField = &node.op.Slice.end } });
1371 } else {1375 } else {
1372 node.rtoken = rbracket_token;1376 node.rtoken = rbracket_token;
...@@ -1638,7 +1642,7 @@ pub const Parser = struct {...@@ -1638,7 +1642,7 @@ pub const Parser = struct {
1638 // TODO shouldn't need these casts1642 // TODO shouldn't need these casts
1639 const var_decl = try self.createAttachVarDecl(arena, &block.statements, (?Token)(null),1643 const var_decl = try self.createAttachVarDecl(arena, &block.statements, (?Token)(null),
1640 mut_token, (?Token)(comptime_token), (?Token)(null), null);1644 mut_token, (?Token)(comptime_token), (?Token)(null), null);
1641 try stack.append(State { .VarDecl = var_decl });1645 stack.append(State { .VarDecl = var_decl }) catch unreachable;
1642 continue;1646 continue;
1643 }1647 }
1644 self.putBackToken(mut_token);1648 self.putBackToken(mut_token);
...@@ -1652,7 +1656,7 @@ pub const Parser = struct {...@@ -1652,7 +1656,7 @@ pub const Parser = struct {
1652 // TODO shouldn't need these casts1656 // TODO shouldn't need these casts
1653 const var_decl = try self.createAttachVarDecl(arena, &block.statements, (?Token)(null),1657 const var_decl = try self.createAttachVarDecl(arena, &block.statements, (?Token)(null),
1654 mut_token, (?Token)(null), (?Token)(null), null);1658 mut_token, (?Token)(null), (?Token)(null), null);
1655 try stack.append(State { .VarDecl = var_decl });1659 stack.append(State { .VarDecl = var_decl }) catch unreachable;
1656 continue;1660 continue;
1657 }1661 }
1658 self.putBackToken(mut_token);1662 self.putBackToken(mut_token);
...@@ -2132,6 +2136,7 @@ pub const Parser = struct {...@@ -2132,6 +2136,7 @@ pub const Parser = struct {
2132 Expression: &ast.Node,2136 Expression: &ast.Node,
2133 VarDecl: &ast.NodeVarDecl,2137 VarDecl: &ast.NodeVarDecl,
2134 Statement: &ast.Node,2138 Statement: &ast.Node,
2139 FieldInitializer: &ast.NodeFieldInitializer,
2135 PrintIndent,2140 PrintIndent,
2136 Indent: usize,2141 Indent: usize,
2137 };2142 };
...@@ -2246,6 +2251,12 @@ pub const Parser = struct {...@@ -2246,6 +2251,12 @@ pub const Parser = struct {
2246 }2251 }
2247 },2252 },
22482253
2254 RenderState.FieldInitializer => |field_init| {
2255 try stream.print(".{}", self.tokenizer.getTokenSlice(field_init.name_token));
2256 try stream.print(" = ");
2257 try stack.append(RenderState { .Expression = field_init.expr });
2258 },
2259
2249 RenderState.VarDecl => |var_decl| {2260 RenderState.VarDecl => |var_decl| {
2250 try stack.append(RenderState { .Text = ";" });2261 try stack.append(RenderState { .Text = ";" });
2251 if (var_decl.init_node) |init_node| {2262 if (var_decl.init_node) |init_node| {
...@@ -2481,8 +2492,34 @@ pub const Parser = struct {...@@ -2481,8 +2492,34 @@ pub const Parser = struct {
2481 try stack.append(RenderState { .Expression = range.start});2492 try stack.append(RenderState { .Expression = range.start});
2482 try stack.append(RenderState { .Text = "["});2493 try stack.append(RenderState { .Text = "["});
2483 },2494 },
2484 ast.NodeSuffixOp.SuffixOp.StructInitializer => @panic("TODO: StructInitializer"),2495 ast.NodeSuffixOp.SuffixOp.StructInitializer => |field_inits| {
2485 ast.NodeSuffixOp.SuffixOp.ArrayInitializer => @panic("TODO: ArrayInitializer"),2496 try stack.append(RenderState { .Text = " }"});
2497 var i = field_inits.len;
2498 while (i != 0) {
2499 i -= 1;
2500 const field_init = field_inits.at(i);
2501 try stack.append(RenderState { .FieldInitializer = field_init });
2502 try stack.append(RenderState { .Text = " " });
2503 if (i != 0) {
2504 try stack.append(RenderState { .Text = "," });
2505 }
2506 }
2507 try stack.append(RenderState { .Text = "{"});
2508 },
2509 ast.NodeSuffixOp.SuffixOp.ArrayInitializer => |exprs| {
2510 try stack.append(RenderState { .Text = " }"});
2511 var i = exprs.len;
2512 while (i != 0) {
2513 i -= 1;
2514 const expr = exprs.at(i);
2515 try stack.append(RenderState { .Expression = expr });
2516 try stack.append(RenderState { .Text = " " });
2517 if (i != 0) {
2518 try stack.append(RenderState { .Text = "," });
2519 }
2520 }
2521 try stack.append(RenderState { .Text = "{"});
2522 },
2486 }2523 }
24872524
2488 try stack.append(RenderState { .Expression = suffix_op.lhs });2525 try stack.append(RenderState { .Expression = suffix_op.lhs });
...@@ -3011,6 +3048,10 @@ test "zig fmt: precedence" {...@@ -3011,6 +3048,10 @@ test "zig fmt: precedence" {
3011 \\ (a!b)();3048 \\ (a!b)();
3012 \\ !a!b;3049 \\ !a!b;
3013 \\ !(a!b);3050 \\ !(a!b);
3051 \\ !a{ };
3052 \\ !(a{ });
3053 \\ a + b{ };
3054 \\ (a + b){ };
3014 \\ a << b + c;3055 \\ a << b + c;
3015 \\ (a << b) + c;3056 \\ (a << b) + c;
3016 \\ a & b << c;3057 \\ a & b << c;
...@@ -3030,10 +3071,6 @@ test "zig fmt: precedence" {...@@ -3030,10 +3071,6 @@ test "zig fmt: precedence" {
3030 \\ (a = b) or c;3071 \\ (a = b) or c;
3031 \\}3072 \\}
3032 \\3073 \\
3033 //\\ !a{};
3034 //\\ !(a{});
3035 //\\ a + b{};
3036 //\\ (a + b){};
3037 );3074 );
3038}3075}
30393076
...@@ -3233,23 +3270,12 @@ test "zig fmt: error set declaration" {...@@ -3233,23 +3270,12 @@ test "zig fmt: error set declaration" {
3233 );3270 );
3234}3271}
32353272
3236test "zig fmt: catch" {
3237 try testCanonical(
3238 \\test "catch" {
3239 \\ const a: error!u8 = 0;
3240 \\ _ = a catch return;
3241 \\ _ = a catch |err| return;
3242 \\}
3243 \\
3244 );
3245}
3246
3247test "zig fmt: arrays" {3273test "zig fmt: arrays" {
3248 try testCanonical(3274 try testCanonical(
3249 \\test "test array" {3275 \\test "test array" {
3250 \\ const a: [2]u8 = [2]u8{ 1, 2 };3276 \\ const a: [2]u8 = [2]u8{ 1, 2 };
3251 \\ const a: [2]u8 = []u8{ 1, 2 };3277 \\ const a: [2]u8 = []u8{ 1, 2 };
3252 \\ const a: [0]u8 = []u8{};3278 \\ const a: [0]u8 = []u8{ };
3253 \\}3279 \\}
3254 \\3280 \\
3255 );3281 );
...@@ -3260,7 +3286,18 @@ test "zig fmt: container initializers" {...@@ -3260,7 +3286,18 @@ test "zig fmt: container initializers" {
3260 \\const a1 = []u8{ };3286 \\const a1 = []u8{ };
3261 \\const a2 = []u8{ 1, 2, 3, 4 };3287 \\const a2 = []u8{ 1, 2, 3, 4 };
3262 \\const s1 = S{ };3288 \\const s1 = S{ };
3263 \\const s2 = S{ .a = 1, .b = 2, };3289 \\const s2 = S{ .a = 1, .b = 2 };
3290 \\
3291 );
3292}
3293
3294test "zig fmt: catch" {
3295 try testCanonical(
3296 \\test "catch" {
3297 \\ const a: error!u8 = 0;
3298 \\ _ = a catch return;
3299 \\ _ = a catch |err| return;
3300 \\}
3264 \\3301 \\
3265 );3302 );
3266}3303}