Remove leading '#' for tuple definitions.

This possibly breaks many Aiken programs out there, but it's for the
  best. We haven't released the alpha yet so we still have a bit of
  freedom when it comes to breaking change.

  Plus, the migration path is easy, simply run:

  ```
  find . -name "*.ak" | xargs sed -i "s/#(/(/g"
  ```

  (or `-i ''` on MacOS).
This commit is contained in:
KtorZ 2023-01-14 11:44:08 +01:00
parent db22395764
commit 5b7147fc43
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
5 changed files with 125 additions and 73 deletions

View File

@ -703,7 +703,7 @@ pub enum Pattern<Constructor, Type> {
}, },
/// A name given to a sub-pattern using the `as` keyword. /// A name given to a sub-pattern using the `as` keyword.
/// e.g. `assert #(1, [_, _] as the_list) = x` /// e.g. `assert (1, [_, _] as the_list) = x`
Assign { Assign {
name: String, name: String,
location: Span, location: Span,

View File

@ -396,13 +396,9 @@ impl<'comments> Formatter<'comments> {
.. ..
} => docvec![module, ".", name], } => docvec![module, ".", name],
Constant::Tuple { elements, .. } => "#" Constant::Tuple { elements, .. } => {
.to_doc() wrap_args(false, elements.iter().map(|e| (self.const_expr(e), false))).group()
.append(wrap_args( }
false,
elements.iter().map(|e| (self.const_expr(e), false)),
))
.group(),
} }
} }
@ -480,10 +476,9 @@ impl<'comments> Formatter<'comments> {
.append(break_("", " ").append(self.annotation(retrn)).nest(INDENT)), .append(break_("", " ").append(self.annotation(retrn)).nest(INDENT)),
Annotation::Var { name, .. } => name.to_doc(), Annotation::Var { name, .. } => name.to_doc(),
Annotation::Tuple { elems, .. } => "#".to_doc().append(wrap_args( Annotation::Tuple { elems, .. } => {
false, wrap_args(false, elems.iter().map(|t| (self.annotation(t), false)))
elems.iter().map(|t| (self.annotation(t), false)), }
)),
} }
.group() .group()
} }
@ -768,13 +763,9 @@ impl<'comments> Formatter<'comments> {
.. ..
} => self.record_update(constructor, spread, args), } => self.record_update(constructor, spread, args),
UntypedExpr::Tuple { elems, .. } => "#" UntypedExpr::Tuple { elems, .. } => {
.to_doc() wrap_args(false, elems.iter().map(|e| (self.wrap_expr(e), false))).group()
.append(wrap_args( }
false,
elems.iter().map(|e| (self.wrap_expr(e), false)),
))
.group(),
UntypedExpr::TupleIndex { index, tuple, .. } => { UntypedExpr::TupleIndex { index, tuple, .. } => {
let suffix = Ordinal(*index + 1).suffix().to_doc(); let suffix = Ordinal(*index + 1).suffix().to_doc();
@ -1500,13 +1491,9 @@ impl<'comments> Formatter<'comments> {
Pattern::Discard { name, .. } => name.to_doc(), Pattern::Discard { name, .. } => name.to_doc(),
Pattern::Tuple { elems, .. } => "#" Pattern::Tuple { elems, .. } => {
.to_doc() wrap_args(false, elems.iter().map(|e| (self.pattern(e), false))).group()
.append(wrap_args( }
false,
elems.iter().map(|e| (self.pattern(e), false)),
))
.group(),
Pattern::List { elements, tail, .. } => { Pattern::List { elements, tail, .. } => {
let elements_document = let elements_document =

View File

@ -346,12 +346,14 @@ fn constant_value_parser() -> impl Parser<Token, ast::UntypedConstant, Error = P
} }
}); });
let constant_tuple_parser = just(Token::Hash) let constant_tuple_parser = r
.ignore_then( .clone()
r.clone()
.separated_by(just(Token::Comma)) .separated_by(just(Token::Comma))
.at_least(2)
.allow_trailing() .allow_trailing()
.delimited_by(just(Token::LeftParen), just(Token::RightParen)), .delimited_by(
choice((just(Token::LeftParen), just(Token::NewLineLeftParen))),
just(Token::RightParen),
) )
.map_with_span(|elements, span| ast::UntypedConstant::Tuple { .map_with_span(|elements, span| ast::UntypedConstant::Tuple {
location: span, location: span,
@ -897,12 +899,14 @@ pub fn expr_parser(
label, label,
}); });
let tuple = just(Token::Hash) let tuple = r
.ignore_then( .clone()
r.clone()
.separated_by(just(Token::Comma)) .separated_by(just(Token::Comma))
.at_least(2)
.allow_trailing() .allow_trailing()
.delimited_by(just(Token::LeftParen), just(Token::RightParen)), .delimited_by(
choice((just(Token::LeftParen), just(Token::NewLineLeftParen))),
just(Token::RightParen),
) )
.map_with_span(|elems, span| expr::UntypedExpr::Tuple { .map_with_span(|elems, span| expr::UntypedExpr::Tuple {
location: span, location: span,
@ -1380,13 +1384,11 @@ pub fn type_parser() -> impl Parser<Token, ast::Annotation, Error = ParseError>
} }
}), }),
// Tuple // Tuple
just(Token::Hash)
.ignore_then(
r.clone() r.clone()
.separated_by(just(Token::Comma)) .separated_by(just(Token::Comma))
.at_least(2)
.allow_trailing() .allow_trailing()
.delimited_by(just(Token::LeftParen), just(Token::RightParen)), .delimited_by(just(Token::LeftParen), just(Token::RightParen))
)
.map_with_span(|elems, span| ast::Annotation::Tuple { .map_with_span(|elems, span| ast::Annotation::Tuple {
location: span, location: span,
elems, elems,
@ -1607,13 +1609,10 @@ pub fn pattern_parser() -> impl Parser<Token, ast::UntypedPattern, Error = Parse
value, value,
} }
}), }),
just(Token::Hash)
.ignore_then(
r.clone() r.clone()
.separated_by(just(Token::Comma)) .separated_by(just(Token::Comma))
.allow_trailing() .allow_trailing()
.delimited_by(just(Token::LeftParen), just(Token::RightParen)), .delimited_by(just(Token::LeftParen), just(Token::RightParen))
)
.map_with_span(|elems, span| ast::UntypedPattern::Tuple { .map_with_span(|elems, span| ast::UntypedPattern::Tuple {
location: span, location: span,
elems, elems,

View File

@ -1444,7 +1444,7 @@ fn record_create_unlabeled() {
fn parse_tuple() { fn parse_tuple() {
let code = indoc! {r#" let code = indoc! {r#"
fn foo() { fn foo() {
let tuple = #(1, 2, 3, 4) let tuple = (1, 2, 3, 4)
tuple.1st + tuple.2nd + tuple.3rd + tuple.4th tuple.1st + tuple.2nd + tuple.3rd + tuple.4th
} }
"#}; "#};
@ -1454,27 +1454,27 @@ fn parse_tuple() {
vec![ast::UntypedDefinition::Fn(Function { vec![ast::UntypedDefinition::Fn(Function {
arguments: vec![], arguments: vec![],
body: expr::UntypedExpr::Sequence { body: expr::UntypedExpr::Sequence {
location: Span::new((), 13..86), location: Span::new((), 13..85),
expressions: vec![ expressions: vec![
expr::UntypedExpr::Assignment { expr::UntypedExpr::Assignment {
location: Span::new((), 13..38), location: Span::new((), 13..37),
value: Box::new(expr::UntypedExpr::Tuple { value: Box::new(expr::UntypedExpr::Tuple {
location: Span::new((), 25..38), location: Span::new((), 25..37),
elems: vec![ elems: vec![
expr::UntypedExpr::Int { expr::UntypedExpr::Int {
location: Span::new((), 27..28), location: Span::new((), 26..27),
value: "1".to_string(), value: "1".to_string(),
}, },
expr::UntypedExpr::Int { expr::UntypedExpr::Int {
location: Span::new((), 30..31), location: Span::new((), 29..30),
value: "2".to_string(), value: "2".to_string(),
}, },
expr::UntypedExpr::Int { expr::UntypedExpr::Int {
location: Span::new((), 33..34), location: Span::new((), 32..33),
value: "3".to_string(), value: "3".to_string(),
}, },
expr::UntypedExpr::Int { expr::UntypedExpr::Int {
location: Span::new((), 36..37), location: Span::new((), 35..36),
value: "4".to_string(), value: "4".to_string(),
}, },
], ],
@ -1487,45 +1487,45 @@ fn parse_tuple() {
annotation: None, annotation: None,
}, },
expr::UntypedExpr::BinOp { expr::UntypedExpr::BinOp {
location: Span::new((), 41..86), location: Span::new((), 40..85),
name: ast::BinOp::AddInt, name: ast::BinOp::AddInt,
left: Box::new(expr::UntypedExpr::BinOp { left: Box::new(expr::UntypedExpr::BinOp {
location: Span::new((), 41..74), location: Span::new((), 40..73),
name: ast::BinOp::AddInt, name: ast::BinOp::AddInt,
left: Box::new(expr::UntypedExpr::BinOp { left: Box::new(expr::UntypedExpr::BinOp {
location: Span::new((), 41..62), location: Span::new((), 40..61),
name: ast::BinOp::AddInt, name: ast::BinOp::AddInt,
left: Box::new(expr::UntypedExpr::TupleIndex { left: Box::new(expr::UntypedExpr::TupleIndex {
location: Span::new((), 41..50), location: Span::new((), 40..49),
index: 0, index: 0,
tuple: Box::new(expr::UntypedExpr::Var { tuple: Box::new(expr::UntypedExpr::Var {
location: Span::new((), 41..46), location: Span::new((), 40..45),
name: "tuple".to_string(), name: "tuple".to_string(),
}), }),
}), }),
right: Box::new(expr::UntypedExpr::TupleIndex { right: Box::new(expr::UntypedExpr::TupleIndex {
location: Span::new((), 53..62), location: Span::new((), 52..61),
index: 1, index: 1,
tuple: Box::new(expr::UntypedExpr::Var { tuple: Box::new(expr::UntypedExpr::Var {
location: Span::new((), 53..58), location: Span::new((), 52..57),
name: "tuple".to_string(), name: "tuple".to_string(),
}), }),
}), }),
}), }),
right: Box::new(expr::UntypedExpr::TupleIndex { right: Box::new(expr::UntypedExpr::TupleIndex {
location: Span::new((), 65..74), location: Span::new((), 64..73),
index: 2, index: 2,
tuple: Box::new(expr::UntypedExpr::Var { tuple: Box::new(expr::UntypedExpr::Var {
location: Span::new((), 65..70), location: Span::new((), 64..69),
name: "tuple".to_string(), name: "tuple".to_string(),
}), }),
}), }),
}), }),
right: Box::new(expr::UntypedExpr::TupleIndex { right: Box::new(expr::UntypedExpr::TupleIndex {
location: Span::new((), 77..86), location: Span::new((), 76..85),
index: 3, index: 3,
tuple: Box::new(expr::UntypedExpr::Var { tuple: Box::new(expr::UntypedExpr::Var {
location: Span::new((), 77..82), location: Span::new((), 76..81),
name: "tuple".to_string(), name: "tuple".to_string(),
}), }),
}), }),
@ -1538,11 +1538,77 @@ fn parse_tuple() {
public: false, public: false,
return_annotation: None, return_annotation: None,
return_type: (), return_type: (),
end_position: 87, end_position: 86,
})], })],
) )
} }
#[test]
fn parse_tuple2() {
let code = indoc! {r#"
fn foo() {
let a = foo(14)
(a, 42)
}
"#};
assert_definitions(
code,
vec![ast::UntypedDefinition::Fn(Function {
arguments: vec![],
body: expr::UntypedExpr::Sequence {
location: Span::new((), 13..38),
expressions: vec![
expr::UntypedExpr::Assignment {
location: Span::new((), 13..28),
value: Box::new(expr::UntypedExpr::Call {
arguments: vec![ast::CallArg {
label: None,
location: Span::new((), 25..27),
value: expr::UntypedExpr::Int {
location: Span::new((), 25..27),
value: "14".to_string(),
},
}],
fun: Box::new(expr::UntypedExpr::Var {
location: Span::new((), 21..24),
name: "foo".to_string(),
}),
location: Span::new((), 24..28),
}),
pattern: ast::Pattern::Var {
location: Span::new((), 17..18),
name: "a".to_string(),
},
kind: ast::AssignmentKind::Let,
annotation: None,
},
expr::UntypedExpr::Tuple {
location: Span::new((), 31..38),
elems: vec![
expr::UntypedExpr::Var {
location: Span::new((), 32..33),
name: "a".to_string(),
},
expr::UntypedExpr::Int {
location: Span::new((), 35..37),
value: "42".to_string(),
},
],
},
],
},
doc: None,
location: Span::new((), 0..8),
name: "foo".to_string(),
public: false,
return_annotation: None,
return_type: (),
end_position: 39,
})],
);
}
#[test] #[test]
fn plain_bytearray_literals() { fn plain_bytearray_literals() {
let code = indoc! {r#" let code = indoc! {r#"

View File

@ -75,7 +75,7 @@ impl Printer {
Type::Var { tipo: typ, .. } => self.type_var_doc(&typ.borrow()), Type::Var { tipo: typ, .. } => self.type_var_doc(&typ.borrow()),
Type::Tuple { elems, .. } => self.args_to_aiken_doc(elems).surround("#(", ")"), Type::Tuple { elems, .. } => self.args_to_aiken_doc(elems).surround("(", ")"),
} }
} }