Skip to content

Commit

Permalink
Update Fmt_ast to new label representation
Browse files Browse the repository at this point in the history
This is mostly a type-directed refactoring.
  • Loading branch information
Julow committed Nov 18, 2024
1 parent e5645f5 commit 4daebeb
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 126 deletions.
13 changes: 8 additions & 5 deletions lib/Ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ let longident_fit_margin (c : Conf.t) x = x * 3 < c.fmt_opts.margin.v * 2
let longident_is_simple c x =
let rec length x =
match x with
| Longident.Lident x -> String.length x
| Ldot (x, y) -> length x + 1 + String.length y
| Longident.Lident (x, _) -> String.length x
| Ldot (x, y, _) -> length x + 1 + String.length y
| Lapply (x, y) -> length x + length y + 3
in
longident_fit_margin c (length x)
Expand Down Expand Up @@ -156,7 +156,8 @@ module Exp = struct
| Pexp_construct (_, exp) -> Option.for_all exp ~f:is_trivial
| Pexp_prefix (_, e) -> is_trivial e
| Pexp_apply
({pexp_desc= Pexp_ident {txt= Lident "not"; _}; _}, [(_, e1)]) ->
({pexp_desc= Pexp_ident {txt= Lident ("not", _); _}; _}, [(_, e1)])
->
is_trivial e1
| Pexp_variant (_, None) -> true
| Pexp_array [] | Pexp_list [] -> true
Expand Down Expand Up @@ -1618,7 +1619,8 @@ end = struct
| Pexp_cons l ->
Some (ColonColon, if exp == List.last_exn l then Right else Left)
| Pexp_construct
({txt= Lident "[]"; _}, Some {pexp_desc= Pexp_tuple [_; _]; _}) ->
( {txt= Lident ("[]", Lconstruct); _}
, Some {pexp_desc= Pexp_tuple [_; _]; _} ) ->
Some (Semi, Non)
| Pexp_array _ | Pexp_list _ -> Some (Semi, Non)
| Pexp_construct (_, Some _)
Expand Down Expand Up @@ -2287,7 +2289,8 @@ end = struct
true
| ( Exp {pexp_desc= Pexp_infix (_, _, e1); _}
, { pexp_desc=
Pexp_apply ({pexp_desc= Pexp_ident {txt= Lident "not"; _}; _}, _)
Pexp_apply
({pexp_desc= Pexp_ident {txt= Lident ("not", _); _}; _}, _)
; _ } )
when not (e1 == exp) ->
true
Expand Down
10 changes: 7 additions & 3 deletions lib/Cmts.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ module Layout_cache = struct

let pattern_to_string e = Format.asprintf "%a" Printast.pattern e

let sexp_of_arg_label = function
let sexp_of_arg_label =
let sexp_of_label cstr lb =
Sexp.List [Atom cstr; sexp_of_string (fst lb.Location.txt)]
in
function
| Asttypes.Nolabel -> Sexp.Atom "Nolabel"
| Labelled label -> List [Atom "Labelled"; sexp_of_string label.txt]
| Optional label -> List [Atom "Optional"; sexp_of_string label.txt]
| Labelled label -> sexp_of_label "Labelled" label
| Optional label -> sexp_of_label "Optional" label

let sexp_of_t = function
| Arg (label, expression) ->
Expand Down
14 changes: 9 additions & 5 deletions lib/Extended_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ module Parse = struct
match (t, v) with
(* [{ x = x }] -> [{ x }] *)
| _, Some {ppat_desc= Ppat_var {txt= v_txt; _}; ppat_attributes= []; _}
when Std_longident.field_alias ~field:f.txt (Lident v_txt) ->
when Std_longident.field_alias_label ~field:f.txt v_txt ->
(f, t, None)
(* [{ x = (x : t) }] -> [{ x : t}] *)
| ( None
Expand All @@ -147,7 +147,7 @@ module Parse = struct
; ppat_attributes= []
; _ } )
when enable_short_field_annot
&& Std_longident.field_alias ~field:f.txt (Lident v_txt) ->
&& Std_longident.field_alias_label ~field:f.txt v_txt ->
(f, Some t, None)
| _ -> (f, t, Option.map ~f:(m.pat m) v)
in
Expand All @@ -163,7 +163,9 @@ module Parse = struct
| {ppat_desc= Ppat_cons (_ :: _ :: _ :: _ as l); _} as p
when match List.last_exn l with
(* Empty lists are always represented as Lident [] *)
| { ppat_desc= Ppat_construct ({txt= Lident "[]"; loc= _}, None)
| { ppat_desc=
Ppat_construct
({txt= Lident ("[]", Lconstruct); loc= _}, None)
; ppat_attributes= []
; _ } ->
true
Expand All @@ -187,7 +189,9 @@ module Parse = struct
| {pexp_desc= Pexp_cons (_ :: _ :: _ :: _ as l); _} as e
when match List.last_exn l with
(* Empty lists are always represented as Lident [] *)
| { pexp_desc= Pexp_construct ({txt= Lident "[]"; loc= _}, None)
| { pexp_desc=
Pexp_construct
({txt= Lident ("[]", Lconstruct); loc= _}, None)
; pexp_attributes= []
; _ } ->
true
Expand All @@ -208,7 +212,7 @@ module Parse = struct
| { pexp_desc=
Pexp_apply
( { pexp_desc=
Pexp_ident {txt= Lident op as longident; loc= loc_op}
Pexp_ident {txt= Lident (op, _) as longident; loc= loc_op}
; pexp_attributes= []
; _ }
, [(Nolabel, l); (Nolabel, r)] )
Expand Down
Loading

0 comments on commit 4daebeb

Please sign in to comment.