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 {...@@ -401,7 +401,6 @@ pub const Node = struct {
401 /// All the child Node types use this same Iterator state for their iteration.401 /// All the child Node types use this same Iterator state for their iteration.
402 pub const Iterator = struct {402 pub const Iterator = struct {
403 parent_node: *const Node,403 parent_node: *const Node,
404 node: ?*LinkedList(*Node).Node,
405 index: usize,404 index: usize,
406405
407 pub fn next(it: *Iterator) ?*Node {406 pub fn next(it: *Iterator) ?*Node {
...@@ -648,7 +647,7 @@ pub const Node = struct {...@@ -648,7 +647,7 @@ pub const Node = struct {
648 }647 }
649648
650 pub fn iterate(self: *const Root) Node.Iterator {649 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 };
652 }651 }
653652
654 pub fn iterateNext(self: *const Root, it: *Node.Iterator) ?*Node {653 pub fn iterateNext(self: *const Root, it: *Node.Iterator) ?*Node {
...@@ -702,7 +701,7 @@ pub const Node = struct {...@@ -702,7 +701,7 @@ pub const Node = struct {
702 semicolon_token: TokenIndex,701 semicolon_token: TokenIndex,
703702
704 pub fn iterate(self: *const VarDecl) Node.Iterator {703 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 };
706 }705 }
707706
708 pub fn iterateNext(self: *const VarDecl, it: *Node.Iterator) ?*Node {707 pub fn iterateNext(self: *const VarDecl, it: *Node.Iterator) ?*Node {
...@@ -756,7 +755,7 @@ pub const Node = struct {...@@ -756,7 +755,7 @@ pub const Node = struct {
756 semicolon_token: TokenIndex,755 semicolon_token: TokenIndex,
757756
758 pub fn iterate(self: *const Use) Node.Iterator {757 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 };
760 }759 }
761760
762 pub fn iterateNext(self: *const Use, it: *Node.Iterator) ?*Node {761 pub fn iterateNext(self: *const Use, it: *Node.Iterator) ?*Node {
...@@ -797,13 +796,17 @@ pub const Node = struct {...@@ -797,13 +796,17 @@ pub const Node = struct {
797 }796 }
798797
799 pub fn iterate(self: *const ErrorSetDecl) Node.Iterator {798 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 };
801 }800 }
802801
803 pub fn iterateNext(self: *const ErrorSetDecl, it: *Node.Iterator) ?*Node {802 pub fn iterateNext(self: *const ErrorSetDecl, it: *Node.Iterator) ?*Node {
804 const decl = it.node orelse return null;803 var i = it.index;
805 it.node = decl.next;804 it.index += 1;
806 return decl.data;805
806 if (i < self.decls_len) return self.declsConst()[i];
807 i -= self.decls_len;
808
809 return null;
807 }810 }
808811
809 pub fn firstToken(self: *const ErrorSetDecl) TokenIndex {812 pub fn firstToken(self: *const ErrorSetDecl) TokenIndex {
...@@ -857,7 +860,7 @@ pub const Node = struct {...@@ -857,7 +860,7 @@ pub const Node = struct {
857 }860 }
858861
859 pub fn iterate(self: *const ContainerDecl) Node.Iterator {862 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 };
861 }864 }
862865
863 pub fn iterateNext(self: *const ContainerDecl, it: *Node.Iterator) ?*Node {866 pub fn iterateNext(self: *const ContainerDecl, it: *Node.Iterator) ?*Node {
...@@ -914,7 +917,7 @@ pub const Node = struct {...@@ -914,7 +917,7 @@ pub const Node = struct {
914 align_expr: ?*Node,917 align_expr: ?*Node,
915918
916 pub fn iterate(self: *const ContainerField) Node.Iterator {919 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 };
918 }921 }
919922
920 pub fn iterateNext(self: *const ContainerField, it: *Node.Iterator) ?*Node {923 pub fn iterateNext(self: *const ContainerField, it: *Node.Iterator) ?*Node {
...@@ -966,7 +969,7 @@ pub const Node = struct {...@@ -966,7 +969,7 @@ pub const Node = struct {
966 name_token: TokenIndex,969 name_token: TokenIndex,
967970
968 pub fn iterate(self: *const ErrorTag) Node.Iterator {971 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 };
970 }973 }
971974
972 pub fn iterateNext(self: *const ErrorTag, it: *Node.Iterator) ?*Node {975 pub fn iterateNext(self: *const ErrorTag, it: *Node.Iterator) ?*Node {
...@@ -995,7 +998,7 @@ pub const Node = struct {...@@ -995,7 +998,7 @@ pub const Node = struct {
995 token: TokenIndex,998 token: TokenIndex,
996999
997 pub fn iterate(self: *const Identifier) Node.Iterator {1000 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 };
999 }1002 }
10001003
1001 pub fn iterateNext(self: *const Identifier, it: *Node.Iterator) ?*Node {1004 pub fn iterateNext(self: *const Identifier, it: *Node.Iterator) ?*Node {
...@@ -1050,7 +1053,7 @@ pub const Node = struct {...@@ -1050,7 +1053,7 @@ pub const Node = struct {
1050 };1053 };
10511054
1052 pub fn iterate(self: *const ParamDecl) Node.Iterator {1055 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 };
1054 }1057 }
10551058
1056 pub fn iterateNext(self: *const ParamDecl, it: *Node.Iterator) ?*Node {1059 pub fn iterateNext(self: *const ParamDecl, it: *Node.Iterator) ?*Node {
...@@ -1098,7 +1101,7 @@ pub const Node = struct {...@@ -1098,7 +1101,7 @@ pub const Node = struct {
1098 }1101 }
10991102
1100 pub fn iterate(self: *const FnProto) Node.Iterator {1103 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 };
1102 }1105 }
11031106
1104 pub fn iterateNext(self: *const FnProto, it: *Node.Iterator) ?*Node {1107 pub fn iterateNext(self: *const FnProto, it: *Node.Iterator) ?*Node {
...@@ -1189,7 +1192,7 @@ pub const Node = struct {...@@ -1189,7 +1192,7 @@ pub const Node = struct {
1189 };1192 };
11901193
1191 pub fn iterate(self: *const AnyFrameType) Node.Iterator {1194 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 };
1193 }1196 }
11941197
1195 pub fn iterateNext(self: *const AnyFrameType, it: *Node.Iterator) ?*Node {1198 pub fn iterateNext(self: *const AnyFrameType, it: *Node.Iterator) ?*Node {
...@@ -1234,7 +1237,7 @@ pub const Node = struct {...@@ -1234,7 +1237,7 @@ pub const Node = struct {
1234 }1237 }
12351238
1236 pub fn iterate(self: *const Block) Node.Iterator {1239 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 };
1238 }1241 }
12391242
1240 pub fn iterateNext(self: *const Block, it: *Node.Iterator) ?*Node {1243 pub fn iterateNext(self: *const Block, it: *Node.Iterator) ?*Node {
...@@ -1281,7 +1284,7 @@ pub const Node = struct {...@@ -1281,7 +1284,7 @@ pub const Node = struct {
1281 expr: *Node,1284 expr: *Node,
12821285
1283 pub fn iterate(self: *const Defer) Node.Iterator {1286 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 };
1285 }1288 }
12861289
1287 pub fn iterateNext(self: *const Defer, it: *Node.Iterator) ?*Node {1290 pub fn iterateNext(self: *const Defer, it: *Node.Iterator) ?*Node {
...@@ -1310,7 +1313,7 @@ pub const Node = struct {...@@ -1310,7 +1313,7 @@ pub const Node = struct {
1310 expr: *Node,1313 expr: *Node,
13111314
1312 pub fn iterate(self: *const Comptime) Node.Iterator {1315 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 };
1314 }1317 }
13151318
1316 pub fn iterateNext(self: *const Comptime, it: *Node.Iterator) ?*Node {1319 pub fn iterateNext(self: *const Comptime, it: *Node.Iterator) ?*Node {
...@@ -1338,7 +1341,7 @@ pub const Node = struct {...@@ -1338,7 +1341,7 @@ pub const Node = struct {
1338 expr: *Node,1341 expr: *Node,
13391342
1340 pub fn iterate(self: *const Nosuspend) Node.Iterator {1343 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 };
1342 }1345 }
13431346
1344 pub fn iterateNext(self: *const Nosuspend, it: *Node.Iterator) ?*Node {1347 pub fn iterateNext(self: *const Nosuspend, it: *Node.Iterator) ?*Node {
...@@ -1367,7 +1370,7 @@ pub const Node = struct {...@@ -1367,7 +1370,7 @@ pub const Node = struct {
1367 rpipe: TokenIndex,1370 rpipe: TokenIndex,
13681371
1369 pub fn iterate(self: *const Payload) Node.Iterator {1372 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 };
1371 }1374 }
13721375
1373 pub fn iterateNext(self: *const Payload, it: *Node.Iterator) ?*Node {1376 pub fn iterateNext(self: *const Payload, it: *Node.Iterator) ?*Node {
...@@ -1397,7 +1400,7 @@ pub const Node = struct {...@@ -1397,7 +1400,7 @@ pub const Node = struct {
1397 rpipe: TokenIndex,1400 rpipe: TokenIndex,
13981401
1399 pub fn iterate(self: *const PointerPayload) Node.Iterator {1402 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 };
1401 }1404 }
14021405
1403 pub fn iterateNext(self: *const PointerPayload, it: *Node.Iterator) ?*Node {1406 pub fn iterateNext(self: *const PointerPayload, it: *Node.Iterator) ?*Node {
...@@ -1428,7 +1431,7 @@ pub const Node = struct {...@@ -1428,7 +1431,7 @@ pub const Node = struct {
1428 rpipe: TokenIndex,1431 rpipe: TokenIndex,
14291432
1430 pub fn iterate(self: *const PointerIndexPayload) Node.Iterator {1433 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 };
1432 }1435 }
14331436
1434 pub fn iterateNext(self: *const PointerIndexPayload, it: *Node.Iterator) ?*Node {1437 pub fn iterateNext(self: *const PointerIndexPayload, it: *Node.Iterator) ?*Node {
...@@ -1462,7 +1465,7 @@ pub const Node = struct {...@@ -1462,7 +1465,7 @@ pub const Node = struct {
1462 body: *Node,1465 body: *Node,
14631466
1464 pub fn iterate(self: *const Else) Node.Iterator {1467 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 };
1466 }1469 }
14671470
1468 pub fn iterateNext(self: *const Else, it: *Node.Iterator) ?*Node {1471 pub fn iterateNext(self: *const Else, it: *Node.Iterator) ?*Node {
...@@ -1510,7 +1513,7 @@ pub const Node = struct {...@@ -1510,7 +1513,7 @@ pub const Node = struct {
1510 }1513 }
15111514
1512 pub fn iterate(self: *const Switch) Node.Iterator {1515 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 };
1514 }1517 }
15151518
1516 pub fn iterateNext(self: *const Switch, it: *Node.Iterator) ?*Node {1519 pub fn iterateNext(self: *const Switch, it: *Node.Iterator) ?*Node {
...@@ -1520,11 +1523,8 @@ pub const Node = struct {...@@ -1520,11 +1523,8 @@ pub const Node = struct {
1520 if (i < 1) return self.expr;1523 if (i < 1) return self.expr;
1521 i -= 1;1524 i -= 1;
15221525
1523 if (it.node) |child| {1526 if (i < self.cases_len) return self.casesConst()[i];
1524 it.index -= 1;1527 i -= self.cases_len;
1525 it.node = child.next;
1526 return child.data;
1527 }
15281528
1529 return null;1529 return null;
1530 }1530 }
...@@ -1572,7 +1572,7 @@ pub const Node = struct {...@@ -1572,7 +1572,7 @@ pub const Node = struct {
1572 }1572 }
15731573
1574 pub fn iterate(self: *const SwitchCase) Node.Iterator {1574 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 };
1576 }1576 }
15771577
1578 pub fn iterateNext(self: *const SwitchCase, it: *Node.Iterator) ?*Node {1578 pub fn iterateNext(self: *const SwitchCase, it: *Node.Iterator) ?*Node {
...@@ -1621,7 +1621,7 @@ pub const Node = struct {...@@ -1621,7 +1621,7 @@ pub const Node = struct {
1621 token: TokenIndex,1621 token: TokenIndex,
16221622
1623 pub fn iterate(self: *const SwitchElse) Node.Iterator {1623 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 };
1625 }1625 }
16261626
1627 pub fn iterateNext(self: *const SwitchElse, it: *Node.Iterator) ?*Node {1627 pub fn iterateNext(self: *const SwitchElse, it: *Node.Iterator) ?*Node {
...@@ -1649,7 +1649,7 @@ pub const Node = struct {...@@ -1649,7 +1649,7 @@ pub const Node = struct {
1649 @"else": ?*Else,1649 @"else": ?*Else,
16501650
1651 pub fn iterate(self: *const While) Node.Iterator {1651 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 };
1653 }1653 }
16541654
1655 pub fn iterateNext(self: *const While, it: *Node.Iterator) ?*Node {1655 pub fn iterateNext(self: *const While, it: *Node.Iterator) ?*Node {
...@@ -1712,7 +1712,7 @@ pub const Node = struct {...@@ -1712,7 +1712,7 @@ pub const Node = struct {
1712 @"else": ?*Else,1712 @"else": ?*Else,
17131713
1714 pub fn iterate(self: *const For) Node.Iterator {1714 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 };
1716 }1716 }
17171717
1718 pub fn iterateNext(self: *const For, it: *Node.Iterator) ?*Node {1718 pub fn iterateNext(self: *const For, it: *Node.Iterator) ?*Node {
...@@ -1766,7 +1766,7 @@ pub const Node = struct {...@@ -1766,7 +1766,7 @@ pub const Node = struct {
1766 @"else": ?*Else,1766 @"else": ?*Else,
17671767
1768 pub fn iterate(self: *const If) Node.Iterator {1768 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 };
1770 }1770 }
17711771
1772 pub fn iterateNext(self: *const If, it: *Node.Iterator) ?*Node {1772 pub fn iterateNext(self: *const If, it: *Node.Iterator) ?*Node {
...@@ -1859,7 +1859,7 @@ pub const Node = struct {...@@ -1859,7 +1859,7 @@ pub const Node = struct {
1859 };1859 };
18601860
1861 pub fn iterate(self: *const InfixOp) Node.Iterator {1861 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 };
1863 }1863 }
18641864
1865 pub fn iterateNext(self: *const InfixOp, it: *Node.Iterator) ?*Node {1865 pub fn iterateNext(self: *const InfixOp, it: *Node.Iterator) ?*Node {
...@@ -1982,7 +1982,7 @@ pub const Node = struct {...@@ -1982,7 +1982,7 @@ pub const Node = struct {
1982 };1982 };
19831983
1984 pub fn iterate(self: *const PrefixOp) Node.Iterator {1984 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 };
1986 }1986 }
19871987
1988 pub fn iterateNext(self: *const PrefixOp, it: *Node.Iterator) ?*Node {1988 pub fn iterateNext(self: *const PrefixOp, it: *Node.Iterator) ?*Node {
...@@ -2045,7 +2045,7 @@ pub const Node = struct {...@@ -2045,7 +2045,7 @@ pub const Node = struct {
2045 expr: *Node,2045 expr: *Node,
20462046
2047 pub fn iterate(self: *const FieldInitializer) Node.Iterator {2047 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 };
2049 }2049 }
20502050
2051 pub fn iterateNext(self: *const FieldInitializer, it: *Node.Iterator) ?*Node {2051 pub fn iterateNext(self: *const FieldInitializer, it: *Node.Iterator) ?*Node {
...@@ -2086,7 +2086,7 @@ pub const Node = struct {...@@ -2086,7 +2086,7 @@ pub const Node = struct {
2086 }2086 }
20872087
2088 pub fn iterate(self: *const ArrayInitializer) Node.Iterator {2088 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 };
2090 }2090 }
20912091
2092 pub fn iterateNext(self: *const ArrayInitializer, it: *Node.Iterator) ?*Node {2092 pub fn iterateNext(self: *const ArrayInitializer, it: *Node.Iterator) ?*Node {
...@@ -2144,7 +2144,7 @@ pub const Node = struct {...@@ -2144,7 +2144,7 @@ pub const Node = struct {
2144 }2144 }
21452145
2146 pub fn iterate(self: *const ArrayInitializerDot) Node.Iterator {2146 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 };
2148 }2148 }
21492149
2150 pub fn iterateNext(self: *const ArrayInitializerDot, it: *Node.Iterator) ?*Node {2150 pub fn iterateNext(self: *const ArrayInitializerDot, it: *Node.Iterator) ?*Node {
...@@ -2199,7 +2199,7 @@ pub const Node = struct {...@@ -2199,7 +2199,7 @@ pub const Node = struct {
2199 }2199 }
22002200
2201 pub fn iterate(self: *const StructInitializer) Node.Iterator {2201 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 };
2203 }2203 }
22042204
2205 pub fn iterateNext(self: *const StructInitializer, it: *Node.Iterator) ?*Node {2205 pub fn iterateNext(self: *const StructInitializer, it: *Node.Iterator) ?*Node {
...@@ -2257,7 +2257,7 @@ pub const Node = struct {...@@ -2257,7 +2257,7 @@ pub const Node = struct {
2257 }2257 }
22582258
2259 pub fn iterate(self: *const StructInitializerDot) Node.Iterator {2259 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 };
2261 }2261 }
22622262
2263 pub fn iterateNext(self: *const StructInitializerDot, it: *Node.Iterator) ?*Node {2263 pub fn iterateNext(self: *const StructInitializerDot, it: *Node.Iterator) ?*Node {
...@@ -2313,7 +2313,7 @@ pub const Node = struct {...@@ -2313,7 +2313,7 @@ pub const Node = struct {
2313 }2313 }
23142314
2315 pub fn iterate(self: *const Call) Node.Iterator {2315 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};
2317 }2317 }
23182318
2319 pub fn iterateNext(self: *const Call, it: *Node.Iterator) ?*Node {2319 pub fn iterateNext(self: *const Call, it: *Node.Iterator) ?*Node {
...@@ -2373,7 +2373,7 @@ pub const Node = struct {...@@ -2373,7 +2373,7 @@ pub const Node = struct {
2373 };2373 };
23742374
2375 pub fn iterate(self: *const SuffixOp) Node.Iterator {2375 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};
2377 }2377 }
23782378
2379 pub fn iterateNext(self: *const SuffixOp, it: *Node.Iterator) ?*Node {2379 pub fn iterateNext(self: *const SuffixOp, it: *Node.Iterator) ?*Node {
...@@ -2425,7 +2425,7 @@ pub const Node = struct {...@@ -2425,7 +2425,7 @@ pub const Node = struct {
2425 rparen: TokenIndex,2425 rparen: TokenIndex,
24262426
2427 pub fn iterate(self: *const GroupedExpression) Node.Iterator {2427 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 };
2429 }2429 }
24302430
2431 pub fn iterateNext(self: *const GroupedExpression, it: *Node.Iterator) ?*Node {2431 pub fn iterateNext(self: *const GroupedExpression, it: *Node.Iterator) ?*Node {
...@@ -2460,7 +2460,7 @@ pub const Node = struct {...@@ -2460,7 +2460,7 @@ pub const Node = struct {
2460 };2460 };
24612461
2462 pub fn iterate(self: *const ControlFlowExpression) Node.Iterator {2462 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 };
2464 }2464 }
24652465
2466 pub fn iterateNext(self: *const ControlFlowExpression, it: *Node.Iterator) ?*Node {2466 pub fn iterateNext(self: *const ControlFlowExpression, it: *Node.Iterator) ?*Node {
...@@ -2513,7 +2513,7 @@ pub const Node = struct {...@@ -2513,7 +2513,7 @@ pub const Node = struct {
2513 body: ?*Node,2513 body: ?*Node,
25142514
2515 pub fn iterate(self: *const Suspend) Node.Iterator {2515 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 };
2517 }2517 }
25182518
2519 pub fn iterateNext(self: *const Suspend, it: *Node.Iterator) ?*Node {2519 pub fn iterateNext(self: *const Suspend, it: *Node.Iterator) ?*Node {
...@@ -2546,7 +2546,7 @@ pub const Node = struct {...@@ -2546,7 +2546,7 @@ pub const Node = struct {
2546 token: TokenIndex,2546 token: TokenIndex,
25472547
2548 pub fn iterate(self: *const IntegerLiteral) Node.Iterator {2548 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 };
2550 }2550 }
25512551
2552 pub fn iterateNext(self: *const IntegerLiteral, it: *Node.Iterator) ?*Node {2552 pub fn iterateNext(self: *const IntegerLiteral, it: *Node.Iterator) ?*Node {
...@@ -2568,7 +2568,7 @@ pub const Node = struct {...@@ -2568,7 +2568,7 @@ pub const Node = struct {
2568 name: TokenIndex,2568 name: TokenIndex,
25692569
2570 pub fn iterate(self: *const EnumLiteral) Node.Iterator {2570 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 };
2572 }2572 }
25732573
2574 pub fn iterateNext(self: *const EnumLiteral, it: *Node.Iterator) ?*Node {2574 pub fn iterateNext(self: *const EnumLiteral, it: *Node.Iterator) ?*Node {
...@@ -2589,7 +2589,7 @@ pub const Node = struct {...@@ -2589,7 +2589,7 @@ pub const Node = struct {
2589 token: TokenIndex,2589 token: TokenIndex,
25902590
2591 pub fn iterate(self: *const FloatLiteral) Node.Iterator {2591 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 };
2593 }2593 }
25942594
2595 pub fn iterateNext(self: *const FloatLiteral, it: *Node.Iterator) ?*Node {2595 pub fn iterateNext(self: *const FloatLiteral, it: *Node.Iterator) ?*Node {
...@@ -2624,7 +2624,7 @@ pub const Node = struct {...@@ -2624,7 +2624,7 @@ pub const Node = struct {
2624 }2624 }
26252625
2626 pub fn iterate(self: *const BuiltinCall) Node.Iterator {2626 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 };
2628 }2628 }
26292629
2630 pub fn iterateNext(self: *const BuiltinCall, it: *Node.Iterator) ?*Node {2630 pub fn iterateNext(self: *const BuiltinCall, it: *Node.Iterator) ?*Node {
...@@ -2665,7 +2665,7 @@ pub const Node = struct {...@@ -2665,7 +2665,7 @@ pub const Node = struct {
2665 token: TokenIndex,2665 token: TokenIndex,
26662666
2667 pub fn iterate(self: *const StringLiteral) Node.Iterator {2667 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 };
2669 }2669 }
26702670
2671 pub fn iterateNext(self: *const StringLiteral, it: *Node.Iterator) ?*Node {2671 pub fn iterateNext(self: *const StringLiteral, it: *Node.Iterator) ?*Node {
...@@ -2681,14 +2681,24 @@ pub const Node = struct {...@@ -2681,14 +2681,24 @@ pub const Node = struct {
2681 }2681 }
2682 };2682 };
26832683
2684 /// The string literal tokens appear directly in memory after MultilineStringLiteral.
2684 pub const MultilineStringLiteral = struct {2685 pub const MultilineStringLiteral = struct {
2685 base: Node = Node{ .id = .MultilineStringLiteral },2686 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
2690 pub fn iterate(self: *const MultilineStringLiteral) Node.Iterator {2700 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 };
2692 }2702 }
26932703
2694 pub fn iterateNext(self: *const MultilineStringLiteral, it: *Node.Iterator) ?*Node {2704 pub fn iterateNext(self: *const MultilineStringLiteral, it: *Node.Iterator) ?*Node {
...@@ -2696,14 +2706,25 @@ pub const Node = struct {...@@ -2696,14 +2706,25 @@ pub const Node = struct {
2696 }2706 }
26972707
2698 pub fn firstToken(self: *const MultilineStringLiteral) TokenIndex {2708 pub fn firstToken(self: *const MultilineStringLiteral) TokenIndex {
2699 return self.lines.first.?.data;2709 return self.linesConst()[0];
2700 }2710 }
27012711
2702 pub fn lastToken(self: *const MultilineStringLiteral) TokenIndex {2712 pub fn lastToken(self: *const MultilineStringLiteral) TokenIndex {
2703 var node = self.lines.first.?;2713 return self.linesConst()[self.lines_len - 1];
2704 while (true) {2714 }
2705 node = node.next orelse return node.data;2715
2706 }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);
2707 }2728 }
2708 };2729 };
27092730
...@@ -2712,7 +2733,7 @@ pub const Node = struct {...@@ -2712,7 +2733,7 @@ pub const Node = struct {
2712 token: TokenIndex,2733 token: TokenIndex,
27132734
2714 pub fn iterate(self: *const CharLiteral) Node.Iterator {2735 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 };
2716 }2737 }
27172738
2718 pub fn iterateNext(self: *const CharLiteral, it: *Node.Iterator) ?*Node {2739 pub fn iterateNext(self: *const CharLiteral, it: *Node.Iterator) ?*Node {
...@@ -2733,7 +2754,7 @@ pub const Node = struct {...@@ -2733,7 +2754,7 @@ pub const Node = struct {
2733 token: TokenIndex,2754 token: TokenIndex,
27342755
2735 pub fn iterate(self: *const BoolLiteral) Node.Iterator {2756 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 };
2737 }2758 }
27382759
2739 pub fn iterateNext(self: *const BoolLiteral, it: *Node.Iterator) ?*Node {2760 pub fn iterateNext(self: *const BoolLiteral, it: *Node.Iterator) ?*Node {
...@@ -2754,7 +2775,7 @@ pub const Node = struct {...@@ -2754,7 +2775,7 @@ pub const Node = struct {
2754 token: TokenIndex,2775 token: TokenIndex,
27552776
2756 pub fn iterate(self: *const NullLiteral) Node.Iterator {2777 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 };
2758 }2779 }
27592780
2760 pub fn iterateNext(self: *const NullLiteral, it: *Node.Iterator) ?*Node {2781 pub fn iterateNext(self: *const NullLiteral, it: *Node.Iterator) ?*Node {
...@@ -2775,7 +2796,7 @@ pub const Node = struct {...@@ -2775,7 +2796,7 @@ pub const Node = struct {
2775 token: TokenIndex,2796 token: TokenIndex,
27762797
2777 pub fn iterate(self: *const UndefinedLiteral) Node.Iterator {2798 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 };
2779 }2800 }
27802801
2781 pub fn iterateNext(self: *const UndefinedLiteral, it: *Node.Iterator) ?*Node {2802 pub fn iterateNext(self: *const UndefinedLiteral, it: *Node.Iterator) ?*Node {
...@@ -2815,7 +2836,7 @@ pub const Node = struct {...@@ -2815,7 +2836,7 @@ pub const Node = struct {
2815 };2836 };
28162837
2817 pub fn iterate(self: *const Output) Node.Iterator {2838 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 };
2819 }2840 }
28202841
2821 pub fn iterateNext(self: *const Output, it: *Node.Iterator) ?*Node {2842 pub fn iterateNext(self: *const Output, it: *Node.Iterator) ?*Node {
...@@ -2859,7 +2880,7 @@ pub const Node = struct {...@@ -2859,7 +2880,7 @@ pub const Node = struct {
2859 rparen: TokenIndex,2880 rparen: TokenIndex,
28602881
2861 pub fn iterate(self: *const Input) Node.Iterator {2882 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 };
2863 }2884 }
28642885
2865 pub fn iterateNext(self: *const Input, it: *Node.Iterator) ?*Node {2886 pub fn iterateNext(self: *const Input, it: *Node.Iterator) ?*Node {
...@@ -2889,7 +2910,7 @@ pub const Node = struct {...@@ -2889,7 +2910,7 @@ pub const Node = struct {
28892910
28902911
2891 pub fn iterate(self: *const Asm) Node.Iterator {2912 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};
2893 }2914 }
28942915
2895 pub fn iterateNext(self: *const Asm, it: *Node.Iterator) ?*Node {2916 pub fn iterateNext(self: *const Asm, it: *Node.Iterator) ?*Node {
...@@ -2932,7 +2953,7 @@ pub const Node = struct {...@@ -2932,7 +2953,7 @@ pub const Node = struct {
2932 token: TokenIndex,2953 token: TokenIndex,
29332954
2934 pub fn iterate(self: *const Unreachable) Node.Iterator {2955 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 };
2936 }2957 }
29372958
2938 pub fn iterateNext(self: *const Unreachable, it: *Node.Iterator) ?*Node {2959 pub fn iterateNext(self: *const Unreachable, it: *Node.Iterator) ?*Node {
...@@ -2953,7 +2974,7 @@ pub const Node = struct {...@@ -2953,7 +2974,7 @@ pub const Node = struct {
2953 token: TokenIndex,2974 token: TokenIndex,
29542975
2955 pub fn iterate(self: *const ErrorType) Node.Iterator {2976 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 };
2957 }2978 }
29582979
2959 pub fn iterateNext(self: *const ErrorType, it: *Node.Iterator) ?*Node {2980 pub fn iterateNext(self: *const ErrorType, it: *Node.Iterator) ?*Node {
...@@ -2974,7 +2995,7 @@ pub const Node = struct {...@@ -2974,7 +2995,7 @@ pub const Node = struct {
2974 token: TokenIndex,2995 token: TokenIndex,
29752996
2976 pub fn iterate(self: *const VarType) Node.Iterator {2997 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 };
2978 }2999 }
29793000
2980 pub fn iterateNext(self: *const VarType, it: *Node.Iterator) ?*Node {3001 pub fn iterateNext(self: *const VarType, it: *Node.Iterator) ?*Node {
...@@ -2997,7 +3018,7 @@ pub const Node = struct {...@@ -2997,7 +3018,7 @@ pub const Node = struct {
2997 pub const LineList = LinkedList(TokenIndex);3018 pub const LineList = LinkedList(TokenIndex);
29983019
2999 pub fn iterate(self: *const DocComment) Node.Iterator {3020 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 };
3001 }3022 }
30023023
3003 pub fn iterateNext(self: *const DocComment, it: *Node.Iterator) ?*Node {3024 pub fn iterateNext(self: *const DocComment, it: *Node.Iterator) ?*Node {
...@@ -3024,7 +3045,7 @@ pub const Node = struct {...@@ -3024,7 +3045,7 @@ pub const Node = struct {
3024 body_node: *Node,3045 body_node: *Node,
30253046
3026 pub fn iterate(self: *const TestDecl) Node.Iterator {3047 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 };
3028 }3049 }
30293050
3030 pub fn iterateNext(self: *const TestDecl, it: *Node.Iterator) ?*Node {3051 pub fn iterateNext(self: *const TestDecl, it: *Node.Iterator) ?*Node {
lib/std/zig/parse.zig+27-8
...@@ -3105,15 +3105,34 @@ const Parser = struct {...@@ -3105,15 +3105,34 @@ const Parser = struct {
3105 if (try p.parseStringLiteralSingle()) |node| return node;3105 if (try p.parseStringLiteralSingle()) |node| return node;
31063106
3107 if (p.eatToken(.MultilineStringLiteralLine)) |first_line| {3107 if (p.eatToken(.MultilineStringLiteralLine)) |first_line| {
3108 const node = try p.arena.allocator.create(Node.MultilineStringLiteral);3108 const start_tok_i = p.tok_i;
3109 node.* = .{3109 var tok_i = start_tok_i;
3110 .lines = Node.MultilineStringLiteral.LineList{},3110 var count: usize = 1; // including first_line
3111 };3111 while (true) : (tok_i += 1) {
3112 var lines_it = &node.lines.first;3112 switch (p.tokens[tok_i].id) {
3113 lines_it = try p.llpush(TokenIndex, lines_it, first_line);3113 .LineComment => continue,
3114 while (p.eatToken(.MultilineStringLiteralLine)) |line|3114 .MultilineStringLiteralLine => count += 1,
3115 lines_it = try p.llpush(TokenIndex, lines_it, line);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;
3117 return &node.base;3136 return &node.base;
3118 }3137 }
31193138
lib/std/zig/render.zig+1-3
...@@ -1376,9 +1376,7 @@ fn renderExpression(...@@ -1376,9 +1376,7 @@ fn renderExpression(
1376 skip_first_indent = false;1376 skip_first_indent = false;
1377 }1377 }
13781378
1379 var it = multiline_str_literal.lines.first;1379 for (multiline_str_literal.lines()) |t| {
1380 while (it) |t_node| : (it = t_node.next) {
1381 const t = t_node.data;
1382 if (!skip_first_indent) {1380 if (!skip_first_indent) {
1383 try stream.writeByteNTimes(' ', indent + indent_delta);1381 try stream.writeByteNTimes(' ', indent + indent_delta);
1384 }1382 }