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