Skip to content

Commit c90f3d9

Browse files
committed
wip: await AST
1 parent 41e41b9 commit c90f3d9

24 files changed

+90
-71
lines changed

analysis/src/CompletionFrontEnd.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,12 @@ let rec exprToContextPathInner ~(inJsxContext : bool) (e : Parsetree.expression)
312312
if List.length exprs = List.length exprsAsContextPaths then
313313
Some (CTuple exprsAsContextPaths)
314314
else None
315+
| Pexp_await e -> exprToContextPathInner ~inJsxContext e
315316
| _ -> None
316317

317318
and exprToContextPath ~(inJsxContext : bool) (e : Parsetree.expression) =
318319
match
319-
( Res_parsetree_viewer.has_await_attribute e.pexp_attributes,
320+
( Res_parsetree_viewer.has_await_attribute e,
320321
exprToContextPathInner ~inJsxContext e )
321322
with
322323
| true, Some ctxPath -> Some (CPAwait ctxPath)

analysis/src/Utils.ml

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ let identifyPexp pexp =
111111
| Pexp_pack _ -> "Pexp_pack"
112112
| Pexp_extension _ -> "Pexp_extension"
113113
| Pexp_open _ -> "Pexp_open"
114+
| Pexp_await _ -> "Pexp_await"
114115

115116
let identifyPpat pat =
116117
match pat with

compiler/frontend/bs_ast_mapper.ml

+1
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ module E = struct
366366
| Pexp_open (ovf, lid, e) ->
367367
open_ ~loc ~attrs ovf (map_loc sub lid) (sub.expr sub e)
368368
| Pexp_extension x -> extension ~loc ~attrs (sub.extension sub x)
369+
| Pexp_await e -> await ~loc ~attrs (sub.expr sub e)
369370
end
370371

371372
module P = struct

compiler/frontend/bs_builtin_ppx.ml

+12-9
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
177177
(* module M = await Belt.List *)
178178
| Pexp_letmodule
179179
(lid, ({pmod_desc = Pmod_ident {txt}; pmod_attributes} as me), expr)
180-
when Res_parsetree_viewer.has_await_attribute pmod_attributes ->
180+
when Res_parsetree_viewer.has_await_attribute2 pmod_attributes ->
181181
let safe_module_type_lid : Ast_helper.lid =
182182
{txt = Lident (local_module_type_name txt); loc = me.pmod_loc}
183183
in
@@ -201,8 +201,8 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
201201
pmod_attributes = attrs2;
202202
} as me),
203203
expr )
204-
when Res_parsetree_viewer.has_await_attribute attrs1
205-
|| Res_parsetree_viewer.has_await_attribute attrs2 ->
204+
when Res_parsetree_viewer.has_await_attribute2 attrs1
205+
|| Res_parsetree_viewer.has_await_attribute2 attrs2 ->
206206
{
207207
e with
208208
pexp_desc =
@@ -242,10 +242,12 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
242242
|| Ast_attributes.has_await_payload attrs2 ->
243243
check_await ();
244244
result
245-
| _ when Ast_attributes.has_await_payload e.pexp_attributes ->
246-
check_await ();
247-
Ast_await.create_await_expression result
248-
| _ -> result
245+
| _ -> (
246+
match result.pexp_desc with
247+
| Pexp_await e ->
248+
check_await ();
249+
Ast_await.create_await_expression e
250+
| _ -> result)
249251

250252
let typ_mapper (self : mapper) (typ : Parsetree.core_type) =
251253
Ast_core_type_class_type.typ_mapper self typ
@@ -493,7 +495,7 @@ let rec structure_mapper ~await_context (self : mapper) (stru : Ast_structure.t)
493495
| Pstr_module
494496
({pmb_expr = {pmod_desc = Pmod_ident {txt; loc}; pmod_attributes} as me}
495497
as mb)
496-
when Res_parsetree_viewer.has_await_attribute pmod_attributes ->
498+
when Res_parsetree_viewer.has_await_attribute2 pmod_attributes ->
497499
let item = self.structure_item self item in
498500
let safe_module_type_name = local_module_type_name txt in
499501
let has_local_module_name =
@@ -544,7 +546,8 @@ let rec structure_mapper ~await_context (self : mapper) (stru : Ast_structure.t)
544546
( _,
545547
({pmod_desc = Pmod_ident {txt; loc}; pmod_attributes} as me),
546548
expr )
547-
when Res_parsetree_viewer.has_await_attribute pmod_attributes -> (
549+
when Res_parsetree_viewer.has_await_attribute2 pmod_attributes
550+
-> (
548551
let safe_module_type_name = local_module_type_name txt in
549552
let has_local_module_name =
550553
Hashtbl.find_opt !await_context safe_module_type_name

compiler/ml/ast_helper.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ module Exp = struct
180180
let pack ?loc ?attrs a = mk ?loc ?attrs (Pexp_pack a)
181181
let open_ ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_open (a, b, c))
182182
let extension ?loc ?attrs a = mk ?loc ?attrs (Pexp_extension a)
183-
183+
let await ?loc ?attrs a = mk ?loc ?attrs (Pexp_await a)
184184
let case lhs ?guard rhs = {pc_lhs = lhs; pc_guard = guard; pc_rhs = rhs}
185185
end
186186

compiler/ml/ast_helper.mli

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ module Exp : sig
210210
val extension : ?loc:loc -> ?attrs:attrs -> extension -> expression
211211

212212
val case : pattern -> ?guard:expression -> expression -> case
213+
val await : ?loc:loc -> ?attrs:attrs -> expression -> expression
213214
end
214215

215216
(** Value declarations *)

compiler/ml/ast_iterator.ml

+1
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ module E = struct
344344
iter_loc sub lid;
345345
sub.expr sub e
346346
| Pexp_extension x -> sub.extension sub x
347+
| Pexp_await e -> sub.expr sub e
347348
end
348349

349350
module P = struct

compiler/ml/ast_mapper.ml

+1
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ module E = struct
329329
| Pexp_open (ovf, lid, e) ->
330330
open_ ~loc ~attrs ovf (map_loc sub lid) (sub.expr sub e)
331331
| Pexp_extension x -> extension ~loc ~attrs (sub.extension sub x)
332+
| Pexp_await e -> await ~loc ~attrs (sub.expr sub e)
332333
end
333334

334335
module P = struct

compiler/ml/ast_mapper_to0.ml

+1
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ module E = struct
407407
| Pexp_open (ovf, lid, e) ->
408408
open_ ~loc ~attrs ovf (map_loc sub lid) (sub.expr sub e)
409409
| Pexp_extension x -> extension ~loc ~attrs (sub.extension sub x)
410+
| Pexp_await _ -> (* TODO *) assert false
410411
end
411412

412413
module P = struct

compiler/ml/depend.ml

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ let rec add_expr bv exp =
289289
| Pstr_eval ({pexp_desc = Pexp_construct (c, None)}, _) -> add bv c
290290
| _ -> handle_extension e)
291291
| Pexp_extension e -> handle_extension e
292+
| Pexp_await e -> add_expr bv e
292293

293294
and add_cases bv cases = List.iter (add_case bv) cases
294295

compiler/ml/parsetree.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,9 @@ and expression_desc =
313313
let open M in E
314314
let! open M in E *)
315315
| Pexp_extension of extension
316-
(* [%id] *)
317-
(* . *)
316+
(* [%id] *)
317+
(* . *)
318+
| Pexp_await of expression
318319

