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