authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-03-09 11:02:16+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-03-09 12:23:30+02:00
log6f8d732599461aa816f545b658a068eceb6ac9bc
tree5fda80a4507cf0676c1eb9a2d4badfd17d5ee599
parente2fd289a33bb35cf4b86daa4d80adb7cc0c2c2b0
signaturelock-open Commit is signed but in an unrecognized format.

update parsers to new noasync syntax


6 files changed, 108 insertions(+), 43 deletions(-)

lib/std/zig/ast.zig+26-3
......@@ -431,6 +431,7 @@ pub const Node = struct {
431431 ContainerDecl,
432432 Asm,
433433 Comptime,
434 Noasync,
434435 Block,
435436
436437 // Misc
......@@ -1078,6 +1079,30 @@ pub const Node = struct {
10781079 }
10791080 };
10801081
1082 pub const Noasync = struct {
1083 base: Node = Node{ .id = .Noasync },
1084 doc_comments: ?*DocComment,
1085 noasync_token: TokenIndex,
1086 expr: *Node,
1087
1088 pub fn iterate(self: *Noasync, index: usize) ?*Node {
1089 var i = index;
1090
1091 if (i < 1) return self.expr;
1092 i -= 1;
1093
1094 return null;
1095 }
1096
1097 pub fn firstToken(self: *const Noasync) TokenIndex {
1098 return self.noasync_token;
1099 }
1100
1101 pub fn lastToken(self: *const Noasync) TokenIndex {
1102 return self.expr.lastToken();
1103 }
1104 };
1105
10811106 pub const Payload = struct {
10821107 base: Node = Node{ .id = .Payload },
10831108 lpipe: TokenIndex,
......@@ -1560,9 +1585,7 @@ pub const Node = struct {
15601585 pub const Op = union(enum) {
15611586 AddressOf,
15621587 ArrayType: ArrayInfo,
1563 Await: struct {
1564 noasync_token: ?TokenIndex = null,
1565 },
1588 Await,
15661589 BitNot,
15671590 BoolNot,
15681591 Cancel,
lib/std/zig/parse.zig+37-25
......@@ -856,6 +856,7 @@ fn parsePrefixExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
856856/// / IfExpr
857857/// / KEYWORD_break BreakLabel? Expr?
858858/// / KEYWORD_comptime Expr
859/// / KEYWORD_noasync Expr
859860/// / KEYWORD_continue BreakLabel?
860861/// / KEYWORD_resume Expr
861862/// / KEYWORD_return Expr?
......@@ -870,7 +871,7 @@ fn parsePrimaryExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
870871 const label = try parseBreakLabel(arena, it, tree);
871872 const expr_node = try parseExpr(arena, it, tree);
872873 const node = try arena.create(Node.ControlFlowExpression);
873 node.* = Node.ControlFlowExpression{
874 node.* = .{
874875 .ltoken = token,
875876 .kind = Node.ControlFlowExpression.Kind{ .Break = label },
876877 .rhs = expr_node,
......@@ -883,7 +884,7 @@ fn parsePrimaryExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
883884 .ExpectedExpr = AstError.ExpectedExpr{ .token = it.index },
884885 });
885886 const node = try arena.create(Node.Comptime);
886 node.* = Node.Comptime{
887 node.* = .{
887888 .doc_comments = null,
888889 .comptime_token = token,
889890 .expr = expr_node,
......@@ -891,10 +892,23 @@ fn parsePrimaryExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
891892 return &node.base;
892893 }
893894
895 if (eatToken(it, .Keyword_noasync)) |token| {
896 const expr_node = try expectNode(arena, it, tree, parseExpr, AstError{
897 .ExpectedExpr = AstError.ExpectedExpr{ .token = it.index },
898 });
899 const node = try arena.create(Node.Noasync);
900 node.* = .{
901 .doc_comments = null,
902 .noasync_token = token,
903 .expr = expr_node,
904 };
905 return &node.base;
906 }
907
894908 if (eatToken(it, .Keyword_continue)) |token| {
895909 const label = try parseBreakLabel(arena, it, tree);
896910 const node = try arena.create(Node.ControlFlowExpression);
897 node.* = Node.ControlFlowExpression{
911 node.* = .{
898912 .ltoken = token,
899913 .kind = Node.ControlFlowExpression.Kind{ .Continue = label },
900914 .rhs = null,
......@@ -907,7 +921,7 @@ fn parsePrimaryExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
907921 .ExpectedExpr = AstError.ExpectedExpr{ .token = it.index },
908922 });
909923 const node = try arena.create(Node.PrefixOp);
910 node.* = Node.PrefixOp{
924 node.* = .{
911925 .op_token = token,
912926 .op = Node.PrefixOp.Op.Resume,
913927 .rhs = expr_node,
......@@ -918,7 +932,7 @@ fn parsePrimaryExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
918932 if (eatToken(it, .Keyword_return)) |token| {
919933 const expr_node = try parseExpr(arena, it, tree);
920934 const node = try arena.create(Node.ControlFlowExpression);
921 node.* = Node.ControlFlowExpression{
935 node.* = .{
922936 .ltoken = token,
923937 .kind = Node.ControlFlowExpression.Kind.Return,
924938 .rhs = expr_node,
......@@ -1126,19 +1140,18 @@ fn parseErrorUnionExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*No
11261140
11271141/// SuffixExpr
11281142/// <- KEYWORD_async PrimaryTypeExpr SuffixOp* FnCallArguments
1129/// / KEYWORD_noasync PrimaryTypeExpr SuffixOp* FnCallArguments
11301143/// / PrimaryTypeExpr (SuffixOp / FnCallArguments)*
11311144fn parseSuffixExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
1132 const maybe_async = eatAnnotatedToken(it, .Keyword_async) orelse eatAnnotatedToken(it, .Keyword_noasync);
1145 const maybe_async = eatToken(it, .Keyword_async);
11331146 if (maybe_async) |async_token| {
11341147 const token_fn = eatToken(it, .Keyword_fn);
1135 if (async_token.ptr.id == .Keyword_async and token_fn != null) {
1148 if (token_fn != null) {
11361149 // HACK: If we see the keyword `fn`, then we assume that
11371150 // we are parsing an async fn proto, and not a call.
11381151 // We therefore put back all tokens consumed by the async
11391152 // prefix...
11401153 putBackToken(it, token_fn.?);
1141 putBackToken(it, async_token.index);
1154 putBackToken(it, async_token);
11421155 return parsePrimaryTypeExpr(arena, it, tree);
11431156 }
11441157 // TODO: Implement hack for parsing `async fn ...` in ast_parse_suffix_expr
......@@ -1167,7 +1180,7 @@ fn parseSuffixExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
11671180 .op = Node.SuffixOp.Op{
11681181 .Call = Node.SuffixOp.Op.Call{
11691182 .params = params.list,
1170 .async_token = async_token.index,
1183 .async_token = async_token,
11711184 },
11721185 },
11731186 .rtoken = params.rparen,
......@@ -1224,6 +1237,7 @@ fn parseSuffixExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
12241237/// / IfTypeExpr
12251238/// / INTEGER
12261239/// / KEYWORD_comptime TypeExpr
1240/// / KEYWORD_noasync TypeExpr
12271241/// / KEYWORD_error DOT IDENTIFIER
12281242/// / KEYWORD_false
12291243/// / KEYWORD_null
......@@ -1255,13 +1269,23 @@ fn parsePrimaryTypeExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*N
12551269 if (eatToken(it, .Keyword_comptime)) |token| {
12561270 const expr = (try parseTypeExpr(arena, it, tree)) orelse return null;
12571271 const node = try arena.create(Node.Comptime);
1258 node.* = Node.Comptime{
1272 node.* = .{
12591273 .doc_comments = null,
12601274 .comptime_token = token,
12611275 .expr = expr,
12621276 };
12631277 return &node.base;
12641278 }
1279 if (eatToken(it, .Keyword_noasync)) |token| {
1280 const expr = (try parseTypeExpr(arena, it, tree)) orelse return null;
1281 const node = try arena.create(Node.Noasync);
1282 node.* = .{
1283 .doc_comments = null,
1284 .noasync_token = token,
1285 .expr = expr,
1286 };
1287 return &node.base;
1288 }
12651289 if (eatToken(it, .Keyword_error)) |token| {
12661290 const period = try expectToken(it, tree, .Period);
12671291 const identifier = try expectNode(arena, it, tree, parseIdentifier, AstError{
......@@ -1269,7 +1293,7 @@ fn parsePrimaryTypeExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*N
12691293 });
12701294 const global_error_set = try createLiteral(arena, Node.ErrorType, token);
12711295 const node = try arena.create(Node.InfixOp);
1272 node.* = Node.InfixOp{
1296 node.* = .{
12731297 .op_token = period,
12741298 .lhs = global_error_set,
12751299 .op = Node.InfixOp.Op.Period,
......@@ -1281,7 +1305,7 @@ fn parsePrimaryTypeExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*N
12811305 if (eatToken(it, .Keyword_null)) |token| return createLiteral(arena, Node.NullLiteral, token);
12821306 if (eatToken(it, .Keyword_anyframe)) |token| {
12831307 const node = try arena.create(Node.AnyFrameType);
1284 node.* = Node.AnyFrameType{
1308 node.* = .{
12851309 .anyframe_token = token,
12861310 .result = null,
12871311 };
......@@ -2180,18 +2204,6 @@ fn parsePrefixOp(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
21802204 .Ampersand => ops{ .AddressOf = {} },
21812205 .Keyword_try => ops{ .Try = {} },
21822206 .Keyword_await => ops{ .Await = .{} },
2183 .Keyword_noasync => if (eatToken(it, .Keyword_await)) |await_tok| {
2184 const node = try arena.create(Node.PrefixOp);
2185 node.* = Node.PrefixOp{
2186 .op_token = await_tok,
2187 .op = .{ .Await = .{ .noasync_token = token.index } },
2188 .rhs = undefined, // set by caller
2189 };
2190 return &node.base;
2191 } else {
2192 putBackToken(it, token.index);
2193 return null;
2194 },
21952207 else => {
21962208 putBackToken(it, token.index);
21972209 return null;
lib/std/zig/render.zig+6-3
......@@ -390,6 +390,12 @@ fn renderExpression(
390390 try renderToken(tree, stream, comptime_node.comptime_token, indent, start_col, Space.Space);
391391 return renderExpression(allocator, stream, tree, indent, start_col, comptime_node.expr, space);
392392 },
393 .Noasync => {
394 const noasync_node = @fieldParentPtr(ast.Node.Noasync, "base", base);
395
396 try renderToken(tree, stream, noasync_node.noasync_token, indent, start_col, Space.Space);
397 return renderExpression(allocator, stream, tree, indent, start_col, noasync_node.expr, space);
398 },
393399
394400 .Suspend => {
395401 const suspend_node = @fieldParentPtr(ast.Node.Suspend, "base", base);
......@@ -590,9 +596,6 @@ fn renderExpression(
590596 },
591597
592598 .Await => |await_info| {
593 if (await_info.noasync_token) |tok| {
594 try renderToken(tree, stream, tok, indent, start_col, Space.Space);
595 }
596599 try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.Space);
597600 },
598601 }
src/all_types.hpp+6-1
......@@ -651,6 +651,7 @@ enum NodeType {
651651 NodeTypeSwitchProng,
652652 NodeTypeSwitchRange,
653653 NodeTypeCompTime,
654 NodeTypeNoAsync,
654655 NodeTypeBreak,
655656 NodeTypeContinue,
656657 NodeTypeAsmExpr,
......@@ -991,6 +992,10 @@ struct AstNodeCompTime {
991992 AstNode *expr;
992993};
993994
995struct AstNodeNoAsync {
996 AstNode *expr;
997};
998
994999struct AsmOutput {
9951000 Buf *asm_symbolic_name;
9961001 Buf *constraint;
......@@ -1148,7 +1153,6 @@ struct AstNodeErrorType {
11481153};
11491154
11501155struct AstNodeAwaitExpr {
1151 Token *noasync_token;
11521156 AstNode *expr;
11531157};
11541158
......@@ -1199,6 +1203,7 @@ struct AstNode {
11991203 AstNodeSwitchProng switch_prong;
12001204 AstNodeSwitchRange switch_range;
12011205 AstNodeCompTime comptime_expr;
1206 AstNodeNoAsync noasync_expr;
12021207 AstNodeAsmExpr asm_expr;
12031208 AstNodeFieldAccessExpr field_access_expr;
12041209 AstNodePtrDerefExpr ptr_deref_expr;
src/ast_render.cpp+8
......@@ -220,6 +220,8 @@ static const char *node_type_str(NodeType node_type) {
220220 return "SwitchRange";
221221 case NodeTypeCompTime:
222222 return "CompTime";
223 case NodeTypeNoAsync:
224 return "NoAsync";
223225 case NodeTypeBreak:
224226 return "Break";
225227 case NodeTypeContinue:
......@@ -1091,6 +1093,12 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
10911093 render_node_grouped(ar, node->data.comptime_expr.expr);
10921094 break;
10931095 }
1096 case NodeTypeNoAsync:
1097 {
1098 fprintf(ar->f, "noasync ");
1099 render_node_grouped(ar, node->data.noasync_expr.expr);
1100 break;
1101 }
10941102 case NodeTypeForExpr:
10951103 {
10961104 if (node->data.for_expr.name != nullptr) {
src/parser.cpp+25-11
......@@ -1237,6 +1237,7 @@ static AstNode *ast_parse_prefix_expr(ParseContext *pc) {
12371237// / IfExpr
12381238// / KEYWORD_break BreakLabel? Expr?
12391239// / KEYWORD_comptime Expr
1240// / KEYWORD_noasync Expr
12401241// / KEYWORD_continue BreakLabel?
12411242// / KEYWORD_resume Expr
12421243// / KEYWORD_return Expr?
......@@ -1271,6 +1272,14 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc) {
12711272 return res;
12721273 }
12731274
1275 Token *noasync = eat_token_if(pc, TokenIdKeywordNoAsync);
1276 if (noasync != nullptr) {
1277 AstNode *expr = ast_expect(pc, ast_parse_expr);
1278 AstNode *res = ast_create_node(pc, NodeTypeNoAsync, noasync);
1279 res->data.noasync_expr.expr = expr;
1280 return res;
1281 }
1282
12741283 Token *continue_token = eat_token_if(pc, TokenIdKeywordContinue);
12751284 if (continue_token != nullptr) {
12761285 Token *label = ast_parse_break_label(pc);
......@@ -1459,13 +1468,11 @@ static AstNode *ast_parse_error_union_expr(ParseContext *pc) {
14591468
14601469// SuffixExpr
14611470// <- KEYWORD_async PrimaryTypeExpr SuffixOp* FnCallArguments
1462// / KEYWORD_noasync PrimaryTypeExpr SuffixOp* FnCallArguments
14631471// / PrimaryTypeExpr (SuffixOp / FnCallArguments)*
14641472static AstNode *ast_parse_suffix_expr(ParseContext *pc) {
1465 Token *async_token = eat_token(pc);
1466 bool is_async = async_token->id == TokenIdKeywordAsync;
1467 if (is_async || async_token->id == TokenIdKeywordNoAsync) {
1468 if (is_async && eat_token_if(pc, TokenIdKeywordFn) != nullptr) {
1473 Token *async_token = eat_token_if(pc, TokenIdKeywordAsync);
1474 if (async_token) {
1475 if (eat_token_if(pc, TokenIdKeywordFn) != nullptr) {
14691476 // HACK: If we see the keyword `fn`, then we assume that
14701477 // we are parsing an async fn proto, and not a call.
14711478 // We therefore put back all tokens consumed by the async
......@@ -1515,13 +1522,12 @@ static AstNode *ast_parse_suffix_expr(ParseContext *pc) {
15151522 assert(args->type == NodeTypeFnCallExpr);
15161523
15171524 AstNode *res = ast_create_node(pc, NodeTypeFnCallExpr, async_token);
1518 res->data.fn_call_expr.modifier = is_async ? CallModifierAsync : CallModifierNoAsync;
1525 res->data.fn_call_expr.modifier = CallModifierAsync;
15191526 res->data.fn_call_expr.seen = false;
15201527 res->data.fn_call_expr.fn_ref_expr = child;
15211528 res->data.fn_call_expr.params = args->data.fn_call_expr.params;
15221529 return res;
15231530 }
1524 put_back_token(pc);
15251531
15261532 AstNode *res = ast_parse_primary_type_expr(pc);
15271533 if (res == nullptr)
......@@ -1582,6 +1588,7 @@ static AstNode *ast_parse_suffix_expr(ParseContext *pc) {
15821588// / IfTypeExpr
15831589// / INTEGER
15841590// / KEYWORD_comptime TypeExpr
1591// / KEYWORD_noasync TypeExpr
15851592// / KEYWORD_error DOT IDENTIFIER
15861593// / KEYWORD_false
15871594// / KEYWORD_null
......@@ -1683,6 +1690,14 @@ static AstNode *ast_parse_primary_type_expr(ParseContext *pc) {
16831690 return res;
16841691 }
16851692
1693 Token *noasync = eat_token_if(pc, TokenIdKeywordNoAsync);
1694 if (noasync != nullptr) {
1695 AstNode *expr = ast_expect(pc, ast_parse_type_expr);
1696 AstNode *res = ast_create_node(pc, NodeTypeNoAsync, noasync);
1697 res->data.noasync_expr.expr = expr;
1698 return res;
1699 }
1700
16861701 Token *error = eat_token_if(pc, TokenIdKeywordError);
16871702 if (error != nullptr) {
16881703 Token *dot = expect_token(pc, TokenIdDot);
......@@ -2599,14 +2614,10 @@ static AstNode *ast_parse_prefix_op(ParseContext *pc) {
25992614 return res;
26002615 }
26012616
2602 Token *noasync_token = eat_token_if(pc, TokenIdKeywordNoAsync);
26032617 Token *await = eat_token_if(pc, TokenIdKeywordAwait);
26042618 if (await != nullptr) {
26052619 AstNode *res = ast_create_node(pc, NodeTypeAwaitExpr, await);
2606 res->data.await_expr.noasync_token = noasync_token;
26072620 return res;
2608 } else if (noasync_token != nullptr) {
2609 put_back_token(pc);
26102621 }
26112622
26122623 return nullptr;
......@@ -3125,6 +3136,9 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
31253136 case NodeTypeCompTime:
31263137 visit_field(&node->data.comptime_expr.expr, visit, context);
31273138 break;
3139 case NodeTypeNoAsync:
3140 visit_field(&node->data.comptime_expr.expr, visit, context);
3141 break;
31283142 case NodeTypeBreak:
31293143 // none
31303144 break;