@@ -58,6 +58,7 @@ let mkmod ~loc ?attrs d = Mod.mk ~loc:(make_loc loc) ?attrs d
5858let mkstr ~loc d = Str. mk ~loc: (make_loc loc) d
5959let mkclass ~loc ?attrs d = Cl. mk ~loc: (make_loc loc) ?attrs d
6060let 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
6263let 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
482494let 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
487500let 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
25022515simple_expr:
@@ -3736,17 +3749,24 @@ meth_list:
37363749/* Constants */
37373750
37383751constant:
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;
37443760signed_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 */
0 commit comments