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 {
246246 .FnProtoMulti,
247247 .FnProtoOne,
248248 .FnProto,
249 .ArrayType,
250 .ArrayTypeSentinel,
249251 => return main_tokens[n],
250252
251253 .ArrayInitDot,
......@@ -363,8 +365,6 @@ pub const Tree = struct {
363365 }
364366 },
365367
366 .ArrayType => unreachable, // TODO
367 .ArrayTypeSentinel => unreachable, // TODO
368368 .PtrTypeAligned => unreachable, // TODO
369369 .PtrTypeSentinel => unreachable, // TODO
370370 .PtrType => unreachable, // TODO
......@@ -925,6 +925,33 @@ pub const Tree = struct {
925925 };
926926 }
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
928955 fn fullVarDecl(tree: Tree, info: Full.VarDecl.Ast) Full.VarDecl {
929956 const token_tags = tree.tokens.items(.tag);
930957 var result: Full.VarDecl = .{
......@@ -1087,6 +1114,17 @@ pub const Full = struct {
10871114 type_expr: Node.Index,
10881115 };
10891116 };
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 };
10901128};
10911129
10921130pub const Error = union(enum) {
lib/std/zig/parser_test.zig+58
......@@ -490,6 +490,64 @@ test "zig fmt: anon list literal 3 element comma" {
490490 );
491491}
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
493551//test "zig fmt: async function" {
494552// try testCanonical(
495553// \\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
359359 return renderExpression(ais, tree, datas[node].lhs, space);
360360 },
361361
362 .ArrayType => unreachable, // TODO
363 //.ArrayType => {
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 //},
362 .ArrayType => return renderArrayType(ais, tree, tree.arrayType(node), space),
363 .ArrayTypeSentinel => return renderArrayType(ais, tree, tree.arrayTypeSentinel(node), space),
390364
391365 .PtrType => unreachable, // TODO
392366 .PtrTypeAligned => unreachable, // TODO
......@@ -1279,47 +1253,21 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
12791253 }
12801254}
12811255
1256// TODO: handle comments inside the brackets
12821257fn renderArrayType(
1283 allocator: *mem.Allocator,
12841258 ais: *Ais,
12851259 tree: ast.Tree,
1286 lbracket: ast.TokenIndex,
1287 rhs: ast.Node.Index,
1288 len_expr: ast.Node.Index,
1289 opt_sentinel: ?ast.Node.Index,
1260 array_type: ast.Full.ArrayType,
12901261 space: Space,
12911262) Error!void {
1292 const rbracket = tree.nextToken(if (opt_sentinel) |sentinel|
1293 sentinel.lastToken()
1294 else
1295 len_expr.lastToken());
1296
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 }
1263 try renderToken(ais, tree, array_type.ast.lbracket, .None); // lbracket
1264 try renderExpression(ais, tree, array_type.ast.elem_count, .None);
1265 if (array_type.ast.sentinel) |sentinel| {
1266 try renderToken(ais, tree, tree.firstToken(sentinel) - 1, .None); // colon
1267 try renderExpression(ais, tree, sentinel, .None);
13191268 }
1320 try renderToken(ais, tree, rbracket, Space.None); // ]
1321
1322 return renderExpression(ais, tree, rhs, space);
1269 try renderToken(ais, tree, tree.firstToken(array_type.ast.elem_type) - 1, .None); // rbracket
1270 return renderExpression(ais, tree, array_type.ast.elem_type, space);
13231271}
13241272
13251273fn renderAsmOutput(
......@@ -1900,6 +1848,7 @@ fn renderBlock(
19001848 }
19011849}
19021850
1851// TODO: handle comments between fields
19031852fn renderStructInit(
19041853 ais: *Ais,
19051854 tree: ast.Tree,
......@@ -1953,6 +1902,7 @@ fn renderStructInit(
19531902 }
19541903}
19551904
1905// TODO: handle comments between elements
19561906fn renderArrayInit(
19571907 ais: *Ais,
19581908 tree: ast.Tree,