@@ -7022,11 +7022,9 @@ Parser<ManagedTokenSource>::parse_expr_stmt (
7022
7022
}
7023
7023
}
7024
7024
7025
- /* Parses a expression statement containing an expression with block.
7026
- * Disambiguates internally. */
7027
7025
template <typename ManagedTokenSource>
7028
- std::unique_ptr<AST::ExprStmtWithBlock >
7029
- Parser<ManagedTokenSource>::parse_expr_stmt_with_block (
7026
+ std::unique_ptr<AST::ExprWithBlock >
7027
+ Parser<ManagedTokenSource>::parse_expr_with_block (
7030
7028
std::vector<AST::Attribute> outer_attrs)
7031
7029
{
7032
7030
std::unique_ptr<AST::ExprWithBlock> expr_parsed = nullptr;
@@ -7113,9 +7111,23 @@ Parser<ManagedTokenSource>::parse_expr_stmt_with_block (
7113
7111
return nullptr;
7114
7112
}
7115
7113
7114
+ return expr_parsed;
7115
+ }
7116
+
7117
+ /* Parses a expression statement containing an expression with block.
7118
+ * Disambiguates internally. */
7119
+ template <typename ManagedTokenSource>
7120
+ std::unique_ptr<AST::ExprStmtWithBlock>
7121
+ Parser<ManagedTokenSource>::parse_expr_stmt_with_block (
7122
+ std::vector<AST::Attribute> outer_attrs)
7123
+ {
7124
+ auto expr_parsed = parse_expr_with_block (std::move (outer_attrs));
7125
+ auto locus = expr_parsed->get_locus ();
7126
+
7116
7127
// return expr stmt created from expr
7117
7128
return std::unique_ptr<AST::ExprStmtWithBlock> (
7118
- new AST::ExprStmtWithBlock (std::move (expr_parsed), t->get_locus ()));
7129
+ new AST::ExprStmtWithBlock (std::move (expr_parsed), locus,
7130
+ lexer.peek_token ()->get_id () == SEMICOLON));
7119
7131
}
7120
7132
7121
7133
/* Parses an expression statement containing an expression without block.
@@ -7286,7 +7298,7 @@ Parser<ManagedTokenSource>::parse_block_expr (
7286
7298
7287
7299
// parse statements and expression
7288
7300
std::vector<std::unique_ptr<AST::Stmt>> stmts;
7289
- std::unique_ptr<AST::ExprWithoutBlock > expr = nullptr;
7301
+ std::unique_ptr<AST::Expr > expr = nullptr;
7290
7302
7291
7303
const_TokenPtr t = lexer.peek_token ();
7292
7304
while (t->get_id () != RIGHT_CURLY)
@@ -11438,6 +11450,29 @@ Parser<ManagedTokenSource>::parse_struct_pattern_field_partial (
11438
11450
}
11439
11451
}
11440
11452
11453
+ template <typename ManagedTokenSource>
11454
+ ExprOrStmt
11455
+ Parser<ManagedTokenSource>::parse_stmt_or_expr_with_block (
11456
+ std::vector<AST::Attribute> outer_attrs)
11457
+ {
11458
+ auto expr = parse_expr_with_block (std::move (outer_attrs));
11459
+ auto tok = lexer.peek_token ();
11460
+
11461
+ // tail expr in a block expr
11462
+ if (tok->get_id () == RIGHT_CURLY)
11463
+ return ExprOrStmt (std::move (expr));
11464
+
11465
+ // internal block expr must either have semicolons followed, or evaluate to ()
11466
+ auto locus = expr->get_locus_slow ();
11467
+ std::unique_ptr<AST::ExprStmtWithBlock> stmt (
11468
+ new AST::ExprStmtWithBlock (std::move (expr), locus,
11469
+ tok->get_id () == SEMICOLON));
11470
+ if (tok->get_id () == SEMICOLON)
11471
+ lexer.skip_token ();
11472
+
11473
+ return ExprOrStmt (std::move (stmt));
11474
+ }
11475
+
11441
11476
/* Parses a statement or expression (depending on whether a trailing semicolon
11442
11477
* exists). Useful for block expressions where it cannot be determined through
11443
11478
* lookahead whether it is a statement or expression to be parsed. */
@@ -11508,9 +11543,7 @@ Parser<ManagedTokenSource>::parse_stmt_or_expr_without_block ()
11508
11543
{
11509
11544
case LEFT_CURLY: {
11510
11545
// unsafe block
11511
- std::unique_ptr<AST::ExprStmtWithBlock> stmt (
11512
- parse_expr_stmt_with_block (std::move (outer_attrs)));
11513
- return ExprOrStmt (std::move (stmt));
11546
+ return parse_stmt_or_expr_with_block (std::move (outer_attrs));
11514
11547
}
11515
11548
case TRAIT: {
11516
11549
// unsafe trait
@@ -11577,11 +11610,7 @@ Parser<ManagedTokenSource>::parse_stmt_or_expr_without_block ()
11577
11610
case MATCH_TOK:
11578
11611
case LEFT_CURLY:
11579
11612
case ASYNC: {
11580
- // all expressions with block, so cannot be final expr without block in
11581
- // function
11582
- std::unique_ptr<AST::ExprStmtWithBlock> stmt (
11583
- parse_expr_stmt_with_block (std::move (outer_attrs)));
11584
- return ExprOrStmt (std::move (stmt));
11613
+ return parse_stmt_or_expr_with_block (std::move (outer_attrs));
11585
11614
}
11586
11615
case LIFETIME: {
11587
11616
/* FIXME: are there any expressions without blocks that can have
@@ -11592,9 +11621,7 @@ Parser<ManagedTokenSource>::parse_stmt_or_expr_without_block ()
11592
11621
&& (t2->get_id () == LOOP || t2->get_id () == WHILE
11593
11622
|| t2->get_id () == FOR))
11594
11623
{
11595
- std::unique_ptr<AST::ExprStmtWithBlock> stmt (
11596
- parse_expr_stmt_with_block (std::move (outer_attrs)));
11597
- return ExprOrStmt (std::move (stmt));
11624
+ return parse_stmt_or_expr_with_block (std::move (outer_attrs));
11598
11625
}
11599
11626
else
11600
11627
{
0 commit comments