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:2922–2954  ·  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

2920 ///
2921 /// See: <https://docs.python.org/3/reference/expressions.html#assignment-expressions>
2922 pub(super) fn parse_named_expression(
2923 &mut self,
2924 mut target: Expr,
2925 start: TextSize,
2926 ) -> ast::ExprNamed {
2927 self.bump(TokenKind::ColonEqual);
2928
2929 if !target.is_name_expr() {
2930 self.add_error(ParseErrorType::InvalidNamedAssignmentTarget, target.range());
2931 }
2932 helpers::set_expr_ctx(&mut target, ExprContext::Store);
2933
2934 let value = self.parse_conditional_expression_or_higher();
2935
2936 let range = self.node_range(start);
2937
2938 // test_err walrus_py37
2939 // # parse_options: { "target-version": "3.7" }
2940 // (x := 1)
2941
2942 // test_ok walrus_py38
2943 // # parse_options: { "target-version": "3.8" }
2944 // (x := 1)
2945
2946 self.add_unsupported_syntax_error(UnsupportedSyntaxErrorKind::Walrus, range);
2947
2948 ast::ExprNamed {
2949 target: Box::new(target),
2950 value: Box::new(value.expr),
2951 range,
2952 node_index: AtomicNodeIndex::NONE,
2953 }
2954 }
2955
2956 /// Parses a lambda expression.
2957 ///

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