Skip to content

Fix match parsing #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions ast/src/source_locator.rs
Original file line number Diff line number Diff line change
@@ -274,6 +274,34 @@ impl crate::fold::Fold<TextRange> for LinearLocator<'_> {
keywords,
})
}

fn fold_pattern_match_mapping(
&mut self,
node: crate::PatternMatchMapping<TextRange>,
) -> Result<crate::PatternMatchMapping<Self::TargetU>, Self::Error> {
let crate::PatternMatchMapping {
keys,
patterns,
rest,
range,
} = node;
let context = self.will_map_user(&range);

let mut located_keys = Vec::with_capacity(keys.len());
let mut located_patterns = Vec::with_capacity(patterns.len());
for (key, value) in keys.into_iter().zip(patterns.into_iter()) {
located_keys.push(self.fold(key)?);
located_patterns.push(self.fold(value)?);
}
let rest = self.fold(rest)?;
let range = self.map_user(range, context)?;
Ok(crate::PatternMatchMapping {
keys: located_keys,
patterns: located_patterns,
rest,
range,
})
}
}

struct LinearLookaheadLocator<'a, 'b>(&'b mut LinearLocator<'a>);
3 changes: 2 additions & 1 deletion parser/src/python.lalrpop
Original file line number Diff line number Diff line change
@@ -370,13 +370,14 @@ MatchStatement: ast::Stmt = {
.last()
.unwrap()
.end();
let subject_range = (subjects.first().unwrap().start()..subjects.last().unwrap().end()).into();
ast::Stmt::Match(
ast::StmtMatch {
subject: Box::new(ast::Expr::Tuple(
ast::ExprTuple {
elts: subjects,
ctx: ast::ExprContext::Load,
range: (location..end_location).into()
range: subject_range,
},
)),
cases,
5 changes: 3 additions & 2 deletions parser/src/python.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.