authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-30 20:25:54-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-30 20:25:54-04:00
log7c822869feac7713063da320c12a960f3ae58298
tree5f22a156388f344623e278b5f8dcd1fe2bcb4636
parent61a726c290a4e569ae28da59c94ba6a40df59a20

zig fmt: only some docs have doc comments


3 files changed, 125 insertions(+), 89 deletions(-)

std/zig/ast.zig+42-1
...@@ -6,7 +6,6 @@ const mem = std.mem;...@@ -6,7 +6,6 @@ const mem = std.mem;
66
7pub const Node = struct {7pub const Node = struct {
8 id: Id,8 id: Id,
9 doc_comments: ?&DocComment,
10 same_line_comment: ?&Token,9 same_line_comment: ?&Token,
1110
12 pub const Id = enum {11 pub const Id = enum {
...@@ -70,6 +69,7 @@ pub const Node = struct {...@@ -70,6 +69,7 @@ pub const Node = struct {
70 StructField,69 StructField,
71 UnionTag,70 UnionTag,
72 EnumTag,71 EnumTag,
72 ErrorTag,
73 AsmInput,73 AsmInput,
74 AsmOutput,74 AsmOutput,
75 AsyncAttribute,75 AsyncAttribute,
...@@ -77,6 +77,13 @@ pub const Node = struct {...@@ -77,6 +77,13 @@ pub const Node = struct {
77 FieldInitializer,77 FieldInitializer,
78 };78 };
7979
80 pub fn cast(base: &Node, comptime T: type) ?&T {
81 if (base.id == comptime typeToId(T)) {
82 return @fieldParentPtr(T, "base", base);
83 }
84 return null;
85 }
86
80 pub fn iterate(base: &Node, index: usize) ?&Node {87 pub fn iterate(base: &Node, index: usize) ?&Node {
81 comptime var i = 0;88 comptime var i = 0;
82 inline while (i < @memberCount(Id)) : (i += 1) {89 inline while (i < @memberCount(Id)) : (i += 1) {
...@@ -122,6 +129,7 @@ pub const Node = struct {...@@ -122,6 +129,7 @@ pub const Node = struct {
122129
123 pub const Root = struct {130 pub const Root = struct {
124 base: Node,131 base: Node,
132 doc_comments: ?&DocComment,
125 decls: ArrayList(&Node),133 decls: ArrayList(&Node),
126 eof_token: Token,134 eof_token: Token,
127135
...@@ -143,6 +151,7 @@ pub const Node = struct {...@@ -143,6 +151,7 @@ pub const Node = struct {
143151
144 pub const VarDecl = struct {152 pub const VarDecl = struct {
145 base: Node,153 base: Node,
154 doc_comments: ?&DocComment,
146 visib_token: ?Token,155 visib_token: ?Token,
147 name_token: Token,156 name_token: Token,
148 eq_token: Token,157 eq_token: Token,
...@@ -191,6 +200,7 @@ pub const Node = struct {...@@ -191,6 +200,7 @@ pub const Node = struct {
191200
192 pub const Use = struct {201 pub const Use = struct {
193 base: Node,202 base: Node,
203 doc_comments: ?&DocComment,
194 visib_token: ?Token,204 visib_token: ?Token,
195 expr: &Node,205 expr: &Node,
196 semicolon_token: Token,206 semicolon_token: Token,
...@@ -294,6 +304,7 @@ pub const Node = struct {...@@ -294,6 +304,7 @@ pub const Node = struct {
294304
295 pub const StructField = struct {305 pub const StructField = struct {
296 base: Node,306 base: Node,
307 doc_comments: ?&DocComment,
297 visib_token: ?Token,308 visib_token: ?Token,
298 name_token: Token,309 name_token: Token,
299 type_expr: &Node,310 type_expr: &Node,
...@@ -319,6 +330,7 @@ pub const Node = struct {...@@ -319,6 +330,7 @@ pub const Node = struct {
319330
320 pub const UnionTag = struct {331 pub const UnionTag = struct {
321 base: Node,332 base: Node,
333 doc_comments: ?&DocComment,
322 name_token: Token,334 name_token: Token,
323 type_expr: ?&Node,335 type_expr: ?&Node,
324 value_expr: ?&Node,336 value_expr: ?&Node,
...@@ -357,6 +369,7 @@ pub const Node = struct {...@@ -357,6 +369,7 @@ pub const Node = struct {
357369
358 pub const EnumTag = struct {370 pub const EnumTag = struct {
359 base: Node,371 base: Node,
372 doc_comments: ?&DocComment,
360 name_token: Token,373 name_token: Token,
361 value: ?&Node,374 value: ?&Node,
362375
...@@ -384,6 +397,31 @@ pub const Node = struct {...@@ -384,6 +397,31 @@ pub const Node = struct {
384 }397 }
385 };398 };
386399
400 pub const ErrorTag = struct {
401 base: Node,
402 doc_comments: ?&DocComment,
403 name_token: Token,
404
405 pub fn iterate(self: &ErrorTag, index: usize) ?&Node {
406 var i = index;
407
408 if (self.doc_comments) |comments| {
409 if (i < 1) return &comments.base;
410 i -= 1;
411 }
412
413 return null;
414 }
415
416 pub fn firstToken(self: &ErrorTag) Token {
417 return self.name_token;
418 }
419
420 pub fn lastToken(self: &ErrorTag) Token {
421 return self.name_token;
422 }
423 };
424
387 pub const Identifier = struct {425 pub const Identifier = struct {
388 base: Node,426 base: Node,
389 token: Token,427 token: Token,
...@@ -433,6 +471,7 @@ pub const Node = struct {...@@ -433,6 +471,7 @@ pub const Node = struct {
433471
434 pub const FnProto = struct {472 pub const FnProto = struct {
435 base: Node,473 base: Node,
474 doc_comments: ?&DocComment,
436 visib_token: ?Token,475 visib_token: ?Token,
437 fn_token: Token,476 fn_token: Token,
438 name_token: ?Token,477 name_token: ?Token,
...@@ -626,6 +665,7 @@ pub const Node = struct {...@@ -626,6 +665,7 @@ pub const Node = struct {
626665
627 pub const Comptime = struct {666 pub const Comptime = struct {
628 base: Node,667 base: Node,
668 doc_comments: ?&DocComment,
629 comptime_token: Token,669 comptime_token: Token,
630 expr: &Node,670 expr: &Node,
631671
...@@ -1794,6 +1834,7 @@ pub const Node = struct {...@@ -1794,6 +1834,7 @@ pub const Node = struct {
17941834
1795 pub const TestDecl = struct {1835 pub const TestDecl = struct {
1796 base: Node,1836 base: Node,
1837 doc_comments: ?&DocComment,
1797 test_token: Token,1838 test_token: Token,
1798 name: &Node,1839 name: &Node,
1799 body_node: &Node,1840 body_node: &Node,
std/zig/parser.zig+82-71
...@@ -228,7 +228,6 @@ pub const Parser = struct {...@@ -228,7 +228,6 @@ pub const Parser = struct {
228 Statement: &ast.Node.Block,228 Statement: &ast.Node.Block,
229 ComptimeStatement: ComptimeStatementCtx,229 ComptimeStatement: ComptimeStatementCtx,
230 Semicolon: &&ast.Node,230 Semicolon: &&ast.Node,
231 AddComments: AddCommentsCtx,
232 LookForSameLineComment: &&ast.Node,231 LookForSameLineComment: &&ast.Node,
233 LookForSameLineCommentDirect: &ast.Node,232 LookForSameLineCommentDirect: &ast.Node,
234233
...@@ -243,8 +242,8 @@ pub const Parser = struct {...@@ -243,8 +242,8 @@ pub const Parser = struct {
243 FieldInitListCommaOrEnd: ListSave(&ast.Node.FieldInitializer),242 FieldInitListCommaOrEnd: ListSave(&ast.Node.FieldInitializer),
244 FieldListCommaOrEnd: &ast.Node.ContainerDecl,243 FieldListCommaOrEnd: &ast.Node.ContainerDecl,
245 FieldInitValue: OptionalCtx,244 FieldInitValue: OptionalCtx,
246 IdentifierListItemOrEnd: ListSave(&ast.Node),245 ErrorTagListItemOrEnd: ListSave(&ast.Node),
247 IdentifierListCommaOrEnd: ListSave(&ast.Node),246 ErrorTagListCommaOrEnd: ListSave(&ast.Node),
248 SwitchCaseOrEnd: ListSave(&ast.Node),247 SwitchCaseOrEnd: ListSave(&ast.Node),
249 SwitchCaseCommaOrEnd: ListSave(&ast.Node),248 SwitchCaseCommaOrEnd: ListSave(&ast.Node),
250 SwitchCaseFirstItem: &ArrayList(&ast.Node),249 SwitchCaseFirstItem: &ArrayList(&ast.Node),
...@@ -301,6 +300,7 @@ pub const Parser = struct {...@@ -301,6 +300,7 @@ pub const Parser = struct {
301 ErrorTypeOrSetDecl: ErrorTypeOrSetDeclCtx,300 ErrorTypeOrSetDecl: ErrorTypeOrSetDeclCtx,
302 StringLiteral: OptionalCtx,301 StringLiteral: OptionalCtx,
303 Identifier: OptionalCtx,302 Identifier: OptionalCtx,
303 ErrorTag: &&ast.Node,
304304
305305
306 IfToken: @TagType(Token.Id),306 IfToken: @TagType(Token.Id),
...@@ -325,6 +325,7 @@ pub const Parser = struct {...@@ -325,6 +325,7 @@ pub const Parser = struct {
325 ast.Node.Root {325 ast.Node.Root {
326 .base = undefined,326 .base = undefined,
327 .decls = ArrayList(&ast.Node).init(arena),327 .decls = ArrayList(&ast.Node).init(arena),
328 .doc_comments = null,
328 // initialized when we get the eof token329 // initialized when we get the eof token
329 .eof_token = undefined,330 .eof_token = undefined,
330 }331 }
...@@ -354,7 +355,7 @@ pub const Parser = struct {...@@ -354,7 +355,7 @@ pub const Parser = struct {
354 try root_node.decls.append(&line_comment.base);355 try root_node.decls.append(&line_comment.base);
355 }356 }
356357
357 const comments = try self.eatComments(arena);358 const comments = try self.eatDocComments(arena);
358 const token = self.getNextToken();359 const token = self.getNextToken();
359 switch (token.id) {360 switch (token.id) {
360 Token.Id.Keyword_test => {361 Token.Id.Keyword_test => {
...@@ -363,7 +364,6 @@ pub const Parser = struct {...@@ -363,7 +364,6 @@ pub const Parser = struct {
363 const block = try arena.construct(ast.Node.Block {364 const block = try arena.construct(ast.Node.Block {
364 .base = ast.Node {365 .base = ast.Node {
365 .id = ast.Node.Id.Block,366 .id = ast.Node.Id.Block,
366 .doc_comments = null,
367 .same_line_comment = null,367 .same_line_comment = null,
368 },368 },
369 .label = null,369 .label = null,
...@@ -374,9 +374,9 @@ pub const Parser = struct {...@@ -374,9 +374,9 @@ pub const Parser = struct {
374 const test_node = try arena.construct(ast.Node.TestDecl {374 const test_node = try arena.construct(ast.Node.TestDecl {
375 .base = ast.Node {375 .base = ast.Node {
376 .id = ast.Node.Id.TestDecl,376 .id = ast.Node.Id.TestDecl,
377 .doc_comments = comments,
378 .same_line_comment = null,377 .same_line_comment = null,
379 },378 },
379 .doc_comments = comments,
380 .test_token = token,380 .test_token = token,
381 .name = undefined,381 .name = undefined,
382 .body_node = &block.base,382 .body_node = &block.base,
...@@ -394,7 +394,11 @@ pub const Parser = struct {...@@ -394,7 +394,11 @@ pub const Parser = struct {
394 },394 },
395 Token.Id.Eof => {395 Token.Id.Eof => {
396 root_node.eof_token = token;396 root_node.eof_token = token;
397 return Tree {.root_node = root_node, .arena_allocator = arena_allocator};397 root_node.doc_comments = comments;
398 return Tree {
399 .root_node = root_node,
400 .arena_allocator = arena_allocator,
401 };
398 },402 },
399 Token.Id.Keyword_pub => {403 Token.Id.Keyword_pub => {
400 stack.append(State.TopLevel) catch unreachable;404 stack.append(State.TopLevel) catch unreachable;
...@@ -424,6 +428,7 @@ pub const Parser = struct {...@@ -424,6 +428,7 @@ pub const Parser = struct {
424 .base = undefined,428 .base = undefined,
425 .comptime_token = token,429 .comptime_token = token,
426 .expr = &block.base,430 .expr = &block.base,
431 .doc_comments = comments,
427 }432 }
428 );433 );
429 stack.append(State.TopLevel) catch unreachable;434 stack.append(State.TopLevel) catch unreachable;
...@@ -520,6 +525,7 @@ pub const Parser = struct {...@@ -520,6 +525,7 @@ pub const Parser = struct {
520 .visib_token = ctx.visib_token,525 .visib_token = ctx.visib_token,
521 .expr = undefined,526 .expr = undefined,
522 .semicolon_token = undefined,527 .semicolon_token = undefined,
528 .doc_comments = ctx.comments,
523 }529 }
524 );530 );
525 stack.append(State {531 stack.append(State {
...@@ -556,9 +562,9 @@ pub const Parser = struct {...@@ -556,9 +562,9 @@ pub const Parser = struct {
556 const fn_proto = try arena.construct(ast.Node.FnProto {562 const fn_proto = try arena.construct(ast.Node.FnProto {
557 .base = ast.Node {563 .base = ast.Node {
558 .id = ast.Node.Id.FnProto,564 .id = ast.Node.Id.FnProto,
559 .doc_comments = ctx.comments,
560 .same_line_comment = null,565 .same_line_comment = null,
561 },566 },
567 .doc_comments = ctx.comments,
562 .visib_token = ctx.visib_token,568 .visib_token = ctx.visib_token,
563 .name_token = null,569 .name_token = null,
564 .fn_token = undefined,570 .fn_token = undefined,
...@@ -625,9 +631,9 @@ pub const Parser = struct {...@@ -625,9 +631,9 @@ pub const Parser = struct {
625 const node = try arena.construct(ast.Node.StructField {631 const node = try arena.construct(ast.Node.StructField {
626 .base = ast.Node {632 .base = ast.Node {
627 .id = ast.Node.Id.StructField,633 .id = ast.Node.Id.StructField,
628 .doc_comments = null,
629 .same_line_comment = null,634 .same_line_comment = null,
630 },635 },
636 .doc_comments = ctx.comments,
631 .visib_token = ctx.visib_token,637 .visib_token = ctx.visib_token,
632 .name_token = identifier,638 .name_token = identifier,
633 .type_expr = undefined,639 .type_expr = undefined,
...@@ -734,7 +740,7 @@ pub const Parser = struct {...@@ -734,7 +740,7 @@ pub const Parser = struct {
734 try container_decl.fields_and_decls.append(&line_comment.base);740 try container_decl.fields_and_decls.append(&line_comment.base);
735 }741 }
736742
737 const comments = try self.eatComments(arena);743 const comments = try self.eatDocComments(arena);
738 const token = self.getNextToken();744 const token = self.getNextToken();
739 switch (token.id) {745 switch (token.id) {
740 Token.Id.Identifier => {746 Token.Id.Identifier => {
...@@ -743,9 +749,9 @@ pub const Parser = struct {...@@ -743,9 +749,9 @@ pub const Parser = struct {
743 const node = try arena.construct(ast.Node.StructField {749 const node = try arena.construct(ast.Node.StructField {
744 .base = ast.Node {750 .base = ast.Node {
745 .id = ast.Node.Id.StructField,751 .id = ast.Node.Id.StructField,
746 .doc_comments = comments,
747 .same_line_comment = null,752 .same_line_comment = null,
748 },753 },
754 .doc_comments = comments,
749 .visib_token = null,755 .visib_token = null,
750 .name_token = token,756 .name_token = token,
751 .type_expr = undefined,757 .type_expr = undefined,
...@@ -765,6 +771,7 @@ pub const Parser = struct {...@@ -765,6 +771,7 @@ pub const Parser = struct {
765 .name_token = token,771 .name_token = token,
766 .type_expr = null,772 .type_expr = null,
767 .value_expr = null,773 .value_expr = null,
774 .doc_comments = comments,
768 }775 }
769 );776 );
770777
...@@ -780,6 +787,7 @@ pub const Parser = struct {...@@ -780,6 +787,7 @@ pub const Parser = struct {
780 .base = undefined,787 .base = undefined,
781 .name_token = token,788 .name_token = token,
782 .value = null,789 .value = null,
790 .doc_comments = comments,
783 }791 }
784 );792 );
785793
...@@ -831,6 +839,9 @@ pub const Parser = struct {...@@ -831,6 +839,9 @@ pub const Parser = struct {
831 continue;839 continue;
832 },840 },
833 Token.Id.RBrace => {841 Token.Id.RBrace => {
842 if (comments != null) {
843 return self.parseError(token, "doc comments must be attached to a node");
844 }
834 container_decl.rbrace_token = token;845 container_decl.rbrace_token = token;
835 continue;846 continue;
836 },847 },
...@@ -856,9 +867,9 @@ pub const Parser = struct {...@@ -856,9 +867,9 @@ pub const Parser = struct {
856 const var_decl = try arena.construct(ast.Node.VarDecl {867 const var_decl = try arena.construct(ast.Node.VarDecl {
857 .base = ast.Node {868 .base = ast.Node {
858 .id = ast.Node.Id.VarDecl,869 .id = ast.Node.Id.VarDecl,
859 .doc_comments = ctx.comments,
860 .same_line_comment = null,870 .same_line_comment = null,
861 },871 },
872 .doc_comments = ctx.comments,
862 .visib_token = ctx.visib_token,873 .visib_token = ctx.visib_token,
863 .mut_token = ctx.mut_token,874 .mut_token = ctx.mut_token,
864 .comptime_token = ctx.comptime_token,875 .comptime_token = ctx.comptime_token,
...@@ -1119,7 +1130,6 @@ pub const Parser = struct {...@@ -1119,7 +1130,6 @@ pub const Parser = struct {
1119 const node = try arena.construct(ast.Node.Suspend {1130 const node = try arena.construct(ast.Node.Suspend {
1120 .base = ast.Node {1131 .base = ast.Node {
1121 .id = ast.Node.Id.Suspend,1132 .id = ast.Node.Id.Suspend,
1122 .doc_comments = null,
1123 .same_line_comment = null,1133 .same_line_comment = null,
1124 },1134 },
1125 .label = ctx.label,1135 .label = ctx.label,
...@@ -1283,7 +1293,6 @@ pub const Parser = struct {...@@ -1283,7 +1293,6 @@ pub const Parser = struct {
1283 }1293 }
1284 },1294 },
1285 State.Statement => |block| {1295 State.Statement => |block| {
1286 const comments = try self.eatComments(arena);
1287 const token = self.getNextToken();1296 const token = self.getNextToken();
1288 switch (token.id) {1297 switch (token.id) {
1289 Token.Id.Keyword_comptime => {1298 Token.Id.Keyword_comptime => {
...@@ -1298,7 +1307,7 @@ pub const Parser = struct {...@@ -1298,7 +1307,7 @@ pub const Parser = struct {
1298 Token.Id.Keyword_var, Token.Id.Keyword_const => {1307 Token.Id.Keyword_var, Token.Id.Keyword_const => {
1299 stack.append(State {1308 stack.append(State {
1300 .VarDecl = VarDeclCtx {1309 .VarDecl = VarDeclCtx {
1301 .comments = comments,1310 .comments = null,
1302 .visib_token = null,1311 .visib_token = null,
1303 .comptime_token = null,1312 .comptime_token = null,
1304 .extern_export_token = null,1313 .extern_export_token = null,
...@@ -1313,7 +1322,6 @@ pub const Parser = struct {...@@ -1313,7 +1322,6 @@ pub const Parser = struct {
1313 const node = try arena.construct(ast.Node.Defer {1322 const node = try arena.construct(ast.Node.Defer {
1314 .base = ast.Node {1323 .base = ast.Node {
1315 .id = ast.Node.Id.Defer,1324 .id = ast.Node.Id.Defer,
1316 .doc_comments = comments,
1317 .same_line_comment = null,1325 .same_line_comment = null,
1318 },1326 },
1319 .defer_token = token,1327 .defer_token = token,
...@@ -1349,23 +1357,18 @@ pub const Parser = struct {...@@ -1349,23 +1357,18 @@ pub const Parser = struct {
1349 const statement = try block.statements.addOne();1357 const statement = try block.statements.addOne();
1350 stack.append(State { .LookForSameLineComment = statement }) catch unreachable;1358 stack.append(State { .LookForSameLineComment = statement }) catch unreachable;
1351 try stack.append(State { .Semicolon = statement });1359 try stack.append(State { .Semicolon = statement });
1352 try stack.append(State { .AddComments = AddCommentsCtx {
1353 .node_ptr = statement,
1354 .comments = comments,
1355 }});
1356 try stack.append(State { .AssignmentExpressionBegin = OptionalCtx{ .Required = statement } });1360 try stack.append(State { .AssignmentExpressionBegin = OptionalCtx{ .Required = statement } });
1357 continue;1361 continue;
1358 }1362 }
1359 }1363 }
1360 },1364 },
1361 State.ComptimeStatement => |ctx| {1365 State.ComptimeStatement => |ctx| {
1362 const comments = try self.eatComments(arena);
1363 const token = self.getNextToken();1366 const token = self.getNextToken();
1364 switch (token.id) {1367 switch (token.id) {
1365 Token.Id.Keyword_var, Token.Id.Keyword_const => {1368 Token.Id.Keyword_var, Token.Id.Keyword_const => {
1366 stack.append(State {1369 stack.append(State {
1367 .VarDecl = VarDeclCtx {1370 .VarDecl = VarDeclCtx {
1368 .comments = comments,1371 .comments = null,
1369 .visib_token = null,1372 .visib_token = null,
1370 .comptime_token = ctx.comptime_token,1373 .comptime_token = ctx.comptime_token,
1371 .extern_export_token = null,1374 .extern_export_token = null,
...@@ -1395,12 +1398,6 @@ pub const Parser = struct {...@@ -1395,12 +1398,6 @@ pub const Parser = struct {
1395 continue;1398 continue;
1396 },1399 },
13971400
1398 State.AddComments => |add_comments_ctx| {
1399 const node = *add_comments_ctx.node_ptr;
1400 node.doc_comments = add_comments_ctx.comments;
1401 continue;
1402 },
1403
1404 State.LookForSameLineComment => |node_ptr| {1401 State.LookForSameLineComment => |node_ptr| {
1405 try self.lookForSameLineComment(arena, *node_ptr);1402 try self.lookForSameLineComment(arena, *node_ptr);
1406 continue;1403 continue;
...@@ -1521,7 +1518,6 @@ pub const Parser = struct {...@@ -1521,7 +1518,6 @@ pub const Parser = struct {
1521 const node = try arena.construct(ast.Node.FieldInitializer {1518 const node = try arena.construct(ast.Node.FieldInitializer {
1522 .base = ast.Node {1519 .base = ast.Node {
1523 .id = ast.Node.Id.FieldInitializer,1520 .id = ast.Node.Id.FieldInitializer,
1524 .doc_comments = null,
1525 .same_line_comment = null,1521 .same_line_comment = null,
1526 },1522 },
1527 .period_token = undefined,1523 .period_token = undefined,
...@@ -1566,7 +1562,7 @@ pub const Parser = struct {...@@ -1566,7 +1562,7 @@ pub const Parser = struct {
1566 try stack.append(State { .ContainerDecl = container_decl });1562 try stack.append(State { .ContainerDecl = container_decl });
1567 continue;1563 continue;
1568 },1564 },
1569 State.IdentifierListItemOrEnd => |list_state| {1565 State.ErrorTagListItemOrEnd => |list_state| {
1570 while (try self.eatLineComment(arena)) |line_comment| {1566 while (try self.eatLineComment(arena)) |line_comment| {
1571 try list_state.list.append(&line_comment.base);1567 try list_state.list.append(&line_comment.base);
1572 }1568 }
...@@ -1576,23 +1572,18 @@ pub const Parser = struct {...@@ -1576,23 +1572,18 @@ pub const Parser = struct {
1576 continue;1572 continue;
1577 }1573 }
15781574
1579 const comments = try self.eatComments(arena);
1580 const node_ptr = try list_state.list.addOne();1575 const node_ptr = try list_state.list.addOne();
15811576
1582 try stack.append(State { .AddComments = AddCommentsCtx {1577 try stack.append(State { .ErrorTagListCommaOrEnd = list_state });
1583 .node_ptr = node_ptr,1578 try stack.append(State { .ErrorTag = node_ptr });
1584 .comments = comments,
1585 }});
1586 try stack.append(State { .IdentifierListCommaOrEnd = list_state });
1587 try stack.append(State { .Identifier = OptionalCtx { .Required = node_ptr } });
1588 continue;1579 continue;
1589 },1580 },
1590 State.IdentifierListCommaOrEnd => |list_state| {1581 State.ErrorTagListCommaOrEnd => |list_state| {
1591 if (try self.expectCommaOrEnd(Token.Id.RBrace)) |end| {1582 if (try self.expectCommaOrEnd(Token.Id.RBrace)) |end| {
1592 *list_state.ptr = end;1583 *list_state.ptr = end;
1593 continue;1584 continue;
1594 } else {1585 } else {
1595 stack.append(State { .IdentifierListItemOrEnd = list_state }) catch unreachable;1586 stack.append(State { .ErrorTagListItemOrEnd = list_state }) catch unreachable;
1596 continue;1587 continue;
1597 }1588 }
1598 },1589 },
...@@ -1606,11 +1597,10 @@ pub const Parser = struct {...@@ -1606,11 +1597,10 @@ pub const Parser = struct {
1606 continue;1597 continue;
1607 }1598 }
16081599
1609 const comments = try self.eatComments(arena);1600 const comments = try self.eatDocComments(arena);
1610 const node = try arena.construct(ast.Node.SwitchCase {1601 const node = try arena.construct(ast.Node.SwitchCase {
1611 .base = ast.Node {1602 .base = ast.Node {
1612 .id = ast.Node.Id.SwitchCase,1603 .id = ast.Node.Id.SwitchCase,
1613 .doc_comments = comments,
1614 .same_line_comment = null,1604 .same_line_comment = null,
1615 },1605 },
1616 .items = ArrayList(&ast.Node).init(arena),1606 .items = ArrayList(&ast.Node).init(arena),
...@@ -1723,9 +1713,9 @@ pub const Parser = struct {...@@ -1723,9 +1713,9 @@ pub const Parser = struct {
1723 const fn_proto = try arena.construct(ast.Node.FnProto {1713 const fn_proto = try arena.construct(ast.Node.FnProto {
1724 .base = ast.Node {1714 .base = ast.Node {
1725 .id = ast.Node.Id.FnProto,1715 .id = ast.Node.Id.FnProto,
1726 .doc_comments = ctx.comments,
1727 .same_line_comment = null,1716 .same_line_comment = null,
1728 },1717 },
1718 .doc_comments = ctx.comments,
1729 .visib_token = null,1719 .visib_token = null,
1730 .name_token = null,1720 .name_token = null,
1731 .fn_token = fn_token,1721 .fn_token = fn_token,
...@@ -2593,7 +2583,6 @@ pub const Parser = struct {...@@ -2593,7 +2583,6 @@ pub const Parser = struct {
2593 const node = try arena.construct(ast.Node.PromiseType {2583 const node = try arena.construct(ast.Node.PromiseType {
2594 .base = ast.Node {2584 .base = ast.Node {
2595 .id = ast.Node.Id.PromiseType,2585 .id = ast.Node.Id.PromiseType,
2596 .doc_comments = null,
2597 .same_line_comment = null,2586 .same_line_comment = null,
2598 },2587 },
2599 .promise_token = token,2588 .promise_token = token,
...@@ -2719,9 +2708,9 @@ pub const Parser = struct {...@@ -2719,9 +2708,9 @@ pub const Parser = struct {
2719 const fn_proto = try arena.construct(ast.Node.FnProto {2708 const fn_proto = try arena.construct(ast.Node.FnProto {
2720 .base = ast.Node {2709 .base = ast.Node {
2721 .id = ast.Node.Id.FnProto,2710 .id = ast.Node.Id.FnProto,
2722 .doc_comments = null,
2723 .same_line_comment = null,2711 .same_line_comment = null,
2724 },2712 },
2713 .doc_comments = null,
2725 .visib_token = null,2714 .visib_token = null,
2726 .name_token = null,2715 .name_token = null,
2727 .fn_token = token,2716 .fn_token = token,
...@@ -2743,9 +2732,9 @@ pub const Parser = struct {...@@ -2743,9 +2732,9 @@ pub const Parser = struct {
2743 const fn_proto = try arena.construct(ast.Node.FnProto {2732 const fn_proto = try arena.construct(ast.Node.FnProto {
2744 .base = ast.Node {2733 .base = ast.Node {
2745 .id = ast.Node.Id.FnProto,2734 .id = ast.Node.Id.FnProto,
2746 .doc_comments = null,
2747 .same_line_comment = null,2735 .same_line_comment = null,
2748 },2736 },
2737 .doc_comments = null,
2749 .visib_token = null,2738 .visib_token = null,
2750 .name_token = null,2739 .name_token = null,
2751 .fn_token = undefined,2740 .fn_token = undefined,
...@@ -2836,7 +2825,6 @@ pub const Parser = struct {...@@ -2836,7 +2825,6 @@ pub const Parser = struct {
2836 const node = try arena.construct(ast.Node.ErrorSetDecl {2825 const node = try arena.construct(ast.Node.ErrorSetDecl {
2837 .base = ast.Node {2826 .base = ast.Node {
2838 .id = ast.Node.Id.ErrorSetDecl,2827 .id = ast.Node.Id.ErrorSetDecl,
2839 .doc_comments = null,
2840 .same_line_comment = null,2828 .same_line_comment = null,
2841 },2829 },
2842 .error_token = ctx.error_token,2830 .error_token = ctx.error_token,
...@@ -2846,7 +2834,7 @@ pub const Parser = struct {...@@ -2846,7 +2834,7 @@ pub const Parser = struct {
2846 ctx.opt_ctx.store(&node.base);2834 ctx.opt_ctx.store(&node.base);
28472835
2848 stack.append(State {2836 stack.append(State {
2849 .IdentifierListItemOrEnd = ListSave(&ast.Node) {2837 .ErrorTagListItemOrEnd = ListSave(&ast.Node) {
2850 .list = &node.decls,2838 .list = &node.decls,
2851 .ptr = &node.rbrace_token,2839 .ptr = &node.rbrace_token,
2852 }2840 }
...@@ -2866,6 +2854,7 @@ pub const Parser = struct {...@@ -2866,6 +2854,7 @@ pub const Parser = struct {
2866 }2854 }
2867 );2855 );
2868 },2856 },
2857
2869 State.Identifier => |opt_ctx| {2858 State.Identifier => |opt_ctx| {
2870 if (self.eatToken(Token.Id.Identifier)) |ident_token| {2859 if (self.eatToken(Token.Id.Identifier)) |ident_token| {
2871 _ = try self.createToCtxLiteral(arena, opt_ctx, ast.Node.Identifier, ident_token);2860 _ = try self.createToCtxLiteral(arena, opt_ctx, ast.Node.Identifier, ident_token);
...@@ -2878,6 +2867,25 @@ pub const Parser = struct {...@@ -2878,6 +2867,25 @@ pub const Parser = struct {
2878 }2867 }
2879 },2868 },
28802869
2870 State.ErrorTag => |node_ptr| {
2871 const comments = try self.eatDocComments(arena);
2872 const ident_token = self.getNextToken();
2873 if (ident_token.id != Token.Id.Identifier) {
2874 return self.parseError(ident_token, "expected {}, found {}",
2875 @tagName(Token.Id.Identifier), @tagName(ident_token.id));
2876 }
2877
2878 const node = try arena.construct(ast.Node.ErrorTag {
2879 .base = ast.Node {
2880 .id = ast.Node.Id.ErrorTag,
2881 .same_line_comment = null,
2882 },
2883 .doc_comments = comments,
2884 .name_token = ident_token,
2885 });
2886 *node_ptr = &node.base;
2887 continue;
2888 },
28812889
2882 State.ExpectToken => |token_id| {2890 State.ExpectToken => |token_id| {
2883 _ = try self.expectToken(token_id);2891 _ = try self.expectToken(token_id);
...@@ -2916,7 +2924,7 @@ pub const Parser = struct {...@@ -2916,7 +2924,7 @@ pub const Parser = struct {
2916 }2924 }
2917 }2925 }
29182926
2919 fn eatComments(self: &Parser, arena: &mem.Allocator) !?&ast.Node.DocComment {2927 fn eatDocComments(self: &Parser, arena: &mem.Allocator) !?&ast.Node.DocComment {
2920 var result: ?&ast.Node.DocComment = null;2928 var result: ?&ast.Node.DocComment = null;
2921 while (true) {2929 while (true) {
2922 if (self.eatToken(Token.Id.DocComment)) |line_comment| {2930 if (self.eatToken(Token.Id.DocComment)) |line_comment| {
...@@ -2927,7 +2935,6 @@ pub const Parser = struct {...@@ -2927,7 +2935,6 @@ pub const Parser = struct {
2927 const comment_node = try arena.construct(ast.Node.DocComment {2935 const comment_node = try arena.construct(ast.Node.DocComment {
2928 .base = ast.Node {2936 .base = ast.Node {
2929 .id = ast.Node.Id.DocComment,2937 .id = ast.Node.Id.DocComment,
2930 .doc_comments = null,
2931 .same_line_comment = null,2938 .same_line_comment = null,
2932 },2939 },
2933 .lines = ArrayList(Token).init(arena),2940 .lines = ArrayList(Token).init(arena),
...@@ -2949,7 +2956,6 @@ pub const Parser = struct {...@@ -2949,7 +2956,6 @@ pub const Parser = struct {
2949 return try arena.construct(ast.Node.LineComment {2956 return try arena.construct(ast.Node.LineComment {
2950 .base = ast.Node {2957 .base = ast.Node {
2951 .id = ast.Node.Id.LineComment,2958 .id = ast.Node.Id.LineComment,
2952 .doc_comments = null,
2953 .same_line_comment = null,2959 .same_line_comment = null,
2954 },2960 },
2955 .token = token,2961 .token = token,
...@@ -3142,7 +3148,6 @@ pub const Parser = struct {...@@ -3142,7 +3148,6 @@ pub const Parser = struct {
3142 const node = try arena.construct(ast.Node.Switch {3148 const node = try arena.construct(ast.Node.Switch {
3143 .base = ast.Node {3149 .base = ast.Node {
3144 .id = ast.Node.Id.Switch,3150 .id = ast.Node.Id.Switch,
3145 .doc_comments = null,
3146 .same_line_comment = null,3151 .same_line_comment = null,
3147 },3152 },
3148 .switch_token = *token,3153 .switch_token = *token,
...@@ -3170,6 +3175,7 @@ pub const Parser = struct {...@@ -3170,6 +3175,7 @@ pub const Parser = struct {
3170 .base = undefined,3175 .base = undefined,
3171 .comptime_token = *token,3176 .comptime_token = *token,
3172 .expr = undefined,3177 .expr = undefined,
3178 .doc_comments = null,
3173 }3179 }
3174 );3180 );
3175 try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } });3181 try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } });
...@@ -3312,7 +3318,6 @@ pub const Parser = struct {...@@ -3312,7 +3318,6 @@ pub const Parser = struct {
3312 const id = ast.Node.typeToId(T);3318 const id = ast.Node.typeToId(T);
3313 break :blk ast.Node {3319 break :blk ast.Node {
3314 .id = id,3320 .id = id,
3315 .doc_comments = null,
3316 .same_line_comment = null,3321 .same_line_comment = null,
3317 };3322 };
3318 };3323 };
...@@ -3451,7 +3456,6 @@ pub const Parser = struct {...@@ -3451,7 +3456,6 @@ pub const Parser = struct {
3451 PrintIndent,3456 PrintIndent,
3452 Indent: usize,3457 Indent: usize,
3453 PrintSameLineComment: ?&Token,3458 PrintSameLineComment: ?&Token,
3454 PrintComments: &ast.Node,
3455 };3459 };
34563460
3457 pub fn renderSource(self: &Parser, stream: var, root_node: &ast.Node.Root) !void {3461 pub fn renderSource(self: &Parser, stream: var, root_node: &ast.Node.Root) !void {
...@@ -3490,7 +3494,7 @@ pub const Parser = struct {...@@ -3490,7 +3494,7 @@ pub const Parser = struct {
3490 switch (decl.id) {3494 switch (decl.id) {
3491 ast.Node.Id.FnProto => {3495 ast.Node.Id.FnProto => {
3492 const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", decl);3496 const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", decl);
3493 try self.renderComments(stream, &fn_proto.base, indent);3497 try self.renderComments(stream, fn_proto, indent);
34943498
3495 if (fn_proto.body_node) |body_node| {3499 if (fn_proto.body_node) |body_node| {
3496 stack.append(RenderState { .Expression = body_node}) catch unreachable;3500 stack.append(RenderState { .Expression = body_node}) catch unreachable;
...@@ -3512,12 +3516,12 @@ pub const Parser = struct {...@@ -3512,12 +3516,12 @@ pub const Parser = struct {
3512 },3516 },
3513 ast.Node.Id.VarDecl => {3517 ast.Node.Id.VarDecl => {
3514 const var_decl = @fieldParentPtr(ast.Node.VarDecl, "base", decl);3518 const var_decl = @fieldParentPtr(ast.Node.VarDecl, "base", decl);
3515 try self.renderComments(stream, &var_decl.base, indent);3519 try self.renderComments(stream, var_decl, indent);
3516 try stack.append(RenderState { .VarDecl = var_decl});3520 try stack.append(RenderState { .VarDecl = var_decl});
3517 },3521 },
3518 ast.Node.Id.TestDecl => {3522 ast.Node.Id.TestDecl => {
3519 const test_decl = @fieldParentPtr(ast.Node.TestDecl, "base", decl);3523 const test_decl = @fieldParentPtr(ast.Node.TestDecl, "base", decl);
3520 try self.renderComments(stream, &test_decl.base, indent);3524 try self.renderComments(stream, test_decl, indent);
3521 try stream.print("test ");3525 try stream.print("test ");
3522 try stack.append(RenderState { .Expression = test_decl.body_node });3526 try stack.append(RenderState { .Expression = test_decl.body_node });
3523 try stack.append(RenderState { .Text = " " });3527 try stack.append(RenderState { .Text = " " });
...@@ -3525,7 +3529,7 @@ pub const Parser = struct {...@@ -3525,7 +3529,7 @@ pub const Parser = struct {
3525 },3529 },
3526 ast.Node.Id.StructField => {3530 ast.Node.Id.StructField => {
3527 const field = @fieldParentPtr(ast.Node.StructField, "base", decl);3531 const field = @fieldParentPtr(ast.Node.StructField, "base", decl);
3528 try self.renderComments(stream, &field.base, indent);3532 try self.renderComments(stream, field, indent);
3529 if (field.visib_token) |visib_token| {3533 if (field.visib_token) |visib_token| {
3530 try stream.print("{} ", self.tokenizer.getTokenSlice(visib_token));3534 try stream.print("{} ", self.tokenizer.getTokenSlice(visib_token));
3531 }3535 }
...@@ -3535,7 +3539,7 @@ pub const Parser = struct {...@@ -3535,7 +3539,7 @@ pub const Parser = struct {
3535 },3539 },
3536 ast.Node.Id.UnionTag => {3540 ast.Node.Id.UnionTag => {
3537 const tag = @fieldParentPtr(ast.Node.UnionTag, "base", decl);3541 const tag = @fieldParentPtr(ast.Node.UnionTag, "base", decl);
3538 try self.renderComments(stream, &tag.base, indent);3542 try self.renderComments(stream, tag, indent);
3539 try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token));3543 try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token));
35403544
3541 try stack.append(RenderState { .Text = "," });3545 try stack.append(RenderState { .Text = "," });
...@@ -3552,7 +3556,7 @@ pub const Parser = struct {...@@ -3552,7 +3556,7 @@ pub const Parser = struct {
3552 },3556 },
3553 ast.Node.Id.EnumTag => {3557 ast.Node.Id.EnumTag => {
3554 const tag = @fieldParentPtr(ast.Node.EnumTag, "base", decl);3558 const tag = @fieldParentPtr(ast.Node.EnumTag, "base", decl);
3555 try self.renderComments(stream, &tag.base, indent);3559 try self.renderComments(stream, tag, indent);
3556 try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token));3560 try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token));
35573561
3558 try stack.append(RenderState { .Text = "," });3562 try stack.append(RenderState { .Text = "," });
...@@ -3561,6 +3565,11 @@ pub const Parser = struct {...@@ -3561,6 +3565,11 @@ pub const Parser = struct {
3561 try stack.append(RenderState { .Expression = value});3565 try stack.append(RenderState { .Expression = value});
3562 }3566 }
3563 },3567 },
3568 ast.Node.Id.ErrorTag => {
3569 const tag = @fieldParentPtr(ast.Node.ErrorTag, "base", decl);
3570 try self.renderComments(stream, tag, indent);
3571 try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token));
3572 },
3564 ast.Node.Id.Comptime => {3573 ast.Node.Id.Comptime => {
3565 if (requireSemiColon(decl)) {3574 if (requireSemiColon(decl)) {
3566 try stack.append(RenderState { .Text = ";" });3575 try stack.append(RenderState { .Text = ";" });
...@@ -4122,11 +4131,20 @@ pub const Parser = struct {...@@ -4122,11 +4131,20 @@ pub const Parser = struct {
41224131
4123 if (decls.len == 1) blk: {4132 if (decls.len == 1) blk: {
4124 const node = decls[0];4133 const node = decls[0];
4125 if (node.same_line_comment != null or node.doc_comments != null) break :blk;4134
4135 // if there are any doc comments or same line comments
4136 // don't try to put it all on one line
4137 if (node.same_line_comment != null) break :blk;
4138 if (node.cast(ast.Node.ErrorTag)) |tag| {
4139 if (tag.doc_comments != null) break :blk;
4140 } else {
4141 break :blk;
4142 }
4143
41264144
4127 try stream.write("error{");4145 try stream.write("error{");
4128 try stack.append(RenderState { .Text = "}" });4146 try stack.append(RenderState { .Text = "}" });
4129 try stack.append(RenderState { .Expression = node });4147 try stack.append(RenderState { .TopLevelDecl = node });
4130 continue;4148 continue;
4131 }4149 }
41324150
...@@ -4144,8 +4162,7 @@ pub const Parser = struct {...@@ -4144,8 +4162,7 @@ pub const Parser = struct {
4144 if (node.id != ast.Node.Id.LineComment) {4162 if (node.id != ast.Node.Id.LineComment) {
4145 try stack.append(RenderState { .Text = "," });4163 try stack.append(RenderState { .Text = "," });
4146 }4164 }
4147 try stack.append(RenderState { .Expression = node });4165 try stack.append(RenderState { .TopLevelDecl = node });
4148 try stack.append(RenderState { .PrintComments = node });
4149 try stack.append(RenderState.PrintIndent);4166 try stack.append(RenderState.PrintIndent);
4150 try stack.append(RenderState {4167 try stack.append(RenderState {
4151 .Text = blk: {4168 .Text = blk: {
...@@ -4304,8 +4321,6 @@ pub const Parser = struct {...@@ -4304,8 +4321,6 @@ pub const Parser = struct {
4304 ast.Node.Id.SwitchCase => {4321 ast.Node.Id.SwitchCase => {
4305 const switch_case = @fieldParentPtr(ast.Node.SwitchCase, "base", base);4322 const switch_case = @fieldParentPtr(ast.Node.SwitchCase, "base", base);
43064323
4307 try self.renderComments(stream, base, indent);
4308
4309 try stack.append(RenderState { .PrintSameLineComment = base.same_line_comment });4324 try stack.append(RenderState { .PrintSameLineComment = base.same_line_comment });
4310 try stack.append(RenderState { .Text = "," });4325 try stack.append(RenderState { .Text = "," });
4311 try stack.append(RenderState { .Expression = switch_case.expr });4326 try stack.append(RenderState { .Expression = switch_case.expr });
...@@ -4617,6 +4632,7 @@ pub const Parser = struct {...@@ -4617,6 +4632,7 @@ pub const Parser = struct {
4617 ast.Node.Id.StructField,4632 ast.Node.Id.StructField,
4618 ast.Node.Id.UnionTag,4633 ast.Node.Id.UnionTag,
4619 ast.Node.Id.EnumTag,4634 ast.Node.Id.EnumTag,
4635 ast.Node.Id.ErrorTag,
4620 ast.Node.Id.Root,4636 ast.Node.Id.Root,
4621 ast.Node.Id.VarDecl,4637 ast.Node.Id.VarDecl,
4622 ast.Node.Id.Use,4638 ast.Node.Id.Use,
...@@ -4624,7 +4640,6 @@ pub const Parser = struct {...@@ -4624,7 +4640,6 @@ pub const Parser = struct {
4624 ast.Node.Id.ParamDecl => unreachable,4640 ast.Node.Id.ParamDecl => unreachable,
4625 },4641 },
4626 RenderState.Statement => |base| {4642 RenderState.Statement => |base| {
4627 try self.renderComments(stream, base, indent);
4628 try stack.append(RenderState { .PrintSameLineComment = base.same_line_comment } );4643 try stack.append(RenderState { .PrintSameLineComment = base.same_line_comment } );
4629 switch (base.id) {4644 switch (base.id) {
4630 ast.Node.Id.VarDecl => {4645 ast.Node.Id.VarDecl => {
...@@ -4645,15 +4660,11 @@ pub const Parser = struct {...@@ -4645,15 +4660,11 @@ pub const Parser = struct {
4645 const comment_token = maybe_comment ?? break :blk;4660 const comment_token = maybe_comment ?? break :blk;
4646 try stream.print(" {}", self.tokenizer.getTokenSlice(comment_token));4661 try stream.print(" {}", self.tokenizer.getTokenSlice(comment_token));
4647 },4662 },
4648
4649 RenderState.PrintComments => |node| blk: {
4650 try self.renderComments(stream, node, indent);
4651 },
4652 }4663 }
4653 }4664 }
4654 }4665 }
46554666
4656 fn renderComments(self: &Parser, stream: var, node: &ast.Node, indent: usize) !void {4667 fn renderComments(self: &Parser, stream: var, node: var, indent: usize) !void {
4657 const comment = node.doc_comments ?? return;4668 const comment = node.doc_comments ?? return;
4658 for (comment.lines.toSliceConst()) |line_token| {4669 for (comment.lines.toSliceConst()) |line_token| {
4659 try stream.print("{}\n", self.tokenizer.getTokenSlice(line_token));4670 try stream.print("{}\n", self.tokenizer.getTokenSlice(line_token));
std/zig/parser_test.zig+1-17
...@@ -181,7 +181,7 @@ test "zig fmt: comments before global variables" {...@@ -181,7 +181,7 @@ test "zig fmt: comments before global variables" {
181 );181 );
182}182}
183183
184test "zig fmt: comments before statements" {184test "zig fmt: comments in statements" {
185 try testCanonical(185 try testCanonical(
186 \\test "std" {186 \\test "std" {
187 \\ // statement comment187 \\ // statement comment
...@@ -211,22 +211,6 @@ test "zig fmt: comments before test decl" {...@@ -211,22 +211,6 @@ test "zig fmt: comments before test decl" {
211 );211 );
212}212}
213213
214test "zig fmt: comments before variable declarations" {
215 try testCanonical(
216 \\const std = @import("std");
217 \\
218 \\pub fn main() !void {
219 \\ /// If this program is run without stdout attached, exit with an error.
220 \\ /// another comment
221 \\ var stdout_file = try std.io.getStdOut;
222 \\ // If this program is run without stdout attached, exit with an error.
223 \\ // another comment
224 \\ var stdout_file = try std.io.getStdOut;
225 \\}
226 \\
227 );
228}
229
230test "zig fmt: preserve spacing" {214test "zig fmt: preserve spacing" {
231 try testCanonical(215 try testCanonical(
232 \\const std = @import("std");216 \\const std = @import("std");