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

Method parse_lhs_expression

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

Parses the left-hand side of an expression. This includes prefix expressions such as unary operators, boolean `not`, `await`, `lambda`. It also parses atoms and postfix expressions. The given [`OperatorPrecedence`] is used to determine if the parsed expression is valid in that context. For example, a unary operator is not valid in an `await` expression in which case the `left_precedence` would b

(
        &mut self,
        left_precedence: OperatorPrecedence,
        context: ExpressionContext,
    )

Source from the content-addressed store, hash-verified

330 /// in an `await` expression in which case the `left_precedence` would
331 /// be [`OperatorPrecedence::Await`].
332 fn parse_lhs_expression(
333 &mut self,
334 left_precedence: OperatorPrecedence,
335 context: ExpressionContext,
336 ) -> ParsedExpr {
337 let token = self.current_token_kind();
338 let start = self.node_start();
339
340 if let Some(unary_op) = token.as_unary_operator() {
341 let expr = self.parse_unary_expression(unary_op, context);
342
343 if matches!(unary_op, UnaryOp::Not) {
344 if left_precedence > OperatorPrecedence::Not {
345 self.add_error(
346 ParseErrorType::OtherError(
347 "Boolean 'not' expression cannot be used here".to_string(),
348 ),
349 &expr,
350 );
351 }
352 } else {
353 // > The power operator `**` binds less tightly than an arithmetic
354 // > or bitwise unary operator on its right, that is, 2**-1 is 0.5.
355 //
356 // Reference: https://docs.python.org/3/reference/expressions.html#id21
357 if left_precedence > OperatorPrecedence::PosNegBitNot
358 && left_precedence != OperatorPrecedence::Exponent
359 {
360 self.add_error(
361 ParseErrorType::OtherError(format!(
362 "Unary '{unary_op}' expression cannot be used here",
363 )),
364 &expr,
365 );
366 }
367 }
368
369 return Expr::UnaryOp(expr).into();
370 }
371
372 match token {
373 TokenKind::Star => {
374 let starred_expr = self.parse_starred_expression(context);
375
376 if left_precedence > OperatorPrecedence::None
377 || !context.is_starred_expression_allowed()
378 {
379 self.add_error(ParseErrorType::InvalidStarredExpressionUsage, &starred_expr);
380 }
381
382 return Expr::Starred(starred_expr).into();
383 }
384 TokenKind::Await => {
385 let await_expr = self.parse_await_expression();
386
387 // `await` expressions cannot be nested
388 if left_precedence >= OperatorPrecedence::Await {
389 self.add_error(

Callers 1

Calls 15

UnaryOpEnum · 0.85
current_token_kindMethod · 0.80
node_startMethod · 0.80
as_unary_operatorMethod · 0.80
parse_lambda_exprMethod · 0.80
parse_atomMethod · 0.80

Tested by

no test coverage detected