authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-07-11 13:54:16+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-07-11 17:41:16+03:00
logc2fb4bfff3b1a2bf4e7072cec04d67e8152900c1
tree62a192b5c071037c7bd9dfbefcf7109d32eb005c
parent2e037fd827487ce94ae0beb313a1f0536085d61e
signaturelock-open Commit is signed but in an unrecognized format.

add 'anytype' to self-hosted parser


4 files changed, 45 insertions(+), 35 deletions(-)

lib/std/zig/ast.zig+6-6
...@@ -434,7 +434,7 @@ pub const Node = struct {...@@ -434,7 +434,7 @@ pub const Node = struct {
434 Suspend,434 Suspend,
435435
436 // Type expressions436 // Type expressions
437 VarType,437 AnyType,
438 ErrorType,438 ErrorType,
439 FnProto,439 FnProto,
440 AnyFrameType,440 AnyFrameType,
...@@ -2732,19 +2732,19 @@ pub const Node = struct {...@@ -2732,19 +2732,19 @@ pub const Node = struct {
2732 }2732 }
2733 };2733 };
27342734
2735 pub const VarType = struct {2735 pub const AnyType = struct {
2736 base: Node = Node{ .id = .VarType },2736 base: Node = Node{ .id = .AnyType },
2737 token: TokenIndex,2737 token: TokenIndex,
27382738
2739 pub fn iterate(self: *const VarType, index: usize) ?*Node {2739 pub fn iterate(self: *const AnyType, index: usize) ?*Node {
2740 return null;2740 return null;
2741 }2741 }
27422742
2743 pub fn firstToken(self: *const VarType) TokenIndex {2743 pub fn firstToken(self: *const AnyType) TokenIndex {
2744 return self.token;2744 return self.token;
2745 }2745 }
27462746
2747 pub fn lastToken(self: *const VarType) TokenIndex {2747 pub fn lastToken(self: *const AnyType) TokenIndex {
2748 return self.token;2748 return self.token;
2749 }2749 }
2750 };2750 };
lib/std/zig/parse.zig+8-7
...@@ -488,7 +488,7 @@ const Parser = struct {...@@ -488,7 +488,7 @@ const Parser = struct {
488 return p.parseUse();488 return p.parseUse();
489 }489 }
490490
491 /// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr)491 /// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (Keyword_anytype / TypeExpr)
492 fn parseFnProto(p: *Parser) !?*Node {492 fn parseFnProto(p: *Parser) !?*Node {
493 // TODO: Remove once extern/async fn rewriting is493 // TODO: Remove once extern/async fn rewriting is
494 var is_async = false;494 var is_async = false;
...@@ -618,9 +618,9 @@ const Parser = struct {...@@ -618,9 +618,9 @@ const Parser = struct {
618 var align_expr: ?*Node = null;618 var align_expr: ?*Node = null;
619 var type_expr: ?*Node = null;619 var type_expr: ?*Node = null;
620 if (p.eatToken(.Colon)) |_| {620 if (p.eatToken(.Colon)) |_| {
621 if (p.eatToken(.Keyword_var)) |var_tok| {621 if (p.eatToken(.Keyword_anytype) orelse p.eatToken(.Keyword_var)) |anytype_tok| {
622 const node = try p.arena.allocator.create(Node.VarType);622 const node = try p.arena.allocator.create(Node.AnyType);
623 node.* = .{ .token = var_tok };623 node.* = .{ .token = anytype_tok };
624 type_expr = &node.base;624 type_expr = &node.base;
625 } else {625 } else {
626 type_expr = try p.expectNode(parseTypeExpr, .{626 type_expr = try p.expectNode(parseTypeExpr, .{
...@@ -2022,7 +2022,7 @@ const Parser = struct {...@@ -2022,7 +2022,7 @@ const Parser = struct {
2022 }2022 }
20232023
2024 /// ParamType2024 /// ParamType
2025 /// <- KEYWORD_var2025 /// <- Keyword_anytype
2026 /// / DOT32026 /// / DOT3
2027 /// / TypeExpr2027 /// / TypeExpr
2028 fn parseParamType(p: *Parser) !?Node.FnProto.ParamDecl.ParamType {2028 fn parseParamType(p: *Parser) !?Node.FnProto.ParamDecl.ParamType {
...@@ -3058,8 +3058,9 @@ const Parser = struct {...@@ -3058,8 +3058,9 @@ const Parser = struct {
3058 }3058 }
30593059
3060 fn parseVarType(p: *Parser) !?*Node {3060 fn parseVarType(p: *Parser) !?*Node {
3061 const token = p.eatToken(.Keyword_var) orelse return null;3061 const token = p.eatToken(.Keyword_anytype) orelse
3062 const node = try p.arena.allocator.create(Node.VarType);3062 p.eatToken(.Keyword_var) orelse return null; // TODO remove in next release cycle
3063 const node = try p.arena.allocator.create(Node.AnyType);
3063 node.* = .{3064 node.* = .{
3064 .token = token,3065 .token = token,
3065 };3066 };
lib/std/zig/render.zig+27-21
...@@ -12,7 +12,7 @@ pub const Error = error{...@@ -12,7 +12,7 @@ pub const Error = error{
12};12};
1313
14/// Returns whether anything changed14/// Returns whether anything changed
15pub fn render(allocator: *mem.Allocator, stream: var, tree: *ast.Tree) (@TypeOf(stream).Error || Error)!bool {15pub fn render(allocator: *mem.Allocator, stream: anytype, tree: *ast.Tree) (@TypeOf(stream).Error || Error)!bool {
16 // cannot render an invalid tree16 // cannot render an invalid tree
17 std.debug.assert(tree.errors.len == 0);17 std.debug.assert(tree.errors.len == 0);
1818
...@@ -64,7 +64,7 @@ pub fn render(allocator: *mem.Allocator, stream: var, tree: *ast.Tree) (@TypeOf(...@@ -64,7 +64,7 @@ pub fn render(allocator: *mem.Allocator, stream: var, tree: *ast.Tree) (@TypeOf(
6464
65fn renderRoot(65fn renderRoot(
66 allocator: *mem.Allocator,66 allocator: *mem.Allocator,
67 stream: var,67 stream: anytype,
68 tree: *ast.Tree,68 tree: *ast.Tree,
69) (@TypeOf(stream).Error || Error)!void {69) (@TypeOf(stream).Error || Error)!void {
70 // render all the line comments at the beginning of the file70 // render all the line comments at the beginning of the file
...@@ -191,13 +191,13 @@ fn renderRoot(...@@ -191,13 +191,13 @@ fn renderRoot(
191 }191 }
192}192}
193193
194fn renderExtraNewline(tree: *ast.Tree, stream: var, start_col: *usize, node: *ast.Node) @TypeOf(stream).Error!void {194fn renderExtraNewline(tree: *ast.Tree, stream: anytype, start_col: *usize, node: *ast.Node) @TypeOf(stream).Error!void {
195 return renderExtraNewlineToken(tree, stream, start_col, node.firstToken());195 return renderExtraNewlineToken(tree, stream, start_col, node.firstToken());
196}196}
197197
198fn renderExtraNewlineToken(198fn renderExtraNewlineToken(
199 tree: *ast.Tree,199 tree: *ast.Tree,
200 stream: var,200 stream: anytype,
201 start_col: *usize,201 start_col: *usize,
202 first_token: ast.TokenIndex,202 first_token: ast.TokenIndex,
203) @TypeOf(stream).Error!void {203) @TypeOf(stream).Error!void {
...@@ -218,11 +218,11 @@ fn renderExtraNewlineToken(...@@ -218,11 +218,11 @@ fn renderExtraNewlineToken(
218 }218 }
219}219}
220220
221fn renderTopLevelDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, indent: usize, start_col: *usize, decl: *ast.Node) (@TypeOf(stream).Error || Error)!void {221fn renderTopLevelDecl(allocator: *mem.Allocator, stream: anytype, tree: *ast.Tree, indent: usize, start_col: *usize, decl: *ast.Node) (@TypeOf(stream).Error || Error)!void {
222 try renderContainerDecl(allocator, stream, tree, indent, start_col, decl, .Newline);222 try renderContainerDecl(allocator, stream, tree, indent, start_col, decl, .Newline);
223}223}
224224
225fn renderContainerDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, indent: usize, start_col: *usize, decl: *ast.Node, space: Space) (@TypeOf(stream).Error || Error)!void {225fn renderContainerDecl(allocator: *mem.Allocator, stream: anytype, tree: *ast.Tree, indent: usize, start_col: *usize, decl: *ast.Node, space: Space) (@TypeOf(stream).Error || Error)!void {
226 switch (decl.id) {226 switch (decl.id) {
227 .FnProto => {227 .FnProto => {
228 const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", decl);228 const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", decl);
...@@ -358,7 +358,7 @@ fn renderContainerDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree,...@@ -358,7 +358,7 @@ fn renderContainerDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree,
358358
359fn renderExpression(359fn renderExpression(
360 allocator: *mem.Allocator,360 allocator: *mem.Allocator,
361 stream: var,361 stream: anytype,
362 tree: *ast.Tree,362 tree: *ast.Tree,
363 indent: usize,363 indent: usize,
364 start_col: *usize,364 start_col: *usize,
...@@ -1179,9 +1179,15 @@ fn renderExpression(...@@ -1179,9 +1179,15 @@ fn renderExpression(
1179 const error_type = @fieldParentPtr(ast.Node.ErrorType, "base", base);1179 const error_type = @fieldParentPtr(ast.Node.ErrorType, "base", base);
1180 return renderToken(tree, stream, error_type.token, indent, start_col, space);1180 return renderToken(tree, stream, error_type.token, indent, start_col, space);
1181 },1181 },
1182 .VarType => {1182 .AnyType => {
1183 const var_type = @fieldParentPtr(ast.Node.VarType, "base", base);1183 const any_type = @fieldParentPtr(ast.Node.AnyType, "base", base);
1184 return renderToken(tree, stream, var_type.token, indent, start_col, space);1184 if (mem.eql(u8, tree.tokenSlice(any_type.token), "var")) {
1185 // TODO remove in next release cycle
1186 try stream.writeAll("anytype");
1187 if (space == .Comma) try stream.writeAll(",\n");
1188 return;
1189 }
1190 return renderToken(tree, stream, any_type.token, indent, start_col, space);
1185 },1191 },
1186 .ContainerDecl => {1192 .ContainerDecl => {
1187 const container_decl = @fieldParentPtr(ast.Node.ContainerDecl, "base", base);1193 const container_decl = @fieldParentPtr(ast.Node.ContainerDecl, "base", base);
...@@ -2053,7 +2059,7 @@ fn renderExpression(...@@ -2053,7 +2059,7 @@ fn renderExpression(
20532059
2054fn renderAsmOutput(2060fn renderAsmOutput(
2055 allocator: *mem.Allocator,2061 allocator: *mem.Allocator,
2056 stream: var,2062 stream: anytype,
2057 tree: *ast.Tree,2063 tree: *ast.Tree,
2058 indent: usize,2064 indent: usize,
2059 start_col: *usize,2065 start_col: *usize,
...@@ -2081,7 +2087,7 @@ fn renderAsmOutput(...@@ -2081,7 +2087,7 @@ fn renderAsmOutput(
20812087
2082fn renderAsmInput(2088fn renderAsmInput(
2083 allocator: *mem.Allocator,2089 allocator: *mem.Allocator,
2084 stream: var,2090 stream: anytype,
2085 tree: *ast.Tree,2091 tree: *ast.Tree,
2086 indent: usize,2092 indent: usize,
2087 start_col: *usize,2093 start_col: *usize,
...@@ -2099,7 +2105,7 @@ fn renderAsmInput(...@@ -2099,7 +2105,7 @@ fn renderAsmInput(
20992105
2100fn renderVarDecl(2106fn renderVarDecl(
2101 allocator: *mem.Allocator,2107 allocator: *mem.Allocator,
2102 stream: var,2108 stream: anytype,
2103 tree: *ast.Tree,2109 tree: *ast.Tree,
2104 indent: usize,2110 indent: usize,
2105 start_col: *usize,2111 start_col: *usize,
...@@ -2171,7 +2177,7 @@ fn renderVarDecl(...@@ -2171,7 +2177,7 @@ fn renderVarDecl(
21712177
2172fn renderParamDecl(2178fn renderParamDecl(
2173 allocator: *mem.Allocator,2179 allocator: *mem.Allocator,
2174 stream: var,2180 stream: anytype,
2175 tree: *ast.Tree,2181 tree: *ast.Tree,
2176 indent: usize,2182 indent: usize,
2177 start_col: *usize,2183 start_col: *usize,
...@@ -2198,7 +2204,7 @@ fn renderParamDecl(...@@ -2198,7 +2204,7 @@ fn renderParamDecl(
21982204
2199fn renderStatement(2205fn renderStatement(
2200 allocator: *mem.Allocator,2206 allocator: *mem.Allocator,
2201 stream: var,2207 stream: anytype,
2202 tree: *ast.Tree,2208 tree: *ast.Tree,
2203 indent: usize,2209 indent: usize,
2204 start_col: *usize,2210 start_col: *usize,
...@@ -2236,7 +2242,7 @@ const Space = enum {...@@ -2236,7 +2242,7 @@ const Space = enum {
22362242
2237fn renderTokenOffset(2243fn renderTokenOffset(
2238 tree: *ast.Tree,2244 tree: *ast.Tree,
2239 stream: var,2245 stream: anytype,
2240 token_index: ast.TokenIndex,2246 token_index: ast.TokenIndex,
2241 indent: usize,2247 indent: usize,
2242 start_col: *usize,2248 start_col: *usize,
...@@ -2434,7 +2440,7 @@ fn renderTokenOffset(...@@ -2434,7 +2440,7 @@ fn renderTokenOffset(
24342440
2435fn renderToken(2441fn renderToken(
2436 tree: *ast.Tree,2442 tree: *ast.Tree,
2437 stream: var,2443 stream: anytype,
2438 token_index: ast.TokenIndex,2444 token_index: ast.TokenIndex,
2439 indent: usize,2445 indent: usize,
2440 start_col: *usize,2446 start_col: *usize,
...@@ -2445,8 +2451,8 @@ fn renderToken(...@@ -2445,8 +2451,8 @@ fn renderToken(
24452451
2446fn renderDocComments(2452fn renderDocComments(
2447 tree: *ast.Tree,2453 tree: *ast.Tree,
2448 stream: var,2454 stream: anytype,
2449 node: var,2455 node: anytype,
2450 indent: usize,2456 indent: usize,
2451 start_col: *usize,2457 start_col: *usize,
2452) (@TypeOf(stream).Error || Error)!void {2458) (@TypeOf(stream).Error || Error)!void {
...@@ -2456,7 +2462,7 @@ fn renderDocComments(...@@ -2456,7 +2462,7 @@ fn renderDocComments(
24562462
2457fn renderDocCommentsToken(2463fn renderDocCommentsToken(
2458 tree: *ast.Tree,2464 tree: *ast.Tree,
2459 stream: var,2465 stream: anytype,
2460 comment: *ast.Node.DocComment,2466 comment: *ast.Node.DocComment,
2461 first_token: ast.TokenIndex,2467 first_token: ast.TokenIndex,
2462 indent: usize,2468 indent: usize,
...@@ -2532,7 +2538,7 @@ const FindByteOutStream = struct {...@@ -2532,7 +2538,7 @@ const FindByteOutStream = struct {
2532 }2538 }
2533};2539};
25342540
2535fn copyFixingWhitespace(stream: var, slice: []const u8) @TypeOf(stream).Error!void {2541fn copyFixingWhitespace(stream: anytype, slice: []const u8) @TypeOf(stream).Error!void {
2536 for (slice) |byte| switch (byte) {2542 for (slice) |byte| switch (byte) {
2537 '\t' => try stream.writeAll(" "),2543 '\t' => try stream.writeAll(" "),
2538 '\r' => {},2544 '\r' => {},
lib/std/zig/tokenizer.zig+4-1
...@@ -15,6 +15,7 @@ pub const Token = struct {...@@ -15,6 +15,7 @@ pub const Token = struct {
15 .{ "allowzero", .Keyword_allowzero },15 .{ "allowzero", .Keyword_allowzero },
16 .{ "and", .Keyword_and },16 .{ "and", .Keyword_and },
17 .{ "anyframe", .Keyword_anyframe },17 .{ "anyframe", .Keyword_anyframe },
18 .{ "anytype", .Keyword_anytype },
18 .{ "asm", .Keyword_asm },19 .{ "asm", .Keyword_asm },
19 .{ "async", .Keyword_async },20 .{ "async", .Keyword_async },
20 .{ "await", .Keyword_await },21 .{ "await", .Keyword_await },
...@@ -140,6 +141,8 @@ pub const Token = struct {...@@ -140,6 +141,8 @@ pub const Token = struct {
140 Keyword_align,141 Keyword_align,
141 Keyword_allowzero,142 Keyword_allowzero,
142 Keyword_and,143 Keyword_and,
144 Keyword_anyframe,
145 Keyword_anytype,
143 Keyword_asm,146 Keyword_asm,
144 Keyword_async,147 Keyword_async,
145 Keyword_await,148 Keyword_await,
...@@ -168,7 +171,6 @@ pub const Token = struct {...@@ -168,7 +171,6 @@ pub const Token = struct {
168 Keyword_or,171 Keyword_or,
169 Keyword_orelse,172 Keyword_orelse,
170 Keyword_packed,173 Keyword_packed,
171 Keyword_anyframe,
172 Keyword_pub,174 Keyword_pub,
173 Keyword_resume,175 Keyword_resume,
174 Keyword_return,176 Keyword_return,
...@@ -263,6 +265,7 @@ pub const Token = struct {...@@ -263,6 +265,7 @@ pub const Token = struct {
263 .Keyword_allowzero => "allowzero",265 .Keyword_allowzero => "allowzero",
264 .Keyword_and => "and",266 .Keyword_and => "and",
265 .Keyword_anyframe => "anyframe",267 .Keyword_anyframe => "anyframe",
268 .Keyword_anytype => "anytype",
266 .Keyword_asm => "asm",269 .Keyword_asm => "asm",
267 .Keyword_async => "async",270 .Keyword_async => "async",
268 .Keyword_await => "await",271 .Keyword_await => "await",