authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-02-07 22:26:43+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-07 14:51:37-08:00
log0e38362d244f118565f447f8ee2c6b8a700d05bf
treedc069b1282f0bae3f7a6aec92c95d021b897e0dd
parent1d71b19c0d025aeeede229e714679f4b4fb7880d

zig fmt: split Slice and SliceSentinel

This saves 4 whole bytes in the common case where there is no sentinel.

3 files changed, 64 insertions(+), 24 deletions(-)

lib/std/zig/ast.zig+34-7
...@@ -304,6 +304,7 @@ pub const Tree = struct {...@@ -304,6 +304,7 @@ pub const Tree = struct {
304 .BoolOr,304 .BoolOr,
305 .SliceOpen,305 .SliceOpen,
306 .Slice,306 .Slice,
307 .SliceSentinel,
307 .Deref,308 .Deref,
308 .ArrayAccess,309 .ArrayAccess,
309 .ArrayInitOne,310 .ArrayInitOne,
...@@ -694,19 +695,22 @@ pub const Tree = struct {...@@ -694,19 +695,22 @@ pub const Tree = struct {
694 return main_tokens[n] + end_offset;695 return main_tokens[n] + end_offset;
695 }696 }
696 },697 },
698
697 .SliceOpen => {699 .SliceOpen => {
698 end_offset += 2; // ellipsis2 and rbracket700 end_offset += 2; // ellipsis2 and rbracket
699 n = datas[n].rhs;701 n = datas[n].rhs;
700 },702 },
701 .Slice => {703 .Slice => {
702 const extra = tree.extraData(datas[n].rhs, Node.Slice);704 const extra = tree.extraData(datas[n].rhs, Node.Slice);
703 if (extra.sentinel != 0) {705 assert(extra.end != 0); // should have used SliceOpen
704 n = extra.sentinel;
705 } else {
706 assert(extra.end != 0); // should have used SliceOpen if end and sentinel are 0
707 n = extra.end;
708 }
709 end_offset += 1; // rbracket706 end_offset += 1; // rbracket
707 n = extra.end;
708 },
709 .SliceSentinel => {
710 const extra = tree.extraData(datas[n].rhs, Node.SliceSentinel);
711 assert(extra.sentinel != 0); // should have used Slice
712 end_offset += 1; // rbracket
713 n = extra.sentinel;
710 },714 },
711715
712 // These are not supported by lastToken() because implementation would716 // These are not supported by lastToken() because implementation would
...@@ -1129,6 +1133,21 @@ pub const Tree = struct {...@@ -1129,6 +1133,21 @@ pub const Tree = struct {
1129 assert(tree.nodes.items(.tag)[node] == .Slice);1133 assert(tree.nodes.items(.tag)[node] == .Slice);
1130 const data = tree.nodes.items(.data)[node];1134 const data = tree.nodes.items(.data)[node];
1131 const extra = tree.extraData(data.rhs, Node.Slice);1135 const extra = tree.extraData(data.rhs, Node.Slice);
1136 return .{
1137 .ast = .{
1138 .sliced = data.lhs,
1139 .lbracket = tree.nodes.items(.main_token)[node],
1140 .start = extra.start,
1141 .end = extra.end,
1142 .sentinel = 0,
1143 },
1144 };
1145 }
1146
1147 pub fn sliceSentinel(tree: Tree, node: Node.Index) Full.Slice {
1148 assert(tree.nodes.items(.tag)[node] == .SliceSentinel);
1149 const data = tree.nodes.items(.data)[node];
1150 const extra = tree.extraData(data.rhs, Node.SliceSentinel);
1132 return .{1151 return .{
1133 .ast = .{1152 .ast = .{
1134 .sliced = data.lhs,1153 .sliced = data.lhs,
...@@ -1922,9 +1941,12 @@ pub const Node = struct {...@@ -1922,9 +1941,12 @@ pub const Node = struct {
1922 /// `lhs[rhs..]`1941 /// `lhs[rhs..]`
1923 /// main_token is the lbracket.1942 /// main_token is the lbracket.
1924 SliceOpen,1943 SliceOpen,
1925 /// `lhs[b..c :d]`. rhs is index into Slice1944 /// `lhs[b..c]`. rhs is index into Slice
1926 /// main_token is the lbracket.1945 /// main_token is the lbracket.
1927 Slice,1946 Slice,
1947 /// `lhs[b..c :d]`. rhs is index into SliceSentinel
1948 /// main_token is the lbracket.
1949 SliceSentinel,
1928 /// `lhs.*`. rhs is unused.1950 /// `lhs.*`. rhs is unused.
1929 Deref,1951 Deref,
1930 /// `lhs[rhs]`.1952 /// `lhs[rhs]`.
...@@ -2202,6 +2224,11 @@ pub const Node = struct {...@@ -2202,6 +2224,11 @@ pub const Node = struct {
2202 pub const Slice = struct {2224 pub const Slice = struct {
2203 start: Index,2225 start: Index,
2204 end: Index,2226 end: Index,
2227 };
2228
2229 pub const SliceSentinel = struct {
2230 start: Index,
2231 end: Index,
2205 sentinel: Index,2232 sentinel: Index,
2206 };2233 };
22072234
lib/std/zig/parse.zig+29-17
...@@ -3326,23 +3326,35 @@ const Parser = struct {...@@ -3326,23 +3326,35 @@ const Parser = struct {
3326 },3326 },
3327 });3327 });
3328 }3328 }
3329 const sentinel: Node.Index = if (p.eatToken(.Colon)) |_|3329 if (p.eatToken(.Colon)) |_| {
3330 try p.parseExpr()3330 const sentinel = try p.parseExpr();
3331 else3331 _ = try p.expectToken(.RBracket);
3332 0;3332 return p.addNode(.{
3333 _ = try p.expectToken(.RBracket);3333 .tag = .SliceSentinel,
3334 return p.addNode(.{3334 .main_token = lbracket,
3335 .tag = .Slice,3335 .data = .{
3336 .main_token = lbracket,3336 .lhs = lhs,
3337 .data = .{3337 .rhs = try p.addExtra(Node.SliceSentinel{
3338 .lhs = lhs,3338 .start = index_expr,
3339 .rhs = try p.addExtra(.{3339 .end = end_expr,
3340 .start = index_expr,3340 .sentinel = sentinel,
3341 .end = end_expr,3341 }),
3342 .sentinel = sentinel,3342 },
3343 }),3343 });
3344 },3344 } else {
3345 });3345 _ = try p.expectToken(.RBracket);
3346 return p.addNode(.{
3347 .tag = .Slice,
3348 .main_token = lbracket,
3349 .data = .{
3350 .lhs = lhs,
3351 .rhs = try p.addExtra(Node.Slice{
3352 .start = index_expr,
3353 .end = end_expr,
3354 }),
3355 },
3356 });
3357 }
3346 }3358 }
3347 _ = try p.expectToken(.RBracket);3359 _ = try p.expectToken(.RBracket);
3348 return p.addNode(.{3360 return p.addNode(.{
lib/std/zig/render.zig+1
...@@ -470,6 +470,7 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac...@@ -470,6 +470,7 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac
470470
471 .SliceOpen => try renderSlice(ais, tree, tree.sliceOpen(node), space),471 .SliceOpen => try renderSlice(ais, tree, tree.sliceOpen(node), space),
472 .Slice => try renderSlice(ais, tree, tree.slice(node), space),472 .Slice => try renderSlice(ais, tree, tree.slice(node), space),
473 .SliceSentinel => try renderSlice(ais, tree, tree.sliceSentinel(node), space),
473474
474 .Deref => {475 .Deref => {
475 try renderExpression(ais, tree, datas[node].lhs, .None);476 try renderExpression(ais, tree, datas[node].lhs, .None);