mirror of
https://github.com/ziglang/zig.git
synced 2026-01-09 08:55:36 +00:00
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
This commit is contained in:
parent
aa09e7b639
commit
db9a9f3a6c
@ -55,6 +55,7 @@ pub const Node = struct {
|
||||
AsmOutput,
|
||||
Unreachable,
|
||||
ErrorType,
|
||||
VarType,
|
||||
BuiltinCall,
|
||||
LineComment,
|
||||
TestDecl,
|
||||
@ -108,6 +109,7 @@ pub const Node = struct {
|
||||
Id.AsmOutput => @fieldParentPtr(NodeAsmOutput, "base", base).iterate(index),
|
||||
Id.Unreachable => @fieldParentPtr(NodeUnreachable, "base", base).iterate(index),
|
||||
Id.ErrorType => @fieldParentPtr(NodeErrorType, "base", base).iterate(index),
|
||||
Id.VarType => @fieldParentPtr(NodeVarType, "base", base).iterate(index),
|
||||
Id.BuiltinCall => @fieldParentPtr(NodeBuiltinCall, "base", base).iterate(index),
|
||||
Id.LineComment => @fieldParentPtr(NodeLineComment, "base", base).iterate(index),
|
||||
Id.TestDecl => @fieldParentPtr(NodeTestDecl, "base", base).iterate(index),
|
||||
@ -162,6 +164,7 @@ pub const Node = struct {
|
||||
Id.AsmInput => @fieldParentPtr(NodeAsmInput, "base", base).firstToken(),
|
||||
Id.AsmOutput => @fieldParentPtr(NodeAsmOutput, "base", base).firstToken(),
|
||||
Id.ErrorType => @fieldParentPtr(NodeErrorType, "base", base).firstToken(),
|
||||
Id.VarType => @fieldParentPtr(NodeVarType, "base", base).firstToken(),
|
||||
Id.BuiltinCall => @fieldParentPtr(NodeBuiltinCall, "base", base).firstToken(),
|
||||
Id.LineComment => @fieldParentPtr(NodeLineComment, "base", base).firstToken(),
|
||||
Id.TestDecl => @fieldParentPtr(NodeTestDecl, "base", base).firstToken(),
|
||||
@ -216,6 +219,7 @@ pub const Node = struct {
|
||||
Id.AsmOutput => @fieldParentPtr(NodeAsmOutput, "base", base).lastToken(),
|
||||
Id.Unreachable => @fieldParentPtr(NodeUnreachable, "base", base).lastToken(),
|
||||
Id.ErrorType => @fieldParentPtr(NodeErrorType, "base", base).lastToken(),
|
||||
Id.VarType => @fieldParentPtr(NodeVarType, "base", base).lastToken(),
|
||||
Id.BuiltinCall => @fieldParentPtr(NodeBuiltinCall, "base", base).lastToken(),
|
||||
Id.LineComment => @fieldParentPtr(NodeLineComment, "base", base).lastToken(),
|
||||
Id.TestDecl => @fieldParentPtr(NodeTestDecl, "base", base).lastToken(),
|
||||
@ -541,7 +545,6 @@ pub const NodeFnProto = struct {
|
||||
|
||||
pub const ReturnType = union(enum) {
|
||||
Explicit: &Node,
|
||||
Infer: Token,
|
||||
InferErrorSet: &Node,
|
||||
};
|
||||
|
||||
@ -597,7 +600,6 @@ pub const NodeFnProto = struct {
|
||||
// TODO allow this and next prong to share bodies since the types are the same
|
||||
ReturnType.Explicit => |node| return node.lastToken(),
|
||||
ReturnType.InferErrorSet => |node| return node.lastToken(),
|
||||
ReturnType.Infer => |token| return token,
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -1788,6 +1790,23 @@ pub const NodeErrorType = struct {
|
||||
}
|
||||
};
|
||||
|
||||
pub const NodeVarType = struct {
|
||||
base: Node,
|
||||
token: Token,
|
||||
|
||||
pub fn iterate(self: &NodeVarType, index: usize) ?&Node {
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn firstToken(self: &NodeVarType) Token {
|
||||
return self.token;
|
||||
}
|
||||
|
||||
pub fn lastToken(self: &NodeVarType) Token {
|
||||
return self.token;
|
||||
}
|
||||
};
|
||||
|
||||
pub const NodeLineComment = struct {
|
||||
base: Node,
|
||||
lines: ArrayList(Token),
|
||||
|
||||
@ -1437,6 +1437,14 @@ pub const Parser = struct {
|
||||
dest_ptr.store(&node.base);
|
||||
continue;
|
||||
},
|
||||
Token.Id.Keyword_var => {
|
||||
const node = try arena.create(ast.NodeVarType);
|
||||
*node = ast.NodeVarType {
|
||||
.base = self.initNode(ast.Node.Id.VarType),
|
||||
.token = token,
|
||||
};
|
||||
dest_ptr.store(&node.base);
|
||||
},
|
||||
Token.Id.Keyword_unreachable => {
|
||||
const node = try arena.create(ast.NodeUnreachable);
|
||||
*node = ast.NodeUnreachable {
|
||||
@ -2192,9 +2200,6 @@ pub const Parser = struct {
|
||||
State.FnProtoReturnType => |fn_proto| {
|
||||
const token = self.getNextToken();
|
||||
switch (token.id) {
|
||||
Token.Id.Keyword_var => {
|
||||
fn_proto.return_type = ast.NodeFnProto.ReturnType { .Infer = token };
|
||||
},
|
||||
Token.Id.Bang => {
|
||||
fn_proto.return_type = ast.NodeFnProto.ReturnType { .InferErrorSet = undefined };
|
||||
stack.append(State {
|
||||
@ -3573,6 +3578,10 @@ pub const Parser = struct {
|
||||
const error_type = @fieldParentPtr(ast.NodeErrorType, "base", base);
|
||||
try stream.print("{}", self.tokenizer.getTokenSlice(error_type.token));
|
||||
},
|
||||
ast.Node.Id.VarType => {
|
||||
const var_type = @fieldParentPtr(ast.NodeVarType, "base", base);
|
||||
try stream.print("{}", self.tokenizer.getTokenSlice(var_type.token));
|
||||
},
|
||||
ast.Node.Id.ContainerDecl => {
|
||||
const container_decl = @fieldParentPtr(ast.NodeContainerDecl, "base", base);
|
||||
|
||||
@ -3711,9 +3720,6 @@ pub const Parser = struct {
|
||||
ast.NodeFnProto.ReturnType.Explicit => |node| {
|
||||
try stack.append(RenderState { .Expression = node});
|
||||
},
|
||||
ast.NodeFnProto.ReturnType.Infer => {
|
||||
try stack.append(RenderState { .Text = "var"});
|
||||
},
|
||||
ast.NodeFnProto.ReturnType.InferErrorSet => |node| {
|
||||
try stack.append(RenderState { .Expression = node});
|
||||
try stack.append(RenderState { .Text = "!"});
|
||||
@ -4136,9 +4142,6 @@ pub const Parser = struct {
|
||||
ast.NodeFnProto.ReturnType.Explicit => |node| {
|
||||
try stack.append(RenderState { .Expression = node});
|
||||
},
|
||||
ast.NodeFnProto.ReturnType.Infer => {
|
||||
try stream.print("var");
|
||||
},
|
||||
ast.NodeFnProto.ReturnType.InferErrorSet => |node| {
|
||||
try stream.print("!");
|
||||
try stack.append(RenderState { .Expression = node});
|
||||
@ -4489,6 +4492,15 @@ test "zig fmt: var args" {
|
||||
);
|
||||
}
|
||||
|
||||
test "zig fmt: var type" {
|
||||
try testCanonical(
|
||||
\\fn print(args: var) var {}
|
||||
\\const Var = var;
|
||||
\\const i: var = 0;
|
||||
\\
|
||||
);
|
||||
}
|
||||
|
||||
test "zig fmt: extern function" {
|
||||
try testCanonical(
|
||||
\\extern fn puts(s: &const u8) c_int;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user