Commit c2fb4bfff3

Vexu <git@vexu.eu>
2020-07-11 12:54:16
add 'anytype' to self-hosted parser
1 parent 2e037fd
Changed files (4)
lib/std/zig/ast.zig
@@ -434,7 +434,7 @@ pub const Node = struct {
         Suspend,
 
         // Type expressions
-        VarType,
+        AnyType,
         ErrorType,
         FnProto,
         AnyFrameType,
@@ -2732,19 +2732,19 @@ pub const Node = struct {
         }
     };
 
-    pub const VarType = struct {
-        base: Node = Node{ .id = .VarType },
+    pub const AnyType = struct {
+        base: Node = Node{ .id = .AnyType },
         token: TokenIndex,
 
-        pub fn iterate(self: *const VarType, index: usize) ?*Node {
+        pub fn iterate(self: *const AnyType, index: usize) ?*Node {
             return null;
         }
 
-        pub fn firstToken(self: *const VarType) TokenIndex {
+        pub fn firstToken(self: *const AnyType) TokenIndex {
             return self.token;
         }
 
-        pub fn lastToken(self: *const VarType) TokenIndex {
+        pub fn lastToken(self: *const AnyType) TokenIndex {
             return self.token;
         }
     };
lib/std/zig/parse.zig
@@ -488,7 +488,7 @@ const Parser = struct {
         return p.parseUse();
     }
 
-    /// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr)
+    /// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (Keyword_anytype / TypeExpr)
     fn parseFnProto(p: *Parser) !?*Node {
         // TODO: Remove once extern/async fn rewriting is
         var is_async = false;
@@ -618,9 +618,9 @@ const Parser = struct {
         var align_expr: ?*Node = null;
         var type_expr: ?*Node = null;
         if (p.eatToken(.Colon)) |_| {
-            if (p.eatToken(.Keyword_var)) |var_tok| {
-                const node = try p.arena.allocator.create(Node.VarType);
-                node.* = .{ .token = var_tok };
+            if (p.eatToken(.Keyword_anytype) orelse p.eatToken(.Keyword_var)) |anytype_tok| {
+                const node = try p.arena.allocator.create(Node.AnyType);
+                node.* = .{ .token = anytype_tok };
                 type_expr = &node.base;
             } else {
                 type_expr = try p.expectNode(parseTypeExpr, .{
@@ -2022,7 +2022,7 @@ const Parser = struct {
     }
 
     /// ParamType
-    ///     <- KEYWORD_var
+    ///     <- Keyword_anytype
     ///      / DOT3
     ///      / TypeExpr
     fn parseParamType(p: *Parser) !?Node.FnProto.ParamDecl.ParamType {
@@ -3058,8 +3058,9 @@ const Parser = struct {
     }
 
     fn parseVarType(p: *Parser) !?*Node {
-        const token = p.eatToken(.Keyword_var) orelse return null;
-        const node = try p.arena.allocator.create(Node.VarType);
+        const token = p.eatToken(.Keyword_anytype) orelse
+            p.eatToken(.Keyword_var) orelse return null; // TODO remove in next release cycle
+        const node = try p.arena.allocator.create(Node.AnyType);
         node.* = .{
             .token = token,
         };
lib/std/zig/render.zig
@@ -12,7 +12,7 @@ pub const Error = error{
 };
 
 /// Returns whether anything changed
-pub fn render(allocator: *mem.Allocator, stream: var, tree: *ast.Tree) (@TypeOf(stream).Error || Error)!bool {
+pub fn render(allocator: *mem.Allocator, stream: anytype, tree: *ast.Tree) (@TypeOf(stream).Error || Error)!bool {
     // cannot render an invalid tree
     std.debug.assert(tree.errors.len == 0);
 
@@ -64,7 +64,7 @@ pub fn render(allocator: *mem.Allocator, stream: var, tree: *ast.Tree) (@TypeOf(
 
 fn renderRoot(
     allocator: *mem.Allocator,
-    stream: var,
+    stream: anytype,
     tree: *ast.Tree,
 ) (@TypeOf(stream).Error || Error)!void {
     // render all the line comments at the beginning of the file
@@ -191,13 +191,13 @@ fn renderRoot(
     }
 }
 
-fn renderExtraNewline(tree: *ast.Tree, stream: var, start_col: *usize, node: *ast.Node) @TypeOf(stream).Error!void {
+fn renderExtraNewline(tree: *ast.Tree, stream: anytype, start_col: *usize, node: *ast.Node) @TypeOf(stream).Error!void {
     return renderExtraNewlineToken(tree, stream, start_col, node.firstToken());
 }
 
 fn renderExtraNewlineToken(
     tree: *ast.Tree,
-    stream: var,
+    stream: anytype,
     start_col: *usize,
     first_token: ast.TokenIndex,
 ) @TypeOf(stream).Error!void {
@@ -218,11 +218,11 @@ fn renderExtraNewlineToken(
     }
 }
 
-fn renderTopLevelDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, indent: usize, start_col: *usize, decl: *ast.Node) (@TypeOf(stream).Error || Error)!void {
+fn renderTopLevelDecl(allocator: *mem.Allocator, stream: anytype, tree: *ast.Tree, indent: usize, start_col: *usize, decl: *ast.Node) (@TypeOf(stream).Error || Error)!void {
     try renderContainerDecl(allocator, stream, tree, indent, start_col, decl, .Newline);
 }
 
-fn renderContainerDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, indent: usize, start_col: *usize, decl: *ast.Node, space: Space) (@TypeOf(stream).Error || Error)!void {
+fn renderContainerDecl(allocator: *mem.Allocator, stream: anytype, tree: *ast.Tree, indent: usize, start_col: *usize, decl: *ast.Node, space: Space) (@TypeOf(stream).Error || Error)!void {
     switch (decl.id) {
         .FnProto => {
             const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", decl);
@@ -358,7 +358,7 @@ fn renderContainerDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree,
 
 fn renderExpression(
     allocator: *mem.Allocator,
-    stream: var,
+    stream: anytype,
     tree: *ast.Tree,
     indent: usize,
     start_col: *usize,
@@ -1179,9 +1179,15 @@ fn renderExpression(
             const error_type = @fieldParentPtr(ast.Node.ErrorType, "base", base);
             return renderToken(tree, stream, error_type.token, indent, start_col, space);
         },
-        .VarType => {
-            const var_type = @fieldParentPtr(ast.Node.VarType, "base", base);
-            return renderToken(tree, stream, var_type.token, indent, start_col, space);
+        .AnyType => {
+            const any_type = @fieldParentPtr(ast.Node.AnyType, "base", base);
+            if (mem.eql(u8, tree.tokenSlice(any_type.token), "var")) {
+                // TODO remove in next release cycle
+                try stream.writeAll("anytype");
+                if (space == .Comma) try stream.writeAll(",\n");
+                return;
+            }
+            return renderToken(tree, stream, any_type.token, indent, start_col, space);
         },
         .ContainerDecl => {
             const container_decl = @fieldParentPtr(ast.Node.ContainerDecl, "base", base);
@@ -2053,7 +2059,7 @@ fn renderExpression(
 
 fn renderAsmOutput(
     allocator: *mem.Allocator,
-    stream: var,
+    stream: anytype,
     tree: *ast.Tree,
     indent: usize,
     start_col: *usize,
@@ -2081,7 +2087,7 @@ fn renderAsmOutput(
 
 fn renderAsmInput(
     allocator: *mem.Allocator,
-    stream: var,
+    stream: anytype,
     tree: *ast.Tree,
     indent: usize,
     start_col: *usize,
@@ -2099,7 +2105,7 @@ fn renderAsmInput(
 
 fn renderVarDecl(
     allocator: *mem.Allocator,
-    stream: var,
+    stream: anytype,
     tree: *ast.Tree,
     indent: usize,
     start_col: *usize,
@@ -2171,7 +2177,7 @@ fn renderVarDecl(
 
 fn renderParamDecl(
     allocator: *mem.Allocator,
-    stream: var,
+    stream: anytype,
     tree: *ast.Tree,
     indent: usize,
     start_col: *usize,
@@ -2198,7 +2204,7 @@ fn renderParamDecl(
 
 fn renderStatement(
     allocator: *mem.Allocator,
-    stream: var,
+    stream: anytype,
     tree: *ast.Tree,
     indent: usize,
     start_col: *usize,
@@ -2236,7 +2242,7 @@ const Space = enum {
 
 fn renderTokenOffset(
     tree: *ast.Tree,
-    stream: var,
+    stream: anytype,
     token_index: ast.TokenIndex,
     indent: usize,
     start_col: *usize,
@@ -2434,7 +2440,7 @@ fn renderTokenOffset(
 
 fn renderToken(
     tree: *ast.Tree,
-    stream: var,
+    stream: anytype,
     token_index: ast.TokenIndex,
     indent: usize,
     start_col: *usize,
@@ -2445,8 +2451,8 @@ fn renderToken(
 
 fn renderDocComments(
     tree: *ast.Tree,
-    stream: var,
-    node: var,
+    stream: anytype,
+    node: anytype,
     indent: usize,
     start_col: *usize,
 ) (@TypeOf(stream).Error || Error)!void {
@@ -2456,7 +2462,7 @@ fn renderDocComments(
 
 fn renderDocCommentsToken(
     tree: *ast.Tree,
-    stream: var,
+    stream: anytype,
     comment: *ast.Node.DocComment,
     first_token: ast.TokenIndex,
     indent: usize,
@@ -2532,7 +2538,7 @@ const FindByteOutStream = struct {
     }
 };
 
-fn copyFixingWhitespace(stream: var, slice: []const u8) @TypeOf(stream).Error!void {
+fn copyFixingWhitespace(stream: anytype, slice: []const u8) @TypeOf(stream).Error!void {
     for (slice) |byte| switch (byte) {
         '\t' => try stream.writeAll("    "),
         '\r' => {},
lib/std/zig/tokenizer.zig
@@ -15,6 +15,7 @@ pub const Token = struct {
         .{ "allowzero", .Keyword_allowzero },
         .{ "and", .Keyword_and },
         .{ "anyframe", .Keyword_anyframe },
+        .{ "anytype", .Keyword_anytype },
         .{ "asm", .Keyword_asm },
         .{ "async", .Keyword_async },
         .{ "await", .Keyword_await },
@@ -140,6 +141,8 @@ pub const Token = struct {
         Keyword_align,
         Keyword_allowzero,
         Keyword_and,
+        Keyword_anyframe,
+        Keyword_anytype,
         Keyword_asm,
         Keyword_async,
         Keyword_await,
@@ -168,7 +171,6 @@ pub const Token = struct {
         Keyword_or,
         Keyword_orelse,
         Keyword_packed,
-        Keyword_anyframe,
         Keyword_pub,
         Keyword_resume,
         Keyword_return,
@@ -263,6 +265,7 @@ pub const Token = struct {
                 .Keyword_allowzero => "allowzero",
                 .Keyword_and => "and",
                 .Keyword_anyframe => "anyframe",
+                .Keyword_anytype => "anytype",
                 .Keyword_asm => "asm",
                 .Keyword_async => "async",
                 .Keyword_await => "await",