MCPcopy Create free account
hub / github.com/astral-sh/ruff / parse_import_statement

Method parse_import_statement

crates/ruff_python_parser/src/parser/statement.rs:612–646  ·  view source on GitHub ↗

Parses an import statement. # Panics If the parser isn't positioned at an `import` token. See:

(&mut self, start: TextSize, is_lazy: bool)

Source from the content-addressed store, hash-verified

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 ///

Callers 1

Calls 10

parse_aliasMethod · 0.80
current_token_rangeMethod · 0.80
node_rangeMethod · 0.80
bumpMethod · 0.45
snapshotMethod · 0.45
pushMethod · 0.45
takeMethod · 0.45
is_emptyMethod · 0.45
add_errorMethod · 0.45

Tested by

no test coverage detected