Parses a slice expression. See:
(&mut self)
| 998 | /// |
| 999 | /// See: <https://docs.python.org/3/reference/expressions.html#slicings> |
| 1000 | fn parse_slice(&mut self) -> Expr { |
| 1001 | const UPPER_END_SET: TokenSet = |
| 1002 | TokenSet::new([TokenKind::Comma, TokenKind::Colon, TokenKind::Rsqb]) |
| 1003 | .union(NEWLINE_EOF_SET); |
| 1004 | const STEP_END_SET: TokenSet = |
| 1005 | TokenSet::new([TokenKind::Comma, TokenKind::Rsqb]).union(NEWLINE_EOF_SET); |
| 1006 | |
| 1007 | // test_err named_expr_slice |
| 1008 | // # even after 3.9, an unparenthesized named expression is not allowed in a slice |
| 1009 | // lst[x:=1:-1] |
| 1010 | // lst[1:x:=1] |
| 1011 | // lst[1:3:x:=1] |
| 1012 | |
| 1013 | // test_err named_expr_slice_parse_error |
| 1014 | // # parse_options: {"target-version": "3.8"} |
| 1015 | // # before 3.9, only emit the parse error, not the unsupported syntax error |
| 1016 | // lst[x:=1:-1] |
| 1017 | |
| 1018 | let start = self.node_start(); |
| 1019 | |
| 1020 | let lower = if self.at_expr() { |
| 1021 | let lower = |
| 1022 | self.parse_named_expression_or_higher(ExpressionContext::starred_conditional()); |
| 1023 | |
| 1024 | // This means we're in a subscript. |
| 1025 | if self.at_ts(NEWLINE_EOF_SET.union([TokenKind::Rsqb, TokenKind::Comma].into())) { |
| 1026 | // test_ok parenthesized_named_expr_index_py38 |
| 1027 | // # parse_options: {"target-version": "3.8"} |
| 1028 | // lst[(x:=1)] |
| 1029 | |
| 1030 | // test_ok unparenthesized_named_expr_index_py39 |
| 1031 | // # parse_options: {"target-version": "3.9"} |
| 1032 | // lst[x:=1] |
| 1033 | |
| 1034 | // test_err unparenthesized_named_expr_index_py38 |
| 1035 | // # parse_options: {"target-version": "3.8"} |
| 1036 | // lst[x:=1] |
| 1037 | if lower.is_unparenthesized_named_expr() { |
| 1038 | self.add_unsupported_syntax_error( |
| 1039 | UnsupportedSyntaxErrorKind::UnparenthesizedNamedExpr( |
| 1040 | UnparenthesizedNamedExprKind::SequenceIndex, |
| 1041 | ), |
| 1042 | lower.range(), |
| 1043 | ); |
| 1044 | } |
| 1045 | return lower.expr; |
| 1046 | } |
| 1047 | |
| 1048 | // Now we know we're in a slice. |
| 1049 | if !lower.is_parenthesized { |
| 1050 | match lower.expr { |
| 1051 | Expr::Starred(_) => { |
| 1052 | self.add_error(ParseErrorType::InvalidStarredExpressionUsage, &lower); |
| 1053 | } |
| 1054 | Expr::Named(_) => { |
| 1055 | self.add_error(ParseErrorType::UnparenthesizedNamedExpression, &lower); |
| 1056 | } |
| 1057 | _ => {} |
no test coverage detected