Parses a mapping pattern. # Panics If the parser isn't positioned at a `{` token. See:
(&mut self)
| 181 | /// |
| 182 | /// See: <https://docs.python.org/3/reference/compound_stmts.html#mapping-patterns> |
| 183 | fn parse_match_pattern_mapping(&mut self) -> ast::PatternMatchMapping { |
| 184 | let start = self.node_start(); |
| 185 | self.bump(TokenKind::Lbrace); |
| 186 | |
| 187 | let mut keys = PatternKeys::new(); |
| 188 | let mut patterns = Patterns::new(); |
| 189 | let mut rest = None; |
| 190 | |
| 191 | self.parse_comma_separated_list(RecoveryContextKind::MatchPatternMapping, |parser| { |
| 192 | let mapping_item_start = parser.node_start(); |
| 193 | |
| 194 | if parser.eat(TokenKind::DoubleStar) { |
| 195 | let identifier = parser.parse_match_pattern_target(); |
| 196 | if rest.is_some() { |
| 197 | parser.add_error( |
| 198 | ParseErrorType::OtherError( |
| 199 | "Only one double star pattern is allowed".to_string(), |
| 200 | ), |
| 201 | parser.node_range(mapping_item_start), |
| 202 | ); |
| 203 | } |
| 204 | // TODO(dhruvmanila): It's not possible to retain multiple double starred |
| 205 | // patterns because of the way the mapping node is represented in the grammar. |
| 206 | // The last value will always win. Update the AST representation. |
| 207 | // See: https://github.com/astral-sh/ruff/pull/10477#discussion_r1535143536 |
| 208 | rest = Some(identifier); |
| 209 | } else { |
| 210 | let key = match parser.parse_match_pattern_lhs(AllowStarPattern::No) { |
| 211 | Pattern::MatchValue(ast::PatternMatchValue { value, .. }) => *value, |
| 212 | Pattern::MatchSingleton(ast::PatternMatchSingleton { |
| 213 | value, |
| 214 | range, |
| 215 | node_index, |
| 216 | }) => match value { |
| 217 | Singleton::None => { |
| 218 | Expr::NoneLiteral(ast::ExprNoneLiteral { range, node_index }) |
| 219 | } |
| 220 | Singleton::True => Expr::BooleanLiteral(ast::ExprBooleanLiteral { |
| 221 | value: true, |
| 222 | range, |
| 223 | node_index, |
| 224 | }), |
| 225 | Singleton::False => Expr::BooleanLiteral(ast::ExprBooleanLiteral { |
| 226 | value: false, |
| 227 | range, |
| 228 | node_index, |
| 229 | }), |
| 230 | }, |
| 231 | pattern => { |
| 232 | parser.add_error( |
| 233 | ParseErrorType::OtherError("Invalid mapping pattern key".to_string()), |
| 234 | &pattern, |
| 235 | ); |
| 236 | recovery::pattern_to_expr(pattern) |
| 237 | } |
| 238 | }; |
| 239 | keys.push(key); |
| 240 |
no test coverage detected