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:607–641  ·  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

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

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