Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.2 support: Raw identifiers (by changing the Parsetree) #2620

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ profile. This started with version 0.26.0.

### Highlight

- \* Support OCaml 5.2 syntax (#2519, #2544, #2590, #2596, @Julow, @EmileTrotignon)
This includes local open in types and the new representation for functions.
- \* Support OCaml 5.2 syntax (#2519, #2544, #2590, #2596, #2620, @Julow, @EmileTrotignon)
This includes local open in types, raw identifiers, and the new
representation for functions.
This might change the formatting of some functions due to the formatting code
being completely rewritten.

Expand Down
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
Loading