Skip to content

Commit

Permalink
more fix for async
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Oct 3, 2023
1 parent 4b17dfb commit 9302343
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
16 changes: 16 additions & 0 deletions compiler/lib/js_parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ decl:
{ let i,f = $1 in Function_declaration (i,f), p $symbolstartpos }
| generator_decl
{ let i,f = $1 in Function_declaration (i,f), p $symbolstartpos }
| async_generator_decl
{ let i,f = $1 in Function_declaration (i,f), p $symbolstartpos }
| async_decl
{ let i,f = $1 in Function_declaration (i,f), p $symbolstartpos }
| lexical_decl { $1, p $symbolstartpos }
Expand Down Expand Up @@ -432,6 +434,18 @@ async_function_expr:
| T_ASYNC T_FUNCTION name=ident? args=call_signature "{" b=function_body "}"
{ EFun (name, ({async = true; generator = false}, args, b, p $symbolstartpos)) }

(*************************************************************************)
(* async generators *)
(*************************************************************************)

async_generator_decl:
| T_ASYNC T_FUNCTION "*" name=ident args=call_signature "{" b=function_body "}"
{ (name, ({async = true; generator = true}, args, b, p $symbolstartpos)) }

async_generator_expr:
| T_ASYNC T_FUNCTION "*" name=ident? args=call_signature "{" b=function_body "}"
{ EFun (name, ({async = true; generator = true}, args, b, p $symbolstartpos)) }

(*************************************************************************)
(* Class declaration *)
(*************************************************************************)
Expand Down Expand Up @@ -766,6 +780,7 @@ primary_with_stmt:
| generator_expr { $1 }
(* es7: *)
| async_function_expr { $1 }
| async_generator_expr{ $1 }


primary_expr_no_braces:
Expand Down Expand Up @@ -988,6 +1003,7 @@ primary_for_consise_body:
| generator_expr { $1 }
(* es7: *)
| async_function_expr { $1 }
| async_generator_expr{ $1 }

assignment_expr_for_consise_body:
| conditional_expr(primary_for_consise_body) { $1 }
Expand Down
7 changes: 5 additions & 2 deletions compiler/lib/parse_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,12 @@ let rec offer_one t (lexbuf : Lexer.t) =
* one LineTerminator, then a semicolon is automatically inserted before the
* restricted token. *)
match State.Cursor.last_token h, tok with
| ( Some (((T_RETURN | T_CONTINUE | T_BREAK | T_THROW | T_YIELD), _, _), _)
| ( Some
(((T_RETURN | T_CONTINUE | T_BREAK | T_THROW | T_YIELD | T_ASYNC), _, _), _)
, (((T_SEMICOLON | T_VIRTUAL_SEMICOLON), _, _) as tok) ) -> tok
| Some (((T_RETURN | T_CONTINUE | T_BREAK | T_THROW | T_YIELD), _, _), _), _
| ( Some
(((T_RETURN | T_CONTINUE | T_BREAK | T_THROW | T_YIELD | T_ASYNC), _, _), _)
, _ )
when nl_separated h tok && acceptable t T_VIRTUAL_SEMICOLON ->
(* restricted token can also appear as regular identifier such
as in [x.return]. In such case, feeding a virtual semicolon
Expand Down
44 changes: 42 additions & 2 deletions compiler/tests-compiler/js_parser_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,20 @@ let%expect_test "async/await" =
const glslang = await glslangModule.default();
return glslang.compileGLSL(src, "compute");
}


async
function test() { }

async function test() { }

async
function* test() { }

async function * test() { }

1 + async function * test() { }

|};

[%expect
Expand All @@ -355,7 +369,14 @@ let%expect_test "async/await" =
glslang = await /*<<fake:7:33>>*/ glslangModule.default();
/*<<fake:8:11>>*/ return /*<<fake:8:18>>*/ glslang.compileGLSL
(src, "compute");
/*<<fake:2:9>>*/ } |}]
/*<<fake:2:9>>*/ }
/*<<fake:12:4>>*/ async;
/*<<fake:13:4>>*/ function test(){ /*<<fake:13:22>>*/ }
/*<<fake:15:4>>*/ async function test(){ /*<<fake:15:4>>*/ }
/*<<fake:17:4>>*/ async;
/*<<fake:18:4>>*/ function* test(){ /*<<fake:18:4>>*/ }
/*<<fake:20:4>>*/ async function* test(){ /*<<fake:20:4>>*/ }
/*<<fake:22:4>>*/ 1 + async function* test(){ /*<<fake:22:8>>*/ }; |}]

let%expect_test "get/set property" =
(* GH#1017 *)
Expand Down Expand Up @@ -948,6 +969,18 @@ a:while(true){
do { x } while (true) y
do ; while (true) y

async
function test() { }

async function test() { }

async
function* test() { }

async function * test() { }

1 + async function * test() { }

|};
[%expect
{|
Expand All @@ -974,7 +1007,14 @@ a:while(true){
26: 4:a (identifier), 6:=, 8:b (identifier), 10:+, 12:c (identifier),
27: 4:(, 5:d (identifier), 7:+, 9:e (identifier), 10:), 11:., 12:print (identifier), 17:(, 18:), 0:; (virtual),
29: 4:do, 7:{, 9:x (identifier), 0:; (virtual), 11:}, 13:while, 19:(, 20:true, 24:), 0:; (virtual), 26:y (identifier), 0:; (virtual),
30: 4:do, 7:;, 9:while, 15:(, 16:true, 20:), 0:; (virtual), 22:y (identifier), 0:; (virtual), |}]
30: 4:do, 7:;, 9:while, 15:(, 16:true, 20:), 0:; (virtual), 22:y (identifier), 0:; (virtual),
32: 4:async, 0:; (virtual),
33: 4:function, 13:test (identifier), 17:(, 18:), 20:{, 22:},
35: 4:async, 10:function, 19:test (identifier), 23:(, 24:), 26:{, 28:},
37: 4:async, 0:; (virtual),
38: 4:function, 12:*, 14:test (identifier), 18:(, 19:), 21:{, 23:},
40: 4:async, 10:function, 19:*, 21:test (identifier), 25:(, 26:), 28:{, 30:},
42: 4:1, 6:+, 8:async, 14:function, 23:*, 25:test (identifier), 29:(, 30:), 32:{, 34:}, 0:; (virtual), |}]

let%expect_test _ =
parse_print_token
Expand Down

0 comments on commit 9302343

Please sign in to comment.