@@ -719,90 +719,6 @@ let break_between s ~cmts ~has_cmts_before ~has_cmts_after (i1, c1) (i2, c2)
719
719
true (* always break between an item and a directive *)
720
720
| _ -> assert false
721
721
722
- (* * Precedence levels of Ast terms. *)
723
- type prec =
724
- | Low
725
- | Semi
726
- | LessMinus
727
- | ColonEqual
728
- | As
729
- | Comma
730
- | MinusGreater
731
- | BarBar
732
- | AmperAmper
733
- | InfixOp0
734
- | InfixOp1
735
- | ColonColon
736
- | InfixOp2
737
- | InfixOp3
738
- | InfixOp4
739
- | UMinus
740
- | Apply
741
- | HashOp
742
- | Dot
743
- | High
744
- | Atomic
745
-
746
- let compare_prec : prec -> prec -> int = Poly. compare
747
-
748
- let equal_prec a b = compare_prec a b = 0
749
-
750
- let string_of_prec = function
751
- | Low -> " Low"
752
- | Semi -> " Semi"
753
- | LessMinus -> " LessMinus"
754
- | ColonEqual -> " ColonEqual"
755
- | As -> " As"
756
- | Comma -> " Comma"
757
- | MinusGreater -> " MinusGreater"
758
- | BarBar -> " BarBar"
759
- | AmperAmper -> " AmperAmper"
760
- | InfixOp0 -> " InfixOp0"
761
- | InfixOp1 -> " InfixOp1"
762
- | ColonColon -> " ColonColon"
763
- | InfixOp2 -> " InfixOp2"
764
- | InfixOp3 -> " InfixOp3"
765
- | InfixOp4 -> " InfixOp4"
766
- | UMinus -> " UMinus"
767
- | Apply -> " Apply"
768
- | Dot -> " Dot"
769
- | HashOp -> " HashOp"
770
- | High -> " High"
771
- | Atomic -> " Atomic"
772
-
773
- let _ = string_of_prec
774
-
775
- (* * Associativities of Ast terms. *)
776
- type assoc = Left | Non | Right
777
-
778
- let string_of_assoc = function
779
- | Left -> " Left"
780
- | Non -> " Non"
781
- | Right -> " Right"
782
-
783
- let _ = string_of_assoc
784
-
785
- let equal_assoc : assoc -> assoc -> bool = Poly. ( = )
786
-
787
- (* * Compute associativity from precedence, since associativity is uniform
788
- across precedence levels. *)
789
- let assoc_of_prec = function
790
- | Low | Semi | LessMinus -> Non
791
- | ColonEqual -> Right
792
- | As -> Non
793
- | Comma -> Non
794
- | MinusGreater | BarBar | AmperAmper -> Right
795
- | InfixOp0 -> Left
796
- | InfixOp1 -> Right
797
- | ColonColon -> Right
798
- | InfixOp2 | InfixOp3 -> Left
799
- | InfixOp4 -> Right
800
- | UMinus | Apply -> Non
801
- | HashOp -> Left
802
- | Dot -> Left
803
- | High -> Non
804
- | Atomic -> Non
805
-
806
722
(* * Term-in-context, [{ctx; ast}] records that [ast] is (considered to be) an
807
723
immediate sub-term of [ctx] as assumed by the operations in
808
724
[Requires_sub_terms]. *)
@@ -866,7 +782,7 @@ and Requires_sub_terms : sig
866
782
867
783
val exposed_left_exp : expression -> bool
868
784
869
- val prec_ast : T .t -> prec option
785
+ val prec_ast : T .t -> Prec .t option
870
786
871
787
val parenze_typ : core_type In_ctx .xt -> bool
872
788
@@ -1557,6 +1473,8 @@ end = struct
1557
1473
[ctx]. Also returns whether [ast] is the left, right, or neither child
1558
1474
of [ctx]. Meaningful for binary operators, otherwise returns [None]. *)
1559
1475
let prec_ctx ctx =
1476
+ let open Prec in
1477
+ let open Assoc in
1560
1478
let is_tuple_lvl1_in_constructor ty = function
1561
1479
| {ptype_kind = Ptype_variant cd1N ; _} ->
1562
1480
List. exists cd1N ~f: (function
@@ -1701,7 +1619,9 @@ end = struct
1701
1619
1702
1620
(* * [prec_ast ast] is the precedence of [ast]. Meaningful for binary
1703
1621
operators, otherwise returns [None]. *)
1704
- let prec_ast = function
1622
+ let prec_ast =
1623
+ let open Prec in
1624
+ function
1705
1625
| Pld _ -> None
1706
1626
| Typ {ptyp_desc; _} -> (
1707
1627
match ptyp_desc with
@@ -1801,22 +1721,23 @@ end = struct
1801
1721
binary operators, otherwise returns [None] if [ctx] has no precedence
1802
1722
or [Some None] if [ctx] does but [ast] does not. *)
1803
1723
let ambig_prec ({ast; _} as xast ) =
1804
- prec_ctx xast
1805
- >> | fun (prec_ctx , which_child ) ->
1806
- prec_ast ast
1807
- >> | fun prec_ast ->
1808
- match compare_prec prec_ctx prec_ast with
1809
- | 0 ->
1810
- (* which child and associativity match: no parens *)
1811
- (* which child and assoc conflict: add parens *)
1812
- equal_assoc which_child Non
1813
- || not (equal_assoc (assoc_of_prec prec_ast) which_child)
1814
- | cmp when cmp < 0 ->
1815
- (* ast higher precedence than context: no parens *)
1816
- false
1817
- | _ (* > 0 *) ->
1818
- (* context higher prec than ast: add parens *)
1819
- true
1724
+ match prec_ctx xast with
1725
+ | Some (prec_ctx , which_child ) -> (
1726
+ match prec_ast ast with
1727
+ | Some prec_ast ->
1728
+ let ambiguous =
1729
+ match Prec. compare prec_ctx prec_ast with
1730
+ | 0 ->
1731
+ (* which child and associativity match: no parens *)
1732
+ (* which child and assoc conflict: add parens *)
1733
+ Assoc. equal which_child Non
1734
+ || not (Assoc. equal (Assoc. of_prec prec_ast) which_child)
1735
+ (* add parens only when the context has a higher prec than ast *)
1736
+ | cmp -> cmp > = 0
1737
+ in
1738
+ if ambiguous then `Ambiguous else `Non_ambiguous
1739
+ | None -> `No_prec_ast )
1740
+ | None -> `No_prec_ctx
1820
1741
1821
1742
(* * [parenze_typ {ctx; ast}] holds when type [ast] should be parenthesized
1822
1743
in context [ctx]. *)
@@ -1864,15 +1785,15 @@ end = struct
1864
1785
true
1865
1786
| _ -> (
1866
1787
match ambig_prec (sub_ast ~ctx (Typ typ)) with
1867
- | Some (Some true ) -> true
1788
+ | `Ambiguous -> true
1868
1789
| _ -> false )
1869
1790
1870
1791
(* * [parenze_cty {ctx; ast}] holds when class type [ast] should be
1871
1792
parenthesized in context [ctx]. *)
1872
1793
let parenze_cty ({ctx; ast = cty } as xcty ) =
1873
1794
assert_check_cty xcty ;
1874
1795
match ambig_prec (sub_ast ~ctx (Cty cty)) with
1875
- | Some (Some true ) -> true
1796
+ | `Ambiguous -> true
1876
1797
| _ -> false
1877
1798
1878
1799
(* * [parenze_mty {ctx; ast}] holds when module type [ast] should be
@@ -2224,7 +2145,7 @@ end = struct
2224
2145
({pexp_desc= Pexp_ident {txt= Lident i; _}; _}, _ :: (_, e2) :: _)
2225
2146
when e2 == exp && is_infix_id i
2226
2147
&& Option. value_map ~default: false (prec_ast ctx) ~f: (fun p ->
2227
- compare_prec p Apply < 0 ) ->
2148
+ Prec. compare p Apply < 0 ) ->
2228
2149
true
2229
2150
| Pexp_apply
2230
2151
({pexp_desc= Pexp_ident lid; _}, (_ :: (_, e2) :: _ as args))
@@ -2235,8 +2156,8 @@ end = struct
2235
2156
| _ -> false
2236
2157
in
2237
2158
match ambig_prec (sub_ast ~ctx (Exp exp)) with
2238
- | None -> false (* ctx not apply *)
2239
- | Some (Some true ) -> true (* exp is apply and ambig *)
2159
+ | `No_prec_ctx -> false (* ctx not apply *)
2160
+ | `Ambiguous -> true (* exp is apply and ambig *)
2240
2161
| _ -> (
2241
2162
match ctx with
2242
2163
| Exp {pexp_desc; _} ->
@@ -2403,8 +2324,8 @@ end = struct
2403
2324
and parenze_cl ({ctx; ast = cl } as xcl ) =
2404
2325
assert_check_cl xcl ;
2405
2326
match ambig_prec (sub_ast ~ctx (Cl cl)) with
2406
- | None -> false
2407
- | Some (Some true ) -> true
2327
+ | `No_prec_ctx -> false
2328
+ | `Ambiguous -> true
2408
2329
| _ -> exposed_right_cl Non_apply cl
2409
2330
2410
2331
let parenze_nested_exp {ctx; ast = exp } =
@@ -2428,7 +2349,7 @@ end = struct
2428
2349
noise *)
2429
2350
false
2430
2351
| None , _ | _ , None -> false
2431
- | Some p1 , Some p2 -> not (equal_prec p1 p2)
2352
+ | Some p1 , Some p2 -> not (Prec. equal p1 p2)
2432
2353
end
2433
2354
2434
2355
include In_ctx
0 commit comments