authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-11 12:05:10+02:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-11 12:05:10+02:00
log281c17f6ae3c294d0b7139fe640dd0cb30123ea1
tree22597baab53aa9bd2b79405d0cade6e200950a75
parentdae287524d94219b1f3a2ec82d410b9f34fb2546

std.zig.parser:

* Renamed eatToken to expectToken * A new eatToken fn, which only eats the token, if the id match * Inlined initNode, as it is not suppose to be used outside createNode

1 files changed, 148 insertions(+), 158 deletions(-)

std/zig/parser.zig+148-158
...@@ -266,8 +266,7 @@ pub const Parser = struct {...@@ -266,8 +266,7 @@ pub const Parser = struct {
266266
267 // look for line comments267 // look for line comments
268 while (true) {268 while (true) {
269 const token = self.getNextToken();269 if (self.eatToken(Token.Id.LineComment)) |line_comment| {
270 if (token.id == Token.Id.LineComment) {
271 const node = blk: {270 const node = blk: {
272 if (self.pending_line_comment_node) |comment_node| {271 if (self.pending_line_comment_node) |comment_node| {
273 break :blk comment_node;272 break :blk comment_node;
...@@ -284,10 +283,9 @@ pub const Parser = struct {...@@ -284,10 +283,9 @@ pub const Parser = struct {
284 break :blk comment_node;283 break :blk comment_node;
285 }284 }
286 };285 };
287 try node.lines.append(token);286 try node.lines.append(line_comment);
288 continue;287 continue;
289 }288 }
290 self.putBackToken(token);
291 break;289 break;
292 }290 }
293291
...@@ -301,8 +299,8 @@ pub const Parser = struct {...@@ -301,8 +299,8 @@ pub const Parser = struct {
301 Token.Id.Keyword_test => {299 Token.Id.Keyword_test => {
302 stack.append(State.TopLevel) catch unreachable;300 stack.append(State.TopLevel) catch unreachable;
303301
304 const name_token = (try self.eatToken(&stack, Token.Id.StringLiteral)) ?? continue;302 const name_token = (try self.expectToken(&stack, Token.Id.StringLiteral)) ?? continue;
305 const lbrace = (try self.eatToken(&stack, Token.Id.LBrace)) ?? continue;303 const lbrace = (try self.expectToken(&stack, Token.Id.LBrace)) ?? continue;
306304
307 const block = try self.createNode(arena, ast.NodeBlock,305 const block = try self.createNode(arena, ast.NodeBlock,
308 ast.NodeBlock {306 ast.NodeBlock {
...@@ -580,25 +578,27 @@ pub const Parser = struct {...@@ -580,25 +578,27 @@ pub const Parser = struct {
580 },578 },
581 State.VarDeclEq => |var_decl| {579 State.VarDeclEq => |var_decl| {
582 const token = self.getNextToken();580 const token = self.getNextToken();
583 if (token.id == Token.Id.Equal) {581 switch (token.id) {
584 var_decl.eq_token = token;582 Token.Id.Equal => {
585 stack.append(State {583 var_decl.eq_token = token;
586 .ExpectTokenSave = ExpectTokenSave {584 stack.append(State {
587 .id = Token.Id.Semicolon,585 .ExpectTokenSave = ExpectTokenSave {
588 .ptr = &var_decl.semicolon_token,586 .id = Token.Id.Semicolon,
589 },587 .ptr = &var_decl.semicolon_token,
590 }) catch unreachable;588 },
591 try stack.append(State {589 }) catch unreachable;
592 .Expression = DestPtr {.NullableField = &var_decl.init_node},590 try stack.append(State { .Expression = DestPtr {.NullableField = &var_decl.init_node} });
593 });591 continue;
594 continue;592 },
595 }593 Token.Id.Semicolon => {
596 if (token.id == Token.Id.Semicolon) {594 var_decl.semicolon_token = token;
597 var_decl.semicolon_token = token;595 continue;
598 continue;596 },
597 else => {
598 try self.parseError(&stack, token, "expected '=' or ';', found {}", @tagName(token.id));
599 continue;
600 }
599 }601 }
600 try self.parseError(&stack, token, "expected '=' or ';', found {}", @tagName(token.id));
601 continue;
602 },602 },
603603
604 State.ContainerExtern => |ctx| {604 State.ContainerExtern => |ctx| {
...@@ -752,12 +752,12 @@ pub const Parser = struct {...@@ -752,12 +752,12 @@ pub const Parser = struct {
752 },752 },
753753
754 State.ExpectToken => |token_id| {754 State.ExpectToken => |token_id| {
755 _ = (try self.eatToken(&stack, token_id)) ?? continue;755 _ = (try self.expectToken(&stack, token_id)) ?? continue;
756 continue;756 continue;
757 },757 },
758758
759 State.ExpectTokenSave => |expect_token_save| {759 State.ExpectTokenSave => |expect_token_save| {
760 *expect_token_save.ptr = (try self.eatToken(&stack, expect_token_save.id)) ?? continue;760 *expect_token_save.ptr = (try self.expectToken(&stack, expect_token_save.id)) ?? continue;
761 continue;761 continue;
762 },762 },
763763
...@@ -817,7 +817,7 @@ pub const Parser = struct {...@@ -817,7 +817,7 @@ pub const Parser = struct {
817 break :blk null;817 break :blk null;
818 }818 }
819819
820 break :blk (try self.eatToken(&stack, Token.Id.Identifier)) ?? continue;820 break :blk (try self.expectToken(&stack, Token.Id.Identifier)) ?? continue;
821 };821 };
822822
823 const node = try self.createToDestNode(arena, dest_ptr, ast.NodeControlFlowExpression,823 const node = try self.createToDestNode(arena, dest_ptr, ast.NodeControlFlowExpression,
...@@ -979,23 +979,20 @@ pub const Parser = struct {...@@ -979,23 +979,20 @@ pub const Parser = struct {
979 },979 },
980980
981 State.RangeExpressionEnd => |dest_ptr| {981 State.RangeExpressionEnd => |dest_ptr| {
982 const token = self.getNextToken();982 if (self.eatToken(Token.Id.Ellipsis3)) |ellipsis3| {
983 if (token.id == Token.Id.Ellipsis3) {
984 const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp,983 const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp,
985 ast.NodeInfixOp {984 ast.NodeInfixOp {
986 .base = undefined,985 .base = undefined,
987 .lhs = dest_ptr.get(),986 .lhs = dest_ptr.get(),
988 .op_token = token,987 .op_token = ellipsis3,
989 .op = ast.NodeInfixOp.InfixOp.Range,988 .op = ast.NodeInfixOp.InfixOp.Range,
990 .rhs = undefined,989 .rhs = undefined,
991 }990 }
992 );991 );
993 stack.append(State { .Expression = DestPtr { .Field = &node.rhs } }) catch unreachable;992 stack.append(State { .Expression = DestPtr { .Field = &node.rhs } }) catch unreachable;
994 continue;
995 } else {
996 self.putBackToken(token);
997 continue;
998 }993 }
994
995 continue;
999 },996 },
1000997
1001 State.AssignmentExpressionBegin => |dest_ptr| {998 State.AssignmentExpressionBegin => |dest_ptr| {
...@@ -1329,57 +1326,49 @@ pub const Parser = struct {...@@ -1329,57 +1326,49 @@ pub const Parser = struct {
1329 },1326 },
13301327
1331 State.CurlySuffixExpressionEnd => |dest_ptr| {1328 State.CurlySuffixExpressionEnd => |dest_ptr| {
1332 const token = self.getNextToken();1329 if (self.eatToken(Token.Id.LBrace) == null) {
1333 if (token.id != Token.Id.LBrace) {
1334 self.putBackToken(token);
1335 continue;1330 continue;
1336 }1331 }
13371332
1338 const next = self.getNextToken();1333 if (self.isPeekToken(Token.Id.Period)) {
1339 switch (next.id) {1334 const node = try self.createToDestNode(arena, dest_ptr, ast.NodeSuffixOp,
1340 Token.Id.Period => {1335 ast.NodeSuffixOp {
1341 const node = try self.createToDestNode(arena, dest_ptr, ast.NodeSuffixOp,1336 .base = undefined,
1342 ast.NodeSuffixOp {1337 .lhs = dest_ptr.get(),
1343 .base = undefined,1338 .op = ast.NodeSuffixOp.SuffixOp {
1344 .lhs = dest_ptr.get(),1339 .StructInitializer = ArrayList(&ast.NodeFieldInitializer).init(arena),
1345 .op = ast.NodeSuffixOp.SuffixOp {1340 },
1346 .StructInitializer = ArrayList(&ast.NodeFieldInitializer).init(arena),1341 .rtoken = undefined,
1347 },1342 }
1348 .rtoken = undefined,1343 );
1349 }1344 stack.append(State { .CurlySuffixExpressionEnd = dest_ptr }) catch unreachable;
1350 );1345 try stack.append(State {
1351 stack.append(State { .CurlySuffixExpressionEnd = dest_ptr }) catch unreachable;1346 .FieldInitListItemOrEnd = ListSave(&ast.NodeFieldInitializer) {
1352 try stack.append(State {1347 .list = &node.op.StructInitializer,
1353 .FieldInitListItemOrEnd = ListSave(&ast.NodeFieldInitializer) {1348 .ptr = &node.rtoken,
1354 .list = &node.op.StructInitializer,1349 }
1355 .ptr = &node.rtoken,1350 });
1356 }1351 continue;
1357 });1352 } else {
1358 self.putBackToken(next);1353 const node = try self.createToDestNode(arena, dest_ptr, ast.NodeSuffixOp,
1359 continue;1354 ast.NodeSuffixOp {
1360 },1355 .base = undefined,
1361 else => {1356 .lhs = dest_ptr.get(),
1362 const node = try self.createToDestNode(arena, dest_ptr, ast.NodeSuffixOp,1357 .op = ast.NodeSuffixOp.SuffixOp {
1363 ast.NodeSuffixOp {1358 .ArrayInitializer = ArrayList(&ast.Node).init(arena),
1364 .base = undefined,1359 },
1365 .lhs = dest_ptr.get(),1360 .rtoken = undefined,
1366 .op = ast.NodeSuffixOp.SuffixOp {1361 }
1367 .ArrayInitializer = ArrayList(&ast.Node).init(arena),1362 );
1368 },1363 stack.append(State { .CurlySuffixExpressionEnd = dest_ptr }) catch unreachable;
1369 .rtoken = undefined,1364 try stack.append(State {
1370 }1365 .ExprListItemOrEnd = ExprListCtx {
1371 );1366 .list = &node.op.ArrayInitializer,
1372 stack.append(State { .CurlySuffixExpressionEnd = dest_ptr }) catch unreachable;1367 .end = Token.Id.RBrace,
1373 try stack.append(State {1368 .ptr = &node.rtoken,
1374 .ExprListItemOrEnd = ExprListCtx {1369 }
1375 .list = &node.op.ArrayInitializer,1370 });
1376 .end = Token.Id.RBrace,1371 continue;
1377 .ptr = &node.rtoken,
1378 }
1379 });
1380 self.putBackToken(next);
1381 continue;
1382 },
1383 }1372 }
1384 },1373 },
13851374
...@@ -1836,7 +1825,7 @@ pub const Parser = struct {...@@ -1836,7 +1825,7 @@ pub const Parser = struct {
1836 continue;1825 continue;
1837 },1826 },
1838 Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc => {1827 Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc => {
1839 const fn_token = (try self.eatToken(&stack, Token.Id.Keyword_fn)) ?? continue;1828 const fn_token = (try self.expectToken(&stack, Token.Id.Keyword_fn)) ?? continue;
1840 const fn_proto = try self.createToDestNode(arena, dest_ptr, ast.NodeFnProto,1829 const fn_proto = try self.createToDestNode(arena, dest_ptr, ast.NodeFnProto,
1841 ast.NodeFnProto {1830 ast.NodeFnProto {
1842 .base = undefined,1831 .base = undefined,
...@@ -1867,8 +1856,8 @@ pub const Parser = struct {...@@ -1867,8 +1856,8 @@ pub const Parser = struct {
1867 }1856 }
1868 break :blk true;1857 break :blk true;
1869 };1858 };
1870 _ = (try self.eatToken(&stack, Token.Id.LParen)) ?? continue;1859 _ = (try self.expectToken(&stack, Token.Id.LParen)) ?? continue;
1871 const template = (try self.eatToken(&stack, Token.Id.StringLiteral)) ?? continue;1860 const template = (try self.expectToken(&stack, Token.Id.StringLiteral)) ?? continue;
1872 // TODO parse template1861 // TODO parse template
18731862
1874 const node = try self.createToDestNode(arena, dest_ptr, ast.NodeAsm,1863 const node = try self.createToDestNode(arena, dest_ptr, ast.NodeAsm,
...@@ -1964,11 +1953,11 @@ pub const Parser = struct {...@@ -1964,11 +1953,11 @@ pub const Parser = struct {
1964 stack.append(State { .AsmOutputItems = items }) catch unreachable;1953 stack.append(State { .AsmOutputItems = items }) catch unreachable;
1965 try stack.append(State { .IfToken = Token.Id.Comma });1954 try stack.append(State { .IfToken = Token.Id.Comma });
19661955
1967 const symbolic_name = (try self.eatToken(&stack, Token.Id.Identifier)) ?? continue;1956 const symbolic_name = (try self.expectToken(&stack, Token.Id.Identifier)) ?? continue;
1968 _ = (try self.eatToken(&stack, Token.Id.RBracket)) ?? continue;1957 _ = (try self.expectToken(&stack, Token.Id.RBracket)) ?? continue;
1969 const constraint = (try self.eatToken(&stack, Token.Id.StringLiteral)) ?? continue;1958 const constraint = (try self.expectToken(&stack, Token.Id.StringLiteral)) ?? continue;
19701959
1971 _ = (try self.eatToken(&stack, Token.Id.LParen)) ?? continue;1960 _ = (try self.expectToken(&stack, Token.Id.LParen)) ?? continue;
1972 try stack.append(State { .ExpectToken = Token.Id.RParen });1961 try stack.append(State { .ExpectToken = Token.Id.RParen });
19731962
1974 const node = try self.createNode(arena, ast.NodeAsmOutput,1963 const node = try self.createNode(arena, ast.NodeAsmOutput,
...@@ -2009,11 +1998,11 @@ pub const Parser = struct {...@@ -2009,11 +1998,11 @@ pub const Parser = struct {
2009 stack.append(State { .AsmInputItems = items }) catch unreachable;1998 stack.append(State { .AsmInputItems = items }) catch unreachable;
2010 try stack.append(State { .IfToken = Token.Id.Comma });1999 try stack.append(State { .IfToken = Token.Id.Comma });
20112000
2012 const symbolic_name = (try self.eatToken(&stack, Token.Id.Identifier)) ?? continue;2001 const symbolic_name = (try self.expectToken(&stack, Token.Id.Identifier)) ?? continue;
2013 _ = (try self.eatToken(&stack, Token.Id.RBracket)) ?? continue;2002 _ = (try self.expectToken(&stack, Token.Id.RBracket)) ?? continue;
2014 const constraint = (try self.eatToken(&stack, Token.Id.StringLiteral)) ?? continue;2003 const constraint = (try self.expectToken(&stack, Token.Id.StringLiteral)) ?? continue;
20152004
2016 _ = (try self.eatToken(&stack, Token.Id.LParen)) ?? continue;2005 _ = (try self.expectToken(&stack, Token.Id.LParen)) ?? continue;
2017 try stack.append(State { .ExpectToken = Token.Id.RParen });2006 try stack.append(State { .ExpectToken = Token.Id.RParen });
20182007
2019 const node = try self.createNode(arena, ast.NodeAsmInput,2008 const node = try self.createNode(arena, ast.NodeAsmInput,
...@@ -2055,12 +2044,10 @@ pub const Parser = struct {...@@ -2055,12 +2044,10 @@ pub const Parser = struct {
2055 },2044 },
20562045
2057 State.FieldInitListItemOrEnd => |list_state| {2046 State.FieldInitListItemOrEnd => |list_state| {
2058 var token = self.getNextToken();2047 if (self.eatToken(Token.Id.RBrace)) |rbrace| {
2059 if (token.id == Token.Id.RBrace){2048 *list_state.ptr = rbrace;
2060 *list_state.ptr = token;
2061 continue;2049 continue;
2062 }2050 }
2063 self.putBackToken(token);
20642051
2065 const node = try self.createNode(arena, ast.NodeFieldInitializer,2052 const node = try self.createNode(arena, ast.NodeFieldInitializer,
2066 ast.NodeFieldInitializer {2053 ast.NodeFieldInitializer {
...@@ -2090,12 +2077,10 @@ pub const Parser = struct {...@@ -2090,12 +2077,10 @@ pub const Parser = struct {
2090 },2077 },
20912078
2092 State.SwitchCaseOrEnd => |list_state| {2079 State.SwitchCaseOrEnd => |list_state| {
2093 var token = self.getNextToken();2080 if (self.eatToken(Token.Id.RBrace)) |rbrace| {
2094 if (token.id == Token.Id.RBrace){2081 *list_state.ptr = rbrace;
2095 *list_state.ptr = token;
2096 continue;2082 continue;
2097 }2083 }
2098 self.putBackToken(token);
20992084
2100 const node = try self.createNode(arena, ast.NodeSwitchCase,2085 const node = try self.createNode(arena, ast.NodeSwitchCase,
2101 ast.NodeSwitchCase {2086 ast.NodeSwitchCase {
...@@ -2112,12 +2097,12 @@ pub const Parser = struct {...@@ -2112,12 +2097,12 @@ pub const Parser = struct {
21122097
2113 const maybe_else = self.getNextToken();2098 const maybe_else = self.getNextToken();
2114 if (maybe_else.id == Token.Id.Keyword_else) {2099 if (maybe_else.id == Token.Id.Keyword_else) {
2115 const else_node = try arena.create(ast.NodeSwitchElse);2100 const else_node = try self.createAttachNode(arena, &node.items, ast.NodeSwitchElse,
2116 *else_node = ast.NodeSwitchElse {2101 ast.NodeSwitchElse {
2117 .base = self.initNode(ast.Node.Id.SwitchElse),2102 .base = undefined,
2118 .token = maybe_else,2103 .token = maybe_else,
2119 };2104 }
2120 try node.items.append(&else_node.base);2105 );
2121 try stack.append(State { .ExpectToken = Token.Id.EqualAngleBracketRight });2106 try stack.append(State { .ExpectToken = Token.Id.EqualAngleBracketRight });
2122 continue;2107 continue;
2123 } else {2108 } else {
...@@ -2186,7 +2171,7 @@ pub const Parser = struct {...@@ -2186,7 +2171,7 @@ pub const Parser = struct {
2186 continue;2171 continue;
2187 }2172 }
21882173
2189 _ = (try self.eatToken(&stack, Token.Id.LParen)) ?? continue;2174 _ = (try self.expectToken(&stack, Token.Id.LParen)) ?? continue;
2190 stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable;2175 stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable;
2191 try stack.append(State { .AssignmentExpressionBegin = DestPtr { .NullableField = dest } });2176 try stack.append(State { .AssignmentExpressionBegin = DestPtr { .NullableField = dest } });
2192 },2177 },
...@@ -2232,8 +2217,8 @@ pub const Parser = struct {...@@ -2232,8 +2217,8 @@ pub const Parser = struct {
2232 continue;2217 continue;
2233 }2218 }
22342219
2235 const error_symbol = (try self.eatToken(&stack, Token.Id.Identifier)) ?? continue;2220 const error_symbol = (try self.expectToken(&stack, Token.Id.Identifier)) ?? continue;
2236 const rpipe = (try self.eatToken(&stack, Token.Id.Pipe)) ?? continue;2221 const rpipe = (try self.expectToken(&stack, Token.Id.Pipe)) ?? continue;
2237 *dest = try self.createNode(arena, ast.NodePayload,2222 *dest = try self.createNode(arena, ast.NodePayload,
2238 ast.NodePayload {2223 ast.NodePayload {
2239 .base = undefined,2224 .base = undefined,
...@@ -2261,8 +2246,8 @@ pub const Parser = struct {...@@ -2261,8 +2246,8 @@ pub const Parser = struct {
2261 }2246 }
2262 };2247 };
22632248
2264 const value_symbol = (try self.eatToken(&stack, Token.Id.Identifier)) ?? continue;2249 const value_symbol = (try self.expectToken(&stack, Token.Id.Identifier)) ?? continue;
2265 const rpipe = (try self.eatToken(&stack, Token.Id.Pipe)) ?? continue;2250 const rpipe = (try self.expectToken(&stack, Token.Id.Pipe)) ?? continue;
2266 *dest = try self.createNode(arena, ast.NodePointerPayload,2251 *dest = try self.createNode(arena, ast.NodePointerPayload,
2267 ast.NodePointerPayload {2252 ast.NodePointerPayload {
2268 .base = undefined,2253 .base = undefined,
...@@ -2291,7 +2276,7 @@ pub const Parser = struct {...@@ -2291,7 +2276,7 @@ pub const Parser = struct {
2291 }2276 }
2292 };2277 };
22932278
2294 const value_symbol = (try self.eatToken(&stack, Token.Id.Identifier)) ?? continue;2279 const value_symbol = (try self.expectToken(&stack, Token.Id.Identifier)) ?? continue;
2295 const index_symbol = blk: {2280 const index_symbol = blk: {
2296 const comma = self.getNextToken();2281 const comma = self.getNextToken();
2297 if (comma.id != Token.Id.Comma) {2282 if (comma.id != Token.Id.Comma) {
...@@ -2299,11 +2284,11 @@ pub const Parser = struct {...@@ -2299,11 +2284,11 @@ pub const Parser = struct {
2299 break :blk null;2284 break :blk null;
2300 }2285 }
23012286
2302 const symbol = (try self.eatToken(&stack, Token.Id.Identifier)) ?? continue;2287 const symbol = (try self.expectToken(&stack, Token.Id.Identifier)) ?? continue;
2303 break :blk try self.createLiteral(arena, ast.NodeIdentifier, symbol);2288 break :blk try self.createLiteral(arena, ast.NodeIdentifier, symbol);
2304 };2289 };
23052290
2306 const rpipe = (try self.eatToken(&stack, Token.Id.Pipe)) ?? continue;2291 const rpipe = (try self.expectToken(&stack, Token.Id.Pipe)) ?? continue;
2307 *dest = try self.createNode(arena, ast.NodePointerIndexPayload,2292 *dest = try self.createNode(arena, ast.NodePointerIndexPayload,
2308 ast.NodePointerIndexPayload {2293 ast.NodePointerIndexPayload {
2309 .base = undefined,2294 .base = undefined,
...@@ -2370,11 +2355,9 @@ pub const Parser = struct {...@@ -2370,11 +2355,9 @@ pub const Parser = struct {
2370 },2355 },
23712356
2372 State.FnProtoAlign => |fn_proto| {2357 State.FnProtoAlign => |fn_proto| {
2373 const token = self.getNextToken();2358 if (self.eatToken(Token.Id.Keyword_align)) |align_token| {
2374 if (token.id == Token.Id.Keyword_align) {
2375 @panic("TODO fn proto align");2359 @panic("TODO fn proto align");
2376 }2360 }
2377 self.putBackToken(token);
2378 stack.append(State {2361 stack.append(State {
2379 .FnProtoReturnType = fn_proto,2362 .FnProtoReturnType = fn_proto,
2380 }) catch unreachable;2363 }) catch unreachable;
...@@ -2389,6 +2372,11 @@ pub const Parser = struct {...@@ -2389,6 +2372,11 @@ pub const Parser = struct {
2389 stack.append(State {2372 stack.append(State {
2390 .TypeExprBegin = DestPtr {.Field = &fn_proto.return_type.InferErrorSet},2373 .TypeExprBegin = DestPtr {.Field = &fn_proto.return_type.InferErrorSet},
2391 }) catch unreachable;2374 }) catch unreachable;
2375 continue;
2376 },
2377 Token.Id.Keyword_align => {
2378 @panic("TODO fn proto align");
2379 continue;
2392 },2380 },
2393 else => {2381 else => {
2394 self.putBackToken(token);2382 self.putBackToken(token);
...@@ -2396,17 +2384,13 @@ pub const Parser = struct {...@@ -2396,17 +2384,13 @@ pub const Parser = struct {
2396 stack.append(State {2384 stack.append(State {
2397 .TypeExprBegin = DestPtr {.Field = &fn_proto.return_type.Explicit},2385 .TypeExprBegin = DestPtr {.Field = &fn_proto.return_type.Explicit},
2398 }) catch unreachable;2386 }) catch unreachable;
2387 continue;
2399 },2388 },
2400 }2389 }
2401 if (token.id == Token.Id.Keyword_align) {
2402 @panic("TODO fn proto align");
2403 }
2404 continue;
2405 },2390 },
24062391
2407 State.ParamDecl => |fn_proto| {2392 State.ParamDecl => |fn_proto| {
2408 var token = self.getNextToken();2393 if (self.eatToken(Token.Id.RParen)) |_| {
2409 if (token.id == Token.Id.RParen) {
2410 continue;2394 continue;
2411 }2395 }
2412 const param_decl = try self.createAttachNode(arena, &fn_proto.params, ast.NodeParamDecl,2396 const param_decl = try self.createAttachNode(arena, &fn_proto.params, ast.NodeParamDecl,
...@@ -2419,28 +2403,22 @@ pub const Parser = struct {...@@ -2419,28 +2403,22 @@ pub const Parser = struct {
2419 .var_args_token = null,2403 .var_args_token = null,
2420 },2404 },
2421 );2405 );
2422 if (token.id == Token.Id.Keyword_comptime) {2406 if (self.eatToken(Token.Id.Keyword_comptime)) |comptime_token| {
2423 param_decl.comptime_token = token;2407 param_decl.comptime_token = comptime_token;
2424 token = self.getNextToken();2408 } else if (self.eatToken(Token.Id.Keyword_noalias)) |noalias_token| {
2425 } else if (token.id == Token.Id.Keyword_noalias) {2409 param_decl.noalias_token = noalias_token;
2426 param_decl.noalias_token = token;
2427 token = self.getNextToken();
2428 }2410 }
2429 if (token.id == Token.Id.Identifier) {2411 if (self.eatToken(Token.Id.Identifier)) |identifier| {
2430 const next_token = self.getNextToken();2412 if (self.eatToken(Token.Id.Colon)) |_| {
2431 if (next_token.id == Token.Id.Colon) {2413 param_decl.name_token = identifier;
2432 param_decl.name_token = token;
2433 token = self.getNextToken();
2434 } else {2414 } else {
2435 self.putBackToken(next_token);2415 self.putBackToken(identifier);
2436 }2416 }
2437 }2417 }
2438 if (token.id == Token.Id.Ellipsis3) {2418 if (self.eatToken(Token.Id.Ellipsis3)) |ellipsis3| {
2439 param_decl.var_args_token = token;2419 param_decl.var_args_token = ellipsis3;
2440 stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable;2420 stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable;
2441 continue;2421 continue;
2442 } else {
2443 self.putBackToken(token);
2444 }2422 }
24452423
2446 stack.append(State { .ParamDecl = fn_proto }) catch unreachable;2424 stack.append(State { .ParamDecl = fn_proto }) catch unreachable;
...@@ -2736,7 +2714,7 @@ pub const Parser = struct {...@@ -2736,7 +2714,7 @@ pub const Parser = struct {
2736 State.Semicolon => |node_ptr| {2714 State.Semicolon => |node_ptr| {
2737 const node = *node_ptr;2715 const node = *node_ptr;
2738 if (requireSemiColon(node)) {2716 if (requireSemiColon(node)) {
2739 _ = (try self.eatToken(&stack, Token.Id.Semicolon)) ?? continue;2717 _ = (try self.expectToken(&stack, Token.Id.Semicolon)) ?? continue;
2740 }2718 }
2741 }2719 }
2742 }2720 }
...@@ -2923,18 +2901,17 @@ pub const Parser = struct {...@@ -2923,18 +2901,17 @@ pub const Parser = struct {
2923 };2901 };
2924 }2902 }
29252903
2926 fn initNode(self: &Parser, id: ast.Node.Id) ast.Node {
2927 if (self.pending_line_comment_node) |comment_node| {
2928 self.pending_line_comment_node = null;
2929 return ast.Node {.id = id, .comment = comment_node};
2930 }
2931 return ast.Node {.id = id, .comment = null };
2932 }
2933
2934 fn createNode(self: &Parser, arena: &mem.Allocator, comptime T: type, init_to: &const T) !&T {2904 fn createNode(self: &Parser, arena: &mem.Allocator, comptime T: type, init_to: &const T) !&T {
2935 const node = try arena.create(T);2905 const node = try arena.create(T);
2936 *node = *init_to;2906 *node = *init_to;
2937 node.base = self.initNode(ast.Node.typeToId(T));2907 node.base = blk: {
2908 const id = ast.Node.typeToId(T);
2909 if (self.pending_line_comment_node) |comment_node| {
2910 self.pending_line_comment_node = null;
2911 break :blk ast.Node {.id = id, .comment = comment_node};
2912 }
2913 break :blk ast.Node {.id = id, .comment = null };
2914 };
29382915
2939 return node;2916 return node;
2940 }2917 }
...@@ -2986,15 +2963,6 @@ pub const Parser = struct {...@@ -2986,15 +2963,6 @@ pub const Parser = struct {
2986 };2963 };
2987 }2964 }
29882965
2989 fn eatToken(self: &Parser, stack: &ArrayList(State), id: @TagType(Token.Id)) !?Token {
2990 const token = self.getNextToken();
2991 if (token.id != id) {
2992 try self.parseError(stack, token, "expected {}, found {}", @tagName(id), @tagName(token.id));
2993 return null;
2994 }
2995 return token;
2996 }
2997
2998 fn revertIfOptional(self: &Parser, stack: &ArrayList(State)) !void {2966 fn revertIfOptional(self: &Parser, stack: &ArrayList(State)) !void {
2999 while (stack.popOrNull()) |state| {2967 while (stack.popOrNull()) |state| {
3000 switch (state) {2968 switch (state) {
...@@ -3011,6 +2979,22 @@ pub const Parser = struct {...@@ -3011,6 +2979,22 @@ pub const Parser = struct {
3011 return error.NoOptionalStateFound;2979 return error.NoOptionalStateFound;
3012 }2980 }
30132981
2982 fn expectToken(self: &Parser, stack: &ArrayList(State), id: @TagType(Token.Id)) !?Token {
2983 const token = self.getNextToken();
2984 if (token.id != id) {
2985 try self.parseError(stack, token, "expected {}, found {}", @tagName(id), @tagName(token.id));
2986 return null;
2987 }
2988 return token;
2989 }
2990
2991 fn eatToken(self: &Parser, id: @TagType(Token.Id)) ?Token {
2992 if (self.isPeekToken(id)) {
2993 return self.getNextToken();
2994 }
2995 return null;
2996 }
2997
3014 fn putBackToken(self: &Parser, token: &const Token) void {2998 fn putBackToken(self: &Parser, token: &const Token) void {
3015 self.put_back_tokens[self.put_back_count] = *token;2999 self.put_back_tokens[self.put_back_count] = *token;
3016 self.put_back_count += 1;3000 self.put_back_count += 1;
...@@ -3027,6 +3011,12 @@ pub const Parser = struct {...@@ -3027,6 +3011,12 @@ pub const Parser = struct {
3027 }3011 }
3028 }3012 }
30293013
3014 fn isPeekToken(self: &Parser, id: @TagType(Token.Id)) bool {
3015 const token = self.getNextToken();
3016 defer self.putBackToken(token);
3017 return id == token.id;
3018 }
3019
3030 const RenderAstFrame = struct {3020 const RenderAstFrame = struct {
3031 node: &ast.Node,3021 node: &ast.Node,
3032 indent: usize,3022 indent: usize,