Skip to content

Commit 0d8646d

Browse files
committed
std.zig.parser now parses alignment of functions
Related #909 This allows it to parse `std/special/compiler_rt/index.zig`
1 parent 2b86ffe commit 0d8646d

File tree

1 file changed

+19
-32
lines changed

1 file changed

+19
-32
lines changed

std/zig/parser.zig

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,12 +2330,14 @@ pub const Parser = struct {
23302330
},
23312331

23322332
State.FnProtoAlign => |fn_proto| {
2333+
stack.append(State { .FnProtoReturnType = fn_proto }) catch unreachable;
2334+
23332335
if (self.eatToken(Token.Id.Keyword_align)) |align_token| {
2334-
@panic("TODO fn proto align");
2336+
try stack.append(State { .ExpectToken = Token.Id.RParen });
2337+
try stack.append(State { .Expression = DestPtr { .NullableField = &fn_proto.align_expr } });
2338+
try stack.append(State { .ExpectToken = Token.Id.LParen });
23352339
}
2336-
stack.append(State {
2337-
.FnProtoReturnType = fn_proto,
2338-
}) catch unreachable;
2340+
23392341
continue;
23402342
},
23412343

@@ -2349,10 +2351,6 @@ pub const Parser = struct {
23492351
}) catch unreachable;
23502352
continue;
23512353
},
2352-
Token.Id.Keyword_align => {
2353-
@panic("TODO fn proto align");
2354-
continue;
2355-
},
23562354
else => {
23572355
// TODO: this is a special case. Remove this when #760 is fixed
23582356
if (token.id == Token.Id.Keyword_error) {
@@ -3179,7 +3177,6 @@ pub const Parser = struct {
31793177

31803178
const RenderState = union(enum) {
31813179
TopLevelDecl: &ast.Node,
3182-
FnProtoRParen: &ast.NodeFnProto,
31833180
ParamDecl: &ast.Node,
31843181
Text: []const u8,
31853182
Expression: &ast.Node,
@@ -3868,8 +3865,10 @@ pub const Parser = struct {
38683865
},
38693866
}
38703867

3871-
if (fn_proto.align_expr != null) {
3872-
@panic("TODO");
3868+
if (fn_proto.align_expr) |align_expr| {
3869+
try stack.append(RenderState { .Text = ") " });
3870+
try stack.append(RenderState { .Expression = align_expr});
3871+
try stack.append(RenderState { .Text = "align(" });
38733872
}
38743873

38753874
try stack.append(RenderState { .Text = ") " });
@@ -4271,26 +4270,6 @@ pub const Parser = struct {
42714270
ast.Node.Id.TestDecl,
42724271
ast.Node.Id.ParamDecl => unreachable,
42734272
},
4274-
RenderState.FnProtoRParen => |fn_proto| {
4275-
try stream.print(")");
4276-
if (fn_proto.align_expr != null) {
4277-
@panic("TODO");
4278-
}
4279-
try stream.print(" ");
4280-
if (fn_proto.body_node) |body_node| {
4281-
try stack.append(RenderState { .Expression = body_node});
4282-
try stack.append(RenderState { .Text = " "});
4283-
}
4284-
switch (fn_proto.return_type) {
4285-
ast.NodeFnProto.ReturnType.Explicit => |node| {
4286-
try stack.append(RenderState { .Expression = node});
4287-
},
4288-
ast.NodeFnProto.ReturnType.InferErrorSet => |node| {
4289-
try stream.print("!");
4290-
try stack.append(RenderState { .Expression = node});
4291-
},
4292-
}
4293-
},
42944273
RenderState.Statement => |base| {
42954274
if (base.comment) |comment| {
42964275
for (comment.lines.toSliceConst()) |line_token| {
@@ -4644,12 +4623,20 @@ test "zig fmt: var type" {
46444623
);
46454624
}
46464625

4647-
test "zig fmt: extern function" {
4626+
test "zig fmt: functions" {
46484627
try testCanonical(
46494628
\\extern fn puts(s: &const u8) c_int;
46504629
\\extern "c" fn puts(s: &const u8) c_int;
46514630
\\export fn puts(s: &const u8) c_int;
46524631
\\inline fn puts(s: &const u8) c_int;
4632+
\\pub extern fn puts(s: &const u8) c_int;
4633+
\\pub extern "c" fn puts(s: &const u8) c_int;
4634+
\\pub export fn puts(s: &const u8) c_int;
4635+
\\pub inline fn puts(s: &const u8) c_int;
4636+
\\pub extern fn puts(s: &const u8) align(2 + 2) c_int;
4637+
\\pub extern "c" fn puts(s: &const u8) align(2 + 2) c_int;
4638+
\\pub export fn puts(s: &const u8) align(2 + 2) c_int;
4639+
\\pub inline fn puts(s: &const u8) align(2 + 2) c_int;
46534640
\\
46544641
);
46554642
}

0 commit comments

Comments
 (0)