Parses an import statement. # Panics If the parser isn't positioned at an `import` token. See:
(&mut self, start: TextSize, is_lazy: bool)
| 606 | /// |
| 607 | /// See: <https://docs.python.org/3/reference/simple_stmts.html#the-import-statement> |
| 608 | fn parse_import_statement(&mut self, start: TextSize, is_lazy: bool) -> ast::StmtImport { |
| 609 | self.bump(TokenKind::Import); |
| 610 | |
| 611 | // test_err import_stmt_parenthesized_names |
| 612 | // import (a) |
| 613 | // import (a, b) |
| 614 | |
| 615 | // test_err import_stmt_star_import |
| 616 | // import * |
| 617 | // import x, *, y |
| 618 | |
| 619 | // test_err import_stmt_trailing_comma |
| 620 | // import , |
| 621 | // import x, y, |
| 622 | |
| 623 | let mut names = self.parse_comma_separated_list_into_vec_with_capacity( |
| 624 | RecoveryContextKind::ImportNames, |
| 625 | |p| p.parse_alias(ImportStyle::Import), |
| 626 | 1, |
| 627 | ); |
| 628 | |
| 629 | if names.is_empty() { |
| 630 | // test_err import_stmt_empty |
| 631 | // import |
| 632 | self.add_error(ParseErrorType::EmptyImportNames, self.current_token_range()); |
| 633 | } |
| 634 | |
| 635 | names.shrink_to_fit(); |
| 636 | |
| 637 | ast::StmtImport { |
| 638 | names, |
| 639 | is_lazy, |
| 640 | range: self.node_range(start), |
| 641 | node_index: AtomicNodeIndex::NONE, |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | /// Parses a `from` import statement. |
| 646 | /// |
no test coverage detected