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