| ... | ... | @@ -228,7 +228,6 @@ pub const Parser = struct { |
| 228 | 228 | Statement: &ast.Node.Block, |
| 229 | 229 | ComptimeStatement: ComptimeStatementCtx, |
| 230 | 230 | Semicolon: &&ast.Node, |
| 231 | | AddComments: AddCommentsCtx, |
| 232 | 231 | LookForSameLineComment: &&ast.Node, |
| 233 | 232 | LookForSameLineCommentDirect: &ast.Node, |
| 234 | 233 | |
| ... | ... | @@ -243,8 +242,8 @@ pub const Parser = struct { |
| 243 | 242 | FieldInitListCommaOrEnd: ListSave(&ast.Node.FieldInitializer), |
| 244 | 243 | FieldListCommaOrEnd: &ast.Node.ContainerDecl, |
| 245 | 244 | FieldInitValue: OptionalCtx, |
| 246 | | IdentifierListItemOrEnd: ListSave(&ast.Node), |
| 247 | | IdentifierListCommaOrEnd: ListSave(&ast.Node), |
| 245 | ErrorTagListItemOrEnd: ListSave(&ast.Node), |
| 246 | ErrorTagListCommaOrEnd: ListSave(&ast.Node), |
| 248 | 247 | SwitchCaseOrEnd: ListSave(&ast.Node), |
| 249 | 248 | SwitchCaseCommaOrEnd: ListSave(&ast.Node), |
| 250 | 249 | SwitchCaseFirstItem: &ArrayList(&ast.Node), |
| ... | ... | @@ -301,6 +300,7 @@ pub const Parser = struct { |
| 301 | 300 | ErrorTypeOrSetDecl: ErrorTypeOrSetDeclCtx, |
| 302 | 301 | StringLiteral: OptionalCtx, |
| 303 | 302 | Identifier: OptionalCtx, |
| 303 | ErrorTag: &&ast.Node, |
| 304 | 304 | |
| 305 | 305 | |
| 306 | 306 | IfToken: @TagType(Token.Id), |
| ... | ... | @@ -325,6 +325,7 @@ pub const Parser = struct { |
| 325 | 325 | ast.Node.Root { |
| 326 | 326 | .base = undefined, |
| 327 | 327 | .decls = ArrayList(&ast.Node).init(arena), |
| 328 | .doc_comments = null, |
| 328 | 329 | // initialized when we get the eof token |
| 329 | 330 | .eof_token = undefined, |
| 330 | 331 | } |
| ... | ... | @@ -354,7 +355,7 @@ pub const Parser = struct { |
| 354 | 355 | try root_node.decls.append(&line_comment.base); |
| 355 | 356 | } |
| 356 | 357 | |
| 357 | | const comments = try self.eatComments(arena); |
| 358 | const comments = try self.eatDocComments(arena); |
| 358 | 359 | const token = self.getNextToken(); |
| 359 | 360 | switch (token.id) { |
| 360 | 361 | Token.Id.Keyword_test => { |
| ... | ... | @@ -363,7 +364,6 @@ pub const Parser = struct { |
| 363 | 364 | const block = try arena.construct(ast.Node.Block { |
| 364 | 365 | .base = ast.Node { |
| 365 | 366 | .id = ast.Node.Id.Block, |
| 366 | | .doc_comments = null, |
| 367 | 367 | .same_line_comment = null, |
| 368 | 368 | }, |
| 369 | 369 | .label = null, |
| ... | ... | @@ -374,9 +374,9 @@ pub const Parser = struct { |
| 374 | 374 | const test_node = try arena.construct(ast.Node.TestDecl { |
| 375 | 375 | .base = ast.Node { |
| 376 | 376 | .id = ast.Node.Id.TestDecl, |
| 377 | | .doc_comments = comments, |
| 378 | 377 | .same_line_comment = null, |
| 379 | 378 | }, |
| 379 | .doc_comments = comments, |
| 380 | 380 | .test_token = token, |
| 381 | 381 | .name = undefined, |
| 382 | 382 | .body_node = &block.base, |
| ... | ... | @@ -394,7 +394,11 @@ pub const Parser = struct { |
| 394 | 394 | }, |
| 395 | 395 | Token.Id.Eof => { |
| 396 | 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 | 403 | Token.Id.Keyword_pub => { |
| 400 | 404 | stack.append(State.TopLevel) catch unreachable; |
| ... | ... | @@ -424,6 +428,7 @@ pub const Parser = struct { |
| 424 | 428 | .base = undefined, |
| 425 | 429 | .comptime_token = token, |
| 426 | 430 | .expr = &block.base, |
| 431 | .doc_comments = comments, |
| 427 | 432 | } |
| 428 | 433 | ); |
| 429 | 434 | stack.append(State.TopLevel) catch unreachable; |
| ... | ... | @@ -520,6 +525,7 @@ pub const Parser = struct { |
| 520 | 525 | .visib_token = ctx.visib_token, |
| 521 | 526 | .expr = undefined, |
| 522 | 527 | .semicolon_token = undefined, |
| 528 | .doc_comments = ctx.comments, |
| 523 | 529 | } |
| 524 | 530 | ); |
| 525 | 531 | stack.append(State { |
| ... | ... | @@ -556,9 +562,9 @@ pub const Parser = struct { |
| 556 | 562 | const fn_proto = try arena.construct(ast.Node.FnProto { |
| 557 | 563 | .base = ast.Node { |
| 558 | 564 | .id = ast.Node.Id.FnProto, |
| 559 | | .doc_comments = ctx.comments, |
| 560 | 565 | .same_line_comment = null, |
| 561 | 566 | }, |
| 567 | .doc_comments = ctx.comments, |
| 562 | 568 | .visib_token = ctx.visib_token, |
| 563 | 569 | .name_token = null, |
| 564 | 570 | .fn_token = undefined, |
| ... | ... | @@ -625,9 +631,9 @@ pub const Parser = struct { |
| 625 | 631 | const node = try arena.construct(ast.Node.StructField { |
| 626 | 632 | .base = ast.Node { |
| 627 | 633 | .id = ast.Node.Id.StructField, |
| 628 | | .doc_comments = null, |
| 629 | 634 | .same_line_comment = null, |
| 630 | 635 | }, |
| 636 | .doc_comments = ctx.comments, |
| 631 | 637 | .visib_token = ctx.visib_token, |
| 632 | 638 | .name_token = identifier, |
| 633 | 639 | .type_expr = undefined, |
| ... | ... | @@ -734,7 +740,7 @@ pub const Parser = struct { |
| 734 | 740 | try container_decl.fields_and_decls.append(&line_comment.base); |
| 735 | 741 | } |
| 736 | 742 | |
| 737 | | const comments = try self.eatComments(arena); |
| 743 | const comments = try self.eatDocComments(arena); |
| 738 | 744 | const token = self.getNextToken(); |
| 739 | 745 | switch (token.id) { |
| 740 | 746 | Token.Id.Identifier => { |
| ... | ... | @@ -743,9 +749,9 @@ pub const Parser = struct { |
| 743 | 749 | const node = try arena.construct(ast.Node.StructField { |
| 744 | 750 | .base = ast.Node { |
| 745 | 751 | .id = ast.Node.Id.StructField, |
| 746 | | .doc_comments = comments, |
| 747 | 752 | .same_line_comment = null, |
| 748 | 753 | }, |
| 754 | .doc_comments = comments, |
| 749 | 755 | .visib_token = null, |
| 750 | 756 | .name_token = token, |
| 751 | 757 | .type_expr = undefined, |
| ... | ... | @@ -765,6 +771,7 @@ pub const Parser = struct { |
| 765 | 771 | .name_token = token, |
| 766 | 772 | .type_expr = null, |
| 767 | 773 | .value_expr = null, |
| 774 | .doc_comments = comments, |
| 768 | 775 | } |
| 769 | 776 | ); |
| 770 | 777 | |
| ... | ... | @@ -780,6 +787,7 @@ pub const Parser = struct { |
| 780 | 787 | .base = undefined, |
| 781 | 788 | .name_token = token, |
| 782 | 789 | .value = null, |
| 790 | .doc_comments = comments, |
| 783 | 791 | } |
| 784 | 792 | ); |
| 785 | 793 | |
| ... | ... | @@ -831,6 +839,9 @@ pub const Parser = struct { |
| 831 | 839 | continue; |
| 832 | 840 | }, |
| 833 | 841 | Token.Id.RBrace => { |
| 842 | if (comments != null) { |
| 843 | return self.parseError(token, "doc comments must be attached to a node"); |
| 844 | } |
| 834 | 845 | container_decl.rbrace_token = token; |
| 835 | 846 | continue; |
| 836 | 847 | }, |
| ... | ... | @@ -856,9 +867,9 @@ pub const Parser = struct { |
| 856 | 867 | const var_decl = try arena.construct(ast.Node.VarDecl { |
| 857 | 868 | .base = ast.Node { |
| 858 | 869 | .id = ast.Node.Id.VarDecl, |
| 859 | | .doc_comments = ctx.comments, |
| 860 | 870 | .same_line_comment = null, |
| 861 | 871 | }, |
| 872 | .doc_comments = ctx.comments, |
| 862 | 873 | .visib_token = ctx.visib_token, |
| 863 | 874 | .mut_token = ctx.mut_token, |
| 864 | 875 | .comptime_token = ctx.comptime_token, |
| ... | ... | @@ -1119,7 +1130,6 @@ pub const Parser = struct { |
| 1119 | 1130 | const node = try arena.construct(ast.Node.Suspend { |
| 1120 | 1131 | .base = ast.Node { |
| 1121 | 1132 | .id = ast.Node.Id.Suspend, |
| 1122 | | .doc_comments = null, |
| 1123 | 1133 | .same_line_comment = null, |
| 1124 | 1134 | }, |
| 1125 | 1135 | .label = ctx.label, |
| ... | ... | @@ -1283,7 +1293,6 @@ pub const Parser = struct { |
| 1283 | 1293 | } |
| 1284 | 1294 | }, |
| 1285 | 1295 | State.Statement => |block| { |
| 1286 | | const comments = try self.eatComments(arena); |
| 1287 | 1296 | const token = self.getNextToken(); |
| 1288 | 1297 | switch (token.id) { |
| 1289 | 1298 | Token.Id.Keyword_comptime => { |
| ... | ... | @@ -1298,7 +1307,7 @@ pub const Parser = struct { |
| 1298 | 1307 | Token.Id.Keyword_var, Token.Id.Keyword_const => { |
| 1299 | 1308 | stack.append(State { |
| 1300 | 1309 | .VarDecl = VarDeclCtx { |
| 1301 | | .comments = comments, |
| 1310 | .comments = null, |
| 1302 | 1311 | .visib_token = null, |
| 1303 | 1312 | .comptime_token = null, |
| 1304 | 1313 | .extern_export_token = null, |
| ... | ... | @@ -1313,7 +1322,6 @@ pub const Parser = struct { |
| 1313 | 1322 | const node = try arena.construct(ast.Node.Defer { |
| 1314 | 1323 | .base = ast.Node { |
| 1315 | 1324 | .id = ast.Node.Id.Defer, |
| 1316 | | .doc_comments = comments, |
| 1317 | 1325 | .same_line_comment = null, |
| 1318 | 1326 | }, |
| 1319 | 1327 | .defer_token = token, |
| ... | ... | @@ -1349,23 +1357,18 @@ pub const Parser = struct { |
| 1349 | 1357 | const statement = try block.statements.addOne(); |
| 1350 | 1358 | stack.append(State { .LookForSameLineComment = statement }) catch unreachable; |
| 1351 | 1359 | try stack.append(State { .Semicolon = statement }); |
| 1352 | | try stack.append(State { .AddComments = AddCommentsCtx { |
| 1353 | | .node_ptr = statement, |
| 1354 | | .comments = comments, |
| 1355 | | }}); |
| 1356 | 1360 | try stack.append(State { .AssignmentExpressionBegin = OptionalCtx{ .Required = statement } }); |
| 1357 | 1361 | continue; |
| 1358 | 1362 | } |
| 1359 | 1363 | } |
| 1360 | 1364 | }, |
| 1361 | 1365 | State.ComptimeStatement => |ctx| { |
| 1362 | | const comments = try self.eatComments(arena); |
| 1363 | 1366 | const token = self.getNextToken(); |
| 1364 | 1367 | switch (token.id) { |
| 1365 | 1368 | Token.Id.Keyword_var, Token.Id.Keyword_const => { |
| 1366 | 1369 | stack.append(State { |
| 1367 | 1370 | .VarDecl = VarDeclCtx { |
| 1368 | | .comments = comments, |
| 1371 | .comments = null, |
| 1369 | 1372 | .visib_token = null, |
| 1370 | 1373 | .comptime_token = ctx.comptime_token, |
| 1371 | 1374 | .extern_export_token = null, |
| ... | ... | @@ -1395,12 +1398,6 @@ pub const Parser = struct { |
| 1395 | 1398 | continue; |
| 1396 | 1399 | }, |
| 1397 | 1400 | |
| 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 | 1401 | State.LookForSameLineComment => |node_ptr| { |
| 1405 | 1402 | try self.lookForSameLineComment(arena, *node_ptr); |
| 1406 | 1403 | continue; |
| ... | ... | @@ -1521,7 +1518,6 @@ pub const Parser = struct { |
| 1521 | 1518 | const node = try arena.construct(ast.Node.FieldInitializer { |
| 1522 | 1519 | .base = ast.Node { |
| 1523 | 1520 | .id = ast.Node.Id.FieldInitializer, |
| 1524 | | .doc_comments = null, |
| 1525 | 1521 | .same_line_comment = null, |
| 1526 | 1522 | }, |
| 1527 | 1523 | .period_token = undefined, |
| ... | ... | @@ -1566,7 +1562,7 @@ pub const Parser = struct { |
| 1566 | 1562 | try stack.append(State { .ContainerDecl = container_decl }); |
| 1567 | 1563 | continue; |
| 1568 | 1564 | }, |
| 1569 | | State.IdentifierListItemOrEnd => |list_state| { |
| 1565 | State.ErrorTagListItemOrEnd => |list_state| { |
| 1570 | 1566 | while (try self.eatLineComment(arena)) |line_comment| { |
| 1571 | 1567 | try list_state.list.append(&line_comment.base); |
| 1572 | 1568 | } |
| ... | ... | @@ -1576,23 +1572,18 @@ pub const Parser = struct { |
| 1576 | 1572 | continue; |
| 1577 | 1573 | } |
| 1578 | 1574 | |
| 1579 | | const comments = try self.eatComments(arena); |
| 1580 | 1575 | const node_ptr = try list_state.list.addOne(); |
| 1581 | 1576 | |
| 1582 | | try stack.append(State { .AddComments = AddCommentsCtx { |
| 1583 | | .node_ptr = node_ptr, |
| 1584 | | .comments = comments, |
| 1585 | | }}); |
| 1586 | | try stack.append(State { .IdentifierListCommaOrEnd = list_state }); |
| 1587 | | try stack.append(State { .Identifier = OptionalCtx { .Required = node_ptr } }); |
| 1577 | try stack.append(State { .ErrorTagListCommaOrEnd = list_state }); |
| 1578 | try stack.append(State { .ErrorTag = node_ptr }); |
| 1588 | 1579 | continue; |
| 1589 | 1580 | }, |
| 1590 | | State.IdentifierListCommaOrEnd => |list_state| { |
| 1581 | State.ErrorTagListCommaOrEnd => |list_state| { |
| 1591 | 1582 | if (try self.expectCommaOrEnd(Token.Id.RBrace)) |end| { |
| 1592 | 1583 | *list_state.ptr = end; |
| 1593 | 1584 | continue; |
| 1594 | 1585 | } else { |
| 1595 | | stack.append(State { .IdentifierListItemOrEnd = list_state }) catch unreachable; |
| 1586 | stack.append(State { .ErrorTagListItemOrEnd = list_state }) catch unreachable; |
| 1596 | 1587 | continue; |
| 1597 | 1588 | } |
| 1598 | 1589 | }, |
| ... | ... | @@ -1606,11 +1597,10 @@ pub const Parser = struct { |
| 1606 | 1597 | continue; |
| 1607 | 1598 | } |
| 1608 | 1599 | |
| 1609 | | const comments = try self.eatComments(arena); |
| 1600 | const comments = try self.eatDocComments(arena); |
| 1610 | 1601 | const node = try arena.construct(ast.Node.SwitchCase { |
| 1611 | 1602 | .base = ast.Node { |
| 1612 | 1603 | .id = ast.Node.Id.SwitchCase, |
| 1613 | | .doc_comments = comments, |
| 1614 | 1604 | .same_line_comment = null, |
| 1615 | 1605 | }, |
| 1616 | 1606 | .items = ArrayList(&ast.Node).init(arena), |
| ... | ... | @@ -1723,9 +1713,9 @@ pub const Parser = struct { |
| 1723 | 1713 | const fn_proto = try arena.construct(ast.Node.FnProto { |
| 1724 | 1714 | .base = ast.Node { |
| 1725 | 1715 | .id = ast.Node.Id.FnProto, |
| 1726 | | .doc_comments = ctx.comments, |
| 1727 | 1716 | .same_line_comment = null, |
| 1728 | 1717 | }, |
| 1718 | .doc_comments = ctx.comments, |
| 1729 | 1719 | .visib_token = null, |
| 1730 | 1720 | .name_token = null, |
| 1731 | 1721 | .fn_token = fn_token, |
| ... | ... | @@ -2593,7 +2583,6 @@ pub const Parser = struct { |
| 2593 | 2583 | const node = try arena.construct(ast.Node.PromiseType { |
| 2594 | 2584 | .base = ast.Node { |
| 2595 | 2585 | .id = ast.Node.Id.PromiseType, |
| 2596 | | .doc_comments = null, |
| 2597 | 2586 | .same_line_comment = null, |
| 2598 | 2587 | }, |
| 2599 | 2588 | .promise_token = token, |
| ... | ... | @@ -2719,9 +2708,9 @@ pub const Parser = struct { |
| 2719 | 2708 | const fn_proto = try arena.construct(ast.Node.FnProto { |
| 2720 | 2709 | .base = ast.Node { |
| 2721 | 2710 | .id = ast.Node.Id.FnProto, |
| 2722 | | .doc_comments = null, |
| 2723 | 2711 | .same_line_comment = null, |
| 2724 | 2712 | }, |
| 2713 | .doc_comments = null, |
| 2725 | 2714 | .visib_token = null, |
| 2726 | 2715 | .name_token = null, |
| 2727 | 2716 | .fn_token = token, |
| ... | ... | @@ -2743,9 +2732,9 @@ pub const Parser = struct { |
| 2743 | 2732 | const fn_proto = try arena.construct(ast.Node.FnProto { |
| 2744 | 2733 | .base = ast.Node { |
| 2745 | 2734 | .id = ast.Node.Id.FnProto, |
| 2746 | | .doc_comments = null, |
| 2747 | 2735 | .same_line_comment = null, |
| 2748 | 2736 | }, |
| 2737 | .doc_comments = null, |
| 2749 | 2738 | .visib_token = null, |
| 2750 | 2739 | .name_token = null, |
| 2751 | 2740 | .fn_token = undefined, |
| ... | ... | @@ -2836,7 +2825,6 @@ pub const Parser = struct { |
| 2836 | 2825 | const node = try arena.construct(ast.Node.ErrorSetDecl { |
| 2837 | 2826 | .base = ast.Node { |
| 2838 | 2827 | .id = ast.Node.Id.ErrorSetDecl, |
| 2839 | | .doc_comments = null, |
| 2840 | 2828 | .same_line_comment = null, |
| 2841 | 2829 | }, |
| 2842 | 2830 | .error_token = ctx.error_token, |
| ... | ... | @@ -2846,7 +2834,7 @@ pub const Parser = struct { |
| 2846 | 2834 | ctx.opt_ctx.store(&node.base); |
| 2847 | 2835 | |
| 2848 | 2836 | stack.append(State { |
| 2849 | | .IdentifierListItemOrEnd = ListSave(&ast.Node) { |
| 2837 | .ErrorTagListItemOrEnd = ListSave(&ast.Node) { |
| 2850 | 2838 | .list = &node.decls, |
| 2851 | 2839 | .ptr = &node.rbrace_token, |
| 2852 | 2840 | } |
| ... | ... | @@ -2866,6 +2854,7 @@ pub const Parser = struct { |
| 2866 | 2854 | } |
| 2867 | 2855 | ); |
| 2868 | 2856 | }, |
| 2857 | |
| 2869 | 2858 | State.Identifier => |opt_ctx| { |
| 2870 | 2859 | if (self.eatToken(Token.Id.Identifier)) |ident_token| { |
| 2871 | 2860 | _ = try self.createToCtxLiteral(arena, opt_ctx, ast.Node.Identifier, ident_token); |
| ... | ... | @@ -2878,6 +2867,25 @@ pub const Parser = struct { |
| 2878 | 2867 | } |
| 2879 | 2868 | }, |
| 2880 | 2869 | |
| 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 | }, |
| 2881 | 2889 | |
| 2882 | 2890 | State.ExpectToken => |token_id| { |
| 2883 | 2891 | _ = try self.expectToken(token_id); |
| ... | ... | @@ -2916,7 +2924,7 @@ pub const Parser = struct { |
| 2916 | 2924 | } |
| 2917 | 2925 | } |
| 2918 | 2926 | |
| 2919 | | fn eatComments(self: &Parser, arena: &mem.Allocator) !?&ast.Node.DocComment { |
| 2927 | fn eatDocComments(self: &Parser, arena: &mem.Allocator) !?&ast.Node.DocComment { |
| 2920 | 2928 | var result: ?&ast.Node.DocComment = null; |
| 2921 | 2929 | while (true) { |
| 2922 | 2930 | if (self.eatToken(Token.Id.DocComment)) |line_comment| { |
| ... | ... | @@ -2927,7 +2935,6 @@ pub const Parser = struct { |
| 2927 | 2935 | const comment_node = try arena.construct(ast.Node.DocComment { |
| 2928 | 2936 | .base = ast.Node { |
| 2929 | 2937 | .id = ast.Node.Id.DocComment, |
| 2930 | | .doc_comments = null, |
| 2931 | 2938 | .same_line_comment = null, |
| 2932 | 2939 | }, |
| 2933 | 2940 | .lines = ArrayList(Token).init(arena), |
| ... | ... | @@ -2949,7 +2956,6 @@ pub const Parser = struct { |
| 2949 | 2956 | return try arena.construct(ast.Node.LineComment { |
| 2950 | 2957 | .base = ast.Node { |
| 2951 | 2958 | .id = ast.Node.Id.LineComment, |
| 2952 | | .doc_comments = null, |
| 2953 | 2959 | .same_line_comment = null, |
| 2954 | 2960 | }, |
| 2955 | 2961 | .token = token, |
| ... | ... | @@ -3142,7 +3148,6 @@ pub const Parser = struct { |
| 3142 | 3148 | const node = try arena.construct(ast.Node.Switch { |
| 3143 | 3149 | .base = ast.Node { |
| 3144 | 3150 | .id = ast.Node.Id.Switch, |
| 3145 | | .doc_comments = null, |
| 3146 | 3151 | .same_line_comment = null, |
| 3147 | 3152 | }, |
| 3148 | 3153 | .switch_token = *token, |
| ... | ... | @@ -3170,6 +3175,7 @@ pub const Parser = struct { |
| 3170 | 3175 | .base = undefined, |
| 3171 | 3176 | .comptime_token = *token, |
| 3172 | 3177 | .expr = undefined, |
| 3178 | .doc_comments = null, |
| 3173 | 3179 | } |
| 3174 | 3180 | ); |
| 3175 | 3181 | try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } }); |
| ... | ... | @@ -3312,7 +3318,6 @@ pub const Parser = struct { |
| 3312 | 3318 | const id = ast.Node.typeToId(T); |
| 3313 | 3319 | break :blk ast.Node { |
| 3314 | 3320 | .id = id, |
| 3315 | | .doc_comments = null, |
| 3316 | 3321 | .same_line_comment = null, |
| 3317 | 3322 | }; |
| 3318 | 3323 | }; |
| ... | ... | @@ -3451,7 +3456,6 @@ pub const Parser = struct { |
| 3451 | 3456 | PrintIndent, |
| 3452 | 3457 | Indent: usize, |
| 3453 | 3458 | PrintSameLineComment: ?&Token, |
| 3454 | | PrintComments: &ast.Node, |
| 3455 | 3459 | }; |
| 3456 | 3460 | |
| 3457 | 3461 | pub fn renderSource(self: &Parser, stream: var, root_node: &ast.Node.Root) !void { |
| ... | ... | @@ -3490,7 +3494,7 @@ pub const Parser = struct { |
| 3490 | 3494 | switch (decl.id) { |
| 3491 | 3495 | ast.Node.Id.FnProto => { |
| 3492 | 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); |
| 3494 | 3498 | |
| 3495 | 3499 | if (fn_proto.body_node) |body_node| { |
| 3496 | 3500 | stack.append(RenderState { .Expression = body_node}) catch unreachable; |
| ... | ... | @@ -3512,12 +3516,12 @@ pub const Parser = struct { |
| 3512 | 3516 | }, |
| 3513 | 3517 | ast.Node.Id.VarDecl => { |
| 3514 | 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 | 3520 | try stack.append(RenderState { .VarDecl = var_decl}); |
| 3517 | 3521 | }, |
| 3518 | 3522 | ast.Node.Id.TestDecl => { |
| 3519 | 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 | 3525 | try stream.print("test "); |
| 3522 | 3526 | try stack.append(RenderState { .Expression = test_decl.body_node }); |
| 3523 | 3527 | try stack.append(RenderState { .Text = " " }); |
| ... | ... | @@ -3525,7 +3529,7 @@ pub const Parser = struct { |
| 3525 | 3529 | }, |
| 3526 | 3530 | ast.Node.Id.StructField => { |
| 3527 | 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 | 3533 | if (field.visib_token) |visib_token| { |
| 3530 | 3534 | try stream.print("{} ", self.tokenizer.getTokenSlice(visib_token)); |
| 3531 | 3535 | } |
| ... | ... | @@ -3535,7 +3539,7 @@ pub const Parser = struct { |
| 3535 | 3539 | }, |
| 3536 | 3540 | ast.Node.Id.UnionTag => { |
| 3537 | 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 | 3543 | try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token)); |
| 3540 | 3544 | |
| 3541 | 3545 | try stack.append(RenderState { .Text = "," }); |
| ... | ... | @@ -3552,7 +3556,7 @@ pub const Parser = struct { |
| 3552 | 3556 | }, |
| 3553 | 3557 | ast.Node.Id.EnumTag => { |
| 3554 | 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 | 3560 | try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token)); |
| 3557 | 3561 | |
| 3558 | 3562 | try stack.append(RenderState { .Text = "," }); |
| ... | ... | @@ -3561,6 +3565,11 @@ pub const Parser = struct { |
| 3561 | 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 | 3573 | ast.Node.Id.Comptime => { |
| 3565 | 3574 | if (requireSemiColon(decl)) { |
| 3566 | 3575 | try stack.append(RenderState { .Text = ";" }); |
| ... | ... | @@ -4122,11 +4131,20 @@ pub const Parser = struct { |
| 4122 | 4131 | |
| 4123 | 4132 | if (decls.len == 1) blk: { |
| 4124 | 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 | |
| 4126 | 4144 | |
| 4127 | 4145 | try stream.write("error{"); |
| 4128 | 4146 | try stack.append(RenderState { .Text = "}" }); |
| 4129 | | try stack.append(RenderState { .Expression = node }); |
| 4147 | try stack.append(RenderState { .TopLevelDecl = node }); |
| 4130 | 4148 | continue; |
| 4131 | 4149 | } |
| 4132 | 4150 | |
| ... | ... | @@ -4144,8 +4162,7 @@ pub const Parser = struct { |
| 4144 | 4162 | if (node.id != ast.Node.Id.LineComment) { |
| 4145 | 4163 | try stack.append(RenderState { .Text = "," }); |
| 4146 | 4164 | } |
| 4147 | | try stack.append(RenderState { .Expression = node }); |
| 4148 | | try stack.append(RenderState { .PrintComments = node }); |
| 4165 | try stack.append(RenderState { .TopLevelDecl = node }); |
| 4149 | 4166 | try stack.append(RenderState.PrintIndent); |
| 4150 | 4167 | try stack.append(RenderState { |
| 4151 | 4168 | .Text = blk: { |
| ... | ... | @@ -4304,8 +4321,6 @@ pub const Parser = struct { |
| 4304 | 4321 | ast.Node.Id.SwitchCase => { |
| 4305 | 4322 | const switch_case = @fieldParentPtr(ast.Node.SwitchCase, "base", base); |
| 4306 | 4323 | |
| 4307 | | try self.renderComments(stream, base, indent); |
| 4308 | | |
| 4309 | 4324 | try stack.append(RenderState { .PrintSameLineComment = base.same_line_comment }); |
| 4310 | 4325 | try stack.append(RenderState { .Text = "," }); |
| 4311 | 4326 | try stack.append(RenderState { .Expression = switch_case.expr }); |
| ... | ... | @@ -4617,6 +4632,7 @@ pub const Parser = struct { |
| 4617 | 4632 | ast.Node.Id.StructField, |
| 4618 | 4633 | ast.Node.Id.UnionTag, |
| 4619 | 4634 | ast.Node.Id.EnumTag, |
| 4635 | ast.Node.Id.ErrorTag, |
| 4620 | 4636 | ast.Node.Id.Root, |
| 4621 | 4637 | ast.Node.Id.VarDecl, |
| 4622 | 4638 | ast.Node.Id.Use, |
| ... | ... | @@ -4624,7 +4640,6 @@ pub const Parser = struct { |
| 4624 | 4640 | ast.Node.Id.ParamDecl => unreachable, |
| 4625 | 4641 | }, |
| 4626 | 4642 | RenderState.Statement => |base| { |
| 4627 | | try self.renderComments(stream, base, indent); |
| 4628 | 4643 | try stack.append(RenderState { .PrintSameLineComment = base.same_line_comment } ); |
| 4629 | 4644 | switch (base.id) { |
| 4630 | 4645 | ast.Node.Id.VarDecl => { |
| ... | ... | @@ -4645,15 +4660,11 @@ pub const Parser = struct { |
| 4645 | 4660 | const comment_token = maybe_comment ?? break :blk; |
| 4646 | 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 | } |
| 4655 | 4666 | |
| 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 | 4668 | const comment = node.doc_comments ?? return; |
| 4658 | 4669 | for (comment.lines.toSliceConst()) |line_token| { |
| 4659 | 4670 | try stream.print("{}\n", self.tokenizer.getTokenSlice(line_token)); |