319320
and case = {
320321
(* (P -> E) or (P when E0 -> E) *)

compiler/ml/pprintast.ml

+1
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ and expression ctxt f x =
722722
(expression ctxt) e
723723
| Pexp_variant (l, Some eo) -> pp f "@[<2>`%s@;%a@]" l (simple_expr ctxt) eo
724724
| Pexp_extension e -> extension ctxt f e
725+
| Pexp_await e -> pp f "@[<hov2>await@ %a@]" (simple_expr ctxt) e
725726
| _ -> expression1 ctxt f x
726727

727728
and expression1 ctxt f x =

compiler/ml/printast.ml

+3
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ and expression i ppf x =
345345
| Pexp_extension (s, arg) ->
346346
line i ppf "Pexp_extension \"%s\"\n" s.txt;
347347
payload i ppf arg
348+
| Pexp_await e ->
349+
line i ppf "Pexp_await\n";
350+
expression i ppf e
348351

349352
and value_description i ppf x =
350353
line i ppf "value_description %a %a\n" fmt_string_loc x.pval_name fmt_location

compiler/ml/typecore.ml

+2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ let iter_expression f e =
178178
expr e;
179179
module_expr me
180180
| Pexp_pack me -> module_expr me
181+
| Pexp_await _ -> assert false (* should be handled earlier *)
181182
and case {pc_lhs = _; pc_guard; pc_rhs} =
182183
may expr pc_guard;
183184
expr pc_rhs
@@ -3195,6 +3196,7 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
31953196
| _ -> raise (Error (loc, env, Invalid_extension_constructor_payload)))
31963197
| Pexp_extension ext ->
31973198
raise (Error_forward (Builtin_attributes.error_of_extension ext))
3199+
| Pexp_await _ -> (* should be handled earlier *) assert false
31983200
31993201
and type_function ?in_function ~arity ~async loc attrs env ty_expected_ l
32003202
caselist =

