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

Method parse_alias

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

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

(&mut self, style: ImportStyle)

Source from the content-addressed store, hash-verified

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

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