authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-03 15:33:22+02:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-03 15:33:22+02:00
log4fae452684c108275e9d3003b4927c8c8d41c59b
tree1a9e7bba82b6be2a54910776c7ae66af336781b1
parent40f35e997a2155df8141ec309e43d6dad9785965

std.zig.parser Refactored top level decl parsing

* Now, the arraylist from the root node is passed through the states. * This allows us to reuse the code for enums, unions and structs

1 files changed, 25 insertions(+), 16 deletions(-)

std/zig/parser.zig+25-16
......@@ -53,6 +53,7 @@ pub const Parser = struct {
5353 }
5454
5555 const TopLevelDeclCtx = struct {
56 decls: &ArrayList(&ast.Node),
5657 visib_token: ?Token,
5758 extern_token: ?Token,
5859 lib_name: ?&ast.Node,
......@@ -75,7 +76,6 @@ pub const Parser = struct {
7576 const ExpectTokenSave = struct {
7677 id: Token.Id,
7778 ptr: &Token,
78
7979 };
8080
8181 fn ListState(comptime T: type) type {
......@@ -88,7 +88,7 @@ pub const Parser = struct {
8888
8989 const State = union(enum) {
9090 TopLevel,
91 TopLevelExtern: ?Token,
91 TopLevelExtern: TopLevelDeclCtx,
9292 TopLevelDecl: TopLevelDeclCtx,
9393 Expression: DestPtr,
9494 ExpectOperand,
......@@ -182,7 +182,14 @@ pub const Parser = struct {
182182 const token = self.getNextToken();
183183 switch (token.id) {
184184 Token.Id.Keyword_pub, Token.Id.Keyword_export => {
185 stack.append(State { .TopLevelExtern = token }) catch unreachable;
185 stack.append(State {
186 .TopLevelExtern = TopLevelDeclCtx {
187 .decls = &root_node.decls,
188 .visib_token = token,
189 .extern_token = null,
190 .lib_name = null,
191 }
192 }) catch unreachable;
186193 continue;
187194 },
188195 Token.Id.Keyword_test => {
......@@ -208,12 +215,19 @@ pub const Parser = struct {
208215 },
209216 else => {
210217 self.putBackToken(token);
211 stack.append(State { .TopLevelExtern = null }) catch unreachable;
218 stack.append(State {
219 .TopLevelExtern = TopLevelDeclCtx {
220 .decls = &root_node.decls,
221 .visib_token = null,
222 .extern_token = null,
223 .lib_name = null,
224 }
225 }) catch unreachable;
212226 continue;
213227 },
214228 }
215229 },
216 State.TopLevelExtern => |visib_token| {
230 State.TopLevelExtern => |ctx| {
217231 const token = self.getNextToken();
218232 if (token.id == Token.Id.Keyword_extern) {
219233 const lib_name_token = self.getNextToken();
......@@ -229,7 +243,8 @@ pub const Parser = struct {
229243
230244 stack.append(State {
231245 .TopLevelDecl = TopLevelDeclCtx {
232 .visib_token = visib_token,
246 .decls = ctx.decls,
247 .visib_token = ctx.visib_token,
233248 .extern_token = token,
234249 .lib_name = lib_name,
235250 },
......@@ -237,13 +252,7 @@ pub const Parser = struct {
237252 continue;
238253 }
239254 self.putBackToken(token);
240 stack.append(State {
241 .TopLevelDecl = TopLevelDeclCtx {
242 .visib_token = visib_token,
243 .extern_token = null,
244 .lib_name = null,
245 },
246 }) catch unreachable;
255 stack.append(State { .TopLevelDecl = ctx }) catch unreachable;
247256 continue;
248257 },
249258 State.TopLevelDecl => |ctx| {
......@@ -252,7 +261,7 @@ pub const Parser = struct {
252261 Token.Id.Keyword_var, Token.Id.Keyword_const => {
253262 stack.append(State.TopLevel) catch unreachable;
254263 // TODO shouldn't need these casts
255 const var_decl_node = try self.createAttachVarDecl(arena, &root_node.decls, ctx.visib_token,
264 const var_decl_node = try self.createAttachVarDecl(arena, ctx.decls, ctx.visib_token,
256265 token, (?Token)(null), ctx.extern_token, ctx.lib_name);
257266 try stack.append(State { .VarDecl = var_decl_node });
258267 continue;
......@@ -260,7 +269,7 @@ pub const Parser = struct {
260269 Token.Id.Keyword_fn => {
261270 stack.append(State.TopLevel) catch unreachable;
262271 // TODO shouldn't need these casts
263 const fn_proto = try self.createAttachFnProto(arena, &root_node.decls, token,
272 const fn_proto = try self.createAttachFnProto(arena, ctx.decls, token,
264273 ctx.extern_token, ctx.lib_name, (?Token)(null), ctx.visib_token, (?Token)(null));
265274 try stack.append(State { .FnDef = fn_proto });
266275 try stack.append(State { .FnProto = fn_proto });
......@@ -270,7 +279,7 @@ pub const Parser = struct {
270279 stack.append(State.TopLevel) catch unreachable;
271280 const fn_token = try self.eatToken(Token.Id.Keyword_fn);
272281 // TODO shouldn't need this cast
273 const fn_proto = try self.createAttachFnProto(arena, &root_node.decls, fn_token,
282 const fn_proto = try self.createAttachFnProto(arena, ctx.decls, fn_token,
274283 ctx.extern_token, ctx.lib_name, (?Token)(token), (?Token)(null), (?Token)(null));
275284 try stack.append(State { .FnDef = fn_proto });
276285 try stack.append(State { .FnProto = fn_proto });