Refactor the ExprDict node (#11267)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
Alex Waygood 2024-05-07 12:46:10 +01:00 committed by GitHub
parent de270154a1
commit 6774f27f4b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
52 changed files with 2425 additions and 2240 deletions

View file

@ -1544,8 +1544,7 @@ impl<'src> Parser<'src> {
// Return an empty `DictExpr` when finding a `}` right after the `{`
if self.eat(TokenKind::Rbrace) {
return Expr::Dict(ast::ExprDict {
keys: vec![],
values: vec![],
items: vec![],
range: self.node_range(start),
});
}
@ -1794,21 +1793,24 @@ impl<'src> Parser<'src> {
self.expect(TokenKind::Comma);
}
let mut keys = vec![key];
let mut values = vec![value];
let mut items = vec![ast::DictItem { key, value }];
self.parse_comma_separated_list(RecoveryContextKind::DictElements, |parser| {
if parser.eat(TokenKind::DoubleStar) {
keys.push(None);
// Handle dictionary unpacking. Here, the grammar is `'**' bitwise_or`
// which requires limiting the expression.
values.push(parser.parse_expression_with_bitwise_or_precedence().expr);
items.push(ast::DictItem {
key: None,
value: parser.parse_expression_with_bitwise_or_precedence().expr,
});
} else {
keys.push(Some(parser.parse_conditional_expression_or_higher().expr));
let key = parser.parse_conditional_expression_or_higher().expr;
parser.expect(TokenKind::Colon);
values.push(parser.parse_conditional_expression_or_higher().expr);
items.push(ast::DictItem {
key: Some(key),
value: parser.parse_conditional_expression_or_higher().expr,
});
}
});
@ -1816,8 +1818,7 @@ impl<'src> Parser<'src> {
ast::ExprDict {
range: self.node_range(start),
keys,
values,
items,
}
}

View file

@ -51,24 +51,23 @@ pub(super) fn pattern_to_expr(pattern: Pattern) -> Expr {
patterns,
rest,
}) => {
let mut keys = keys.into_iter().map(Option::Some).collect::<Vec<_>>();
let mut values = patterns
let mut items: Vec<ast::DictItem> = keys
.into_iter()
.map(pattern_to_expr)
.collect::<Vec<_>>();
.zip(patterns)
.map(|(key, pattern)| ast::DictItem {
key: Some(key),
value: pattern_to_expr(pattern),
})
.collect();
if let Some(rest) = rest {
keys.push(None);
values.push(Expr::Name(ast::ExprName {
let value = Expr::Name(ast::ExprName {
range: rest.range,
id: rest.id,
ctx: ExprContext::Store,
}));
});
items.push(ast::DictItem { key: None, value });
}
Expr::Dict(ast::ExprDict {
range,
keys,
values,
})
Expr::Dict(ast::ExprDict { range, items })
}
Pattern::MatchClass(ast::PatternMatchClass {
range,