Skip to content

Commit f07b97c

Browse files
committed
Fix match parsing
1 parent e00297d commit f07b97c

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

ast/src/source_locator.rs

+28
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,34 @@ impl crate::fold::Fold<TextRange> for LinearLocator<'_> {
274274
keywords,
275275
})
276276
}
277+
278+
fn fold_pattern_match_mapping(
279+
&mut self,
280+
node: crate::PatternMatchMapping<TextRange>,
281+
) -> Result<crate::PatternMatchMapping<Self::TargetU>, Self::Error> {
282+
let crate::PatternMatchMapping {
283+
keys,
284+
patterns,
285+
rest,
286+
range,
287+
} = node;
288+
let context = self.will_map_user(&range);
289+
290+
let mut located_keys = Vec::with_capacity(keys.len());
291+
let mut located_patterns = Vec::with_capacity(patterns.len());
292+
for (key, value) in keys.into_iter().zip(patterns.into_iter()) {
293+
located_keys.push(self.fold(key)?);
294+
located_patterns.push(self.fold(value)?);
295+
}
296+
let rest = self.fold(rest)?;
297+
let range = self.map_user(range, context)?;
298+
Ok(crate::PatternMatchMapping {
299+
keys: located_keys,
300+
patterns: located_patterns,
301+
rest,
302+
range,
303+
})
304+
}
277305
}
278306

279307
struct LinearLookaheadLocator<'a, 'b>(&'b mut LinearLocator<'a>);

parser/src/python.lalrpop

+2-1
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,14 @@ MatchStatement: ast::Stmt = {
370370
.last()
371371
.unwrap()
372372
.end();
373+
let subject_range = (subjects.first().unwrap().start()..subjects.last().unwrap().end()).into();
373374
ast::Stmt::Match(
374375
ast::StmtMatch {
375376
subject: Box::new(ast::Expr::Tuple(
376377
ast::ExprTuple {
377378
elts: subjects,
378379
ctx: ast::ExprContext::Load,
379-
range: (location..end_location).into()
380+
range: subject_range,
380381
},
381382
)),
382383
cases,

parser/src/python.rs

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parser/src/snapshots/rustpython_parser__parser__tests__patma.snap

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)