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

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![
ast::UntypedRecordUpdateArg {
label: "name".to_string(), label: "name".to_string(),
location: Span::new((), 68..81), location: Span::new((), 68..81),
value: expr::UntypedExpr::String { value: expr::UntypedExpr::String {
location: Span::new((), 74..81), location: Span::new((), 74..81),
value: "Aiken".to_string(), 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,
}), }),
) )
} }