remove coroutine keyword
[hiphop-php.git] / hphp / hack / src / parser / smart_constructors / coroutine_smart_constructor.ml
blob1d6e8615d7d9d91fc37e52a7ab5b1475644d2d6a
1 (*
2 * Performs a regular parse, but determines whether the coroutine keyword
3 * appears in the file so that we know whether to lower coroutines through
4 * a conversion of the positioned syntax to an editable positioned syntax
5 *)
7 open Hh_prelude
8 module ParserEnv = Full_fidelity_parser_env
9 module TokenKind = Full_fidelity_token_kind
11 let ppl_macro_string = "__PPL"
13 module WithSyntax (Syntax : Positioned_syntax_sig.PositionedSyntax_S) = struct
14 module State = struct
15 type r = Syntax.t [@@deriving show]
17 type t = bool [@@deriving show]
19 let env : ParserEnv.t ref = ref @@ ParserEnv.make ()
21 let initial env' =
22 env := env';
23 false
25 let next state _ = state
26 end
28 module SyntaxSC_ = SyntaxSmartConstructors.WithSyntax (Syntax)
29 module SyntaxSC = SyntaxSC_.WithState (State)
31 include SyntaxSC.WithRustParser (struct
32 type r = Syntax.t
34 type t = bool
36 let rust_parse = Syntax.rust_parse_with_coroutine_sc
37 end)
39 let is_coroutine = Syntax.is_coroutine
41 let make_token token state =
42 let token =
43 if ParserEnv.codegen !State.env then
44 Token.with_trailing [] @@ Token.with_leading [] token
45 else
46 token
48 (state, Syntax.make_token token)
50 let make_function_declaration_header
51 modifiers r2 r3 r4 r5 r6 r7 r8 r9 r10 state =
52 let state =
53 state
54 || Syntax.syntax_node_to_list modifiers |> List.exists ~f:is_coroutine
56 ( state,
57 Syntax.make_function_declaration_header
58 modifiers
67 r10 )
69 let make_closure_type_specifier r1 r3 r4 r5 r6 r7 r8 r9 state =
70 (state, Syntax.make_closure_type_specifier r1 r3 r4 r5 r6 r7 r8 r9)
72 let make_anonymous_function r1 r2 r3 r5 r6 r7 r8 r9 r10 r11 r12 state =
73 (state, Syntax.make_anonymous_function r1 r2 r3 r5 r6 r7 r8 r9 r10 r11 r12)
75 let make_lambda_expression r1 r2 r3 r4 r5 state =
76 (state, Syntax.make_lambda_expression r1 r2 r3 r4 r5)
78 let make_awaitable_creation_expression r1 r2 r4 state =
79 (state, Syntax.make_awaitable_creation_expression r1 r2 r4)
81 let make_old_attribute_specification left attribute_name right state =
82 Syntax.(
83 let is_ppl_attribute_folder has_seen_ppl constructor_call =
84 if has_seen_ppl then
85 has_seen_ppl
86 else
87 match Syntax.syntax constructor_call with
88 | Syntax.ConstructorCall
90 constructor_call_type;
91 constructor_call_left_paren;
92 constructor_call_argument_list;
93 constructor_call_right_paren;
94 } ->
95 Syntax.is_missing constructor_call_left_paren
96 && Syntax.is_missing constructor_call_argument_list
97 && Syntax.is_missing constructor_call_right_paren
98 && Option.value_map
99 (Syntax.extract_text constructor_call_type)
100 ~default:false
101 ~f:(fun text -> String.equal text ppl_macro_string)
102 | _ -> false
104 let state =
105 state
106 || syntax_list_fold
107 ~init:false
108 ~f:is_ppl_attribute_folder
109 attribute_name
111 (state, Syntax.make_old_attribute_specification left attribute_name right))