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
615615}
616616
617617/*
618CompTimeExpression = "comptime" Expression
618CompTimeExpression(body) = "comptime" body
619619*/
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) {
621621 Token *comptime_token = &pc->tokens->at(*token_index);
622622 if (comptime_token->id == TokenIdKeywordCompTime) {
623623 *token_index += 1;
......@@ -629,7 +629,10 @@ static AstNode *ast_parse_comptime_expr(ParseContext *pc, size_t *token_index, b
629629 }
630630
631631 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);
633636 return node;
634637}
635638
......@@ -1884,7 +1887,7 @@ static AstNode *ast_parse_block_expr(ParseContext *pc, size_t *token_index, bool
18841887 if (block)
18851888 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);
18881891 if (comptime_node)
18891892 return comptime_node;
18901893
......@@ -2459,12 +2462,12 @@ static AstNode *ast_parse_type_decl(ParseContext *pc, size_t *token_index, Visib
24592462}
24602463
24612464/*
2462TopLevelItem = ErrorValueDecl | CompTimeExpression | TopLevelDecl | TestDecl
2465TopLevelItem = ErrorValueDecl | CompTimeExpression(Block) | TopLevelDecl | TestDecl
24632466TopLevelDecl = option(VisibleMod) (FnDef | ExternDecl | GlobalVarDecl | TypeDecl | UseDecl)
24642467*/
24652468static void ast_parse_top_level_decls(ParseContext *pc, size_t *token_index, ZigList<AstNode *> *top_level_decls) {
24662469 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);
24682471 if (comptime_expr_node) {
24692472 top_level_decls->append(comptime_expr_node);
24702473 continue;