Skip to content

Commit 32f3f00

Browse files
committed
Preserve the property of 1 function per => in function definition.
Before this diff `() => 3` would be a function but `(type a) => 3` would just be a number. Plus, `(type a) => (type b) => (x) => ...` would express a single function. Now, each `=>` represents a function (possibly adding a unit parameter when there are only types but no no term parameters).
1 parent 0cf0b21 commit 32f3f00

File tree

8 files changed

+59
-79
lines changed

8 files changed

+59
-79
lines changed

compiler/syntax/src/res_core.ml

+27-50
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,12 @@ and parse_parameter_list p =
17311731
~f:parse_parameter ~closing:Rparen p
17321732
in
17331733
Parser.expect Rparen p;
1734-
parameters
1734+
let has_term_parameter =
1735+
Ext_list.exists parameters (function
1736+
| TermParameter _ -> true
1737+
| _ -> false)
1738+
in
1739+
(has_term_parameter, parameters)
17351740

17361741
(* parameters ::=
17371742
* | _
@@ -1742,6 +1747,22 @@ and parse_parameter_list p =
17421747
*)
17431748
and parse_parameters p =
17441749
let start_pos = p.Parser.start_pos in
1750+
let unit_term_parameter () =
1751+
let loc = mk_loc start_pos p.Parser.prev_end_pos in
1752+
let unit_pattern =
1753+
Ast_helper.Pat.construct ~loc
1754+
(Location.mkloc (Longident.Lident "()") loc)
1755+
None
1756+
in
1757+
TermParameter
1758+
{
1759+
attrs = [];
1760+
label = Asttypes.Nolabel;
1761+
expr = None;
1762+
pat = unit_pattern;
1763+
pos = start_pos;
1764+
}
1765+
in
17451766
match p.Parser.token with
17461767
| Lident ident ->
17471768
Parser.next p;
@@ -1769,56 +1790,12 @@ and parse_parameters p =
17691790
pos = start_pos;
17701791
};
17711792
]
1772-
| Lparen -> (
1793+
| Lparen ->
17731794
Parser.next p;
1774-
match p.Parser.token with
1775-
| Rparen ->
1776-
Parser.next p;
1777-
let loc = mk_loc start_pos p.Parser.prev_end_pos in
1778-
let unit_pattern =
1779-
Ast_helper.Pat.construct ~loc
1780-
(Location.mkloc (Longident.Lident "()") loc)
1781-
None
1782-
in
1783-
[
1784-
TermParameter
1785-
{
1786-
attrs = [];
1787-
label = Asttypes.Nolabel;
1788-
expr = None;
1789-
pat = unit_pattern;
1790-
pos = start_pos;
1791-
};
1792-
]
1793-
| Dot -> (
1794-
Parser.next p;
1795-
match p.token with
1796-
| Rparen ->
1797-
Parser.next p;
1798-
let loc = mk_loc start_pos p.Parser.prev_end_pos in
1799-
let unit_pattern =
1800-
Ast_helper.Pat.construct ~loc
1801-
(Location.mkloc (Longident.Lident "()") loc)
1802-
None
1803-
in
1804-
[
1805-
TermParameter
1806-
{
1807-
attrs = [];
1808-
label = Asttypes.Nolabel;
1809-
expr = None;
1810-
pat = unit_pattern;
1811-
pos = start_pos;
1812-
};
1813-
]
1814-
| _ -> (
1815-
match parse_parameter_list p with
1816-
| TermParameter p :: rest ->
1817-
TermParameter {p with pos = start_pos} :: rest
1818-
| TypeParameter p :: rest ->
1819-
TypeParameter {p with pos = start_pos} :: rest
1820-
| parameters -> parameters))
1821-
| _ -> parse_parameter_list p)
1795+
ignore (Parser.optional p Dot);
1796+
let has_term_parameter, parameters = parse_parameter_list p in
1797+
if has_term_parameter then parameters
1798+
else parameters @ [unit_term_parameter ()]
18221799
| token ->
18231800
Parser.err p (Diagnostics.unexpected token p.breadcrumbs);
18241801
[]

