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:2856–2884  ·  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

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

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