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