compiler/syntax/src/res_parsetree_viewer.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ let fun_expr expr =
187187
};
188188
} ->
189189
(attrs_before, List.rev acc, rewrite_underscore_apply expr)
190-
| {pexp_desc = Pexp_newtype (string_loc, rest); pexp_attributes = attrs} ->
190+
| {pexp_desc = Pexp_newtype (string_loc, rest); pexp_attributes = attrs}
191+
when n_fun = 0 ->
191192
let string_locs, return_expr = collect_new_types [string_loc] rest in
192193
let param = NewTypes {attrs; locs = string_locs} in
193194
collect ~n_fun attrs_before (param :: acc) return_expr

tests/syntax_tests/data/parsing/grammar/expressions/expected/UncurriedByDefault.res.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ let t0 (type a) (type b) [arity:2](l : a list) (x : a) = x :: l
4444
let t1 (type a) (type b) [arity:2](l : a list) (x : a) = x :: l
4545
let t2 (type a) (type b) [arity:2](l : a list) (x : a) = x :: l
4646
let t3 (type a) (type b) [arity:2](l : a list) (x : a) = x :: l
47-
let t4 (type a) (type b) [arity:2](l : a list) (x : a) = x :: l
48-
let t5 (type a) (type b) [arity:2](l : a list) (x : a) = x :: l
49-
let t6 (type a) (type b) [arity:2](l : a list) (x : a) = x :: l
47+
let t4 (type a) (type b) [arity:1]() [arity:2](l : a list) (x : a) = x :: l
48+
let t5 (type a) (type b) [arity:1]() [arity:2](l : a list) (x : a) = x :: l
49+
let t6 (type a) (type b) [arity:1]() [arity:2](l : a list) (x : a) = x :: l
5050
type nonrec arrowPath1 = int -> string (a:1)
5151
type nonrec arrowPath2 = I.t -> string (a:1)
5252
type nonrec arrowPath3 = int -> string (a:1)

tests/syntax_tests/data/parsing/grammar/expressions/expected/async.res.txt

+12-7
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@ let ex2 = ((3)[@res.await ]) ** ((4)[@res.await ])
3333
let ex3 = ((foo |.u (bar ~arg:((arg)[@res.namedArgLoc ])))[@res.await ])
3434
let ex4 = (((foo.bar).baz)[@res.await ])
3535
let attr1 = ((fun [arity:1]x -> x + 1)[@res.async ][@a ])
36-
let attr2 = ((fun (type a) -> fun (type b) -> fun (type c) ->
37-
fun [arity:1]x -> 3)[@res.async ][@a ])
38-
let attr3 = ((fun (type a) -> ((fun (type b) -> fun (type c) ->
39-
fun [arity:1]x -> 3)[@res.async ]))[@a ])
40-
let attr4 = ((fun (type a) -> ((fun (type b) -> fun (type c) ->
41-
fun [arity:1]x -> 3)[@res.async ][@b ]))[@a ])
36+
let attr2 = ((fun (type a) ->
37+
fun [arity:1]() -> fun (type b) -> fun (type c) -> fun [arity:1]x -> 3)
38+
[@res.async ][@a ])
39+
let attr3 = ((fun (type a) ->
40+
fun [arity:1]() -> ((fun (type b) -> fun (type c) -> fun [arity:1]x -> 3)
41+
[@res.async ]))
42+
[@a ])
43+
let attr4 = ((fun (type a) ->
44+
fun [arity:1]() -> ((fun (type b) -> fun (type c) -> fun [arity:1]x -> 3)
45+
[@res.async ][@b ]))
46+
[@a ])
4247
let (attr5 : int) = ((fun (type a) -> fun (type b) -> fun (type c) ->
43-
fun [arity:1](x : a) -> x)[@res.async ][@a ][@b ])
48+
fun [arity:1]() -> fun [arity:1](x : a) -> x)[@res.async ][@a ][@b ])

tests/syntax_tests/data/printer/comments/expected/expr.res.txt

+2-6
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,8 @@ let f = (
227227
) => /* c7 */ ()
228228

