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

Method parse_with_item

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

Parses a single `with` item. See:

(&mut self, state: WithItemParsingState)

Source from the content-addressed store, hash-verified

2468 ///
2469 /// See: <https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-with_item>
2470 fn parse_with_item(&mut self, state: WithItemParsingState) -> ParsedWithItem {
2471 let start = self.node_start();
2472
2473 // The grammar for the context expression of a with item depends on the state
2474 // of with item parsing.
2475 let context_expr = match state {
2476 WithItemParsingState::Speculative => {
2477 // If it's in a speculative state, the parenthesis (`(`) could be part of any of the
2478 // following expression:
2479 //
2480 // Tuple expression - star_named_expression
2481 // Generator expression - named_expression
2482 // Parenthesized expression - (yield_expr | named_expression)
2483 // Parenthesized with items - expression
2484 //
2485 // Here, the right side specifies the grammar for an element corresponding to the
2486 // expression mentioned in the left side.
2487 //
2488 // So, the grammar used should be able to parse an element belonging to any of the
2489 // above expression. At a later point, once the parser understands where the
2490 // parenthesis belongs to, it'll validate and report errors for any invalid expression
2491 // usage.
2492 //
2493 // Thus, we can conclude that the grammar used should be:
2494 // (yield_expr | star_named_expression)
2495 self.parse_named_expression_or_higher(
2496 ExpressionContext::yield_or_starred_bitwise_or(),
2497 )
2498 }
2499 WithItemParsingState::Regular => self.parse_conditional_expression_or_higher(),
2500 };
2501
2502 let optional_vars = self
2503 .at(TokenKind::As)
2504 .then(|| Box::new(self.parse_with_item_optional_vars().expr));
2505
2506 ParsedWithItem {
2507 is_parenthesized: context_expr.is_parenthesized,
2508 item: ast::WithItem {
2509 range: self.node_range(start),
2510 context_expr: context_expr.expr,
2511 optional_vars,
2512 node_index: AtomicNodeIndex::NONE,
2513 },
2514 }
2515 }
2516
2517 /// Parses the optional variables in a `with` item.
2518 ///

Callers 2

parse_with_itemsMethod · 0.80

Calls 6

node_startMethod · 0.80
node_rangeMethod · 0.80
atMethod · 0.45

Tested by

no test coverage detected