compiler/syntax/src/res_ast_debugger.ml

+1
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ module SexpAst = struct
707707
]
708708
| Pexp_extension ext ->
709709
Sexp.list [Sexp.atom "Pexp_extension"; extension ext]
710+
| Pexp_await e -> Sexp.list [Sexp.atom "Pexp_await"; expression e]
710711
in
711712
Sexp.list [Sexp.atom "expression"; desc]
712713

compiler/syntax/src/res_core.ml

+3-6
Original file line numberDiff line numberDiff line change
@@ -3307,15 +3307,12 @@ and parse_async_arrow_expression ?(arrow_attrs = []) p =
33073307

33083308
and parse_await_expression p =
33093309
let await_loc = mk_loc p.Parser.start_pos p.end_pos in
3310-
let await_attr = make_await_attr await_loc in
33113310
Parser.expect Await p;
33123311
let token_prec = Token.precedence MinusGreater in
33133312
let expr = parse_binary_expr ~context:OrdinaryExpr p token_prec in
3314-
{
3315-
expr with
3316-
pexp_attributes = await_attr :: expr.pexp_attributes;
3317-
pexp_loc = {expr.pexp_loc with loc_start = await_loc.loc_start};
3318-
}
3313+
Ast_helper.Exp.await
3314+
~loc:{expr.pexp_loc with loc_start = await_loc.loc_start}
3315+
~attrs:[] expr
33193316

33203317
and parse_try_expression p =
33213318
let start_pos = p.Parser.start_pos in

compiler/syntax/src/res_parens.ml

+7-15
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ let call_expr expr =
5656
} ->
5757
Parenthesized
5858
| _ when Ast_uncurried.expr_is_uncurried_fun expr -> Parenthesized
59-
| _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
60-
Parenthesized
59+
| _ when ParsetreeViewer.has_await_attribute expr -> Parenthesized
6160
| _ -> Nothing)
6261

6362
let structure_expr expr =
@@ -109,8 +108,7 @@ let unary_expr_operand expr =
109108
| Pexp_try _ | Pexp_while _ | Pexp_for _ | Pexp_ifthenelse _ );
110109
} ->
111110
Parenthesized
112-
| _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
113-
Parenthesized
111+
| _ when ParsetreeViewer.has_await_attribute expr -> Parenthesized
114112
| _ -> Nothing)
115113

