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 {
434434 Suspend,
435435
436436 // Type expressions
437 VarType,
437 AnyType,
438438 ErrorType,
439439 FnProto,
440440 AnyFrameType,
......@@ -2732,19 +2732,19 @@ pub const Node = struct {
27322732 }
27332733 };
27342734
2735 pub const VarType = struct {
2736 base: Node = Node{ .id = .VarType },
2735 pub const AnyType = struct {
2736 base: Node = Node{ .id = .AnyType },
27372737 token: TokenIndex,
27382738
2739 pub fn iterate(self: *const VarType, index: usize) ?*Node {
2739 pub fn iterate(self: *const AnyType, index: usize) ?*Node {
27402740 return null;
27412741 }
27422742
2743 pub fn firstToken(self: *const VarType) TokenIndex {
2743 pub fn firstToken(self: *const AnyType) TokenIndex {
27442744 return self.token;
27452745 }
27462746
2747 pub fn lastToken(self: *const VarType) TokenIndex {
2747 pub fn lastToken(self: *const AnyType) TokenIndex {
27482748 return self.token;
27492749 }
27502750 };
lib/std/zig/parse.zig+8-7
......@@ -488,7 +488,7 @@ const Parser = struct {
488488 return p.parseUse();
489489 }
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)
492492 fn parseFnProto(p: *Parser) !?*Node {
493493 // TODO: Remove once extern/async fn rewriting is
494494 var is_async = false;
......@@ -618,9 +618,9 @@ const Parser = struct {
618618 var align_expr: ?*Node = null;
619619 var type_expr: ?*Node = null;
620620 if (p.eatToken(.Colon)) |_| {
621 if (p.eatToken(.Keyword_var)) |var_tok| {
622 const node = try p.arena.allocator.create(Node.VarType);
623 node.* = .{ .token = var_tok };
621 if (p.eatToken(.Keyword_anytype) orelse p.eatToken(.Keyword_var)) |anytype_tok| {
622 const node = try p.arena.allocator.create(Node.AnyType);
623 node.* = .{ .token = anytype_tok };
624624 type_expr = &node.base;
625625 } else {
626626 type_expr = try p.expectNode(parseTypeExpr, .{
......@@ -2022,7 +2022,7 @@ const Parser = struct {
20222022 }
20232023
20242024 /// ParamType
2025 /// <- KEYWORD_var
2025 /// <- Keyword_anytype
20262026 /// / DOT3
20272027 /// / TypeExpr
20282028 fn parseParamType(p: *Parser) !?Node.FnProto.ParamDecl.ParamType {
......@@ -3058,8 +3058,9 @@ const Parser = struct {
30583058 }
30593059
30603060 fn parseVarType(p: *Parser) !?*Node {
3061 const token = p.eatToken(.Keyword_var) orelse return null;
3062 const node = try p.arena.allocator.create(Node.VarType);
3061 const token = p.eatToken(.Keyword_anytype) orelse
3062 p.eatToken(.Keyword_var) orelse return null; // TODO remove in next release cycle
3063 const node = try p.arena.allocator.create(Node.AnyType);
30633064 node.* = .{
30643065 .token = token,
30653066 };
lib/std/zig/render.zig+27-21
......@@ -12,7 +12,7 @@ pub const Error = error{
1212};
1313
1414/// 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 {
1616 // cannot render an invalid tree
1717 std.debug.assert(tree.errors.len == 0);
1818
......@@ -64,7 +64,7 @@ pub fn render(allocator: *mem.Allocator, stream: var, tree: *ast.Tree) (@TypeOf(
6464
6565fn renderRoot(
6666 allocator: *mem.Allocator,
67 stream: var,
67 stream: anytype,
6868 tree: *ast.Tree,
6969) (@TypeOf(stream).Error || Error)!void {
7070 // render all the line comments at the beginning of the file
......@@ -191,13 +191,13 @@ fn renderRoot(
191191 }
192192}
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 {
195195 return renderExtraNewlineToken(tree, stream, start_col, node.firstToken());
196196}
197197
198198fn renderExtraNewlineToken(
199199 tree: *ast.Tree,
200 stream: var,
200 stream: anytype,
201201 start_col: *usize,
202202 first_token: ast.TokenIndex,
203203) @TypeOf(stream).Error!void {
......@@ -218,11 +218,11 @@ fn renderExtraNewlineToken(
218218 }
219219}
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 {
222222 try renderContainerDecl(allocator, stream, tree, indent, start_col, decl, .Newline);
223223}
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 {
226226 switch (decl.id) {
227227 .FnProto => {
228228 const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", decl);
......@@ -358,7 +358,7 @@ fn renderContainerDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree,
358358
359359fn renderExpression(
360360 allocator: *mem.Allocator,
361 stream: var,
361 stream: anytype,
362362 tree: *ast.Tree,
363363 indent: usize,
364364 start_col: *usize,
......@@ -1179,9 +1179,15 @@ fn renderExpression(
11791179 const error_type = @fieldParentPtr(ast.Node.ErrorType, "base", base);
11801180 return renderToken(tree, stream, error_type.token, indent, start_col, space);
11811181 },
1182 .VarType => {
1183 const var_type = @fieldParentPtr(ast.Node.VarType, "base", base);
1184 return renderToken(tree, stream, var_type.token, indent, start_col, space);
1182 .AnyType => {
1183 const any_type = @fieldParentPtr(ast.Node.AnyType, "base", base);
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);
11851191 },
11861192 .ContainerDecl => {
11871193 const container_decl = @fieldParentPtr(ast.Node.ContainerDecl, "base", base);
......@@ -2053,7 +2059,7 @@ fn renderExpression(
20532059
20542060fn renderAsmOutput(
20552061 allocator: *mem.Allocator,
2056 stream: var,
2062 stream: anytype,
20572063 tree: *ast.Tree,
20582064 indent: usize,
20592065 start_col: *usize,
......@@ -2081,7 +2087,7 @@ fn renderAsmOutput(
20812087
20822088fn renderAsmInput(
20832089 allocator: *mem.Allocator,
2084 stream: var,
2090 stream: anytype,
20852091 tree: *ast.Tree,
20862092 indent: usize,
20872093 start_col: *usize,
......@@ -2099,7 +2105,7 @@ fn renderAsmInput(
20992105
21002106fn renderVarDecl(
21012107 allocator: *mem.Allocator,
2102 stream: var,
2108 stream: anytype,
21032109 tree: *ast.Tree,
21042110 indent: usize,
21052111 start_col: *usize,
......@@ -2171,7 +2177,7 @@ fn renderVarDecl(
21712177
21722178fn renderParamDecl(
21732179 allocator: *mem.Allocator,
2174 stream: var,
2180 stream: anytype,
21752181 tree: *ast.Tree,
21762182 indent: usize,
21772183 start_col: *usize,
......@@ -2198,7 +2204,7 @@ fn renderParamDecl(
21982204
21992205fn renderStatement(
22002206 allocator: *mem.Allocator,
2201 stream: var,
2207 stream: anytype,
22022208 tree: *ast.Tree,
22032209 indent: usize,
22042210 start_col: *usize,
......@@ -2236,7 +2242,7 @@ const Space = enum {
22362242
22372243fn renderTokenOffset(
22382244 tree: *ast.Tree,
2239 stream: var,
2245 stream: anytype,
22402246 token_index: ast.TokenIndex,
22412247 indent: usize,
22422248 start_col: *usize,
......@@ -2434,7 +2440,7 @@ fn renderTokenOffset(
24342440
24352441fn renderToken(
24362442 tree: *ast.Tree,
2437 stream: var,
2443 stream: anytype,
24382444 token_index: ast.TokenIndex,
24392445 indent: usize,
24402446 start_col: *usize,
......@@ -2445,8 +2451,8 @@ fn renderToken(
24452451
24462452fn renderDocComments(
24472453 tree: *ast.Tree,
2448 stream: var,
2449 node: var,
2454 stream: anytype,
2455 node: anytype,
24502456 indent: usize,
24512457 start_col: *usize,
24522458) (@TypeOf(stream).Error || Error)!void {
......@@ -2456,7 +2462,7 @@ fn renderDocComments(
24562462
24572463fn renderDocCommentsToken(
24582464 tree: *ast.Tree,
2459 stream: var,
2465 stream: anytype,
24602466 comment: *ast.Node.DocComment,
24612467 first_token: ast.TokenIndex,
24622468 indent: usize,
......@@ -2532,7 +2538,7 @@ const FindByteOutStream = struct {
25322538 }
25332539};
25342540
2535fn copyFixingWhitespace(stream: var, slice: []const u8) @TypeOf(stream).Error!void {
2541fn copyFixingWhitespace(stream: anytype, slice: []const u8) @TypeOf(stream).Error!void {
25362542 for (slice) |byte| switch (byte) {
25372543 '\t' => try stream.writeAll(" "),
25382544 '\r' => {},
lib/std/zig/tokenizer.zig+4-1
......@@ -15,6 +15,7 @@ pub const Token = struct {
1515 .{ "allowzero", .Keyword_allowzero },
1616 .{ "and", .Keyword_and },
1717 .{ "anyframe", .Keyword_anyframe },
18 .{ "anytype", .Keyword_anytype },
1819 .{ "asm", .Keyword_asm },
1920 .{ "async", .Keyword_async },
2021 .{ "await", .Keyword_await },
......@@ -140,6 +141,8 @@ pub const Token = struct {
140141 Keyword_align,
141142 Keyword_allowzero,
142143 Keyword_and,
144 Keyword_anyframe,
145 Keyword_anytype,
143146 Keyword_asm,
144147 Keyword_async,
145148 Keyword_await,
......@@ -168,7 +171,6 @@ pub const Token = struct {
168171 Keyword_or,
169172 Keyword_orelse,
170173 Keyword_packed,
171 Keyword_anyframe,
172174 Keyword_pub,
173175 Keyword_resume,
174176 Keyword_return,
......@@ -263,6 +265,7 @@ pub const Token = struct {
263265 .Keyword_allowzero => "allowzero",
264266 .Keyword_and => "and",
265267 .Keyword_anyframe => "anyframe",
268 .Keyword_anytype => "anytype",
266269 .Keyword_asm => "asm",
267270 .Keyword_async => "async",
268271 .Keyword_await => "await",