From 85f839abe48ee03e938c23d77099dba3ddf997e8 Mon Sep 17 00:00:00 2001 From: rvcas Date: Sun, 4 Dec 2022 18:58:49 -0500 Subject: [PATCH] feat: support punning in record updates --- crates/lang/src/parser.rs | 34 ++++++++++++++++++++++----------- crates/lang/src/tests/parser.rs | 30 +++++++++++++++++++---------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/crates/lang/src/parser.rs b/crates/lang/src/parser.rs index 0a30466c..f83c28e3 100644 --- a/crates/lang/src/parser.rs +++ b/crates/lang/src/parser.rs @@ -573,18 +573,30 @@ pub fn expr_parser( .then( just(Token::Comma) .ignore_then( - select! { Token::Name {name} => name } - .then_ignore(just(Token::Colon)) - .then(r.clone()) - .map_with_span(|(label, value), span| { - ast::UntypedRecordUpdateArg { - label, - value, + choice(( + select! { Token::Name {name} => name } + .then_ignore(just(Token::Colon)) + .then(r.clone()) + .map_with_span(|(label, value), span| { + ast::UntypedRecordUpdateArg { + label, + value, + location: span, + } + }), + select! {Token::Name {name} => name}.map_with_span( + |name, span| ast::UntypedRecordUpdateArg { location: span, - } - }) - .separated_by(just(Token::Comma)) - .allow_trailing(), + value: expr::UntypedExpr::Var { + name: name.clone(), + location: span, + }, + label: name, + }, + ), + )) + .separated_by(just(Token::Comma)) + .allow_trailing(), ) .or_not(), ) diff --git a/crates/lang/src/tests/parser.rs b/crates/lang/src/tests/parser.rs index 80982d3a..37884205 100644 --- a/crates/lang/src/tests/parser.rs +++ b/crates/lang/src/tests/parser.rs @@ -1177,7 +1177,7 @@ fn call() { fn record_update() { let code = indoc! {r#" 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 { - location: Span::new((), 53..84), + location: Span::new((), 53..88), constructor: Box::new(expr::UntypedExpr::Var { location: Span::new((), 53..57), name: "User".to_string(), @@ -1227,14 +1227,24 @@ fn record_update() { }), location: Span::new((), 60..66), }, - arguments: vec![ast::UntypedRecordUpdateArg { - label: "name".to_string(), - location: Span::new((), 68..81), - value: expr::UntypedExpr::String { - location: Span::new((), 74..81), - value: "Aiken".to_string(), + arguments: vec![ + ast::UntypedRecordUpdateArg { + label: "name".to_string(), + location: Span::new((), 68..81), + value: expr::UntypedExpr::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, location: Span::new((), 0..48), @@ -1247,7 +1257,7 @@ fn record_update() { arguments: vec![], }), return_type: (), - end_position: 85, + end_position: 89, }), ) }