Parses an import statement. # Panics If the parser isn't positioned at an `import` token. See:
(&mut self, start: TextSize, is_lazy: bool)
| 605 | /// |
| 606 | /// See: <https://docs.python.org/3/reference/simple_stmts.html#the-import-statement> |
| 607 | fn parse_import_statement(&mut self, start: TextSize, is_lazy: bool) -> ast::StmtImport { |
| 608 | self.bump(TokenKind::Import); |
| 609 | |
| 610 | // test_err import_stmt_parenthesized_names |
| 611 | // import (a) |
| 612 | // import (a, b) |
| 613 | |
| 614 | // test_err import_stmt_star_import |
| 615 | // import * |
| 616 | // import x, *, y |
| 617 | |
| 618 | // test_err import_stmt_trailing_comma |
| 619 | // import , |
| 620 | // import x, y, |
| 621 | |
| 622 | let names_snapshot = self.alias_scratch.snapshot(); |
| 623 | self.parse_comma_separated_list(RecoveryContextKind::ImportNames, |parser| { |
| 624 | let alias = parser.parse_alias(ImportStyle::Import); |
| 625 | parser.alias_scratch.push(alias); |
| 626 | }); |
| 627 | let names: Vec<_> = self.alias_scratch.take(names_snapshot); |
| 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 | ast::StmtImport { |
| 636 | names, |
| 637 | is_lazy, |
| 638 | range: self.node_range(start), |
| 639 | node_index: AtomicNodeIndex::NONE, |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /// Parses a `from` import statement. |
| 644 | /// |
no test coverage detected