116114
let binary_expr_operand ~is_lhs expr =
@@ -133,8 +131,7 @@ let binary_expr_operand ~is_lhs expr =
133131
| expr when ParsetreeViewer.is_binary_expression expr -> Parenthesized
134132
| expr when ParsetreeViewer.is_ternary_expr expr -> Parenthesized
135133
| {pexp_desc = Pexp_lazy _ | Pexp_assert _} when is_lhs -> Parenthesized
136-
| _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
137-
Parenthesized
134+
| _ when ParsetreeViewer.has_await_attribute expr -> Parenthesized
138135
| {Parsetree.pexp_attributes = attrs} ->
139136
if ParsetreeViewer.has_printable_attributes attrs then Parenthesized
140137
else Nothing)
@@ -229,9 +226,7 @@ let lazy_or_assert_or_await_expr_rhs ?(in_await = false) expr =
229226
| Pexp_while _ | Pexp_for _ | Pexp_ifthenelse _ );
230227
} ->
231228
Parenthesized
232-
| _
233-
when (not in_await)
234-
&& ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
229+
| _ when (not in_await) && ParsetreeViewer.has_await_attribute expr ->
235230
Parenthesized
236231
| _ -> Nothing)
237232

@@ -277,8 +272,7 @@ let field_expr expr =
277272
| Pexp_try _ | Pexp_while _ | Pexp_for _ | Pexp_ifthenelse _ );
278273
} ->
279274
Parenthesized
280-
| _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
281-
Parenthesized
275+
| _ when ParsetreeViewer.has_await_attribute expr -> Parenthesized
282276
| _ -> Nothing)
283277

