Skip to content

Commit 07127fe

Browse files
authored
Remove raw_body from modular extensions setup (#137)
This worsens the error message in a case that should never arise in practice, but it simplifies the process for adding new AST types.
1 parent 3f9bd64 commit 07127fe

File tree

3 files changed

+12
-63
lines changed

3 files changed

+12
-63
lines changed

parsing/extensions_parsing.ml

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ open Parsetree
4545
module Error = struct
4646
type malformed_extension =
4747
| Has_payload of payload
48-
| Wrong_arguments of (Asttypes.arg_label * expression) list
49-
| Wrong_tuple of pattern list
5048

5149
type error =
5250
| Malformed_extension of string list * malformed_extension
@@ -76,23 +74,6 @@ let report_error ~loc = function
7674
"@[Modular extension nodes are not allowed to have a payload,@ \
7775
but \"%s\" does@]"
7876
name
79-
| Wrong_arguments arguments ->
80-
Location.errorf
81-
~loc
82-
"@[Expression modular extension nodes must be applied to exactly@ \
83-
one unlabeled argument, but \"%s\" was applied to@ %s@]"
84-
name
85-
(match arguments with
86-
| [Labelled _, _] -> "a labeled argument"
87-
| [Optional _, _] -> "an optional argument"
88-
| _ -> Int.to_string (List.length arguments) ^ " arguments")
89-
| Wrong_tuple patterns ->
90-
Location.errorf
91-
~loc
92-
"@[Pattern modular extension nodes must be the first component of@ \
93-
a pair, but \"%s\" was the first component of a %d-tuple@]"
94-
name
95-
(1 + List.length patterns)
9677
end
9778
| Unknown_extension name ->
9879
Location.errorf
@@ -147,12 +128,6 @@ module type AST_parameters = sig
147128
[Parsetree.expression_desc]) *)
148129
type ast_desc
149130

150-
(** The type of the subterms that occur in the "body" slot of an extension
151-
use. This may just be [ast], but e.g. for expressions, we use function
152-
applications, and the terms that a function is applied to contain label
153-
information. *)
154-
type raw_body
155-
156131
(** The name for this syntactic category in the plural form; used for error
157132
messages *)
158133
val plural : string
@@ -180,19 +155,11 @@ module type AST_parameters = sig
180155

181156
(** Given an AST node, check if it's of the special syntactic form indicating
182157
that this is a language extension (as created by [make_extension_node]),
183-
split it back up into the extension node and the possible body terms.
158+
split it back up into the extension node and the possible body.
184159
Doesn't do any checking about the name/format of the extension or the
185160
possible body terms (see [AST.match_extension]). Partial inverse of
186161
[make_extension_use]. *)
187-
val match_extension_use : ast -> (extension * raw_body list) option
188-
189-
(** Check if a [raw_body] term is legal to use as a body *)
190-
val validate_extension_body : raw_body -> ast option
191-
192-
(** The error to throw when the list of possible body terms is wrong: either
193-
when the list isn't exactly one term long, or when that single term fails
194-
[validate_extension_body] *)
195-
val malformed_extension : raw_body list -> malformed_extension
162+
val match_extension_use : ast -> (extension * ast) option
196163
end
197164

198165
module type AST = sig
@@ -241,23 +208,19 @@ module Make_AST (AST_parameters : AST_parameters) :
241208
1. The [[%extension.NAME]] extension point has a payload; extensions must
242209
be empty, so other ppxes can traverse "into" them.
243210
244-
2. The [[%extension.NAME]] extension point contains multiple body forms,
245-
or body forms that are "shaped" incorrectly. *)
211+
2. The [[%extension.NAME]] extension point contains
212+
body forms that are "shaped" incorrectly. *)
246213
let match_extension ast =
247214
match match_extension_use ast with
248-
| Some (({txt = ext_name; loc = ext_loc}, ext_payload), body_list) ->
215+
| Some (({txt = ext_name; loc = ext_loc}, ext_payload), body) ->
249216
begin
250217
match String.split_on_char '.' ext_name with
251218
| "extension" :: names when uniformly_handled_extension names -> begin
252219
let raise_malformed err =
253220
raise (Error(ext_loc, Malformed_extension(names, err)))
254221
in
255222
match ext_payload with
256-
| PStr [] -> begin
257-
match List.map validate_extension_body body_list with
258-
| [Some body] -> Some (names, body)
259-
| _ -> raise_malformed (malformed_extension body_list)
260-
end
223+
| PStr [] -> Some (names, body)
261224
| _ -> raise_malformed (Has_payload ext_payload)
262225
end
263226
| _ -> None
@@ -269,7 +232,6 @@ end
269232
module Expression = Make_AST(struct
270233
type ast = expression
271234
type ast_desc = expression_desc
272-
type raw_body = Asttypes.arg_label * expression (* Function arguments *)
273235

274236
let plural = "expressions"
275237

@@ -284,23 +246,17 @@ module Expression = Make_AST(struct
284246

285247
let match_extension_use expr =
286248
match expr.pexp_desc with
287-
| Pexp_apply({pexp_desc = Pexp_extension ext; _}, arguments) ->
288-
Some (ext, arguments)
249+
| Pexp_apply({pexp_desc = Pexp_extension ext; _},
250+
[Asttypes.Nolabel, body]) ->
251+
Some (ext, body)
289252
| _ ->
290253
None
291-
292-
let validate_extension_body = function
293-
| Asttypes.Nolabel, body -> Some body
294-
| _, _ -> None
295-
296-
let malformed_extension args = Wrong_arguments args
297254
end)
298255

299256
(** Patterns; embedded as [[%extension.EXTNAME], BODY]. *)
300257
module Pattern = Make_AST(struct
301258
type ast = pattern
302259
type ast_desc = pattern_desc
303-
type raw_body = pattern
304260

305261
let plural = "patterns"
306262

@@ -315,13 +271,10 @@ module Pattern = Make_AST(struct
315271

316272
let match_extension_use pat =
317273
match pat.ppat_desc with
318-
| Ppat_tuple({ppat_desc = Ppat_extension ext; _} :: patterns) ->
319-
Some (ext, patterns)
274+
| Ppat_tuple([{ppat_desc = Ppat_extension ext; _}; pattern]) ->
275+
Some (ext, pattern)
320276
| _ ->
321277
None
322-
323-
let validate_extension_body = Option.some
324-
let malformed_extension pats = Wrong_tuple pats
325278
end)
326279

327280
(******************************************************************************)

parsing/extensions_parsing.mli

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ module Error : sig
7777
(** Someone used [[%extension.EXTNAME]] wrong *)
7878
type malformed_extension =
7979
| Has_payload of Parsetree.payload
80-
| Wrong_arguments of (Asttypes.arg_label * Parsetree.expression) list
81-
| Wrong_tuple of Parsetree.pattern list
8280

8381
(** An error triggered when desugaring a language extension from an OCaml AST *)
8482
type error =
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
File "user_error2.ml", line 21, characters 46-65:
22
21 | let _malformed_extensions_wrong_arguments = [%extension.something] "two" "arguments";;
33
^^^^^^^^^^^^^^^^^^^
4-
Error: Expression modular extension nodes must be applied to exactly
5-
one unlabeled argument, but "extension.something" was applied to
6-
2 arguments
4+
Error: Uninterpreted extension 'extension.something'.

0 commit comments

Comments
 (0)