authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2017-04-02 11:02:47-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2017-04-02 11:02:47-07:00
log9968879261a8628600bfde4e438e8f8c0d2fa811
treeb858a2f7019ffabdb4e9e926b8c122b60b35ef90
parent087324a6395ba7cbef62345bef84d9d9b0dc25ad

Require top-level-declaration comptime use {}

This forbids `comptime 1 comptime 1` at top-level scope.

1 files changed, 9 insertions(+), 6 deletions(-)

src/parser.cpp+9-6
...@@ -615,9 +615,9 @@ static AstNode *ast_parse_goto_expr(ParseContext *pc, size_t *token_index, bool...@@ -615,9 +615,9 @@ static AstNode *ast_parse_goto_expr(ParseContext *pc, size_t *token_index, bool
615}615}
616616
617/*617/*
618CompTimeExpression = "comptime" Expression618CompTimeExpression(body) = "comptime" body
619*/619*/
620static AstNode *ast_parse_comptime_expr(ParseContext *pc, size_t *token_index, bool mandatory) {620static AstNode *ast_parse_comptime_expr(ParseContext *pc, size_t *token_index, bool require_block_body, bool mandatory) {
621 Token *comptime_token = &pc->tokens->at(*token_index);621 Token *comptime_token = &pc->tokens->at(*token_index);
622 if (comptime_token->id == TokenIdKeywordCompTime) {622 if (comptime_token->id == TokenIdKeywordCompTime) {
623 *token_index += 1;623 *token_index += 1;
...@@ -629,7 +629,10 @@ static AstNode *ast_parse_comptime_expr(ParseContext *pc, size_t *token_index, b...@@ -629,7 +629,10 @@ static AstNode *ast_parse_comptime_expr(ParseContext *pc, size_t *token_index, b
629 }629 }
630630
631 AstNode *node = ast_create_node(pc, NodeTypeCompTime, comptime_token);631 AstNode *node = ast_create_node(pc, NodeTypeCompTime, comptime_token);
632 node->data.comptime_expr.expr = ast_parse_expression(pc, token_index, true);632 if (require_block_body)
633 node->data.comptime_expr.expr = ast_parse_block(pc, token_index, true);
634 else
635 node->data.comptime_expr.expr = ast_parse_expression(pc, token_index, true);
633 return node;636 return node;
634}637}
635638
...@@ -1884,7 +1887,7 @@ static AstNode *ast_parse_block_expr(ParseContext *pc, size_t *token_index, bool...@@ -1884,7 +1887,7 @@ static AstNode *ast_parse_block_expr(ParseContext *pc, size_t *token_index, bool
1884 if (block)1887 if (block)
1885 return block;1888 return block;
18861889
1887 AstNode *comptime_node = ast_parse_comptime_expr(pc, token_index, false);1890 AstNode *comptime_node = ast_parse_comptime_expr(pc, token_index, false, false);
1888 if (comptime_node)1891 if (comptime_node)
1889 return comptime_node;1892 return comptime_node;
18901893
...@@ -2459,12 +2462,12 @@ static AstNode *ast_parse_type_decl(ParseContext *pc, size_t *token_index, Visib...@@ -2459,12 +2462,12 @@ static AstNode *ast_parse_type_decl(ParseContext *pc, size_t *token_index, Visib
2459}2462}
24602463
2461/*2464/*
2462TopLevelItem = ErrorValueDecl | CompTimeExpression | TopLevelDecl | TestDecl2465TopLevelItem = ErrorValueDecl | CompTimeExpression(Block) | TopLevelDecl | TestDecl
2463TopLevelDecl = option(VisibleMod) (FnDef | ExternDecl | GlobalVarDecl | TypeDecl | UseDecl)2466TopLevelDecl = option(VisibleMod) (FnDef | ExternDecl | GlobalVarDecl | TypeDecl | UseDecl)
2464*/2467*/
2465static void ast_parse_top_level_decls(ParseContext *pc, size_t *token_index, ZigList<AstNode *> *top_level_decls) {2468static void ast_parse_top_level_decls(ParseContext *pc, size_t *token_index, ZigList<AstNode *> *top_level_decls) {
2466 for (;;) {2469 for (;;) {
2467 AstNode *comptime_expr_node = ast_parse_comptime_expr(pc, token_index, false);2470 AstNode *comptime_expr_node = ast_parse_comptime_expr(pc, token_index, true, false);
2468 if (comptime_expr_node) {2471 if (comptime_expr_node) {
2469 top_level_decls->append(comptime_expr_node);2472 top_level_decls->append(comptime_expr_node);
2470 continue;2473 continue;