Skip to content

Commit df4c575

Browse files
committed
std.zig.parser now parses inline fn proto
Related #909 Allows parsing of `std/os/zen.zig`.
1 parent fe71462 commit df4c575

File tree

2 files changed

+87
-57
lines changed

2 files changed

+87
-57
lines changed

std/zig/ast.zig

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ pub const NodeVarDecl = struct {
214214
eq_token: Token,
215215
mut_token: Token,
216216
comptime_token: ?Token,
217-
extern_token: ?Token,
217+
extern_export_token: ?Token,
218218
lib_name: ?&Node,
219219
type_node: ?&Node,
220220
align_node: ?&Node,
@@ -245,7 +245,7 @@ pub const NodeVarDecl = struct {
245245
pub fn firstToken(self: &NodeVarDecl) Token {
246246
if (self.visib_token) |visib_token| return visib_token;
247247
if (self.comptime_token) |comptime_token| return comptime_token;
248-
if (self.extern_token) |extern_token| return extern_token;
248+
if (self.extern_export_token) |extern_export_token| return extern_export_token;
249249
assert(self.lib_name == null);
250250
return self.mut_token;
251251
}
@@ -496,8 +496,7 @@ pub const NodeFnProto = struct {
496496
params: ArrayList(&Node),
497497
return_type: ReturnType,
498498
var_args_token: ?Token,
499-
extern_token: ?Token,
500-
inline_token: ?Token,
499+
extern_export_inline_token: ?Token,
501500
cc_token: ?Token,
502501
async_attr: ?&NodeAsyncAttribute,
503502
body_node: ?&Node,
@@ -547,9 +546,8 @@ pub const NodeFnProto = struct {
547546

548547
pub fn firstToken(self: &NodeFnProto) Token {
549548
if (self.visib_token) |visib_token| return visib_token;
550-
if (self.extern_token) |extern_token| return extern_token;
549+
if (self.extern_export_inline_token) |extern_export_inline_token| return extern_export_inline_token;
551550
assert(self.lib_name == null);
552-
if (self.inline_token) |inline_token| return inline_token;
553551
if (self.cc_token) |cc_token| return cc_token;
554552
return self.fn_token;
555553
}

std/zig/parser.zig

Lines changed: 83 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub const Parser = struct {
5555
const TopLevelDeclCtx = struct {
5656
decls: &ArrayList(&ast.Node),
5757
visib_token: ?Token,
58-
extern_token: ?Token,
58+
extern_export_inline_token: ?Token,
5959
lib_name: ?&ast.Node,
6060
};
6161

@@ -142,6 +142,7 @@ pub const Parser = struct {
142142
const State = union(enum) {
143143
TopLevel,
144144
TopLevelExtern: TopLevelDeclCtx,
145+
TopLevelLibname: TopLevelDeclCtx,
145146
TopLevelDecl: TopLevelDeclCtx,
146147
ContainerExtern: ContainerExternCtx,
147148
ContainerDecl: &ast.NodeContainerDecl,
@@ -332,13 +333,13 @@ pub const Parser = struct {
332333
root_node.eof_token = token;
333334
return Tree {.root_node = root_node, .arena_allocator = arena_allocator};
334335
},
335-
Token.Id.Keyword_pub, Token.Id.Keyword_export => {
336+
Token.Id.Keyword_pub => {
336337
stack.append(State.TopLevel) catch unreachable;
337338
try stack.append(State {
338339
.TopLevelExtern = TopLevelDeclCtx {
339340
.decls = &root_node.decls,
340341
.visib_token = token,
341-
.extern_token = null,
342+
.extern_export_inline_token = null,
342343
.lib_name = null,
343344
}
344345
});
@@ -363,7 +364,7 @@ pub const Parser = struct {
363364
.TopLevelExtern = TopLevelDeclCtx {
364365
.decls = &root_node.decls,
365366
.visib_token = null,
366-
.extern_token = null,
367+
.extern_export_inline_token = null,
367368
.lib_name = null,
368369
}
369370
});
@@ -374,39 +375,24 @@ pub const Parser = struct {
374375
State.TopLevelExtern => |ctx| {
375376
const token = self.getNextToken();
376377
switch (token.id) {
377-
Token.Id.Keyword_use => {
378-
const node = try self.createAttachNode(arena, ctx.decls, ast.NodeUse,
379-
ast.NodeUse {
380-
.base = undefined,
381-
.visib_token = ctx.visib_token,
382-
.expr = undefined,
383-
.semicolon_token = undefined,
384-
}
385-
);
378+
Token.Id.Keyword_export, Token.Id.Keyword_inline => {
386379
stack.append(State {
387-
.ExpectTokenSave = ExpectTokenSave {
388-
.id = Token.Id.Semicolon,
389-
.ptr = &node.semicolon_token,
390-
}
380+
.TopLevelDecl = TopLevelDeclCtx {
381+
.decls = ctx.decls,
382+
.visib_token = ctx.visib_token,
383+
.extern_export_inline_token = token,
384+
.lib_name = null,
385+
},
391386
}) catch unreachable;
392-
try stack.append(State { .Expression = DestPtr { .Field = &node.expr } });
393387
continue;
394388
},
395389
Token.Id.Keyword_extern => {
396-
const lib_name = blk: {
397-
const lib_name_token = self.getNextToken();
398-
break :blk (try self.parseStringLiteral(arena, lib_name_token)) ?? {
399-
self.putBackToken(lib_name_token);
400-
break :blk null;
401-
};
402-
};
403-
404390
stack.append(State {
405-
.TopLevelDecl = TopLevelDeclCtx {
391+
.TopLevelLibname = TopLevelDeclCtx {
406392
.decls = ctx.decls,
407393
.visib_token = ctx.visib_token,
408-
.extern_token = token,
409-
.lib_name = lib_name,
394+
.extern_export_inline_token = token,
395+
.lib_name = null,
410396
},
411397
}) catch unreachable;
412398
continue;
@@ -418,17 +404,67 @@ pub const Parser = struct {
418404
}
419405
}
420406
},
407+
408+
State.TopLevelLibname => |ctx| {
409+
const lib_name = blk: {
410+
const lib_name_token = self.getNextToken();
411+
break :blk (try self.parseStringLiteral(arena, lib_name_token)) ?? {
412+
self.putBackToken(lib_name_token);
413+
break :blk null;
414+
};
415+
};
416+
417+
stack.append(State {
418+
.TopLevelDecl = TopLevelDeclCtx {
419+
.decls = ctx.decls,
420+
.visib_token = ctx.visib_token,
421+
.extern_export_inline_token = ctx.extern_export_inline_token,
422+
.lib_name = lib_name,
423+
},
424+
}) catch unreachable;
425+
},
426+
421427
State.TopLevelDecl => |ctx| {
422428
const token = self.getNextToken();
423429
switch (token.id) {
430+
Token.Id.Keyword_use => {
431+
if (ctx.extern_export_inline_token != null) {
432+
try self.parseError(&stack, token, "Invalid token {}", @tagName((??ctx.extern_export_inline_token).id));
433+
continue;
434+
}
435+
436+
const node = try self.createAttachNode(arena, ctx.decls, ast.NodeUse,
437+
ast.NodeUse {
438+
.base = undefined,
439+
.visib_token = ctx.visib_token,
440+
.expr = undefined,
441+
.semicolon_token = undefined,
442+
}
443+
);
444+
stack.append(State {
445+
.ExpectTokenSave = ExpectTokenSave {
446+
.id = Token.Id.Semicolon,
447+
.ptr = &node.semicolon_token,
448+
}
449+
}) catch unreachable;
450+
try stack.append(State { .Expression = DestPtr { .Field = &node.expr } });
451+
continue;
452+
},
424453
Token.Id.Keyword_var, Token.Id.Keyword_const => {
454+
if (ctx.extern_export_inline_token) |extern_export_inline_token| {
455+
if (extern_export_inline_token.id == Token.Id.Keyword_inline) {
456+
try self.parseError(&stack, token, "Invalid token {}", @tagName(extern_export_inline_token.id));
457+
continue;
458+
}
459+
}
460+
425461
const var_decl_node = try self.createAttachNode(arena, ctx.decls, ast.NodeVarDecl,
426462
ast.NodeVarDecl {
427463
.base = undefined,
428464
.visib_token = ctx.visib_token,
429465
.mut_token = token,
430466
.comptime_token = null,
431-
.extern_token = ctx.extern_token,
467+
.extern_export_token = ctx.extern_export_inline_token,
432468
.type_node = null,
433469
.align_node = null,
434470
.init_node = null,
@@ -452,8 +488,7 @@ pub const Parser = struct {
452488
.params = ArrayList(&ast.Node).init(arena),
453489
.return_type = undefined,
454490
.var_args_token = null,
455-
.extern_token = ctx.extern_token,
456-
.inline_token = null,
491+
.extern_export_inline_token = ctx.extern_export_inline_token,
457492
.cc_token = null,
458493
.async_attr = null,
459494
.body_node = null,
@@ -475,8 +510,7 @@ pub const Parser = struct {
475510
.params = ArrayList(&ast.Node).init(arena),
476511
.return_type = undefined,
477512
.var_args_token = null,
478-
.extern_token = ctx.extern_token,
479-
.inline_token = null,
513+
.extern_export_inline_token = ctx.extern_export_inline_token,
480514
.cc_token = token,
481515
.async_attr = null,
482516
.body_node = null,
@@ -513,8 +547,7 @@ pub const Parser = struct {
513547
.params = ArrayList(&ast.Node).init(arena),
514548
.return_type = undefined,
515549
.var_args_token = null,
516-
.extern_token = ctx.extern_token,
517-
.inline_token = null,
550+
.extern_export_inline_token = ctx.extern_export_inline_token,
518551
.cc_token = null,
519552
.async_attr = async_node,
520553
.body_node = null,
@@ -752,7 +785,7 @@ pub const Parser = struct {
752785
.TopLevelExtern = TopLevelDeclCtx {
753786
.decls = &container_decl.fields_and_decls,
754787
.visib_token = token,
755-
.extern_token = null,
788+
.extern_export_inline_token = null,
756789
.lib_name = null,
757790
}
758791
});
@@ -764,7 +797,7 @@ pub const Parser = struct {
764797
.TopLevelExtern = TopLevelDeclCtx {
765798
.decls = &container_decl.fields_and_decls,
766799
.visib_token = token,
767-
.extern_token = null,
800+
.extern_export_inline_token = null,
768801
.lib_name = null,
769802
}
770803
});
@@ -781,7 +814,7 @@ pub const Parser = struct {
781814
.TopLevelExtern = TopLevelDeclCtx {
782815
.decls = &container_decl.fields_and_decls,
783816
.visib_token = null,
784-
.extern_token = null,
817+
.extern_export_inline_token = null,
785818
.lib_name = null,
786819
}
787820
});
@@ -1659,8 +1692,7 @@ pub const Parser = struct {
16591692
.params = ArrayList(&ast.Node).init(arena),
16601693
.return_type = undefined,
16611694
.var_args_token = null,
1662-
.extern_token = token,
1663-
.inline_token = null,
1695+
.extern_export_inline_token = token,
16641696
.cc_token = null,
16651697
.async_attr = null,
16661698
.body_node = null,
@@ -1717,8 +1749,7 @@ pub const Parser = struct {
17171749
.params = ArrayList(&ast.Node).init(arena),
17181750
.return_type = undefined,
17191751
.var_args_token = null,
1720-
.extern_token = null,
1721-
.inline_token = null,
1752+
.extern_export_inline_token = null,
17221753
.cc_token = null,
17231754
.async_attr = null,
17241755
.body_node = null,
@@ -1740,8 +1771,7 @@ pub const Parser = struct {
17401771
.params = ArrayList(&ast.Node).init(arena),
17411772
.return_type = undefined,
17421773
.var_args_token = null,
1743-
.extern_token = null,
1744-
.inline_token = null,
1774+
.extern_export_inline_token = null,
17451775
.cc_token = token,
17461776
.async_attr = null,
17471777
.body_node = null,
@@ -2573,7 +2603,7 @@ pub const Parser = struct {
25732603
.visib_token = null,
25742604
.mut_token = mut_token,
25752605
.comptime_token = next,
2576-
.extern_token = null,
2606+
.extern_export_token = null,
25772607
.type_node = null,
25782608
.align_node = null,
25792609
.init_node = null,
@@ -2601,7 +2631,7 @@ pub const Parser = struct {
26012631
.visib_token = null,
26022632
.mut_token = next,
26032633
.comptime_token = null,
2604-
.extern_token = null,
2634+
.extern_export_token = null,
26052635
.type_node = null,
26062636
.align_node = null,
26072637
.init_node = null,
@@ -3281,13 +3311,13 @@ pub const Parser = struct {
32813311
try stack.append(RenderState { .Text = self.tokenizer.getTokenSlice(comptime_token) });
32823312
}
32833313

3284-
if (var_decl.extern_token) |extern_token| {
3314+
if (var_decl.extern_export_token) |extern_export_token| {
32853315
if (var_decl.lib_name != null) {
32863316
try stack.append(RenderState { .Text = " " });
32873317
try stack.append(RenderState { .Expression = ??var_decl.lib_name });
32883318
}
32893319
try stack.append(RenderState { .Text = " " });
3290-
try stack.append(RenderState { .Text = self.tokenizer.getTokenSlice(extern_token) });
3320+
try stack.append(RenderState { .Text = self.tokenizer.getTokenSlice(extern_export_token) });
32913321
}
32923322

32933323
if (var_decl.visib_token) |visib_token| {
@@ -3865,9 +3895,9 @@ pub const Parser = struct {
38653895
try stack.append(RenderState { .Text = " " });
38663896
try stack.append(RenderState { .Expression = lib_name });
38673897
}
3868-
if (fn_proto.extern_token) |extern_token| {
3898+
if (fn_proto.extern_export_inline_token) |extern_export_inline_token| {
38693899
try stack.append(RenderState { .Text = " " });
3870-
try stack.append(RenderState { .Text = self.tokenizer.getTokenSlice(extern_token) });
3900+
try stack.append(RenderState { .Text = self.tokenizer.getTokenSlice(extern_export_inline_token) });
38713901
}
38723902

38733903
if (fn_proto.visib_token) |visib_token| {
@@ -4608,6 +4638,8 @@ test "zig fmt: extern function" {
46084638
try testCanonical(
46094639
\\extern fn puts(s: &const u8) c_int;
46104640
\\extern "c" fn puts(s: &const u8) c_int;
4641+
\\export fn puts(s: &const u8) c_int;
4642+
\\inline fn puts(s: &const u8) c_int;
46114643
\\
46124644
);
46134645
}

0 commit comments

Comments
 (0)