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

Method parse_match_pattern_mapping

crates/ruff_python_parser/src/parser/pattern.rs:201–283  ·  view source on GitHub ↗

Parses a mapping pattern. # Panics If the parser isn't positioned at a `{` token. See:

(&mut self)

Source from the content-addressed store, hash-verified

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

Callers 1

Calls 13

pattern_to_exprFunction · 0.85
node_startMethod · 0.80
node_rangeMethod · 0.80
expectMethod · 0.80
parse_match_patternMethod · 0.80
bumpMethod · 0.45
eatMethod · 0.45
add_errorMethod · 0.45
to_stringMethod · 0.45

Tested by

no test coverage detected