authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-10 15:16:31+02:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-10 15:16:31+02:00
logdb9a9f3a6c248e74953d9f7cbf6bedc78e3c23ff
treef5ac38593550a832389c2f20b8a2ddd165cf1ce1
parentaa09e7b63995639084d25329954b1972a72ad12d

std.zig.parser now parses the `var` type

* I parse it as a type in all contexts. This is not how the C++ compiler does it, but I think typechecking should catch this

2 files changed, 42 insertions(+), 11 deletions(-)

std/zig/ast.zig+21-2
......@@ -55,6 +55,7 @@ pub const Node = struct {
5555 AsmOutput,
5656 Unreachable,
5757 ErrorType,
58 VarType,
5859 BuiltinCall,
5960 LineComment,
6061 TestDecl,
......@@ -108,6 +109,7 @@ pub const Node = struct {
108109 Id.AsmOutput => @fieldParentPtr(NodeAsmOutput, "base", base).iterate(index),
109110 Id.Unreachable => @fieldParentPtr(NodeUnreachable, "base", base).iterate(index),
110111 Id.ErrorType => @fieldParentPtr(NodeErrorType, "base", base).iterate(index),
112 Id.VarType => @fieldParentPtr(NodeVarType, "base", base).iterate(index),
111113 Id.BuiltinCall => @fieldParentPtr(NodeBuiltinCall, "base", base).iterate(index),
112114 Id.LineComment => @fieldParentPtr(NodeLineComment, "base", base).iterate(index),
113115 Id.TestDecl => @fieldParentPtr(NodeTestDecl, "base", base).iterate(index),
......@@ -162,6 +164,7 @@ pub const Node = struct {
162164 Id.AsmInput => @fieldParentPtr(NodeAsmInput, "base", base).firstToken(),
163165 Id.AsmOutput => @fieldParentPtr(NodeAsmOutput, "base", base).firstToken(),
164166 Id.ErrorType => @fieldParentPtr(NodeErrorType, "base", base).firstToken(),
167 Id.VarType => @fieldParentPtr(NodeVarType, "base", base).firstToken(),
165168 Id.BuiltinCall => @fieldParentPtr(NodeBuiltinCall, "base", base).firstToken(),
166169 Id.LineComment => @fieldParentPtr(NodeLineComment, "base", base).firstToken(),
167170 Id.TestDecl => @fieldParentPtr(NodeTestDecl, "base", base).firstToken(),
......@@ -216,6 +219,7 @@ pub const Node = struct {
216219 Id.AsmOutput => @fieldParentPtr(NodeAsmOutput, "base", base).lastToken(),
217220 Id.Unreachable => @fieldParentPtr(NodeUnreachable, "base", base).lastToken(),
218221 Id.ErrorType => @fieldParentPtr(NodeErrorType, "base", base).lastToken(),
222 Id.VarType => @fieldParentPtr(NodeVarType, "base", base).lastToken(),
219223 Id.BuiltinCall => @fieldParentPtr(NodeBuiltinCall, "base", base).lastToken(),
220224 Id.LineComment => @fieldParentPtr(NodeLineComment, "base", base).lastToken(),
221225 Id.TestDecl => @fieldParentPtr(NodeTestDecl, "base", base).lastToken(),
......@@ -541,7 +545,6 @@ pub const NodeFnProto = struct {
541545
542546 pub const ReturnType = union(enum) {
543547 Explicit: &Node,
544 Infer: Token,
545548 InferErrorSet: &Node,
546549 };
547550
......@@ -597,7 +600,6 @@ pub const NodeFnProto = struct {
597600 // TODO allow this and next prong to share bodies since the types are the same
598601 ReturnType.Explicit => |node| return node.lastToken(),
599602 ReturnType.InferErrorSet => |node| return node.lastToken(),
600 ReturnType.Infer => |token| return token,
601603 }
602604 }
603605};
......@@ -1788,6 +1790,23 @@ pub const NodeErrorType = struct {
17881790 }
17891791};
17901792
1793pub const NodeVarType = struct {
1794 base: Node,
1795 token: Token,
1796
1797 pub fn iterate(self: &NodeVarType, index: usize) ?&Node {
1798 return null;
1799 }
1800
1801 pub fn firstToken(self: &NodeVarType) Token {
1802 return self.token;
1803 }
1804
1805 pub fn lastToken(self: &NodeVarType) Token {
1806 return self.token;
1807 }
1808};
1809
17911810pub const NodeLineComment = struct {
17921811 base: Node,
17931812 lines: ArrayList(Token),
std/zig/parser.zig+21-9
......@@ -1437,6 +1437,14 @@ pub const Parser = struct {
14371437 dest_ptr.store(&node.base);
14381438 continue;
14391439 },
1440 Token.Id.Keyword_var => {
1441 const node = try arena.create(ast.NodeVarType);
1442 *node = ast.NodeVarType {
1443 .base = self.initNode(ast.Node.Id.VarType),
1444 .token = token,
1445 };
1446 dest_ptr.store(&node.base);
1447 },
14401448 Token.Id.Keyword_unreachable => {
14411449 const node = try arena.create(ast.NodeUnreachable);
14421450 *node = ast.NodeUnreachable {
......@@ -2192,9 +2200,6 @@ pub const Parser = struct {
21922200 State.FnProtoReturnType => |fn_proto| {
21932201 const token = self.getNextToken();
21942202 switch (token.id) {
2195 Token.Id.Keyword_var => {
2196 fn_proto.return_type = ast.NodeFnProto.ReturnType { .Infer = token };
2197 },
21982203 Token.Id.Bang => {
21992204 fn_proto.return_type = ast.NodeFnProto.ReturnType { .InferErrorSet = undefined };
22002205 stack.append(State {
......@@ -3573,6 +3578,10 @@ pub const Parser = struct {
35733578 const error_type = @fieldParentPtr(ast.NodeErrorType, "base", base);
35743579 try stream.print("{}", self.tokenizer.getTokenSlice(error_type.token));
35753580 },
3581 ast.Node.Id.VarType => {
3582 const var_type = @fieldParentPtr(ast.NodeVarType, "base", base);
3583 try stream.print("{}", self.tokenizer.getTokenSlice(var_type.token));
3584 },
35763585 ast.Node.Id.ContainerDecl => {
35773586 const container_decl = @fieldParentPtr(ast.NodeContainerDecl, "base", base);
35783587
......@@ -3711,9 +3720,6 @@ pub const Parser = struct {
37113720 ast.NodeFnProto.ReturnType.Explicit => |node| {
37123721 try stack.append(RenderState { .Expression = node});
37133722 },
3714 ast.NodeFnProto.ReturnType.Infer => {
3715 try stack.append(RenderState { .Text = "var"});
3716 },
37173723 ast.NodeFnProto.ReturnType.InferErrorSet => |node| {
37183724 try stack.append(RenderState { .Expression = node});
37193725 try stack.append(RenderState { .Text = "!"});
......@@ -4136,9 +4142,6 @@ pub const Parser = struct {
41364142 ast.NodeFnProto.ReturnType.Explicit => |node| {
41374143 try stack.append(RenderState { .Expression = node});
41384144 },
4139 ast.NodeFnProto.ReturnType.Infer => {
4140 try stream.print("var");
4141 },
41424145 ast.NodeFnProto.ReturnType.InferErrorSet => |node| {
41434146 try stream.print("!");
41444147 try stack.append(RenderState { .Expression = node});
......@@ -4489,6 +4492,15 @@ test "zig fmt: var args" {
44894492 );
44904493}
44914494
4495test "zig fmt: var type" {
4496 try testCanonical(
4497 \\fn print(args: var) var {}
4498 \\const Var = var;
4499 \\const i: var = 0;
4500 \\
4501 );
4502}
4503
44924504test "zig fmt: extern function" {
44934505 try testCanonical(
44944506 \\extern fn puts(s: &const u8) c_int;