MCPcopy Create free account
hub / github.com/Glyphack/enderpy / parse_from_import_statement

Method parse_from_import_statement

parser/src/parser/parser.rs:1415–1457  ·  view source on GitHub ↗

https://docs.python.org/3/reference/simple_stmts.html#the-from-import-statement

(&mut self)

Source from the content-addressed store, hash-verified

1413
1414 // https://docs.python.org/3/reference/simple_stmts.html#the-from-import-statement
1415 fn parse_from_import_statement(&mut self) -> Result<Statement, ParsingError> {
1416 let import_node = self.start_node();
1417 self.bump(Kind::From);
1418 let (module, level) = self.parse_module_name()?;
1419 self.bump(Kind::Import);
1420 let mut aliases = vec![];
1421 if self.eat(Kind::LeftParen) {
1422 while self.at(Kind::Identifier) {
1423 let alias_name = self.start_node();
1424 let name = self.cur_token().to_string(self.source);
1425 self.bump(Kind::Identifier);
1426 let asname = self.parse_alias(name, alias_name);
1427 aliases.push(asname);
1428 if !self.eat(Kind::Comma) {
1429 break;
1430 }
1431 }
1432 self.expect(Kind::RightParen)?;
1433 } else if self.at(Kind::Identifier) {
1434 while self.at(Kind::Identifier) {
1435 let alias_name = self.start_node();
1436 let name = self.cur_token().to_string(self.source);
1437 self.bump(Kind::Identifier);
1438 let asname = self.parse_alias(name, alias_name);
1439 aliases.push(asname);
1440 if !self.eat(Kind::Comma) {
1441 break;
1442 }
1443 }
1444 } else if self.at(Kind::Mul) {
1445 let node = self.start_node();
1446 self.bump_any();
1447 aliases.push(self.parse_alias("*".to_string(), node));
1448 } else {
1449 panic!("Unexpected token {:?}", self.cur_token());
1450 }
1451 Ok(Statement::ImportFrom(Box::new(ImportFrom {
1452 node: self.finish_node(import_node),
1453 module,
1454 names: aliases,
1455 level,
1456 })))
1457 }
1458
1459 fn parse_alias(&mut self, name: String, node: Node) -> Alias {
1460 let asname = if self.eat(Kind::As) {

Callers 1

Calls 12

ImportFromClass · 0.85
start_nodeMethod · 0.80
bumpMethod · 0.80
parse_module_nameMethod · 0.80
eatMethod · 0.80
atMethod · 0.80
to_stringMethod · 0.80
cur_tokenMethod · 0.80
parse_aliasMethod · 0.80
expectMethod · 0.80
bump_anyMethod · 0.80
finish_nodeMethod · 0.80

Tested by

no test coverage detected