| ... | ... | @@ -171,6 +171,22 @@ pub const Parser = struct { |
| 171 | 171 | stack.append(State { .TopLevelExtern = token }) catch unreachable; |
| 172 | 172 | continue; |
| 173 | 173 | }, |
| 174 | Token.Id.Keyword_test => { |
| 175 | stack.append(State.TopLevel) catch unreachable; |
| 176 | |
| 177 | const name_token = self.getNextToken(); |
| 178 | if (name_token.id != Token.Id.StringLiteral) |
| 179 | return self.parseError(token, "expected {}, found {}", @tagName(Token.Id.StringLiteral), @tagName(name_token.id)); |
| 180 | |
| 181 | const lbrace = self.getNextToken(); |
| 182 | if (lbrace.id != Token.Id.LBrace) |
| 183 | return self.parseError(token, "expected {}, found {}", @tagName(Token.Id.LBrace), @tagName(name_token.id)); |
| 184 | |
| 185 | const block = try self.createBlock(arena, token); |
| 186 | const test_decl = try self.createAttachTestDecl(arena, &root_node.decls, token, name_token, block); |
| 187 | try stack.append(State { .Block = block }); |
| 188 | continue; |
| 189 | }, |
| 174 | 190 | Token.Id.Eof => { |
| 175 | 191 | root_node.eof_token = token; |
| 176 | 192 | return Tree {.root_node = root_node, .arena_allocator = arena_allocator}; |
| ... | ... | @@ -733,6 +749,20 @@ pub const Parser = struct { |
| 733 | 749 | return node; |
| 734 | 750 | } |
| 735 | 751 | |
| 752 | fn createTestDecl(self: &Parser, arena: &mem.Allocator, test_token: &const Token, name_token: &const Token, |
| 753 | block: &ast.NodeBlock) !&ast.NodeTestDecl |
| 754 | { |
| 755 | const node = try arena.create(ast.NodeTestDecl); |
| 756 | |
| 757 | *node = ast.NodeTestDecl { |
| 758 | .base = self.initNode(ast.Node.Id.TestDecl), |
| 759 | .test_token = *test_token, |
| 760 | .name_token = *name_token, |
| 761 | .body_node = &block.base, |
| 762 | }; |
| 763 | return node; |
| 764 | } |
| 765 | |
| 736 | 766 | fn createFnProto(self: &Parser, arena: &mem.Allocator, fn_token: &const Token, extern_token: &const ?Token, |
| 737 | 767 | cc_token: &const ?Token, visib_token: &const ?Token, inline_token: &const ?Token) !&ast.NodeFnProto |
| 738 | 768 | { |
| ... | ... | @@ -867,6 +897,14 @@ pub const Parser = struct { |
| 867 | 897 | return node; |
| 868 | 898 | } |
| 869 | 899 | |
| 900 | fn createAttachTestDecl(self: &Parser, arena: &mem.Allocator, list: &ArrayList(&ast.Node), |
| 901 | test_token: &const Token, name_token: &const Token, block: &ast.NodeBlock) !&ast.NodeTestDecl |
| 902 | { |
| 903 | const node = try self.createTestDecl(arena, test_token, name_token, block); |
| 904 | try list.append(&node.base); |
| 905 | return node; |
| 906 | } |
| 907 | |
| 870 | 908 | fn parseError(self: &Parser, token: &const Token, comptime fmt: []const u8, args: ...) (error{ParseError}) { |
| 871 | 909 | const loc = self.tokenizer.getTokenLocation(token); |
| 872 | 910 | warn("{}:{}:{}: error: " ++ fmt ++ "\n", self.source_file_name, token.line + 1, token.column + 1, args); |
| ... | ... | @@ -1032,7 +1070,11 @@ pub const Parser = struct { |
| 1032 | 1070 | ast.Node.Id.VarDecl => { |
| 1033 | 1071 | const var_decl = @fieldParentPtr(ast.NodeVarDecl, "base", decl); |
| 1034 | 1072 | try stack.append(RenderState { .VarDecl = var_decl}); |
| 1035 | | |
| 1073 | }, |
| 1074 | ast.Node.Id.TestDecl => { |
| 1075 | const test_decl = @fieldParentPtr(ast.NodeTestDecl, "base", decl); |
| 1076 | try stream.print("test {} ", self.tokenizer.getTokenSlice(test_decl.name_token)); |
| 1077 | try stack.append(RenderState { .Expression = test_decl.body_node }); |
| 1036 | 1078 | }, |
| 1037 | 1079 | else => unreachable, |
| 1038 | 1080 | } |
| ... | ... | @@ -1201,6 +1243,7 @@ pub const Parser = struct { |
| 1201 | 1243 | |
| 1202 | 1244 | ast.Node.Id.Root, |
| 1203 | 1245 | ast.Node.Id.VarDecl, |
| 1246 | ast.Node.Id.TestDecl, |
| 1204 | 1247 | ast.Node.Id.ParamDecl => unreachable, |
| 1205 | 1248 | }, |
| 1206 | 1249 | RenderState.FnProtoRParen => |fn_proto| { |
| ... | ... | @@ -1422,4 +1465,12 @@ test "zig fmt" { |
| 1422 | 1465 | \\} |
| 1423 | 1466 | \\ |
| 1424 | 1467 | ); |
| 1468 | |
| 1469 | try testCanonical( |
| 1470 | \\test "test name" { |
| 1471 | \\ const a = 1; |
| 1472 | \\ var b = 1; |
| 1473 | \\} |
| 1474 | \\ |
| 1475 | ); |
| 1425 | 1476 | } |