@@ -12,23 +12,25 @@ open Jane_syntax_parsing
12
12
that both [comprehensions] and [immutable_arrays] are enabled. But our
13
13
general mechanism for checking for enabled extensions (in [of_ast]) won't
14
14
work well here: it triggers when converting from
15
- e.g. [[%jane.comprehensions.array] ...] to the comprehensions-specific
16
- AST. But if we spot a [[%jane.comprehensions.immutable]], there is no
17
- expression to translate. So we just check for the immutable arrays extension
18
- when processing a comprehension expression for an immutable array.
15
+ e.g. [[%jane.non_erasable.comprehensions.array] ...] to the
16
+ comprehensions-specific AST. But if we spot a
17
+ [[%jane.non_erasable.comprehensions.immutable]], there is no expression to
18
+ translate. So we just check for the immutable arrays extension when
19
+ processing a comprehension expression for an immutable array.
19
20
20
21
Note [Wrapping with make_entire_jane_syntax]
21
22
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22
23
23
24
The topmost node in the encoded AST must always look like e.g.
24
- [%jane.comprehensions]. This allows the decoding machinery to know what
25
- extension is being used and what function to call to do the decoding.
26
- Accordingly, during encoding, after doing the hard work of converting the
27
- extension syntax tree into e.g. Parsetree.expression, we need to make a final
28
- step of wrapping the result in an [%jane.xyz] node. Ideally, this step would
29
- be done by part of our general structure, like we separate [of_ast] and
30
- [of_ast_internal] in the decode structure; this design would make it
31
- structurally impossible/hard to forget taking this final step.
25
+ [%jane.non_erasable.comprehensions]. (More generally,
26
+ [%jane.ERASABILITY.FEATURE] or [@jane.ERASABILITY.FEATURE].) This allows the
27
+ decoding machinery to know what extension is being used and what function to
28
+ call to do the decoding. Accordingly, during encoding, after doing the hard
29
+ work of converting the extension syntax tree into e.g. Parsetree.expression,
30
+ we need to make a final step of wrapping the result in a [%jane.*.xyz] node.
31
+ Ideally, this step would be done by part of our general structure, like we
32
+ separate [of_ast] and [of_ast_internal] in the decode structure; this design
33
+ would make it structurally impossible/hard to forget taking this final step.
32
34
33
35
However, the final step is only one line of code (a call to
34
36
[make_entire_jane_syntax]), but yet the name of the feature varies, as does
@@ -43,7 +45,8 @@ module With_attributes = With_attributes
43
45
44
46
(* * List and array comprehensions *)
45
47
module Comprehensions = struct
46
- let extension_string = Language_extension. to_string Comprehensions
48
+ let feature : Feature.t = Language_extension Comprehensions
49
+ let extension_string = Feature. extension_component feature
47
50
48
51
type iterator =
49
52
| Range of { start : expression
@@ -93,7 +96,7 @@ module Comprehensions = struct
93
96
94
97
let comprehension_expr names x =
95
98
AST. wrap_desc Expression ~attrs: [] ~loc: x.pexp_loc @@
96
- AST. make_jane_syntax Expression (extension_string :: names) x
99
+ AST. make_jane_syntax Expression feature names x
97
100
98
101
(* * First, we define how to go from the nice AST to the OCaml AST; this is
99
102
the [expr_of_...] family of expressions, culminating in
@@ -142,7 +145,7 @@ module Comprehensions = struct
142
145
143
146
let expr_of ~loc cexpr =
144
147
(* See Note [Wrapping with make_entire_jane_syntax] *)
145
- AST. make_entire_jane_syntax Expression ~loc extension_string (fun () ->
148
+ AST. make_entire_jane_syntax Expression ~loc feature (fun () ->
146
149
match cexpr with
147
150
| Cexp_list_comprehension comp ->
148
151
expr_of_comprehension ~type_: [" list" ] comp
@@ -180,7 +183,7 @@ module Comprehensions = struct
180
183
Location. errorf ~loc
181
184
" Unknown, unexpected, or malformed@ comprehension embedded term %a"
182
185
Embedded_name. pp_quoted_name
183
- Embedded_name. (extension_string :: subparts)
186
+ ( Embedded_name. of_feature feature subparts)
184
187
| No_clauses ->
185
188
Location. errorf ~loc
186
189
" Tried to desugar a comprehension with no clauses"
@@ -200,11 +203,14 @@ module Comprehensions = struct
200
203
attribute removed. *)
201
204
let expand_comprehension_extension_expr expr =
202
205
match find_and_remove_jane_syntax_attribute expr.pexp_attributes with
203
- | Some (comprehensions :: names, attributes)
204
- when String. equal comprehensions extension_string ->
205
- names, { expr with pexp_attributes = attributes }
206
- | Some (ext_name , _ ) ->
207
- Desugaring_error. raise expr (Non_comprehension_embedding ext_name)
206
+ | Some (ext_name , attributes ) -> begin
207
+ match Jane_syntax_parsing.Embedded_name. components ext_name with
208
+ | comprehensions :: names
209
+ when String. equal comprehensions extension_string ->
210
+ names, { expr with pexp_attributes = attributes }
211
+ | _ :: _ ->
212
+ Desugaring_error. raise expr (Non_comprehension_embedding ext_name)
213
+ end
208
214
| None ->
209
215
Desugaring_error. raise expr Non_embedding
210
216
@@ -278,12 +284,12 @@ module Immutable_arrays = struct
278
284
type nonrec pattern =
279
285
| Iapat_immutable_array of pattern list
280
286
281
- let extension_string = Language_extension. to_string Immutable_arrays
287
+ let feature : Feature.t = Language_extension Immutable_arrays
282
288
283
289
let expr_of ~loc = function
284
290
| Iaexp_immutable_array elts ->
285
291
(* See Note [Wrapping with make_entire_jane_syntax] *)
286
- AST. make_entire_jane_syntax Expression ~loc extension_string (fun () ->
292
+ AST. make_entire_jane_syntax Expression ~loc feature (fun () ->
287
293
Ast_helper.Exp. array elts)
288
294
289
295
(* Returns remaining unconsumed attributes *)
@@ -294,7 +300,7 @@ module Immutable_arrays = struct
294
300
let pat_of ~loc = function
295
301
| Iapat_immutable_array elts ->
296
302
(* See Note [Wrapping with make_entire_jane_syntax] *)
297
- AST. make_entire_jane_syntax Pattern ~loc extension_string (fun () ->
303
+ AST. make_entire_jane_syntax Pattern ~loc feature (fun () ->
298
304
Ast_helper.Pat. array elts)
299
305
300
306
(* Returns remaining unconsumed attributes *)
@@ -311,13 +317,13 @@ module Include_functor = struct
311
317
type structure_item =
312
318
| Ifstr_include_functor of include_declaration
313
319
314
- let extension_string = Language_extension. to_string Include_functor
320
+ let feature : Feature.t = Language_extension Include_functor
315
321
316
322
let sig_item_of ~loc = function
317
323
| Ifsig_include_functor incl ->
318
324
(* See Note [Wrapping with make_entire_jane_syntax] *)
319
- AST. make_entire_jane_syntax Signature_item ~loc extension_string
320
- ( fun () -> Ast_helper.Sig. include_ incl)
325
+ AST. make_entire_jane_syntax Signature_item ~loc feature ( fun () ->
326
+ Ast_helper.Sig. include_ incl)
321
327
322
328
let of_sig_item sigi = match sigi.psig_desc with
323
329
| Psig_include incl -> Ifsig_include_functor incl
@@ -326,8 +332,8 @@ module Include_functor = struct
326
332
let str_item_of ~loc = function
327
333
| Ifstr_include_functor incl ->
328
334
(* See Note [Wrapping with make_entire_jane_syntax] *)
329
- AST. make_entire_jane_syntax Structure_item ~loc extension_string
330
- ( fun () -> Ast_helper.Str. include_ incl)
335
+ AST. make_entire_jane_syntax Structure_item ~loc feature ( fun () ->
336
+ Ast_helper.Str. include_ incl)
331
337
332
338
let of_str_item stri = match stri.pstr_desc with
333
339
| Pstr_include incl -> Ifstr_include_functor incl
@@ -339,15 +345,15 @@ module Strengthen = struct
339
345
type nonrec module_type =
340
346
{ mty : Parsetree .module_type ; mod_id : Longident .t Location .loc }
341
347
342
- let extension_string = Language_extension. to_string Module_strengthening
348
+ let feature : Feature.t = Language_extension Module_strengthening
343
349
344
350
(* Encoding: [S with M] becomes [functor (_ : S) -> (module M)], where
345
351
the [(module M)] is a [Pmty_alias]. This isn't syntax we can write, but
346
352
[(module M)] can be the inferred type for [M], so this should be fine. *)
347
353
348
354
let mty_of ~loc { mty; mod_id } =
349
355
(* See Note [Wrapping with make_entire_jane_syntax] *)
350
- AST. make_entire_jane_syntax Module_type ~loc extension_string (fun () ->
356
+ AST. make_entire_jane_syntax Module_type ~loc feature (fun () ->
351
357
Ast_helper.Mty. functor_ (Named (Location. mknoloc None , mty))
352
358
(Ast_helper.Mty. alias mod_id))
353
359
0 commit comments