allow single var patterns to double as a label in records

This commit is contained in:
rvcas 2022-11-17 12:46:03 -05:00 committed by Lucas
parent d94ae82901
commit 89153a4d82
3 changed files with 28 additions and 10 deletions

View File

@ -1270,6 +1270,12 @@ impl<'comments> Formatter<'comments> {
}
fn pattern_call_arg<'a>(&mut self, arg: &'a CallArg<UntypedPattern>) -> Document<'a> {
if let (UntypedPattern::Var { name, .. }, Some(label)) = (&arg.value, &arg.label) {
if name == label {
return self.pattern(&arg.value);
}
}
arg.label
.as_ref()
.map(|s| s.to_doc().append(": "))

View File

@ -913,12 +913,6 @@ pub fn pub_parser() -> impl Parser<Token, (), Error = ParseError> {
pub fn pattern_parser() -> impl Parser<Token, ast::UntypedPattern, Error = ParseError> {
recursive(|r| {
let no_label = r.clone().map_with_span(|pattern, span| ast::CallArg {
location: span,
value: pattern,
label: None,
});
let record_constructor_pattern_arg_parser = choice((
select! {Token::Name {name} => name}
.then_ignore(just(Token::Colon))
@ -928,7 +922,19 @@ pub fn pattern_parser() -> impl Parser<Token, ast::UntypedPattern, Error = Parse
label: Some(name),
value: pattern,
}),
no_label.clone(),
r.clone().map_with_span(|pattern, span| {
let label = if let ast::UntypedPattern::Var { name, .. } = &pattern {
Some(name.clone())
} else {
None
};
ast::CallArg {
location: span,
value: pattern,
label,
}
}),
))
.separated_by(just(Token::Comma))
.allow_trailing()
@ -940,7 +946,13 @@ pub fn pattern_parser() -> impl Parser<Token, ast::UntypedPattern, Error = Parse
)
.delimited_by(just(Token::LeftBrace), just(Token::RightBrace));
let tuple_constructor_pattern_arg_parser = no_label
let tuple_constructor_pattern_arg_parser = r
.clone()
.map_with_span(|pattern, span| ast::CallArg {
location: span,
value: pattern,
label: None,
})
.separated_by(just(Token::Comma))
.allow_trailing()
.then(

View File

@ -35,8 +35,8 @@ pub fn spend(
) -> Bool {
let x = datum.rdmr
when x is {
sample.Buy { fin: fin, .. } -> fin > 0
sample.Sell { find: fin, .. } -> fin > 0
sample.Buy { fin, .. } -> fin > 0
sample.Sell { find, .. } -> find > 0
sample.Hold(some) -> some > 0
}
}