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:2465–2510  ·  view source on GitHub ↗

Parses a single `with` item. See:

(&mut self, state: WithItemParsingState)

Source from the content-addressed store, hash-verified

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

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