229229
let multiply = (type /* c-2 */ t /* c-1 */, /* c0 */ m1 /* c1 */, /* c2 */ m2 /* c3 */) => ()
230-
let multiply = (
231-
type /* c-4 */ t /* c-3 */,
232-
/* c0 */ m1 /* c1 */,
233-
type /* c-2 */ s /* c-1 */,
234-
/* c2 */ m2 /* c3 */,
235-
) => ()
230+
let multiply = (type /* c-4 */ t /* c-3 */, /* c0 */ m1 /* c1 */) =>
231+
(type /* c-2 */ s /* c-1 */, /* c2 */ m2 /* c3 */) => ()
236232

237233
f(
238234
// a

tests/syntax_tests/data/printer/expr/expected/UncurriedByDefault.res.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ let t0 = (type a b, l: list<a>, x: a) => list{x, ...l}
5353
let t1 = (type a b, l: list<a>, x: a) => list{x, ...l}
5454
let t2 = (type a b, l: list<a>, x: a) => list{x, ...l}
5555
let t3 = (type a b, l: list<a>, x: a) => list{x, ...l}
56-
let t4 = (type a b, l: list<a>, x: a) => list{x, ...l}
57-
let t5 = (type a b, l: list<a>, x: a) => list{x, ...l}
58-
let t6 = (type a b, l: list<a>, x: a) => list{x, ...l}
56+
let t4 = (type a b, ()) => (l: list<a>, x: a) => list{x, ...l}
57+
let t5 = (type a b, ()) => (l: list<a>, x: a) => list{x, ...l}
58+
let t6 = (type a b, ()) => (l: list<a>, x: a) => list{x, ...l}
5959

6060
let () = (x => ignore(x))(3)
6161
let () = (x => ignore(x))(3)

tests/syntax_tests/data/printer/expr/expected/asyncAwait.res.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ let f = async (type a, ()) => {
146146
}
147147

148148
let attr1 = @a async x => x + 1
149-
let attr2 = @a async (type a b c, x) => 3
150-
let attr3 = @a (type a, type b c, x) => 3
151-
let attr4 = @a (type a, @b type b c, x) => 3
152-
let attr5: int => promise<int> = @a @b async (type a b c, x: a) => x
149+
let attr2 = @a async (type a, ()) => (type b c, x) => 3
150+
let attr3 = @a (type a, ()) => async (type b c, x) => 3
151+
let attr4 = @a (type a, ()) => @b async (type b c, x) => 3
152+
let attr5: int => promise<int> = @a @b async (type a b c, ()) => (x: a) => x

tests/syntax_tests/data/printer/expr/expected/newtype.res.txt

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
let f = (type t, xs: list<t>) => ()
22
let f = @attr (type t, xs: list<t>) => ()
3-
let f = (type t, xs: list<t>, type s, ys: list<s>) => ()
4-
let f = @attr (type t, xs: list<t>, @attr2 type s, ys: list<s>) => ()
3+
let f = (type t, xs: list<t>) => (type s, ys: list<s>) => ()
4+
let f = @attr (type t, xs: list<t>) => @attr2 (type s, ys: list<s>) => ()
55
let f = (type t u v, xs: list<(t, u, v)>) => ()
66
let f = @attr (type t u v, xs: list<(t, u, v)>) => ()
7-
let f = (type t u v, xs: list<(t, u, v)>, type s w z, ys: list<(s, w, z)>) => ()
8-
let f = @attr (type t u v, xs: list<(t, u, v)>, @attr2 type s w z, ys: list<(s, w, z)>) => ()
7+
let f = (type t u v, xs: list<(t, u, v)>) => (type s w z, ys: list<(s, w, z)>) => ()
8+
let f = @attr (type t u v, xs: list<(t, u, v)>) => @attr2 (type s w z, ys: list<(s, w, z)>) => ()
99
let f = @attr
10-
(type t, @attr type s, xs: list<(t, s)>, @attr type u, @attr type v w, ys: list<(u, v, w)>) => ()
10+
(type t, @attr type s, xs: list<(t, s)>) =>
11+
@attr (type u, @attr type v w, ys: list<(u, v, w)>) => ()
1112

1213
let mk_formatting_gen:
1314
type a b c d e f. formatting_gen<a, b, c, d, e, f> => Parsetree.expression =

0 commit comments

Comments
 (0)