284278
let set_field_expr_rhs expr =
@@ -339,8 +333,7 @@ let jsx_prop_expr expr =
339333
}
340334
when starts_with_minus x ->
341335
Parenthesized
342-
| _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
343-
Parenthesized
336+
| _ when ParsetreeViewer.has_await_attribute expr -> Parenthesized
344337
| {
345338
Parsetree.pexp_desc =
346339
( Pexp_ident _ | Pexp_constant _ | Pexp_field _ | Pexp_construct _
@@ -377,8 +370,7 @@ let jsx_child_expr expr =
377370
}
378371
when starts_with_minus x ->
379372
Parenthesized
380-
| _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
381-
Parenthesized
373+
| _ when ParsetreeViewer.has_await_attribute expr -> Parenthesized
382374
| {
383375
Parsetree.pexp_desc =
384376
( Pexp_ident _ | Pexp_constant _ | Pexp_field _ | Pexp_construct _

compiler/syntax/src/res_parsetree_viewer.ml

+11-1
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,23 @@ let functor_type modtype =
6565
in
6666
process [] modtype
6767

68-
let has_await_attribute attrs =
68+
let has_await_attribute2 attrs =
6969
List.exists
7070
(function
7171
| {Location.txt = "res.await"}, _ -> true
7272
| _ -> false)
7373
attrs
7474

75+
let has_await_attribute e =
76+
(match e.pexp_desc with
77+
| Pexp_await _ -> true
78+
| _ -> false)
79+
|| List.exists
80+
(function
81+
| {Location.txt = "res.await"}, _ -> true
82+
| _ -> false)
83+
e.pexp_attributes
84+
7585
let has_inline_record_definition_attribute attrs =
7686
List.exists
7787
(function

compiler/syntax/src/res_parsetree_viewer.mli

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ val functor_type :
1414
list
1515
* Parsetree.module_type
1616

17-
val has_await_attribute : Parsetree.attributes -> bool
17+
val has_await_attribute : Parsetree.expression -> bool
18+
val has_await_attribute2 : Parsetree.attributes -> bool
1819
val has_inline_record_definition_attribute : Parsetree.attributes -> bool
1920
val has_res_pat_variant_spread_attribute : Parsetree.attributes -> bool
2021
val has_dict_pattern_attribute : Parsetree.attributes -> bool

compiler/syntax/src/res_printer.ml

+14-11
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ and print_module_binding ~state ~is_rec module_binding cmt_tbl i =
700700
match module_binding.pmb_expr with
701701
| {pmod_desc = Pmod_constraint (mod_expr, mod_type)}
702702
when not
703-
(ParsetreeViewer.has_await_attribute
703+
(ParsetreeViewer.has_await_attribute2
704704
module_binding.pmb_expr.pmod_attributes) ->
705705
( print_mod_expr ~state mod_expr cmt_tbl,
706706
Doc.concat [Doc.text ": "; print_mod_type ~state mod_type cmt_tbl] )
@@ -3432,9 +3432,10 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
34323432
Doc.concat [Doc.text "\""; member_doc; Doc.text "\""]
34333433
in
34343434
Doc.group (Doc.concat [parent_doc; Doc.lbracket; member; Doc.rbracket])
3435-
in
3436-
let expr_with_await =
3437-
if ParsetreeViewer.has_await_attribute e.pexp_attributes then
3435+
| Pexp_await e ->
3436+
let printed_expression =
3437+
print_expression_with_comments ~state e cmt_tbl
3438+
in
34383439
let rhs =
34393440
match
34403441
Parens.lazy_or_assert_or_await_expr_rhs ~in_await:true
@@ -3453,7 +3454,6 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
34533454
| Nothing -> printed_expression
34543455
in
34553456
Doc.concat [Doc.text "await "; rhs]
3456-
else printed_expression
34573457
in
34583458
let should_print_its_own_attributes =
34593459
match e.pexp_desc with
@@ -3467,11 +3467,11 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
34673467
| _ -> false
34683468
in
34693469
match e.pexp_attributes with
3470-
| [] -> expr_with_await
3470+
| [] -> printed_expression
34713471
| attrs when not should_print_its_own_attributes ->
34723472
Doc.group
3473-
(Doc.concat [print_attributes ~state attrs cmt_tbl; expr_with_await])
3474-
| _ -> expr_with_await
3473+
(Doc.concat [print_attributes ~state attrs cmt_tbl; printed_expression])
3474+
| _ -> printed_expression
34753475

34763476
and print_pexp_fun ~state ~in_callback e cmt_tbl =
34773477
let async, parameters, return_expr = ParsetreeViewer.fun_expr e in
@@ -3761,7 +3761,10 @@ and print_binary_expression ~state (expr : Parsetree.expression) cmt_tbl =
37613761
| _ -> add_parens doc
37623762
in
37633763
let is_await =
3764-
ParsetreeViewer.has_await_attribute expr.pexp_attributes
3764+
(match expr.pexp_desc with
3765+
| Pexp_await _ -> true
3766+
| _ -> false)
3767+
|| ParsetreeViewer.has_await_attribute expr
37653768
in
37663769
let doc =
37673770
if is_await then
@@ -5298,7 +5301,7 @@ and print_expression_block ~state ~braces expr cmt_tbl =
52985301
match mod_expr.pmod_desc with
52995302
| Pmod_constraint (mod_expr2, mod_type)
53005303
when not
5301-
(ParsetreeViewer.has_await_attribute mod_expr.pmod_attributes)
5304+
(ParsetreeViewer.has_await_attribute2 mod_expr.pmod_attributes)
53025305
->
53035306
let name =
53045307
Doc.concat
@@ -5792,7 +5795,7 @@ and print_mod_expr ~state mod_expr cmt_tbl =
57925795
| Pmod_functor _ -> print_mod_functor ~state mod_expr cmt_tbl
57935796
in
57945797
let doc =
5795-
if ParsetreeViewer.has_await_attribute mod_expr.pmod_attributes then
5798+
if ParsetreeViewer.has_await_attribute2 mod_expr.pmod_attributes then
57965799
match mod_expr.pmod_desc with
57975800
| Pmod_constraint _ ->
57985801
Doc.concat [Doc.text "await "; Doc.lparen; doc; Doc.rparen]

0 commit comments

Comments
 (0)