Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Spread anywhere feature #692

Merged
merged 18 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 45 additions & 26 deletions src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,6 @@ module ErrorMessages = struct
...b}` wouldn't make sense, as `b` would override every field of `a` \
anyway."

let listExprSpread =
"Lists can only have one `...` spread, and at the end.\n\
Explanation: lists are singly-linked list, where a node contains a value \
and points to the next node. `list{a, ...bc}` efficiently creates a new \
item and links `bc` as its next nodes. `list{...bc, a}` would be \
expensive, as it'd need to traverse `bc` and prepend each item to `a` one \
by one. We therefore disallow such syntax sugar.\n\
Solution: directly use `concat`."

let variantIdent =
"A polymorphic variant (e.g. #id) must start with an alphabetical letter \
or be a number (e.g. #742)"
Expand Down Expand Up @@ -3705,38 +3696,66 @@ and parseTupleExpr ~first ~startPos p =
let loc = mkLoc startPos p.prevEndPos in
Ast_helper.Exp.tuple ~loc exprs

and parseSpreadExprRegion p =
and parseSpreadExprRegionWithLoc p =
let startPos = p.Parser.prevEndPos in
match p.Parser.token with
| DotDotDot ->
Parser.next p;
let expr = parseConstrainedOrCoercedExpr p in
Some (true, expr)
Some (true, expr, startPos, p.prevEndPos)
| token when Grammar.isExprStart token ->
Some (false, parseConstrainedOrCoercedExpr p)
Some (false, parseConstrainedOrCoercedExpr p, startPos, p.prevEndPos)
| _ -> None

and parseListExpr ~startPos p =
let check_all_non_spread_exp exprs =
exprs
|> List.map (fun (spread, expr) ->
if spread then
Parser.err p (Diagnostics.message ErrorMessages.listExprSpread);
expr)
|> List.rev
let split_by_spread exprs =
List.fold_left
(fun acc curr ->
match (curr, acc) with
| (true, expr, startPos, endPos), _ ->
(* find a spread expression, prepend a new sublist *)
([], Some expr, startPos, endPos) :: acc
| ( (false, expr, startPos, _endPos),
(no_spreads, spread, _accStartPos, accEndPos) :: acc ) ->
(* find a non-spread expression, and the accumulated is not empty,
* prepend to the first sublist, and update the loc first sublist *)
(expr :: no_spreads, spread, startPos, accEndPos) :: acc
| (false, expr, startPos, endPos), [] ->
(* find a non-spread expression, and the accumulated is empty *)
[([expr], None, startPos, endPos)])
[] exprs
in
let make_sub_expr = function
| exprs, Some spread, startPos, endPos ->
makeListExpression (mkLoc startPos endPos) exprs (Some spread)
| exprs, None, startPos, endPos ->
makeListExpression (mkLoc startPos endPos) exprs None
in
let listExprsRev =
parseCommaDelimitedReversedList p ~grammar:Grammar.ListExpr ~closing:Rbrace
~f:parseSpreadExprRegion
~f:parseSpreadExprRegionWithLoc
in
Parser.expect Rbrace p;
let loc = mkLoc startPos p.prevEndPos in
match listExprsRev with
| (true (* spread expression *), expr) :: exprs ->
let exprs = check_all_non_spread_exp exprs in
makeListExpression loc exprs (Some expr)
match split_by_spread listExprsRev with
| [] -> makeListExpression loc [] None
| [(exprs, Some spread, _, _)] -> makeListExpression loc exprs (Some spread)
| [(exprs, None, _, _)] -> makeListExpression loc exprs None
| exprs ->
let exprs = check_all_non_spread_exp exprs in
makeListExpression loc exprs None
let listExprs = List.map make_sub_expr exprs in
Ast_helper.Exp.apply ~loc
(Ast_helper.Exp.ident ~loc
(Location.mkloc
(Longident.Ldot
(Longident.Ldot (Longident.Lident "Belt", "List"), "concatMany"))
loc))
[(Asttypes.Nolabel, Ast_helper.Exp.array ~loc listExprs)]
(* | (true (\* spread expression *\), expr, _) :: exprs ->
* let exprs = check_all_non_spread_exp exprs in
* makeListExpression loc exprs (Some expr)
* | exprs ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of comment unused code, can you just remove it

* let exprs = check_all_non_spread_exp exprs in
* makeListExpression loc exprs None *)

(* Overparse ... and give a nice error message *)
and parseNonSpreadExp ~msg p =
Expand Down
16 changes: 1 addition & 15 deletions tests/parsing/errors/other/expected/spread.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,6 @@ Explanation: you can't collect a subset of a record's field into its own record,
Solution: you need to pull out each field you want explicitly.


Syntax error!
tests/parsing/errors/other/spread.res:8:1-3

6 │
7 │ let myList = list{...x, ...y}
8 │ let list{...x, ...y} = myList
9 │
10 │ type t = {...a}

Lists can only have one `...` spread, and at the end.
Explanation: lists are singly-linked list, where a node contains a value and points to the next node. `list{a, ...bc}` efficiently creates a new item and links `bc` as its next nodes. `list{...bc, a}` would be expensive, as it'd need to traverse `bc` and prepend each item to `a` one by one. We therefore disallow such syntax sugar.
Solution: directly use `concat`.


Syntax error!
tests/parsing/errors/other/spread.res:8:13-22

Expand Down Expand Up @@ -114,7 +100,7 @@ let arr = [|x;y|]
let [|arr;_|] = [|1;2;3|]
let record = { x with y }
let { x; y } = myRecord
let myList = x :: y
let myList = Belt.List.concatMany [|x;y|]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • this case should be specialized?
  • this case too { ... x}

let x::y = myList
type nonrec t = < a >
type nonrec t =
Expand Down
1 change: 1 addition & 0 deletions tests/parsing/grammar/expressions/expected/list.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ let x = [1; 2; 3]
let x = [1; 2; 3]
let x = [(1 : int); (2 : int); (3 : int)]
let x = 4 :: 5 :: y
let x = Belt.List.concatMany [|(1 :: x);(2 :: 3 :: x)|]
let x = 1 :: 2 :: (y : int list)
3 changes: 3 additions & 0 deletions tests/parsing/grammar/expressions/list.res
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ let x = list{1: int, (2: int), 3: int}
// spread
let x = list{4, 5, ...y}

// spread anywhere
let x = list{1, ...x, 2, 3, ...x}

// spread constrained expression
let x = list{1, 2, ...y: list<int>}
13 changes: 13 additions & 0 deletions tests/printer/expr/expected/list.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ let x = list{}
let x = list{1}
let x = list{1, 2}
let x = list{1, 2, 3}
let x = Belt.List.concatMany([list{1, 2, ...x}, list{3, ...x}])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be improved? print it as list{ 1, 2, ...x , 3 , ...x}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be, but just by changing the printer can cause some problems. For example, list{. . x, . .y} and Belt.List.concatMany([x, y]) will be printed as the same thing. I am still trying to solve this problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add an attribute to Belt.List.concatMany to avoid accidental simplifications?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what is done for string templates, I believe, to differentiate from normal string concatenation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check what isTemplateLiteral does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for all your suggestions, I'll learn more about that.

let x = list{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add some more test cases, some edge cases like:

let x = list{ ...xs}
let x = list{1, ...xs}
let x = list{xs, ... ys}
let x = list{ ...xs, ... ys}

superLoooooooooooooooooooooooooooooongIiiiiiiiiideeeentifieeeeeeeeeeeeeeeeer,
Expand All @@ -17,3 +18,15 @@ let x = list{
superLoooooooooooooooooooooooooooooongIiiiiiiiiideeeentifieeeeeeeeeeeeeeeeer,
...superLoooooooooooooooooooooooooooooongListHere,
}

let x = Belt.List.concatMany([
list{
superLoooooooooooooooooooooooooooooongIiiiiiiiiideeeentifieeeeeeeeeeeeeeeeer,
superLoooooooooooooooooooooooooooooongIiiiiiiiiideeeentifieeeeeeeeeeeeeeeeer,
...superLoooooooooooooooooooooooooooooongListHere,
},
list{
superLoooooooooooooooooooooooooooooongIiiiiiiiiideeeentifieeeeeeeeeeeeeeeeer,
...superLoooooooooooooooooooooooooooooongListHere,
},
])
11 changes: 11 additions & 0 deletions tests/printer/expr/list.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ let x = list{}
let x = list{1}
let x = list{1, 2}
let x = list{1, 2, 3}
let x = list{1, 2, ...x, 3, ...x}

let x = list{
superLoooooooooooooooooooooooooooooongIiiiiiiiiideeeentifieeeeeeeeeeeeeeeeer,
Expand All @@ -18,3 +19,13 @@ let x = list{
superLoooooooooooooooooooooooooooooongIiiiiiiiiideeeentifieeeeeeeeeeeeeeeeer,
...superLoooooooooooooooooooooooooooooongListHere,
}




let x = list{
superLoooooooooooooooooooooooooooooongIiiiiiiiiideeeentifieeeeeeeeeeeeeeeeer,
superLoooooooooooooooooooooooooooooongIiiiiiiiiideeeentifieeeeeeeeeeeeeeeeer, ...superLoooooooooooooooooooooooooooooongListHere,
superLoooooooooooooooooooooooooooooongIiiiiiiiiideeeentifieeeeeeeeeeeeeeeeer,
...superLoooooooooooooooooooooooooooooongListHere,
}