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

Parses a single `with` item. See:

(&mut self, state: WithItemParsingState)

Source from the content-addressed store, hash-verified

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

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