authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-21 23:24:31-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-21 23:25:15-04:00
log8252c8b9d698ca671413ad4e23c5228dcf505632
tree6cf86cafec22f11d7ae1dc929016860f0077b332
parent19de25993654f3e089eaf46391612e8a7e406d7e

stage2 parser: different multiline string literal parsing strategy

and using flat memory rather than singly linked list roughly equivalent performance, slightly reduced memory usage, better API.

3 files changed, 120 insertions(+), 82 deletions(-)

lib/std/zig/ast.zig+92-71
......@@ -401,7 +401,6 @@ pub const Node = struct {
401401 /// All the child Node types use this same Iterator state for their iteration.
402402 pub const Iterator = struct {
403403 parent_node: *const Node,
404 node: ?*LinkedList(*Node).Node,
405404 index: usize,
406405
407406 pub fn next(it: *Iterator) ?*Node {
......@@ -648,7 +647,7 @@ pub const Node = struct {
648647 }
649648
650649 pub fn iterate(self: *const Root) Node.Iterator {
651 return .{ .parent_node = &self.base, .index = 0, .node = null };
650 return .{ .parent_node = &self.base, .index = 0 };
652651 }
653652
654653 pub fn iterateNext(self: *const Root, it: *Node.Iterator) ?*Node {
......@@ -702,7 +701,7 @@ pub const Node = struct {
702701 semicolon_token: TokenIndex,
703702
704703 pub fn iterate(self: *const VarDecl) Node.Iterator {
705 return .{ .parent_node = &self.base, .index = 0, .node = null };
704 return .{ .parent_node = &self.base, .index = 0 };
706705 }
707706
708707 pub fn iterateNext(self: *const VarDecl, it: *Node.Iterator) ?*Node {
......@@ -756,7 +755,7 @@ pub const Node = struct {
756755 semicolon_token: TokenIndex,
757756
758757 pub fn iterate(self: *const Use) Node.Iterator {
759 return .{ .parent_node = &self.base, .index = 0, .node = null };
758 return .{ .parent_node = &self.base, .index = 0 };
760759 }
761760
762761 pub fn iterateNext(self: *const Use, it: *Node.Iterator) ?*Node {
......@@ -797,13 +796,17 @@ pub const Node = struct {
797796 }
798797
799798 pub fn iterate(self: *const ErrorSetDecl) Node.Iterator {
800 return .{ .parent_node = &self.base, .index = 0, .node = null };
799 return .{ .parent_node = &self.base, .index = 0 };
801800 }
802801
803802 pub fn iterateNext(self: *const ErrorSetDecl, it: *Node.Iterator) ?*Node {
804 const decl = it.node orelse return null;
805 it.node = decl.next;
806 return decl.data;
803 var i = it.index;
804 it.index += 1;
805
806 if (i < self.decls_len) return self.declsConst()[i];
807 i -= self.decls_len;
808
809 return null;
807810 }
808811
809812 pub fn firstToken(self: *const ErrorSetDecl) TokenIndex {
......@@ -857,7 +860,7 @@ pub const Node = struct {
857860 }
858861
859862 pub fn iterate(self: *const ContainerDecl) Node.Iterator {
860 return .{ .parent_node = &self.base, .index = 0, .node = null };
863 return .{ .parent_node = &self.base, .index = 0 };
861864 }
862865
863866 pub fn iterateNext(self: *const ContainerDecl, it: *Node.Iterator) ?*Node {
......@@ -914,7 +917,7 @@ pub const Node = struct {
914917 align_expr: ?*Node,
915918
916919 pub fn iterate(self: *const ContainerField) Node.Iterator {
917 return .{ .parent_node = &self.base, .index = 0, .node = null };
920 return .{ .parent_node = &self.base, .index = 0 };
918921 }
919922
920923 pub fn iterateNext(self: *const ContainerField, it: *Node.Iterator) ?*Node {
......@@ -966,7 +969,7 @@ pub const Node = struct {
966969 name_token: TokenIndex,
967970
968971 pub fn iterate(self: *const ErrorTag) Node.Iterator {
969 return .{ .parent_node = &self.base, .index = 0, .node = null };
972 return .{ .parent_node = &self.base, .index = 0 };
970973 }
971974
972975 pub fn iterateNext(self: *const ErrorTag, it: *Node.Iterator) ?*Node {
......@@ -995,7 +998,7 @@ pub const Node = struct {
995998 token: TokenIndex,
996999
9971000 pub fn iterate(self: *const Identifier) Node.Iterator {
998 return .{ .parent_node = &self.base, .index = 0, .node = null };
1001 return .{ .parent_node = &self.base, .index = 0 };
9991002 }
10001003
10011004 pub fn iterateNext(self: *const Identifier, it: *Node.Iterator) ?*Node {
......@@ -1050,7 +1053,7 @@ pub const Node = struct {
10501053 };
10511054
10521055 pub fn iterate(self: *const ParamDecl) Node.Iterator {
1053 return .{ .parent_node = &self.base, .index = 0, .node = null };
1056 return .{ .parent_node = &self.base, .index = 0 };
10541057 }
10551058
10561059 pub fn iterateNext(self: *const ParamDecl, it: *Node.Iterator) ?*Node {
......@@ -1098,7 +1101,7 @@ pub const Node = struct {
10981101 }
10991102
11001103 pub fn iterate(self: *const FnProto) Node.Iterator {
1101 return .{ .parent_node = &self.base, .index = 0, .node = null };
1104 return .{ .parent_node = &self.base, .index = 0 };
11021105 }
11031106
11041107 pub fn iterateNext(self: *const FnProto, it: *Node.Iterator) ?*Node {
......@@ -1189,7 +1192,7 @@ pub const Node = struct {
11891192 };
11901193
11911194 pub fn iterate(self: *const AnyFrameType) Node.Iterator {
1192 return .{ .parent_node = &self.base, .index = 0, .node = null };
1195 return .{ .parent_node = &self.base, .index = 0 };
11931196 }
11941197
11951198 pub fn iterateNext(self: *const AnyFrameType, it: *Node.Iterator) ?*Node {
......@@ -1234,7 +1237,7 @@ pub const Node = struct {
12341237 }
12351238
12361239 pub fn iterate(self: *const Block) Node.Iterator {
1237 return .{ .parent_node = &self.base, .index = 0, .node = null };
1240 return .{ .parent_node = &self.base, .index = 0 };
12381241 }
12391242
12401243 pub fn iterateNext(self: *const Block, it: *Node.Iterator) ?*Node {
......@@ -1281,7 +1284,7 @@ pub const Node = struct {
12811284 expr: *Node,
12821285
12831286 pub fn iterate(self: *const Defer) Node.Iterator {
1284 return .{ .parent_node = &self.base, .index = 0, .node = null };
1287 return .{ .parent_node = &self.base, .index = 0 };
12851288 }
12861289
12871290 pub fn iterateNext(self: *const Defer, it: *Node.Iterator) ?*Node {
......@@ -1310,7 +1313,7 @@ pub const Node = struct {
13101313 expr: *Node,
13111314
13121315 pub fn iterate(self: *const Comptime) Node.Iterator {
1313 return .{ .parent_node = &self.base, .index = 0, .node = null };
1316 return .{ .parent_node = &self.base, .index = 0 };
13141317 }
13151318
13161319 pub fn iterateNext(self: *const Comptime, it: *Node.Iterator) ?*Node {
......@@ -1338,7 +1341,7 @@ pub const Node = struct {
13381341 expr: *Node,
13391342
13401343 pub fn iterate(self: *const Nosuspend) Node.Iterator {
1341 return .{ .parent_node = &self.base, .index = 0, .node = null };
1344 return .{ .parent_node = &self.base, .index = 0 };
13421345 }
13431346
13441347 pub fn iterateNext(self: *const Nosuspend, it: *Node.Iterator) ?*Node {
......@@ -1367,7 +1370,7 @@ pub const Node = struct {
13671370 rpipe: TokenIndex,
13681371
13691372 pub fn iterate(self: *const Payload) Node.Iterator {
1370 return .{ .parent_node = &self.base, .index = 0, .node = null };
1373 return .{ .parent_node = &self.base, .index = 0 };
13711374 }
13721375
13731376 pub fn iterateNext(self: *const Payload, it: *Node.Iterator) ?*Node {
......@@ -1397,7 +1400,7 @@ pub const Node = struct {
13971400 rpipe: TokenIndex,
13981401
13991402 pub fn iterate(self: *const PointerPayload) Node.Iterator {
1400 return .{ .parent_node = &self.base, .index = 0, .node = null };
1403 return .{ .parent_node = &self.base, .index = 0 };
14011404 }
14021405
14031406 pub fn iterateNext(self: *const PointerPayload, it: *Node.Iterator) ?*Node {
......@@ -1428,7 +1431,7 @@ pub const Node = struct {
14281431 rpipe: TokenIndex,
14291432
14301433 pub fn iterate(self: *const PointerIndexPayload) Node.Iterator {
1431 return .{ .parent_node = &self.base, .index = 0, .node = null };
1434 return .{ .parent_node = &self.base, .index = 0 };
14321435 }
14331436
14341437 pub fn iterateNext(self: *const PointerIndexPayload, it: *Node.Iterator) ?*Node {
......@@ -1462,7 +1465,7 @@ pub const Node = struct {
14621465 body: *Node,
14631466
14641467 pub fn iterate(self: *const Else) Node.Iterator {
1465 return .{ .parent_node = &self.base, .index = 0, .node = null };
1468 return .{ .parent_node = &self.base, .index = 0 };
14661469 }
14671470
14681471 pub fn iterateNext(self: *const Else, it: *Node.Iterator) ?*Node {
......@@ -1510,7 +1513,7 @@ pub const Node = struct {
15101513 }
15111514
15121515 pub fn iterate(self: *const Switch) Node.Iterator {
1513 return .{ .parent_node = &self.base, .index = 0, .node = null };
1516 return .{ .parent_node = &self.base, .index = 0 };
15141517 }
15151518
15161519 pub fn iterateNext(self: *const Switch, it: *Node.Iterator) ?*Node {
......@@ -1520,11 +1523,8 @@ pub const Node = struct {
15201523 if (i < 1) return self.expr;
15211524 i -= 1;
15221525
1523 if (it.node) |child| {
1524 it.index -= 1;
1525 it.node = child.next;
1526 return child.data;
1527 }
1526 if (i < self.cases_len) return self.casesConst()[i];
1527 i -= self.cases_len;
15281528
15291529 return null;
15301530 }
......@@ -1572,7 +1572,7 @@ pub const Node = struct {
15721572 }
15731573
15741574 pub fn iterate(self: *const SwitchCase) Node.Iterator {
1575 return .{ .parent_node = &self.base, .index = 0, .node = null };
1575 return .{ .parent_node = &self.base, .index = 0 };
15761576 }
15771577
15781578 pub fn iterateNext(self: *const SwitchCase, it: *Node.Iterator) ?*Node {
......@@ -1621,7 +1621,7 @@ pub const Node = struct {
16211621 token: TokenIndex,
16221622
16231623 pub fn iterate(self: *const SwitchElse) Node.Iterator {
1624 return .{ .parent_node = &self.base, .index = 0, .node = null };
1624 return .{ .parent_node = &self.base, .index = 0 };
16251625 }
16261626
16271627 pub fn iterateNext(self: *const SwitchElse, it: *Node.Iterator) ?*Node {
......@@ -1649,7 +1649,7 @@ pub const Node = struct {
16491649 @"else": ?*Else,
16501650
16511651 pub fn iterate(self: *const While) Node.Iterator {
1652 return .{ .parent_node = &self.base, .index = 0, .node = null };
1652 return .{ .parent_node = &self.base, .index = 0 };
16531653 }
16541654
16551655 pub fn iterateNext(self: *const While, it: *Node.Iterator) ?*Node {
......@@ -1712,7 +1712,7 @@ pub const Node = struct {
17121712 @"else": ?*Else,
17131713
17141714 pub fn iterate(self: *const For) Node.Iterator {
1715 return .{ .parent_node = &self.base, .index = 0, .node = null };
1715 return .{ .parent_node = &self.base, .index = 0 };
17161716 }
17171717
17181718 pub fn iterateNext(self: *const For, it: *Node.Iterator) ?*Node {
......@@ -1766,7 +1766,7 @@ pub const Node = struct {
17661766 @"else": ?*Else,
17671767
17681768 pub fn iterate(self: *const If) Node.Iterator {
1769 return .{ .parent_node = &self.base, .index = 0, .node = null };
1769 return .{ .parent_node = &self.base, .index = 0 };
17701770 }
17711771
17721772 pub fn iterateNext(self: *const If, it: *Node.Iterator) ?*Node {
......@@ -1859,7 +1859,7 @@ pub const Node = struct {
18591859 };
18601860
18611861 pub fn iterate(self: *const InfixOp) Node.Iterator {
1862 return .{ .parent_node = &self.base, .index = 0, .node = null };
1862 return .{ .parent_node = &self.base, .index = 0 };
18631863 }
18641864
18651865 pub fn iterateNext(self: *const InfixOp, it: *Node.Iterator) ?*Node {
......@@ -1982,7 +1982,7 @@ pub const Node = struct {
19821982 };
19831983
19841984 pub fn iterate(self: *const PrefixOp) Node.Iterator {
1985 return .{ .parent_node = &self.base, .index = 0, .node = null };
1985 return .{ .parent_node = &self.base, .index = 0 };
19861986 }
19871987
19881988 pub fn iterateNext(self: *const PrefixOp, it: *Node.Iterator) ?*Node {
......@@ -2045,7 +2045,7 @@ pub const Node = struct {
20452045 expr: *Node,
20462046
20472047 pub fn iterate(self: *const FieldInitializer) Node.Iterator {
2048 return .{ .parent_node = &self.base, .index = 0, .node = null };
2048 return .{ .parent_node = &self.base, .index = 0 };
20492049 }
20502050
20512051 pub fn iterateNext(self: *const FieldInitializer, it: *Node.Iterator) ?*Node {
......@@ -2086,7 +2086,7 @@ pub const Node = struct {
20862086 }
20872087
20882088 pub fn iterate(self: *const ArrayInitializer) Node.Iterator {
2089 return .{ .parent_node = &self.base, .index = 0, .node = null };
2089 return .{ .parent_node = &self.base, .index = 0 };
20902090 }
20912091
20922092 pub fn iterateNext(self: *const ArrayInitializer, it: *Node.Iterator) ?*Node {
......@@ -2144,7 +2144,7 @@ pub const Node = struct {
21442144 }
21452145
21462146 pub fn iterate(self: *const ArrayInitializerDot) Node.Iterator {
2147 return .{ .parent_node = &self.base, .index = 0, .node = null };
2147 return .{ .parent_node = &self.base, .index = 0 };
21482148 }
21492149
21502150 pub fn iterateNext(self: *const ArrayInitializerDot, it: *Node.Iterator) ?*Node {
......@@ -2199,7 +2199,7 @@ pub const Node = struct {
21992199 }
22002200
22012201 pub fn iterate(self: *const StructInitializer) Node.Iterator {
2202 return .{ .parent_node = &self.base, .index = 0, .node = null };
2202 return .{ .parent_node = &self.base, .index = 0 };
22032203 }
22042204
22052205 pub fn iterateNext(self: *const StructInitializer, it: *Node.Iterator) ?*Node {
......@@ -2257,7 +2257,7 @@ pub const Node = struct {
22572257 }
22582258
22592259 pub fn iterate(self: *const StructInitializerDot) Node.Iterator {
2260 return .{ .parent_node = &self.base, .index = 0, .node = null };
2260 return .{ .parent_node = &self.base, .index = 0 };
22612261 }
22622262
22632263 pub fn iterateNext(self: *const StructInitializerDot, it: *Node.Iterator) ?*Node {
......@@ -2313,7 +2313,7 @@ pub const Node = struct {
23132313 }
23142314
23152315 pub fn iterate(self: *const Call) Node.Iterator {
2316 return .{ .parent_node = &self.base, .index = 0, .node = null};
2316 return .{ .parent_node = &self.base, .index = 0};
23172317 }
23182318
23192319 pub fn iterateNext(self: *const Call, it: *Node.Iterator) ?*Node {
......@@ -2373,7 +2373,7 @@ pub const Node = struct {
23732373 };
23742374
23752375 pub fn iterate(self: *const SuffixOp) Node.Iterator {
2376 return .{ .parent_node = &self.base, .index = 0, .node = null};
2376 return .{ .parent_node = &self.base, .index = 0};
23772377 }
23782378
23792379 pub fn iterateNext(self: *const SuffixOp, it: *Node.Iterator) ?*Node {
......@@ -2425,7 +2425,7 @@ pub const Node = struct {
24252425 rparen: TokenIndex,
24262426
24272427 pub fn iterate(self: *const GroupedExpression) Node.Iterator {
2428 return .{ .parent_node = &self.base, .index = 0, .node = null };
2428 return .{ .parent_node = &self.base, .index = 0 };
24292429 }
24302430
24312431 pub fn iterateNext(self: *const GroupedExpression, it: *Node.Iterator) ?*Node {
......@@ -2460,7 +2460,7 @@ pub const Node = struct {
24602460 };
24612461
24622462 pub fn iterate(self: *const ControlFlowExpression) Node.Iterator {
2463 return .{ .parent_node = &self.base, .index = 0, .node = null };
2463 return .{ .parent_node = &self.base, .index = 0 };
24642464 }
24652465
24662466 pub fn iterateNext(self: *const ControlFlowExpression, it: *Node.Iterator) ?*Node {
......@@ -2513,7 +2513,7 @@ pub const Node = struct {
25132513 body: ?*Node,
25142514
25152515 pub fn iterate(self: *const Suspend) Node.Iterator {
2516 return .{ .parent_node = &self.base, .index = 0, .node = null };
2516 return .{ .parent_node = &self.base, .index = 0 };
25172517 }
25182518
25192519 pub fn iterateNext(self: *const Suspend, it: *Node.Iterator) ?*Node {
......@@ -2546,7 +2546,7 @@ pub const Node = struct {
25462546 token: TokenIndex,
25472547
25482548 pub fn iterate(self: *const IntegerLiteral) Node.Iterator {
2549 return .{ .parent_node = &self.base, .index = 0, .node = null };
2549 return .{ .parent_node = &self.base, .index = 0 };
25502550 }
25512551
25522552 pub fn iterateNext(self: *const IntegerLiteral, it: *Node.Iterator) ?*Node {
......@@ -2568,7 +2568,7 @@ pub const Node = struct {
25682568 name: TokenIndex,
25692569
25702570 pub fn iterate(self: *const EnumLiteral) Node.Iterator {
2571 return .{ .parent_node = &self.base, .index = 0, .node = null };
2571 return .{ .parent_node = &self.base, .index = 0 };
25722572 }
25732573
25742574 pub fn iterateNext(self: *const EnumLiteral, it: *Node.Iterator) ?*Node {
......@@ -2589,7 +2589,7 @@ pub const Node = struct {
25892589 token: TokenIndex,
25902590
25912591 pub fn iterate(self: *const FloatLiteral) Node.Iterator {
2592 return .{ .parent_node = &self.base, .index = 0, .node = null };
2592 return .{ .parent_node = &self.base, .index = 0 };
25932593 }
25942594
25952595 pub fn iterateNext(self: *const FloatLiteral, it: *Node.Iterator) ?*Node {
......@@ -2624,7 +2624,7 @@ pub const Node = struct {
26242624 }
26252625
26262626 pub fn iterate(self: *const BuiltinCall) Node.Iterator {
2627 return .{ .parent_node = &self.base, .index = 0, .node = null };
2627 return .{ .parent_node = &self.base, .index = 0 };
26282628 }
26292629
26302630 pub fn iterateNext(self: *const BuiltinCall, it: *Node.Iterator) ?*Node {
......@@ -2665,7 +2665,7 @@ pub const Node = struct {
26652665 token: TokenIndex,
26662666
26672667 pub fn iterate(self: *const StringLiteral) Node.Iterator {
2668 return .{ .parent_node = &self.base, .index = 0, .node = null };
2668 return .{ .parent_node = &self.base, .index = 0 };
26692669 }
26702670
26712671 pub fn iterateNext(self: *const StringLiteral, it: *Node.Iterator) ?*Node {
......@@ -2681,14 +2681,24 @@ pub const Node = struct {
26812681 }
26822682 };
26832683
2684 /// The string literal tokens appear directly in memory after MultilineStringLiteral.
26842685 pub const MultilineStringLiteral = struct {
26852686 base: Node = Node{ .id = .MultilineStringLiteral },
2686 lines: LineList,
2687 lines_len: TokenIndex,
26872688
2688 pub const LineList = LinkedList(TokenIndex);
2689 /// After this the caller must initialize the lines list.
2690 pub fn alloc(allocator: *mem.Allocator, lines_len: NodeIndex) !*MultilineStringLiteral {
2691 const bytes = try allocator.alignedAlloc(u8, @alignOf(MultilineStringLiteral), sizeInBytes(lines_len));
2692 return @ptrCast(*MultilineStringLiteral, bytes.ptr);
2693 }
2694
2695 pub fn free(self: *MultilineStringLiteral, allocator: *mem.Allocator) void {
2696 const bytes = @ptrCast([*]u8, self)[0..sizeInBytes(self.lines_len)];
2697 allocator.free(bytes);
2698 }
26892699
26902700 pub fn iterate(self: *const MultilineStringLiteral) Node.Iterator {
2691 return .{ .parent_node = &self.base, .index = 0, .node = null };
2701 return .{ .parent_node = &self.base, .index = 0 };
26922702 }
26932703
26942704 pub fn iterateNext(self: *const MultilineStringLiteral, it: *Node.Iterator) ?*Node {
......@@ -2696,14 +2706,25 @@ pub const Node = struct {
26962706 }
26972707
26982708 pub fn firstToken(self: *const MultilineStringLiteral) TokenIndex {
2699 return self.lines.first.?.data;
2709 return self.linesConst()[0];
27002710 }
27012711
27022712 pub fn lastToken(self: *const MultilineStringLiteral) TokenIndex {
2703 var node = self.lines.first.?;
2704 while (true) {
2705 node = node.next orelse return node.data;
2706 }
2713 return self.linesConst()[self.lines_len - 1];
2714 }
2715
2716 pub fn lines(self: *MultilineStringLiteral) []TokenIndex {
2717 const decls_start = @ptrCast([*]u8, self) + @sizeOf(MultilineStringLiteral);
2718 return @ptrCast([*]TokenIndex, decls_start)[0..self.lines_len];
2719 }
2720
2721 pub fn linesConst(self: *const MultilineStringLiteral) []const TokenIndex {
2722 const decls_start = @ptrCast([*]const u8, self) + @sizeOf(MultilineStringLiteral);
2723 return @ptrCast([*]const TokenIndex, decls_start)[0..self.lines_len];
2724 }
2725
2726 fn sizeInBytes(lines_len: NodeIndex) usize {
2727 return @sizeOf(MultilineStringLiteral) + @sizeOf(TokenIndex) * @as(usize, lines_len);
27072728 }
27082729 };
27092730
......@@ -2712,7 +2733,7 @@ pub const Node = struct {
27122733 token: TokenIndex,
27132734
27142735 pub fn iterate(self: *const CharLiteral) Node.Iterator {
2715 return .{ .parent_node = &self.base, .index = 0, .node = null };
2736 return .{ .parent_node = &self.base, .index = 0 };
27162737 }
27172738
27182739 pub fn iterateNext(self: *const CharLiteral, it: *Node.Iterator) ?*Node {
......@@ -2733,7 +2754,7 @@ pub const Node = struct {
27332754 token: TokenIndex,
27342755
27352756 pub fn iterate(self: *const BoolLiteral) Node.Iterator {
2736 return .{ .parent_node = &self.base, .index = 0, .node = null };
2757 return .{ .parent_node = &self.base, .index = 0 };
27372758 }
27382759
27392760 pub fn iterateNext(self: *const BoolLiteral, it: *Node.Iterator) ?*Node {
......@@ -2754,7 +2775,7 @@ pub const Node = struct {
27542775 token: TokenIndex,
27552776
27562777 pub fn iterate(self: *const NullLiteral) Node.Iterator {
2757 return .{ .parent_node = &self.base, .index = 0, .node = null };
2778 return .{ .parent_node = &self.base, .index = 0 };
27582779 }
27592780
27602781 pub fn iterateNext(self: *const NullLiteral, it: *Node.Iterator) ?*Node {
......@@ -2775,7 +2796,7 @@ pub const Node = struct {
27752796 token: TokenIndex,
27762797
27772798 pub fn iterate(self: *const UndefinedLiteral) Node.Iterator {
2778 return .{ .parent_node = &self.base, .index = 0, .node = null };
2799 return .{ .parent_node = &self.base, .index = 0 };
27792800 }
27802801
27812802 pub fn iterateNext(self: *const UndefinedLiteral, it: *Node.Iterator) ?*Node {
......@@ -2815,7 +2836,7 @@ pub const Node = struct {
28152836 };
28162837
28172838 pub fn iterate(self: *const Output) Node.Iterator {
2818 return .{ .parent_node = &self.base, .index = 0, .node = null };
2839 return .{ .parent_node = &self.base, .index = 0 };
28192840 }
28202841
28212842 pub fn iterateNext(self: *const Output, it: *Node.Iterator) ?*Node {
......@@ -2859,7 +2880,7 @@ pub const Node = struct {
28592880 rparen: TokenIndex,
28602881
28612882 pub fn iterate(self: *const Input) Node.Iterator {
2862 return .{ .parent_node = &self.base, .index = 0, .node = null };
2883 return .{ .parent_node = &self.base, .index = 0 };
28632884 }
28642885
28652886 pub fn iterateNext(self: *const Input, it: *Node.Iterator) ?*Node {
......@@ -2889,7 +2910,7 @@ pub const Node = struct {
28892910
28902911
28912912 pub fn iterate(self: *const Asm) Node.Iterator {
2892 return .{ .parent_node = &self.base, .index = 0, .node = null};
2913 return .{ .parent_node = &self.base, .index = 0};
28932914 }
28942915
28952916 pub fn iterateNext(self: *const Asm, it: *Node.Iterator) ?*Node {
......@@ -2932,7 +2953,7 @@ pub const Node = struct {
29322953 token: TokenIndex,
29332954
29342955 pub fn iterate(self: *const Unreachable) Node.Iterator {
2935 return .{ .parent_node = &self.base, .index = 0, .node = null };
2956 return .{ .parent_node = &self.base, .index = 0 };
29362957 }
29372958
29382959 pub fn iterateNext(self: *const Unreachable, it: *Node.Iterator) ?*Node {
......@@ -2953,7 +2974,7 @@ pub const Node = struct {
29532974 token: TokenIndex,
29542975
29552976 pub fn iterate(self: *const ErrorType) Node.Iterator {
2956 return .{ .parent_node = &self.base, .index = 0, .node = null };
2977 return .{ .parent_node = &self.base, .index = 0 };
29572978 }
29582979
29592980 pub fn iterateNext(self: *const ErrorType, it: *Node.Iterator) ?*Node {
......@@ -2974,7 +2995,7 @@ pub const Node = struct {
29742995 token: TokenIndex,
29752996
29762997 pub fn iterate(self: *const VarType) Node.Iterator {
2977 return .{ .parent_node = &self.base, .index = 0, .node = null };
2998 return .{ .parent_node = &self.base, .index = 0 };
29782999 }
29793000
29803001 pub fn iterateNext(self: *const VarType, it: *Node.Iterator) ?*Node {
......@@ -2997,7 +3018,7 @@ pub const Node = struct {
29973018 pub const LineList = LinkedList(TokenIndex);
29983019
29993020 pub fn iterate(self: *const DocComment) Node.Iterator {
3000 return .{ .parent_node = &self.base, .index = 0, .node = null };
3021 return .{ .parent_node = &self.base, .index = 0 };
30013022 }
30023023
30033024 pub fn iterateNext(self: *const DocComment, it: *Node.Iterator) ?*Node {
......@@ -3024,7 +3045,7 @@ pub const Node = struct {
30243045 body_node: *Node,
30253046
30263047 pub fn iterate(self: *const TestDecl) Node.Iterator {
3027 return .{ .parent_node = &self.base, .index = 0, .node = null };
3048 return .{ .parent_node = &self.base, .index = 0 };
30283049 }
30293050
30303051 pub fn iterateNext(self: *const TestDecl, it: *Node.Iterator) ?*Node {
lib/std/zig/parse.zig+27-8
......@@ -3105,15 +3105,34 @@ const Parser = struct {
31053105 if (try p.parseStringLiteralSingle()) |node| return node;
31063106
31073107 if (p.eatToken(.MultilineStringLiteralLine)) |first_line| {
3108 const node = try p.arena.allocator.create(Node.MultilineStringLiteral);
3109 node.* = .{
3110 .lines = Node.MultilineStringLiteral.LineList{},
3111 };
3112 var lines_it = &node.lines.first;
3113 lines_it = try p.llpush(TokenIndex, lines_it, first_line);
3114 while (p.eatToken(.MultilineStringLiteralLine)) |line|
3115 lines_it = try p.llpush(TokenIndex, lines_it, line);
3108 const start_tok_i = p.tok_i;
3109 var tok_i = start_tok_i;
3110 var count: usize = 1; // including first_line
3111 while (true) : (tok_i += 1) {
3112 switch (p.tokens[tok_i].id) {
3113 .LineComment => continue,
3114 .MultilineStringLiteralLine => count += 1,
3115 else => break,
3116 }
3117 }
31163118
3119 const node = try Node.MultilineStringLiteral.alloc(&p.arena.allocator, count);
3120 node.* = .{ .lines_len = count };
3121 const lines = node.lines();
3122 tok_i = start_tok_i;
3123 lines[0] = first_line;
3124 count = 1;
3125 while (true) : (tok_i += 1) {
3126 switch (p.tokens[tok_i].id) {
3127 .LineComment => continue,
3128 .MultilineStringLiteralLine => {
3129 lines[count] = tok_i;
3130 count += 1;
3131 },
3132 else => break,
3133 }
3134 }
3135 p.tok_i = tok_i;
31173136 return &node.base;
31183137 }
31193138
lib/std/zig/render.zig+1-3
......@@ -1376,9 +1376,7 @@ fn renderExpression(
13761376 skip_first_indent = false;
13771377 }
13781378
1379 var it = multiline_str_literal.lines.first;
1380 while (it) |t_node| : (it = t_node.next) {
1381 const t = t_node.data;
1379 for (multiline_str_literal.lines()) |t| {
13821380 if (!skip_first_indent) {
13831381 try stream.writeByteNTimes(' ', indent + indent_delta);
13841382 }