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:183–265  ·  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

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

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