Parses an `if` statement. # Panics If the parser isn't positioned at an `if` token. See:
(&mut self)
| 1428 | /// |
| 1429 | /// See: <https://docs.python.org/3/reference/compound_stmts.html#the-if-statement> |
| 1430 | fn parse_if_statement(&mut self) -> ast::StmtIf { |
| 1431 | let start = self.node_start(); |
| 1432 | self.bump(TokenKind::If); |
| 1433 | |
| 1434 | // test_err if_stmt_invalid_test_expr |
| 1435 | // if *x: ... |
| 1436 | // if yield x: ... |
| 1437 | // if yield from x: ... |
| 1438 | |
| 1439 | // test_err if_stmt_missing_test |
| 1440 | // if : ... |
| 1441 | let test = self.parse_named_expression_or_higher(ExpressionContext::default()); |
| 1442 | |
| 1443 | // test_err if_stmt_missing_colon |
| 1444 | // if x |
| 1445 | // if x |
| 1446 | // pass |
| 1447 | // a = 1 |
| 1448 | self.expect(TokenKind::Colon); |
| 1449 | |
| 1450 | // test_err if_stmt_empty_body |
| 1451 | // if True: |
| 1452 | // 1 + 1 |
| 1453 | let body = self.parse_body(Clause::If); |
| 1454 | |
| 1455 | // test_err if_stmt_misspelled_elif |
| 1456 | // if True: |
| 1457 | // pass |
| 1458 | // elf: |
| 1459 | // pass |
| 1460 | // else: |
| 1461 | // pass |
| 1462 | let elif_else_snapshot = self.elif_else_scratch.snapshot(); |
| 1463 | self.parse_clauses(Clause::ElIf, |parser| { |
| 1464 | let clause = parser.parse_elif_or_else_clause(ElifOrElse::Elif); |
| 1465 | parser.elif_else_scratch.push(clause); |
| 1466 | }); |
| 1467 | |
| 1468 | if self.at(TokenKind::Else) { |
| 1469 | let clause = self.parse_elif_or_else_clause(ElifOrElse::Else); |
| 1470 | self.elif_else_scratch.push(clause); |
| 1471 | } |
| 1472 | |
| 1473 | ast::StmtIf { |
| 1474 | test: Box::new(test.expr), |
| 1475 | body, |
| 1476 | elif_else_clauses: self.elif_else_scratch.take(elif_else_snapshot), |
| 1477 | range: self.node_range(start), |
| 1478 | node_index: AtomicNodeIndex::NONE, |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | /// Parses an `elif` or `else` clause. |
| 1483 | /// |
no test coverage detected