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

Method parse_named_expression

crates/ruff_python_parser/src/parser/expression.rs:2848–2876  ·  view source on GitHub ↗

Parses a named expression (`:=`). # Panics If the parser isn't positioned at a `:=` token. See:

(&mut self, mut target: Expr, start: TextSize)

Source from the content-addressed store, hash-verified

2846 ///
2847 /// See: <https://docs.python.org/3/reference/expressions.html#assignment-expressions>
2848 fn parse_named_expression(&mut self, mut target: Expr, start: TextSize) -> ast::ExprNamed {
2849 self.bump(TokenKind::ColonEqual);
2850
2851 if !target.is_name_expr() {
2852 self.add_error(ParseErrorType::InvalidNamedAssignmentTarget, target.range());
2853 }
2854 helpers::set_expr_ctx(&mut target, ExprContext::Store);
2855
2856 let value = self.parse_conditional_expression_or_higher();
2857
2858 let range = self.node_range(start);
2859
2860 // test_err walrus_py37
2861 // # parse_options: { "target-version": "3.7" }
2862 // (x := 1)
2863
2864 // test_ok walrus_py38
2865 // # parse_options: { "target-version": "3.8" }
2866 // (x := 1)
2867
2868 self.add_unsupported_syntax_error(UnsupportedSyntaxErrorKind::Walrus, range);
2869
2870 ast::ExprNamed {
2871 target: Box::new(target),
2872 value: Box::new(value.expr),
2873 range,
2874 node_index: AtomicNodeIndex::NONE,
2875 }
2876 }
2877
2878 /// Parses a lambda expression.
2879 ///

Callers 1

Calls 8

set_expr_ctxFunction · 0.85
is_name_exprMethod · 0.80
node_rangeMethod · 0.80
bumpMethod · 0.45
add_errorMethod · 0.45
rangeMethod · 0.45

Tested by

no test coverage detected