Skip to content

Commit 6e349f3

Browse files
authored
Backport 5.3 changes to Parsetree.constant (#2610)
1 parent ba0f782 commit 6e349f3

File tree

8 files changed

+111
-59
lines changed

8 files changed

+111
-59
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ profile. This started with version 0.26.0.
1313
This might change the formatting of some functions due to the formatting code
1414
being completely rewritten.
1515

16-
- Support OCaml 5.3 syntax (#2609, @Julow)
16+
- Support OCaml 5.3 syntax (#2609, #2610, @Julow)
1717

1818
- Documentation comments are now formatted by default (#2390, @Julow)
1919
Use the option `parse-docstrings = false` to restore the previous behavior.

lib/Normalize_std_ast.ml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ let make_mapper conf ~ignore_doc_comments =
3939
[ ( { pstr_desc=
4040
Pstr_eval
4141
( ( { pexp_desc=
42-
Pexp_constant (Pconst_string (doc, str_loc, None))
42+
Pexp_constant
43+
( { pconst_desc=
44+
Pconst_string (doc, str_loc, None)
45+
; _ } as const )
4346
; _ } as exp )
4447
, [] )
4548
; _ } as pstr ) ]
@@ -56,7 +59,9 @@ let make_mapper conf ~ignore_doc_comments =
5659
( { exp with
5760
pexp_desc=
5861
Pexp_constant
59-
(Pconst_string (doc', str_loc, None))
62+
{ const with
63+
pconst_desc=
64+
Pconst_string (doc', str_loc, None) }
6065
; pexp_loc_stack= [] }
6166
, [] ) } ] }
6267
| _ -> Ast_mapper.default_mapper.attribute m attr
@@ -179,7 +184,9 @@ let make_docstring_mapper docstrings =
179184
, PStr
180185
[ { pstr_desc=
181186
Pstr_eval
182-
( { pexp_desc= Pexp_constant (Pconst_string (doc, _, None))
187+
( { pexp_desc=
188+
Pexp_constant
189+
{pconst_desc= Pconst_string (doc, _, None); _}
183190
; _ }
184191
, [] )
185192
; _ } ] ) ->

vendor/parser-standard/ast_helper.ml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,20 @@ let with_default_loc l f =
3333
Misc.protect_refs [Misc.R (default_loc, l)] f
3434

3535
module Const = struct
36-
let integer ?suffix i = Pconst_integer (i, suffix)
37-
let int ?suffix i = integer ?suffix (Int.to_string i)
38-
let int32 ?(suffix='l') i = integer ~suffix (Int32.to_string i)
39-
let int64 ?(suffix='L') i = integer ~suffix (Int64.to_string i)
40-
let nativeint ?(suffix='n') i = integer ~suffix (Nativeint.to_string i)
41-
let float ?suffix f = Pconst_float (f, suffix)
42-
let char c = Pconst_char c
36+
let mk ?(loc = !default_loc) d =
37+
{pconst_desc = d;
38+
pconst_loc = loc}
39+
40+
let integer ?loc ?suffix i = mk ?loc (Pconst_integer (i, suffix))
41+
let int ?loc ?suffix i = integer ?loc ?suffix (Int.to_string i)
42+
let int32 ?loc ?(suffix='l') i = integer ?loc ~suffix (Int32.to_string i)
43+
let int64 ?loc ?(suffix='L') i = integer ?loc ~suffix (Int64.to_string i)
44+
let nativeint ?loc ?(suffix='n') i =
45+
integer ?loc ~suffix (Nativeint.to_string i)
46+
let float ?loc ?suffix f = mk ?loc (Pconst_float (f, suffix))
47+
let char ?loc c = mk ?loc (Pconst_char c)
4348
let string ?quotation_delimiter ?(loc= !default_loc) s =
44-
Pconst_string (s, loc, quotation_delimiter)
49+
mk ~loc (Pconst_string (s, loc, quotation_delimiter))
4550
end
4651

4752
module Attr = struct

vendor/parser-standard/ast_mapper.ml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,18 @@ let map_loc sub {loc; txt} = {loc = sub.location sub loc; txt}
9595
module C = struct
9696
(* Constants *)
9797

98-
let map sub c = match c with
99-
| Pconst_integer _
100-
| Pconst_char _
101-
| Pconst_float _
102-
-> c
103-
| Pconst_string (s, loc, quotation_delimiter) ->
104-
let loc = sub.location sub loc in
105-
Const.string ~loc ?quotation_delimiter s
98+
let map sub { pconst_desc; pconst_loc } =
99+
let loc = sub.location sub pconst_loc in
100+
let desc =
101+
match pconst_desc with
102+
| Pconst_integer _
103+
| Pconst_char _
104+
| Pconst_float _ ->
105+
pconst_desc
106+
| Pconst_string (s, loc, quotation_delimiter) ->
107+
Pconst_string (s, sub.location sub loc, quotation_delimiter)
108+
in
109+
Const.mk ~loc desc
106110
end
107111

108112
module T = struct
@@ -941,7 +945,8 @@ module PpxContext = struct
941945
let restore fields =
942946
let field name payload =
943947
let rec get_string = function
944-
| { pexp_desc = Pexp_constant (Pconst_string (str, _, None)) } -> str
948+
| {pexp_desc = Pexp_constant
949+
{pconst_desc = Pconst_string (str, _, None); _}} -> str
945950
| _ -> raise_errorf "Internal error: invalid [@@@ocaml.ppx.context \
946951
{ %s }] string syntax" name
947952
and get_bool pexp =

vendor/parser-standard/docstrings.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ let docs_attr ds =
9191
let open Parsetree in
9292
let body = ds.ds_body in
9393
let loc = ds.ds_loc in
94+
let const = { pconst_desc= Pconst_string(body,loc,None); pconst_loc= loc } in
9495
let exp =
95-
{ pexp_desc = Pexp_constant (Pconst_string(body, loc, None));
96+
{ pexp_desc = Pexp_constant const;
9697
pexp_loc = loc;
9798
pexp_loc_stack = [];
9899
pexp_attributes = []; }
@@ -143,8 +144,9 @@ let text_attr ds =
143144
let open Parsetree in
144145
let body = ds.ds_body in
145146
let loc = ds.ds_loc in
147+
let const = { pconst_desc= Pconst_string(body,loc,None); pconst_loc= loc } in
146148
let exp =
147-
{ pexp_desc = Pexp_constant (Pconst_string(body, loc, None));
149+
{ pexp_desc = Pexp_constant const;
148150
pexp_loc = loc;
149151
pexp_loc_stack = [];
150152
pexp_attributes = []; }

vendor/parser-standard/parser.mly

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ let mkmod ~loc ?attrs d = Mod.mk ~loc:(make_loc loc) ?attrs d
5858
let mkstr ~loc d = Str.mk ~loc:(make_loc loc) d
5959
let mkclass ~loc ?attrs d = Cl.mk ~loc:(make_loc loc) ?attrs d
6060
let mkcty ~loc ?attrs d = Cty.mk ~loc:(make_loc loc) ?attrs d
61+
let mkconst ~loc c = Const.mk ~loc:(make_loc loc) c
6162

6263
let pstr_typext (te, ext) =
6364
(Pstr_typext te, ext)
@@ -150,20 +151,31 @@ let neg_string f =
150151
then String.sub f 1 (String.length f - 1)
151152
else "-" ^ f
152153

153-
let mkuminus ~oploc name arg =
154-
match name, arg.pexp_desc with
155-
| "-", Pexp_constant(Pconst_integer (n,m)) ->
156-
Pexp_constant(Pconst_integer(neg_string n,m))
157-
| ("-" | "-."), Pexp_constant(Pconst_float (f, m)) ->
158-
Pexp_constant(Pconst_float(neg_string f, m))
154+
(* Pre-apply the special [-], [-.], [+] and [+.] prefix operators into
155+
constants if possible, otherwise turn them into the corresponding prefix
156+
operators [~-], [~-.], etc.. *)
157+
let mkuminus ~sloc ~oploc name arg =
158+
match name, arg.pexp_desc, arg.pexp_attributes with
159+
| "-",
160+
Pexp_constant({pconst_desc = Pconst_integer (n,m); pconst_loc=_}),
161+
[] ->
162+
Pexp_constant(mkconst ~loc:sloc (Pconst_integer(neg_string n, m)))
163+
| ("-" | "-."),
164+
Pexp_constant({pconst_desc = Pconst_float (f, m); pconst_loc=_}), [] ->
165+
Pexp_constant(mkconst ~loc:sloc (Pconst_float(neg_string f, m)))
159166
| _ ->
160167
Pexp_apply(mkoperator ~loc:oploc ("~" ^ name), [Nolabel, arg])
161168

162-
let mkuplus ~oploc name arg =
169+
let mkuplus ~sloc ~oploc name arg =
163170
let desc = arg.pexp_desc in
164-
match name, desc with
165-
| "+", Pexp_constant(Pconst_integer _)
166-
| ("+" | "+."), Pexp_constant(Pconst_float _) -> desc
171+
match name, desc, arg.pexp_attributes with
172+
| "+",
173+
Pexp_constant({pconst_desc = Pconst_integer _ as desc; pconst_loc=_}),
174+
[]
175+
| ("+" | "+."),
176+
Pexp_constant({pconst_desc = Pconst_float _ as desc; pconst_loc=_}),
177+
[] ->
178+
Pexp_constant(mkconst ~loc:sloc desc)
167179
| _ ->
168180
Pexp_apply(mkoperator ~loc:oploc ("~" ^ name), [Nolabel, arg])
169181

@@ -481,7 +493,8 @@ let wrap_mksig_ext ~loc (item, ext) =
481493

482494
let mk_quotedext ~loc (id, idloc, str, strloc, delim) =
483495
let exp_id = mkloc id idloc in
484-
let e = ghexp ~loc (Pexp_constant (Pconst_string (str, strloc, delim))) in
496+
let const = Const.mk ~loc:strloc (Pconst_string (str, strloc, delim)) in
497+
let e = ghexp ~loc (Pexp_constant const) in
485498
(exp_id, PStr [mkstrexp e []])
486499

487500
let text_str pos = Str.text (rhs_text pos)
@@ -2494,9 +2507,9 @@ fun_expr:
24942507
| e1 = fun_expr op = op(infix_operator) e2 = expr
24952508
{ mkinfix e1 op e2 }
24962509
| subtractive expr %prec prec_unary_minus
2497-
{ mkuminus ~oploc:$loc($1) $1 $2 }
2510+
{ mkuminus ~sloc:$sloc ~oploc:$loc($1) $1 $2 }
24982511
| additive expr %prec prec_unary_plus
2499-
{ mkuplus ~oploc:$loc($1) $1 $2 }
2512+
{ mkuplus ~sloc:$sloc ~oploc:$loc($1) $1 $2 }
25002513
;
25012514

25022515
simple_expr:
@@ -3736,17 +3749,24 @@ meth_list:
37363749
/* Constants */
37373750

37383751
constant:
3739-
| INT { let (n, m) = $1 in Pconst_integer (n, m) }
3740-
| CHAR { Pconst_char $1 }
3741-
| STRING { let (s, strloc, d) = $1 in Pconst_string (s, strloc, d) }
3742-
| FLOAT { let (f, m) = $1 in Pconst_float (f, m) }
3752+
| INT { let (n, m) = $1 in
3753+
mkconst ~loc:$sloc (Pconst_integer (n, m)) }
3754+
| CHAR { mkconst ~loc:$sloc (Pconst_char $1) }
3755+
| STRING { let (s, strloc, d) = $1 in
3756+
mkconst ~loc:$sloc (Pconst_string (s,strloc,d)) }
3757+
| FLOAT { let (f, m) = $1 in
3758+
mkconst ~loc:$sloc (Pconst_float (f, m)) }
37433759
;
37443760
signed_constant:
37453761
constant { $1 }
3746-
| MINUS INT { let (n, m) = $2 in Pconst_integer("-" ^ n, m) }
3747-
| MINUS FLOAT { let (f, m) = $2 in Pconst_float("-" ^ f, m) }
3748-
| PLUS INT { let (n, m) = $2 in Pconst_integer (n, m) }
3749-
| PLUS FLOAT { let (f, m) = $2 in Pconst_float(f, m) }
3762+
| MINUS INT { let (n, m) = $2 in
3763+
mkconst ~loc:$sloc (Pconst_integer("-" ^ n, m)) }
3764+
| MINUS FLOAT { let (f, m) = $2 in
3765+
mkconst ~loc:$sloc (Pconst_float("-" ^ f, m)) }
3766+
| PLUS INT { let (n, m) = $2 in
3767+
mkconst ~loc:$sloc (Pconst_integer (n, m)) }
3768+
| PLUS FLOAT { let (f, m) = $2 in
3769+
mkconst ~loc:$sloc (Pconst_float(f, m)) }
37503770
;
37513771

37523772
/* Identifiers and long identifiers */

vendor/parser-standard/parsetree.mli

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222

2323
open Asttypes
2424

25-
type constant =
25+
type constant = {
26+
pconst_desc : constant_desc;
27+
pconst_loc : Location.t;
28+
}
29+
30+
and constant_desc =
2631
| Pconst_integer of string * char option
2732
(** Integer constants such as [3] [3l] [3L] [3n].
2833
@@ -311,7 +316,7 @@ and expression_desc =
311316
312317
A function must have parameters. [Pexp_function (params, _, body)] must
313318
have non-empty [params] or a [Pfunction_cases _] body.
314-
*)
319+
*)
315320
| Pexp_apply of expression * (arg_label * expression) list
316321
(** [Pexp_apply(E0, [(l1, E1) ; ... ; (ln, En)])]
317322
represents [E0 ~l1:E1 ... ~ln:En]

vendor/parser-standard/printast.ml

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,6 @@ let fmt_char_option f = function
5757
| None -> fprintf f "None"
5858
| Some c -> fprintf f "Some %c" c
5959

60-
let fmt_constant f x =
61-
match x with
62-
| Pconst_integer (i,m) -> fprintf f "PConst_int (%s,%a)" i fmt_char_option m
63-
| Pconst_char (c) -> fprintf f "PConst_char %02x" (Char.code c)
64-
| Pconst_string (s, strloc, None) ->
65-
fprintf f "PConst_string(%S,%a,None)" s fmt_location strloc
66-
| Pconst_string (s, strloc, Some delim) ->
67-
fprintf f "PConst_string (%S,%a,Some %S)" s fmt_location strloc delim
68-
| Pconst_float (s,m) -> fprintf f "PConst_float (%s,%a)" s fmt_char_option m
69-
7060
let fmt_mutable_flag f x =
7161
match x with
7262
| Immutable -> fprintf f "Immutable"
@@ -106,6 +96,18 @@ let line i f s (*...*) =
10696
fprintf f "%s" (String.make ((2*i) mod 72) ' ');
10797
fprintf f s (*...*)
10898

99+
let fmt_constant i f x =
100+
line i f "constant %a\n" fmt_location x.pconst_loc;
101+
let i = i+1 in
102+
match x.pconst_desc with
103+
| Pconst_integer (j,m) -> line i f "PConst_int (%s,%a)\n" j fmt_char_option m
104+
| Pconst_char c -> line i f "PConst_char %02x\n" (Char.code c)
105+
| Pconst_string (s, strloc, None) ->
106+
line i f "PConst_string(%S,%a,None)\n" s fmt_location strloc
107+
| Pconst_string (s, strloc, Some delim) ->
108+
line i f "PConst_string (%S,%a,Some %S)\n" s fmt_location strloc delim
109+
| Pconst_float (s,m) -> line i f "PConst_float (%s,%a)\n" s fmt_char_option m
110+
109111
let list i f ppf l =
110112
match l with
111113
| [] -> line i ppf "[]\n"
@@ -201,9 +203,13 @@ and pattern i ppf x =
201203
| Ppat_alias (p, s) ->
202204
line i ppf "Ppat_alias %a\n" fmt_string_loc s;
203205
pattern i ppf p;
204-
| Ppat_constant (c) -> line i ppf "Ppat_constant %a\n" fmt_constant c;
206+
| Ppat_constant (c) ->
207+
line i ppf "Ppat_constant\n";
208+
fmt_constant i ppf c;
205209
| Ppat_interval (c1, c2) ->
206-
line i ppf "Ppat_interval %a..%a\n" fmt_constant c1 fmt_constant c2;
210+
line i ppf "Ppat_interval\n";
211+
fmt_constant i ppf c1;
212+
fmt_constant i ppf c2;
207213
| Ppat_tuple (l) ->
208214
line i ppf "Ppat_tuple\n";
209215
list i pattern ppf l;
@@ -255,7 +261,9 @@ and expression i ppf x =
255261
let i = i+1 in
256262
match x.pexp_desc with
257263
| Pexp_ident (li) -> line i ppf "Pexp_ident %a\n" fmt_longident_loc li;
258-
| Pexp_constant (c) -> line i ppf "Pexp_constant %a\n" fmt_constant c;
264+
| Pexp_constant (c) ->
265+
line i ppf "Pexp_constant\n";
266+
fmt_constant i ppf c;
259267
| Pexp_let (rf, l, e) ->
260268
line i ppf "Pexp_let %a\n" fmt_rec_flag rf;
261269
list i value_binding ppf l;

0 commit comments

Comments
 (0)