Source file jane_syntax_parsing.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
[@@@ocaml.warning "-missing-record-field-pattern"]
open! Shadow_compiler_distribution
(** As mentioned in the .mli file, there are some gory details around the
particular translation scheme we adopt for moving to and from OCaml ASTs
([Parsetree.expression], etc.). The general idea is that we adopt a scheme
where each novel piece of syntax is represented using one of two embeddings:
1. As an AST item carrying an attribute. The AST item serves as the "body"
of the syntax indicated by the attribute.
2. As a pair of an extension node and an AST item that serves as the "body".
Here, the "pair" is embedded as a pair-like construct in the relevant AST
category, e.g. [include sig [%jane.ERASABILITY.EXTNAME];; BODY end] for
signature items.
In particular, for an language extension named [EXTNAME] (i.e., one that is
enabled by [-extension EXTNAME] on the command line), the attribute (if
used) must be [[@jane.ERASABILITY.EXTNAME]], and the extension node (if
used) must be [[%jane.ERASABILITY.EXTNAME]]. For built-in syntax, we use
[_builtin] instead of an language extension name.
The [ERASABILITY] component indicates to tools such as ocamlformat and
ppxlib whether or not the attribute is erasable. See the documentation of
[Erasability] for more information on how tools make use of this
information.
In the below example, we use attributes an examples, but it applies equally
to extensions. We also provide utilities for further desugaring similar
applications where the embeddings have the longer form
[[@jane.ERASABILITY.FEATNAME.ID1.ID2.….IDn]] (with the outermost one being
the [n = 0] case), as these might be used inside the [EXPR]. (For example,
within the outermost [[@jane.non_erasable.comprehensions]] term for list and
array comprehensions, we can also use
[[@jane.non_erasable.comprehensions.list]],
[[@jane.non_erasable.comprehensions.array]],
[[@jane.non_erasable.comprehensions.for.in]], etc.).
As mentioned, we represent terms as a "pair" and don't use the extension
node or attribute payload; this is so that ppxen can see inside these
extension nodes or attributes. If we put the subexpressions inside the
payload, then we couldn't write something like [[[%string "Hello, %{x}!"]
for x in names]], as [ppx_string] wouldn't traverse inside the payload to
find the [[%string]] extension node.
Our novel syntactic features are of course allowed to impose extra
constraints on what legal bodies are; we're also happy for this translation
to error in various ways on malformed input, since nobody should ever be
writing these forms directly. They're just an implementation detail.
See modules of type AST below to see how different syntactic categories
are represented. For example, expressions are encoded using an attribute.
We provide one module per syntactic category (e.g., [Expression]), of module
type [AST]. They also provide some simple machinery for working with the
general [@jane.ERASABILITY.FEATNAME.ID1.ID2.….IDn] wrapped forms. To
construct one, we provide [make_jane_syntax]; to destructure one, we provide
[match_jane_syntax] (which we expose via [make_of_ast]). Users of this
module still have to write the transformations in both directions for all
new syntax, lowering it to extension nodes or attributes and then lifting it
back out. *)
(** How did we choose between using the attribute embedding and the extension
node embedding for a particular syntactic category?
Generally, we prefer the attribute embedding: it's more compatible with
ppxes that aren't aware of Jane Syntax. (E.g., if a type looks like a tuple,
it truly is a tuple and not an extension node embedding.)
We can't apply the attribute embedding everywhere because some syntactic
categories, like structure items, don't carry attributes. For these, we
use extension nodes.
However, the attribute embedding is more inconvenient in some ways than
the extension node embedding. For example, the attribute embedding requires
callers to strip out Jane Syntax-related attributes from the attribute list
before processing it. We've tried to make this obvious from the signature
of, say, [Jane_syntax.Expression.of_ast], but this is somewhat more
inconvenient than just operating on the [expr_desc]. Nonetheless, because
of the advantages with ppxlib interoperability, we've opted for the
attribute embedding where possible.
*)
open Parsetree
(** We carefully regulate which bindings we import from [Language_extension]
to ensure that we can import this file into the Jane Street internal
repo with no changes.
*)
module Language_extension = struct
include Language_extension_kernel
include (
Language_extension : Language_extension_kernel.Language_extension_for_jane_syntax)
end
module Feature : sig
type t =
| Language_extension : _ Language_extension.t -> t
| Builtin
type error =
| Disabled_extension : _ Language_extension.t -> error
| Unknown_extension of string
val describe_uppercase : t -> string
val extension_component : t -> string
val of_component : string -> (t, error) result
val is_erasable : t -> bool
end = struct
type t =
| Language_extension : _ Language_extension.t -> t
| Builtin
type error =
| Disabled_extension : _ Language_extension.t -> error
| Unknown_extension of string
let builtin_component = "_builtin"
let describe_uppercase = function
| Language_extension ext ->
"The extension \"" ^ Language_extension.to_string ext ^ "\""
| Builtin -> "Built-in syntax"
;;
let extension_component = function
| Language_extension ext -> Language_extension.to_string ext
| Builtin -> builtin_component
;;
let of_component str =
if String.equal str builtin_component
then Ok Builtin
else (
match Language_extension.of_string str with
| Some (Pack ext) ->
if Language_extension.is_enabled ext
then Ok (Language_extension ext)
else Error (Disabled_extension ext)
| None -> Error (Unknown_extension str))
;;
let is_erasable = function
| Language_extension ext -> Language_extension.is_erasable ext
| Builtin -> true
;;
end
(** Was this embedded as an [[%extension_node]] or an [[@attribute]]? Not
exported. Used only for error messages. *)
module Embedding_syntax = struct
type t =
| Extension_node
| Attribute
let name = function
| Extension_node -> "extension node"
| Attribute -> "attribute"
;;
let name_indefinite = function
| Extension_node -> "an extension node"
| Attribute -> "an attribute"
;;
let name_plural = function
| Extension_node -> "extension nodes"
| Attribute -> "attributes"
;;
let pp ppf (t, name) =
let sigil =
match t with
| Extension_node -> "%"
| Attribute -> "@"
in
Format_doc.fprintf ppf "[%s%s]" sigil name
;;
end
module Misnamed_embedding_error = struct
type t =
| No_erasability
| No_feature
| Unknown_erasability of string
let to_string = function
| No_erasability -> "Missing erasability and feature components"
| No_feature -> "Missing a feature component"
| Unknown_erasability str ->
Printf.sprintf "Unrecognized component where erasability was expected: `%s'" str
;;
end
(** The component of an attribute or extension name that identifies whether or
not the embedded syntax is *erasable*; that is, whether or not the
upstream OCaml compiler can safely interpret the AST while ignoring the
attribute or extension. (This means that syntax encoded as extension
nodes should always be non-erasable.) Tools that consume the parse tree
we generate can make use of this information; for instance, ocamlformat
will use it to guide how we present code that can be run with both our
compiler and the upstream compiler, and ppxlib can use it to decide
whether it's ok to allow ppxes to construct syntax that uses this
emedding. In particular, the upstream version of ppxlib will allow ppxes
to produce [[@jane.erasable.*]] attributes, but will report an error if a
ppx produces a [[@jane.non_erasable.*]] attribute.
As mentioned above, unlike for attributes, the erasable/non-erasable
distinction is not meaningful for extension nodes, as the compiler will
always error if it sees an uninterpreted extension node. So, for purposes
of tools in the wider OCaml ecosystem, it is irrelevant whether embeddings
that use extension nodes indicate [Erasable] or [Non_erasable] for this
component, but the semantically correct choice and the one we've settled
on is to use [Non_erasable]. *)
module Erasability = struct
type t =
| Erasable
| Non_erasable
let to_string = function
| Erasable -> "erasable"
| Non_erasable -> "non_erasable"
;;
let of_string = function
| "erasable" -> Ok Erasable
| "non_erasable" -> Ok Non_erasable
| _ -> Error ()
;;
end
(** An AST-style representation of the names used when generating extension
nodes or attributes for modular syntax; see the .mli file for more
details. *)
module Embedded_name : sig
(** A nonempty list of name components, without the first two components.
(That is, without the leading root component that identifies it as part of
the modular syntax mechanism, and without the next component that
identifies the erasability.) See the .mli file for more details. *)
type components = ( :: ) of string * string list
type t =
{ erasability : Erasability.t
; components : components
}
(** See the mli. *)
val of_feature : Feature.t -> string list -> t
val components : t -> components
(** See the mli. *)
val to_string : t -> string
(** Parse a Jane syntax name from the OCaml AST, either as the name of an
extension node or an attribute:
- [Some (Ok _)] if it's a legal Jane-syntax name;
- [Some (Error _)] if the root is present, but the name has fewer than 3
components or the erasability component is malformed; and
- [None] if it doesn't start with the leading root name and isn't part
of our Jane-syntax machinery.
Not exposed. *)
val of_string : string -> (t, Misnamed_embedding_error.t) result option
(** Print out the embedded form of a Jane-syntax name, in quotes; for use in
error messages. *)
val pp_quoted_name : Format_doc.formatter -> t -> unit
(** Print out an empty extension node or attribute with a Jane-syntax name,
accompanied by an indefinite article; for use in error messages. Not
exposed. *)
val pp_a_term : Format_doc.formatter -> Embedding_syntax.t * t -> unit
end = struct
(** The three parameters that control how we encode Jane-syntax extension node
names. When updating these, update comments that refer to them by their
contents! *)
module Config = struct
(** The separator between name components *)
let separator = '.'
(** The leading namespace that identifies this extension node or attribute
as reserved for a piece of modular syntax *)
let root = "jane"
(** For printing purposes, the appropriate indefinite article for [root] *)
let article = "a"
end
include Config
let separator_str = String.make 1 separator
type components = ( :: ) of string * string list
type t =
{ erasability : Erasability.t
; components : components
}
let of_feature feature trailing_components =
let feature_component = Feature.extension_component feature in
let erasability : Erasability.t =
if Feature.is_erasable feature then Erasable else Non_erasable
in
{ erasability; components = feature_component :: trailing_components }
;;
let components t = t.components
let to_string { erasability; components = feat :: subparts } =
String.concat
separator_str
(root :: Erasability.to_string erasability :: feat :: subparts)
;;
let of_string str : (t, Misnamed_embedding_error.t) result option =
match String.split_on_char separator str with
| root' :: parts when String.equal root root' ->
(match parts with
| [] -> Some (Error No_erasability)
| [ _ ] -> Some (Error No_feature)
| erasability :: feat :: subparts ->
(match Erasability.of_string erasability with
| Ok erasability -> Some (Ok { erasability; components = feat :: subparts })
| Error () -> Some (Error (Unknown_erasability erasability))))
| _ :: _ | [] -> None
;;
let pp_quoted_name ppf t = Format_doc.fprintf ppf "\"%s\"" (to_string t)
let pp_a_term ppf (esyn, t) =
Format_doc.fprintf ppf "%s %a" article Embedding_syntax.pp (esyn, to_string t)
;;
end
module Error = struct
(** An error triggered when desugaring a language extension from an OCaml
AST; should always be fatal *)
type error =
| Introduction_has_payload of Embedding_syntax.t * Embedded_name.t * payload
| Unknown_extension of Embedding_syntax.t * Erasability.t * string
| Disabled_extension :
{ ext : _ Language_extension.t
; maturity : Language_extension.maturity option
}
-> error
| Wrong_syntactic_category of Feature.t * string
| Misnamed_embedding of Misnamed_embedding_error.t * string * Embedding_syntax.t
| Bad_introduction of Embedding_syntax.t * Embedded_name.t
(** The exception type thrown when desugaring a piece of modular syntax from
an OCaml AST *)
exception Error of Location.t * error
end
open Error
let assert_extension_enabled (type a) ~loc (ext : a Language_extension.t) (setting : a) =
if not (Language_extension.is_at_least ext setting)
then (
let maturity : Language_extension.maturity option =
match ext with
| Layouts -> Some (setting : Language_extension.maturity)
| _ -> None
in
raise (Error (loc, Disabled_extension { ext; maturity })))
;;
let report_error ~loc = function
| Introduction_has_payload (what, name, _payload) ->
Location.errorf
~loc
"@[Modular syntax %s are not allowed to have a payload,@ but %a does@]"
(Embedding_syntax.name_plural what)
Embedded_name.pp_quoted_name
name
| Unknown_extension (what, erasability, name) ->
let embedded_name = { Embedded_name.erasability; components = [ name ] } in
Location.errorf
~loc
"@[Unknown extension \"%s\" referenced via@ %a %s@]"
name
Embedded_name.pp_a_term
(what, embedded_name)
(Embedding_syntax.name what)
| Disabled_extension { ext; maturity } ->
(match maturity with
| None ->
Location.errorf
~loc
"The extension \"%s\" is disabled and cannot be used"
(Language_extension.to_string ext)
| Some maturity ->
Location.errorf
~loc
"This construct requires the %s version of the extension \"%s\", which is \
disabled and cannot be used"
(Language_extension.maturity_to_string maturity)
(Language_extension.to_string ext))
| Wrong_syntactic_category (feat, cat) ->
Location.errorf ~loc "%s cannot appear in %s" (Feature.describe_uppercase feat) cat
| Misnamed_embedding (err, name, what) ->
Location.errorf
~loc
"Cannot have %s named %a: %s"
(Embedding_syntax.name_indefinite what)
Embedding_syntax.pp
(what, name)
(Misnamed_embedding_error.to_string err)
| Bad_introduction (what, ({ components = ext :: _; _ } as name)) ->
Location.errorf
~loc
"@[The extension \"%s\" was referenced improperly; it started with@ %a %s,@ not %a \
one@]"
ext
Embedded_name.pp_a_term
(what, name)
(Embedding_syntax.name what)
Embedded_name.pp_a_term
(what, { name with components = [ ext ] })
;;
let () =
Location.register_error_of_exn (function
| Error (loc, err) -> Some (report_error ~loc err)
| _ -> None)
;;
(** Generically find and create the OCaml AST syntax used to encode one of our
novel syntactic features. One module per variety of AST (expressions,
patterns, etc.). *)
(** The parameters that define how to look for [[%jane.*.FEATNAME]] and
[[@jane.*.FEATNAME]] inside ASTs of a certain syntactic category. This
module type describes the input to the [Make_with_attribute] and
[Make_with_extension_node] functors (though they stipulate additional
requirements for their inputs).
*)
module type AST_syntactic_category = sig
(** The AST type (e.g., [Parsetree.expression]) *)
type ast
(** The name for this syntactic category in the plural form; used for error
messages (e.g., "expressions") *)
val plural : string
(** How to get the location attached to an AST node. Should just be
[fun tm -> tm.pCAT_loc] for the appropriate syntactic category [CAT]. *)
val location : ast -> Location.t
(** Set the location of an AST node. *)
val with_location : ast -> Location.t -> ast
end
module type AST_internal = sig
include AST_syntactic_category
val embedding_syntax : Embedding_syntax.t
val make_jane_syntax : Embedded_name.t -> ?payload:payload -> ast -> ast
(** Given an AST node, check if it's a representation of a term from one of
our novel syntactic features; if it is, split it back up into its name,
the location of the extension/attribute, any payload, and the body. If
the embedded term is malformed in any way, raises an error; if the input
isn't an embedding of one of our novel syntactic features, returns [None].
Partial inverse of [make_jane_syntax]. *)
val match_jane_syntax
: ast
-> (Embedded_name.t * Location.t * Parsetree.payload * ast) option
end
let parse_embedding_exn ~loc ~name ~embedding_syntax =
let raise_error err = raise (Error (loc, err)) in
match Embedded_name.of_string name with
| Some (Ok name) -> Some name
| Some (Error err) -> raise_error (Misnamed_embedding (err, name, embedding_syntax))
| None -> None
;;
let find_and_remove_jane_syntax_attribute =
let rec loop ~rev_prefix ~suffix =
match rev_prefix with
| [] -> None
| attr :: rev_prefix ->
let { attr_name = { txt = name; loc = attr_loc }; attr_payload } = attr in
(match parse_embedding_exn ~loc:attr_loc ~name ~embedding_syntax:Attribute with
| None -> loop ~rev_prefix ~suffix:(attr :: suffix)
| Some name ->
let unconsumed_attributes = List.rev_append rev_prefix suffix in
Some (name, attr_loc, attr_payload, unconsumed_attributes))
in
fun attributes -> loop ~rev_prefix:(List.rev attributes) ~suffix:[]
;;
let make_jane_syntax_attribute name payload =
{ attr_name = { txt = Embedded_name.to_string name; loc = !Ast_helper.default_loc }
; attr_loc = !Ast_helper.default_loc
; attr_payload = payload
}
;;
(** For a syntactic category, produce translations into and out of
our novel syntax, using parsetree attributes as the encoding.
*)
module Make_with_attribute (AST_syntactic_category : sig
include AST_syntactic_category
val attributes : ast -> attributes
val with_attributes : ast -> attributes -> ast
end) : AST_internal with type ast = AST_syntactic_category.ast = struct
include AST_syntactic_category
let embedding_syntax = Embedding_syntax.Attribute
let make_jane_syntax name ?(payload = PStr []) ast =
let attr = make_jane_syntax_attribute name payload in
with_attributes ast (attributes ast @ [ attr ])
;;
let match_jane_syntax ast =
match find_and_remove_jane_syntax_attribute (attributes ast) with
| None -> None
| Some (name, loc, payload, attrs) ->
Some (name, loc, payload, with_attributes ast attrs)
;;
end
(** For a syntactic category, produce translations into and out of
our novel syntax, using extension nodes as the encoding.
*)
module Make_with_extension_node (AST_syntactic_category : sig
include AST_syntactic_category
(** How to construct an extension node for this AST (something of the
shape [[%name]]). Should just be [Ast_helper.CAT.extension] for the
appropriate syntactic category [CAT]. (This means that [?loc] should
default to [!Ast_helper.default_loc.].) *)
val make_extension_node : ?loc:Location.t -> ?attrs:attributes -> extension -> ast
(** Given an extension node (as created by [make_extension_node]) with an
appropriately-formed name and a body, combine them into the special
syntactic form we use for novel syntactic features in this syntactic
category. Partial inverse of [match_extension_use]. *)
val make_extension_use : extension_node:ast -> ast -> ast
(** Given an AST node, check if it's of the special syntactic form
indicating that this is one of our novel syntactic features (as
created by [make_extension_node]), split it back up into the extension
node and the possible body. Doesn't do any checking about the
name/format of the extension or the possible body terms (for which see
[AST.match_extension]). Partial inverse of [make_extension_use]. *)
val match_extension_use : ast -> (extension * ast) option
end) : AST_internal with type ast = AST_syntactic_category.ast = struct
include AST_syntactic_category
let embedding_syntax = Embedding_syntax.Extension_node
let make_jane_syntax name ?(payload = PStr []) ast =
make_extension_use
ast
~extension_node:
(make_extension_node
({ txt = Embedded_name.to_string name; loc = !Ast_helper.default_loc }, payload))
;;
let match_jane_syntax ast =
match match_extension_use ast with
| None -> None
| Some (({ txt = name; loc = ext_loc }, ext_payload), body) ->
(match parse_embedding_exn ~loc:ext_loc ~name ~embedding_syntax with
| None -> None
| Some name -> Some (name, ext_loc, ext_payload, body))
;;
end
(** The AST parameters for every subset of types; embedded with attributes. *)
module Type_AST_syntactic_category = struct
type ast = core_type
let location typ = typ.ptyp_loc
let with_location typ l = { typ with ptyp_loc = l }
let attributes typ = typ.ptyp_attributes
let with_attributes typ ptyp_attributes = { typ with ptyp_attributes }
end
(** Types; embedded with attributes. *)
module Core_type0 = Make_with_attribute (struct
include Type_AST_syntactic_category
let plural = "types"
end)
(** Constructor arguments; the same as types, but used in fewer places *)
module Constructor_argument0 = Make_with_attribute (struct
include Type_AST_syntactic_category
let plural = "constructor arguments"
end)
(** Expressions; embedded using an attribute on the expression. *)
module Expression0 = Make_with_attribute (struct
type ast = expression
let plural = "expressions"
let location expr = expr.pexp_loc
let with_location expr l = { expr with pexp_loc = l }
let attributes expr = expr.pexp_attributes
let with_attributes expr pexp_attributes = { expr with pexp_attributes }
end)
(** Patterns; embedded using an attribute on the pattern. *)
module Pattern0 = Make_with_attribute (struct
type ast = pattern
let plural = "patterns"
let location pat = pat.ppat_loc
let with_location pat l = { pat with ppat_loc = l }
let attributes pat = pat.ppat_attributes
let with_attributes pat ppat_attributes = { pat with ppat_attributes }
end)
(** Module types; embedded using an attribute on the module type. *)
module Module_type0 = Make_with_attribute (struct
type ast = module_type
let plural = "module types"
let location mty = mty.pmty_loc
let with_location mty l = { mty with pmty_loc = l }
let attributes mty = mty.pmty_attributes
let with_attributes mty pmty_attributes = { mty with pmty_attributes }
end)
(** Extension constructors; embedded using an attribute. *)
module Extension_constructor0 = Make_with_attribute (struct
type ast = extension_constructor
let plural = "extension constructors"
let location ext = ext.pext_loc
let with_location ext l = { ext with pext_loc = l }
let attributes ext = ext.pext_attributes
let with_attributes ext pext_attributes = { ext with pext_attributes }
end)
(** Signature items; embedded as
[include sig [%%extension.EXTNAME];; BODY end]. Signature items don't have
attributes or we'd use them instead.
*)
module Signature_item0 = Make_with_extension_node (struct
type ast = signature_item
let plural = "signature items"
let location sigi = sigi.psig_loc
let with_location sigi l = { sigi with psig_loc = l }
let make_extension_node = Ast_helper.Sig.extension
let make_extension_use ~extension_node sigi =
Ast_helper.Sig.include_
{ pincl_mod = Ast_helper.Mty.signature [ extension_node; sigi ]
; pincl_loc = !Ast_helper.default_loc
; pincl_attributes = []
}
;;
let match_extension_use sigi =
match sigi.psig_desc with
| Psig_include
{ pincl_mod =
{ pmty_desc =
Pmty_signature [ { psig_desc = Psig_extension (ext, []); _ }; sigi ]
; _
}
; _
} -> Some (ext, sigi)
| _ -> None
;;
end)
(** Structure items; embedded as
[include struct [%%extension.EXTNAME];; BODY end]. Structure items don't
have attributes or we'd use them instead.
*)
module Structure_item0 = Make_with_extension_node (struct
type ast = structure_item
let plural = "structure items"
let location stri = stri.pstr_loc
let with_location stri l = { stri with pstr_loc = l }
let make_extension_node = Ast_helper.Str.extension
let make_extension_use ~extension_node stri =
Ast_helper.Str.include_
{ pincl_mod = Ast_helper.Mod.structure [ extension_node; stri ]
; pincl_loc = !Ast_helper.default_loc
; pincl_attributes = []
}
;;
let match_extension_use stri =
match stri.pstr_desc with
| Pstr_include
{ pincl_mod =
{ pmod_desc =
Pmod_structure [ { pstr_desc = Pstr_extension (ext, []); _ }; stri ]
; _
}
; _
} -> Some (ext, stri)
| _ -> None
;;
end)
(** Constructor declarations; embedded with attributes. *)
module Constructor_declaration0 = Make_with_attribute (struct
type ast = Parsetree.constructor_declaration
let plural = "constructor declarations"
let location pcd = pcd.pcd_loc
let with_location pcd loc = { pcd with pcd_loc = loc }
let attributes pcd = pcd.pcd_attributes
let with_attributes pcd pcd_attributes = { pcd with pcd_attributes }
end)
(** Type declarations; embedded with attributes. *)
module Type_declaration0 = Make_with_attribute (struct
type ast = Parsetree.type_declaration
let plural = "type declarations"
let location ptype = ptype.ptype_loc
let with_location ptype loc = { ptype with ptype_loc = loc }
let attributes ptype = ptype.ptype_attributes
let with_attributes ptype ptype_attributes = { ptype with ptype_attributes }
end)
module type AST = sig
type ast
val make_jane_syntax : Feature.t -> string list -> ?payload:payload -> ast -> ast
val make_entire_jane_syntax : loc:Location.t -> Feature.t -> (unit -> ast) -> ast
val make_of_ast : of_ast_internal:(Feature.t -> ast -> 'a option) -> ast -> 'a option
end
let = function
| Feature.Language_extension Mode -> false
| _ -> true
;;
module Make_ast (AST : AST_internal) : AST with type ast = AST.ast = struct
include AST
let make_jane_syntax feature trailing_components ?payload ast =
AST.make_jane_syntax
(Embedded_name.of_feature feature trailing_components)
?payload
ast
;;
let make_entire_jane_syntax ~loc feature ast =
AST.with_location
(Ast_helper.with_default_loc { loc with loc_ghost = true } (fun () ->
make_jane_syntax feature [] (ast ())))
loc
;;
(** Generically lift our custom ASTs for our novel syntax from OCaml ASTs. *)
let make_of_ast ~of_ast_internal =
let of_ast ast =
let loc = AST.location ast in
let raise_error loc err = raise (Error (loc, err)) in
match AST.match_jane_syntax ast with
| Some
( ({ erasability; components = [ name ] } as embedded_name)
, syntax_loc
, payload
, ast ) ->
(match Feature.of_component name with
| Ok feat ->
if needs_extra_checks feat
then (
match payload with
| PStr [] -> ()
| _ ->
raise_error
syntax_loc
(Introduction_has_payload (AST.embedding_syntax, embedded_name, payload)));
(match of_ast_internal feat ast with
| Some ext_ast -> Some ext_ast
| None ->
if needs_extra_checks feat
then raise_error loc (Wrong_syntactic_category (feat, AST.plural))
else None)
| Error err ->
raise_error
loc
(match err with
| Disabled_extension ext -> Disabled_extension { ext; maturity = None }
| Unknown_extension name ->
Unknown_extension (AST.embedding_syntax, erasability, name)))
| Some (({ components = _ :: _ :: _; _ } as name), _, _, _) ->
raise_error loc (Bad_introduction (AST.embedding_syntax, name))
| None -> None
in
of_ast
;;
end
let make_jane_syntax_attribute feature trailing_components payload =
make_jane_syntax_attribute
(Embedded_name.of_feature feature trailing_components)
payload
;;
module Expression = Make_ast (Expression0)
module Pattern = Make_ast (Pattern0)
module Module_type = Make_ast (Module_type0)
module Signature_item = Make_ast (Signature_item0)
module Structure_item = Make_ast (Structure_item0)
module Core_type = Make_ast (Core_type0)
module Constructor_argument = Make_ast (Constructor_argument0)
module Extension_constructor = Make_ast (Extension_constructor0)
module Constructor_declaration = Make_ast (Constructor_declaration0)
module Type_declaration = Make_ast (Type_declaration0)