Skip to content

Commit 505ed6e

Browse files
authored
Support 5.2 raw identifiers (#2621)
Raw identifier syntax is added to the vendored lexer. Contrary to upstream, the escape sequence (`\#`) is part of the ident. This avoids the need to classify identifiers later in Fmt_ast.
1 parent 8af2ec4 commit 505ed6e

File tree

7 files changed

+691
-3
lines changed

7 files changed

+691
-3
lines changed

CHANGES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ profile. This started with version 0.26.0.
88

99
### Highlight
1010

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

test/passing/gen/dune.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3832,6 +3832,21 @@
38323832
(alias runtest)
38333833
(action (diff quoted_strings.ml.err quoted_strings.ml.stderr)))
38343834

3835+
(rule
3836+
(deps .ocamlformat dune-project)
3837+
(action
3838+
(with-stdout-to raw_identifiers.ml.stdout
3839+
(with-stderr-to raw_identifiers.ml.stderr
3840+
(run %{bin:ocamlformat} --name raw_identifiers.ml --margin-check %{dep:../tests/raw_identifiers.ml})))))
3841+
3842+
(rule
3843+
(alias runtest)
3844+
(action (diff raw_identifiers.ml.ref raw_identifiers.ml.stdout)))
3845+
3846+
(rule
3847+
(alias runtest)
3848+
(action (diff raw_identifiers.ml.err raw_identifiers.ml.stderr)))
3849+
38353850
(rule
38363851
(deps .ocamlformat dune-project)
38373852
(action
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
module M : sig
2+
class \#and : object
3+
val mutable \#and : int
4+
method \#and : int
5+
end
6+
end = struct
7+
class \#and =
8+
let \#and = 1 in
9+
object
10+
val mutable \#and = \#and
11+
method \#and = 2
12+
end
13+
end
14+
15+
let obj = new M.\#and
16+
17+
module M : sig
18+
type \#and = int
19+
end = struct
20+
type \#and = string
21+
end
22+
23+
let x = (`\#let `\#and : [ `\#let of [ `\#and ] ])
24+
let (`\#let \#rec) = x
25+
let f g ~\#let ?\#and ?(\#for = \#and) () = g ~\#let ?\#and ()
26+
27+
type t = '\#let
28+
type \#mutable = { mutable \#mutable : \#mutable }
29+
30+
let rec \#rec = { \#mutable = \#rec }
31+
32+
type \#and = ..
33+
type \#and += Foo
34+
(* Not allowed in parser: type t += \#and *)
35+
36+
let x = ( ++ )
37+
let x = \#let
38+
let f ~\#let ?\#and () = 1
39+
40+
module type A = sig
41+
type ('\#let, 'a) \#virtual = '\#let * 'a as '\#mutable
42+
43+
val foo : '\#let 'a. 'a -> '\#let -> unit
44+
45+
type foo = { \#let : int }
46+
end
47+
48+
module M = struct
49+
let ((\#let, foo) as \#val) = (\#mutable, baz)
50+
let _ = fun (type \#let foo) -> 1
51+
let f g ~\#let ?\#and ?(\#for = \#and) () = g ~\#let ?\#and ()
52+
53+
class \#let =
54+
object
55+
inherit \#val \#let as \#mutable
56+
end
57+
end
58+
59+
type 'a \#for = 'a list
60+
type 'a \#sig = 'a \#for
61+
type \#true = bool
62+
type _ t = \#in t
63+
type '\#in t
64+
65+
class ['\#in] c = c
66+
67+
let f \#false = \#false
68+
69+
type t = { x : int \#let }
70+
71+
let x \#let = 42
72+
let x = f ~\#let:42 ~\#and:43
73+
let f ~\#let ~\#and : \#let * \#and = x;;
74+
75+
kind_abbrev_ \#let = \#and
76+
77+
type t = T : 'a list -> t
78+
79+
let g x =
80+
let (T (type \#for) (_ : \#for list)) = x in
81+
()
82+
83+
let ( lsl ) x y = x lsl y
84+
let \#lsl x y = x lsl y
85+
86+
module type \#sig = sig end
87+
88+
module M = struct
89+
let \#mod = 1
90+
end
91+
92+
let _ = M.\#mod
93+
94+
module type \#sig = M
95+
module type M = \#sig
96+
module type M = M with module type \#sig = \#sig
97+
module type M = M with module type \#sig := \#sig
98+
99+
let _ = \#sig.(())
100+
101+
(* Raw idents in module names are not allowed in parser: *)
102+
(* let (module \#sig : S) = () *)
103+
(* module \#sig (A : S) = M *)
104+
(* module \#sig = M *)
105+
(* module M (\#sig : S) = M *)
106+
(* module M = M (functor (\#sig : S) -> struct end) *)
107+
(* module type S = functor (\#sig : S) -> S' *)
108+
(* module type M = M with module \#sig = \#sig *)
109+
(* module type M = M with module \#sig := \#sig *)
110+
let _ =
111+
(* let module \#sig = \#sig in *)
112+
(* let open \#sig in *)
113+
()
114+
115+
let%\#let _ = ()
116+
let _ = [%\#let ()]
117+
let _ = () [@\#let]
118+
let _ = () [@@\#let]
119+
let f : type \#in. t = ()
120+
let f : '\#in. t = ()
121+
let \#mod : '\#mod. \#mod = \#mod
122+
let mlet = M.\#let
123+
let mtrue = M.\#true
124+
let mmod = M.\#mod
125+
126+
type tmod = M.\#mod
127+
type tlet = M.\#let
128+
type ttrue = M.\#true
129+
130+
(* class \#mod = object end *)
131+
let f : #M.\#mod -> _ = (new \#mod, new M.\#mod)
132+
133+
class type \#mod = object end
134+
class type \#let = \#mod
135+
136+
module type \#mod = sig
137+
type \#mod
138+
139+
module type \#mod
140+
end
141+
142+
module type t = \#mod with type \#mod = M.\#mod and module type \#mod = M.\#mod
143+
144+
type \#mod = [ `A | `B ]
145+
146+
let g = function #\#mod | #M.\#mod -> ()
147+
148+
type \#mod = ..
149+
type M.\#mod += A
150+
type t = true of int
151+
152+
let x = true 0
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
module M : sig
2+
class \#and : object
3+
val mutable \#and : int
4+
method \#and : int
5+
end
6+
end = struct
7+
class \#and =
8+
let \#and = 1 in
9+
object
10+
val mutable \#and = \#and
11+
method \#and = 2
12+
end
13+
end
14+
15+
let obj = new M.\#and
16+
17+
module M : sig
18+
type \#and = int
19+
end = struct
20+
type \#and = string
21+
end
22+
23+
let x = (`\#let `\#and : [ `\#let of [ `\#and ] ])
24+
let (`\#let \#rec) = x
25+
let f g ~\#let ?\#and ?(\#for = \#and) () = g ~\#let ?\#and ()
26+
27+
type t = '\#let
28+
type \#mutable = { mutable \#mutable : \#mutable }
29+
30+
let rec \#rec = { \#mutable = \#rec }
31+
32+
type \#and = ..
33+
type \#and += Foo
34+
(* Not allowed in parser: type t += \#and *)
35+
36+
let x = ( ++ )
37+
let x = \#let
38+
let f ~\#let ?\#and () = 1
39+
40+
module type A = sig
41+
type ('\#let, 'a) \#virtual = '\#let * 'a as '\#mutable
42+
43+
val foo : '\#let 'a. 'a -> '\#let -> unit
44+
45+
type foo = { \#let : int }
46+
end
47+
48+
module M = struct
49+
let ((\#let, foo) as \#val) = \#mutable, baz
50+
let _ = fun (type \#let foo) -> 1
51+
let f g ~\#let ?\#and ?(\#for = \#and) () = g ~\#let ?\#and ()
52+
53+
class \#let =
54+
object
55+
inherit \#val \#let as \#mutable
56+
end
57+
end
58+
59+
type 'a \#for = 'a list
60+
type 'a \#sig = 'a \#for
61+
type \#true = bool
62+
type _ t = \#in t
63+
type '\#in t
64+
65+
class ['\#in] c = c
66+
67+
let f \#false = \#false
68+
69+
type t = { x : int \#let }
70+
71+
let x \#let = 42
72+
let x = f ~\#let:42 ~\#and:43
73+
let f ~\#let ~\#and : \#let * \#and = x;;
74+
75+
kind_abbrev_ \#let = \#and
76+
77+
type t = T : 'a list -> t
78+
79+
let g x =
80+
let (T (type \#for) (_ : \#for list)) = x in
81+
()
82+
;;
83+
84+
let ( lsl ) x y = x lsl y
85+
let \#lsl x y = x lsl y
86+
87+
module type \#sig = sig end
88+
89+
module M = struct
90+
let \#mod = 1
91+
end
92+
93+
let _ = M.\#mod
94+
95+
module type \#sig = M
96+
module type M = \#sig
97+
module type M = M with module type \#sig = \#sig
98+
module type M = M with module type \#sig := \#sig
99+
100+
let _ = \#sig.(())
101+
102+
(* Raw idents in module names are not allowed in parser: *)
103+
(* let (module \#sig : S) = () *)
104+
(* module \#sig (A : S) = M *)
105+
(* module \#sig = M *)
106+
(* module M (\#sig : S) = M *)
107+
(* module M = M (functor (\#sig : S) -> struct end) *)
108+
(* module type S = functor (\#sig : S) -> S' *)
109+
(* module type M = M with module \#sig = \#sig *)
110+
(* module type M = M with module \#sig := \#sig *)
111+
let _ =
112+
(* let module \#sig = \#sig in *)
113+
(* let open \#sig in *)
114+
()
115+
;;
116+
117+
let%\#let _ = ()
118+
let _ = [%\#let ()]
119+
let _ = () [@\#let]
120+
let _ = () [@@\#let]
121+
let f : type \#in. t = ()
122+
let f : '\#in. t = ()
123+
let \#mod : '\#mod. \#mod = \#mod
124+
let mlet = M.\#let
125+
let mtrue = M.\#true
126+
let mmod = M.\#mod
127+
128+
type tmod = M.\#mod
129+
type tlet = M.\#let
130+
type ttrue = M.\#true
131+
132+
(* class \#mod = object end *)
133+
let f : #M.\#mod -> _ = new \#mod, new M.\#mod
134+
135+
class type \#mod = object end
136+
class type \#let = \#mod
137+
138+
module type \#mod = sig
139+
type \#mod
140+
141+
module type \#mod
142+
end
143+
144+
module type t = \#mod with type \#mod = M.\#mod and module type \#mod = M.\#mod
145+
146+
type \#mod =
147+
[ `A
148+
| `B
149+
]
150+
151+
let g = function
152+
| #\#mod | #M.\#mod -> ()
153+
;;
154+
155+
type \#mod = ..
156+
type M.\#mod += A
157+
type t = true of int
158+
159+
let x = true 0

0 commit comments

Comments
 (0)