authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-05 19:10:14+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-05 11:36:19-08:00
log0f3fa4d6540af42552571bf46609ab5546c16b72
treea3b3b62e70f907744bd87626209b5c0860247e7e
parent6f3b93e2e8e5cc8c98d473bef4804c61abc4a196

zig fmt: array types


3 files changed, 111 insertions(+), 65 deletions(-)

lib/std/zig/ast.zig+40-2
...@@ -246,6 +246,8 @@ pub const Tree = struct {...@@ -246,6 +246,8 @@ pub const Tree = struct {
246 .FnProtoMulti,246 .FnProtoMulti,
247 .FnProtoOne,247 .FnProtoOne,
248 .FnProto,248 .FnProto,
249 .ArrayType,
250 .ArrayTypeSentinel,
249 => return main_tokens[n],251 => return main_tokens[n],
250252
251 .ArrayInitDot,253 .ArrayInitDot,
...@@ -363,8 +365,6 @@ pub const Tree = struct {...@@ -363,8 +365,6 @@ pub const Tree = struct {
363 }365 }
364 },366 },
365367
366 .ArrayType => unreachable, // TODO
367 .ArrayTypeSentinel => unreachable, // TODO
368 .PtrTypeAligned => unreachable, // TODO368 .PtrTypeAligned => unreachable, // TODO
369 .PtrTypeSentinel => unreachable, // TODO369 .PtrTypeSentinel => unreachable, // TODO
370 .PtrType => unreachable, // TODO370 .PtrType => unreachable, // TODO
...@@ -925,6 +925,33 @@ pub const Tree = struct {...@@ -925,6 +925,33 @@ pub const Tree = struct {
925 };925 };
926 }926 }
927927
928 pub fn arrayType(tree: Tree, node: Node.Index) Full.ArrayType {
929 assert(tree.nodes.items(.tag)[node] == .ArrayType);
930 const data = tree.nodes.items(.data)[node];
931 return .{
932 .ast = .{
933 .lbracket = tree.nodes.items(.main_token)[node],
934 .elem_count = data.lhs,
935 .sentinel = null,
936 .elem_type = data.rhs,
937 },
938 };
939 }
940
941 pub fn arrayTypeSentinel(tree: Tree, node: Node.Index) Full.ArrayType {
942 assert(tree.nodes.items(.tag)[node] == .ArrayTypeSentinel);
943 const data = tree.nodes.items(.data)[node];
944 const extra = tree.extraData(data.rhs, Node.ArrayTypeSentinel);
945 return .{
946 .ast = .{
947 .lbracket = tree.nodes.items(.main_token)[node],
948 .elem_count = data.lhs,
949 .sentinel = extra.sentinel,
950 .elem_type = extra.elem_type,
951 },
952 };
953 }
954
928 fn fullVarDecl(tree: Tree, info: Full.VarDecl.Ast) Full.VarDecl {955 fn fullVarDecl(tree: Tree, info: Full.VarDecl.Ast) Full.VarDecl {
929 const token_tags = tree.tokens.items(.tag);956 const token_tags = tree.tokens.items(.tag);
930 var result: Full.VarDecl = .{957 var result: Full.VarDecl = .{
...@@ -1087,6 +1114,17 @@ pub const Full = struct {...@@ -1087,6 +1114,17 @@ pub const Full = struct {
1087 type_expr: Node.Index,1114 type_expr: Node.Index,
1088 };1115 };
1089 };1116 };
1117
1118 pub const ArrayType = struct {
1119 ast: Ast,
1120
1121 pub const Ast = struct {
1122 lbracket: TokenIndex,
1123 elem_count: Node.Index,
1124 sentinel: ?Node.Index,
1125 elem_type: Node.Index,
1126 };
1127 };
1090};1128};
10911129
1092pub const Error = union(enum) {1130pub const Error = union(enum) {
lib/std/zig/parser_test.zig+58
...@@ -490,6 +490,64 @@ test "zig fmt: anon list literal 3 element comma" {...@@ -490,6 +490,64 @@ test "zig fmt: anon list literal 3 element comma" {
490 );490 );
491}491}
492492
493test "zig fmt: array literal 1 element" {
494 try testCanonical(
495 \\const x = [_]u32{a};
496 \\
497 );
498}
499
500test "zig fmt: array literal 1 element comma" {
501 try testCanonical(
502 \\const x = [1]u32{
503 \\ a,
504 \\};
505 \\
506 );
507}
508
509test "zig fmt: array literal 2 element" {
510 try testCanonical(
511 \\const x = [_]u32{ a, b };
512 \\
513 );
514}
515
516test "zig fmt: array literal 2 element comma" {
517 try testCanonical(
518 \\const x = [2]u32{
519 \\ a,
520 \\ b,
521 \\};
522 \\
523 );
524}
525
526test "zig fmt: array literal 3 element" {
527 try testCanonical(
528 \\const x = [_]u32{ a, b, c };
529 \\
530 );
531}
532
533test "zig fmt: array literal 3 element comma" {
534 try testCanonical(
535 \\const x = [3]u32{
536 \\ a,
537 \\ b,
538 \\ c,
539 \\};
540 \\
541 );
542}
543
544test "zig fmt: sentinel array literal 1 element" {
545 try testCanonical(
546 \\const x = [_:9000]u32{a};
547 \\
548 );
549}
550
493//test "zig fmt: async function" {551//test "zig fmt: async function" {
494// try testCanonical(552// try testCanonical(
495// \\pub const Server = struct {553// \\pub const Server = struct {
lib/std/zig/render.zig+13-63
...@@ -359,34 +359,8 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac...@@ -359,34 +359,8 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
359 return renderExpression(ais, tree, datas[node].lhs, space);359 return renderExpression(ais, tree, datas[node].lhs, space);
360 },360 },
361361
362 .ArrayType => unreachable, // TODO362 .ArrayType => return renderArrayType(ais, tree, tree.arrayType(node), space),
363 //.ArrayType => {363 .ArrayTypeSentinel => return renderArrayType(ais, tree, tree.arrayTypeSentinel(node), space),
364 // const array_type = @fieldParentPtr(ast.Node.ArrayType, "base", base);
365 // return renderArrayType(
366 // allocator,
367 // ais,
368 // tree,
369 // array_type.op_token,
370 // array_type.rhs,
371 // array_type.len_expr,
372 // null,
373 // space,
374 // );
375 //},
376 .ArrayTypeSentinel => unreachable, // TODO
377 //.ArrayTypeSentinel => {
378 // const array_type = @fieldParentPtr(ast.Node.ArrayTypeSentinel, "base", base);
379 // return renderArrayType(
380 // allocator,
381 // ais,
382 // tree,
383 // array_type.op_token,
384 // array_type.rhs,
385 // array_type.len_expr,
386 // array_type.sentinel,
387 // space,
388 // );
389 //},
390364
391 .PtrType => unreachable, // TODO365 .PtrType => unreachable, // TODO
392 .PtrTypeAligned => unreachable, // TODO366 .PtrTypeAligned => unreachable, // TODO
...@@ -1279,47 +1253,21 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac...@@ -1279,47 +1253,21 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
1279 }1253 }
1280}1254}
12811255
1256// TODO: handle comments inside the brackets
1282fn renderArrayType(1257fn renderArrayType(
1283 allocator: *mem.Allocator,
1284 ais: *Ais,1258 ais: *Ais,
1285 tree: ast.Tree,1259 tree: ast.Tree,
1286 lbracket: ast.TokenIndex,1260 array_type: ast.Full.ArrayType,
1287 rhs: ast.Node.Index,
1288 len_expr: ast.Node.Index,
1289 opt_sentinel: ?ast.Node.Index,
1290 space: Space,1261 space: Space,
1291) Error!void {1262) Error!void {
1292 const rbracket = tree.nextToken(if (opt_sentinel) |sentinel|1263 try renderToken(ais, tree, array_type.ast.lbracket, .None); // lbracket
1293 sentinel.lastToken()1264 try renderExpression(ais, tree, array_type.ast.elem_count, .None);
1294 else1265 if (array_type.ast.sentinel) |sentinel| {
1295 len_expr.lastToken());1266 try renderToken(ais, tree, tree.firstToken(sentinel) - 1, .None); // colon
12961267 try renderExpression(ais, tree, sentinel, .None);
1297 const starts_with_comment = tree.token_tags[lbracket + 1] == .LineComment;
1298 const ends_with_comment = tree.token_tags[rbracket - 1] == .LineComment;
1299 const new_space = if (ends_with_comment) Space.Newline else Space.None;
1300 {
1301 const do_indent = (starts_with_comment or ends_with_comment);
1302 if (do_indent) ais.pushIndent();
1303 defer if (do_indent) ais.popIndent();
1304
1305 try renderToken(ais, tree, lbracket, Space.None); // [
1306 try renderExpression(ais, tree, len_expr, new_space);
1307
1308 if (starts_with_comment) {
1309 try ais.maybeInsertNewline();
1310 }
1311 if (opt_sentinel) |sentinel| {
1312 const colon_token = tree.prevToken(sentinel.firstToken());
1313 try renderToken(ais, tree, colon_token, Space.None); // :
1314 try renderExpression(ais, tree, sentinel, Space.None);
1315 }
1316 if (starts_with_comment) {
1317 try ais.maybeInsertNewline();
1318 }
1319 }1268 }
1320 try renderToken(ais, tree, rbracket, Space.None); // ]1269 try renderToken(ais, tree, tree.firstToken(array_type.ast.elem_type) - 1, .None); // rbracket
13211270 return renderExpression(ais, tree, array_type.ast.elem_type, space);
1322 return renderExpression(ais, tree, rhs, space);
1323}1271}
13241272
1325fn renderAsmOutput(1273fn renderAsmOutput(
...@@ -1900,6 +1848,7 @@ fn renderBlock(...@@ -1900,6 +1848,7 @@ fn renderBlock(
1900 }1848 }
1901}1849}
19021850
1851// TODO: handle comments between fields
1903fn renderStructInit(1852fn renderStructInit(
1904 ais: *Ais,1853 ais: *Ais,
1905 tree: ast.Tree,1854 tree: ast.Tree,
...@@ -1953,6 +1902,7 @@ fn renderStructInit(...@@ -1953,6 +1902,7 @@ fn renderStructInit(
1953 }1902 }
1954}1903}
19551904
1905// TODO: handle comments between elements
1956fn renderArrayInit(1906fn renderArrayInit(
1957 ais: *Ais,1907 ais: *Ais,
1958 tree: ast.Tree,1908 tree: ast.Tree,