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

Method parse_alias

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

Parses an `import` or `from` import name. See: - -

(&mut self, style: ImportStyle)

Source from the content-addressed store, hash-verified

776 /// - <https://docs.python.org/3/reference/simple_stmts.html#the-import-statement>
777 /// - <https://docs.python.org/3/library/ast.html#ast.alias>
778 fn parse_alias(&mut self, style: ImportStyle) -> ast::Alias {
779 let start = self.node_start();
780 if self.eat(TokenKind::Star) {
781 let range = self.node_range(start);
782 return ast::Alias {
783 name: ast::Identifier {
784 id: Name::new_static("*"),
785 range,
786 node_index: AtomicNodeIndex::NONE,
787 },
788 asname: None,
789 range,
790 node_index: AtomicNodeIndex::NONE,
791 };
792 }
793
794 let name = match style {
795 ImportStyle::Import => self.parse_dotted_name(),
796 ImportStyle::ImportFrom => self.parse_identifier(),
797 };
798
799 let asname = if self.eat(TokenKind::As) {
800 if self.at_name_or_soft_keyword() {
801 // test_ok import_as_name_soft_keyword
802 // import foo as match
803 // import bar as case
804 // import baz as type
805 // import qux as lazy
806 Some(self.parse_identifier())
807 } else {
808 // test_err import_alias_missing_asname
809 // import x as
810 self.add_error(
811 ParseErrorType::OtherError("Expected symbol after `as`".to_string()),
812 self.current_token_range(),
813 );
814 None
815 }
816 } else {
817 None
818 };
819
820 ast::Alias {
821 range: self.node_range(start),
822 name,
823 asname,
824 node_index: AtomicNodeIndex::NONE,
825 }
826 }
827
828 /// Parses a dotted name.
829 ///

Callers 2

Calls 9

node_startMethod · 0.80
node_rangeMethod · 0.80
parse_dotted_nameMethod · 0.80
parse_identifierMethod · 0.80
current_token_rangeMethod · 0.80
eatMethod · 0.45
add_errorMethod · 0.45
to_stringMethod · 0.45

Tested by

no test coverage detected