feat: support punning in record updates

This commit is contained in:
rvcas 2022-12-04 18:58:49 -05:00 committed by Lucas
parent 9ebc836b89
commit 85f839abe4
2 changed files with 43 additions and 21 deletions

View File

@ -573,18 +573,30 @@ pub fn expr_parser(
.then( .then(
just(Token::Comma) just(Token::Comma)
.ignore_then( .ignore_then(
select! { Token::Name {name} => name } choice((
.then_ignore(just(Token::Colon)) select! { Token::Name {name} => name }
.then(r.clone()) .then_ignore(just(Token::Colon))
.map_with_span(|(label, value), span| { .then(r.clone())
ast::UntypedRecordUpdateArg { .map_with_span(|(label, value), span| {
label, ast::UntypedRecordUpdateArg {
value, label,
value,
location: span,
}
}),
select! {Token::Name {name} => name}.map_with_span(
|name, span| ast::UntypedRecordUpdateArg {
location: span, location: span,
} value: expr::UntypedExpr::Var {
}) name: name.clone(),
.separated_by(just(Token::Comma)) location: span,
.allow_trailing(), },
label: name,
},
),
))
.separated_by(just(Token::Comma))
.allow_trailing(),
) )
.or_not(), .or_not(),
) )

View File

@ -1177,7 +1177,7 @@ fn call() {
fn record_update() { fn record_update() {
let code = indoc! {r#" let code = indoc! {r#"
fn update_name(user: User, name: String) -> User { fn update_name(user: User, name: String) -> User {
User { ..user, name: "Aiken", } User { ..user, name: "Aiken", age }
} }
"#}; "#};
@ -1215,7 +1215,7 @@ fn record_update() {
}, },
], ],
body: expr::UntypedExpr::RecordUpdate { body: expr::UntypedExpr::RecordUpdate {
location: Span::new((), 53..84), location: Span::new((), 53..88),
constructor: Box::new(expr::UntypedExpr::Var { constructor: Box::new(expr::UntypedExpr::Var {
location: Span::new((), 53..57), location: Span::new((), 53..57),
name: "User".to_string(), name: "User".to_string(),
@ -1227,14 +1227,24 @@ fn record_update() {
}), }),
location: Span::new((), 60..66), location: Span::new((), 60..66),
}, },
arguments: vec![ast::UntypedRecordUpdateArg { arguments: vec![
label: "name".to_string(), ast::UntypedRecordUpdateArg {
location: Span::new((), 68..81), label: "name".to_string(),
value: expr::UntypedExpr::String { location: Span::new((), 68..81),
location: Span::new((), 74..81), value: expr::UntypedExpr::String {
value: "Aiken".to_string(), location: Span::new((), 74..81),
value: "Aiken".to_string(),
},
}, },
}], ast::UntypedRecordUpdateArg {
label: "age".to_string(),
location: Span::new((), 83..86),
value: expr::UntypedExpr::Var {
location: Span::new((), 83..86),
name: "age".to_string(),
},
},
],
}, },
doc: None, doc: None,
location: Span::new((), 0..48), location: Span::new((), 0..48),
@ -1247,7 +1257,7 @@ fn record_update() {
arguments: vec![], arguments: vec![],
}), }),
return_type: (), return_type: (),
end_position: 85, end_position: 89,
}), }),
) )
} }