Skip to content

Commit dba5921

Browse files
authored
Depend on odoc-parser instead of odoc (#1683)
1 parent a8c1dc3 commit dba5921

13 files changed

+46
-56
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545

4646
+ Improve position of `;;` tokens (#1688, @gpetiot)
4747

48+
+ Depend on `odoc-parser` instead of `odoc` (#1683, @kit-ty-kate, @jonludlam)
49+
The parser from odoc has been split from the main odoc package and put into its own package, `odoc-parser`.
50+
4851
#### New features
4952

5053
+ Implement OCaml 4.13 features

dune-project

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
(and
6262
:with-test
6363
(>= 2.5.0)))
64-
(odoc
65-
(>= 1.4.2))
64+
(odoc-parser
65+
(>= 0.9.0))
6666
re
6767
(stdio
6868
(< v0.15))
@@ -104,8 +104,8 @@
104104
(and
105105
:with-test
106106
(>= 2.5.0)))
107-
(odoc
108-
(>= 1.4.2))
107+
(odoc-parser
108+
(>= 0.9.0))
109109
re
110110
(stdio
111111
(< v0.15))

lib/Docstring.ml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ let parse ~loc text =
1515
{ location with
1616
pos_cnum= location.pos_cnum + 3 (* Length of comment opening *) }
1717
in
18-
match Odoc_parser.parse_comment_raw ~location ~text with
19-
| exception _ ->
20-
let span = Migrate_ast.Location.to_span loc in
21-
Error [Odoc_model.Error.make "comment could not be parsed" span]
22-
| {value; warnings= []} -> Ok value
23-
| {warnings; _} -> Error warnings
18+
let v = Odoc_parser.parse_comment ~location ~text in
19+
match Odoc_parser.warnings v with
20+
| [] -> Ok (Odoc_parser.ast v)
21+
| warnings -> Error warnings
2422

2523
let warn fmt warning =
2624
Format.fprintf fmt "Warning: Invalid documentation comment:@,%s\n%!"
27-
(Odoc_model.Error.to_string warning)
25+
(Odoc_parser.Warning.to_string warning)

lib/Docstring.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
val parse :
1313
loc:Warnings.loc
1414
-> string
15-
-> (Odoc_parser.Ast.docs, Odoc_model.Error.t list) Result.t
15+
-> (Odoc_parser.Ast.t, Odoc_parser.Warning.t list) Result.t
1616

17-
val warn : Format.formatter -> Odoc_model.Error.t -> unit
17+
val warn : Format.formatter -> Odoc_parser.Warning.t -> unit

lib/Fmt_odoc.ml

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
open Fmt
1313
open Odoc_parser.Ast
14-
module Location_ = Odoc_model.Location_
14+
module Loc = Odoc_parser.Loc
1515

1616
type conf = {fmt_code: string -> (Fmt.t, unit) Result.t}
1717

@@ -56,7 +56,7 @@ let str_normalized ?(escape = escape_all) s =
5656
|> List.filter ~f:(Fn.non String.is_empty)
5757
|> fun s -> list s "@ " (fun s -> escape s |> str)
5858

59-
let ign_loc ~f with_loc = f with_loc.Location_.value
59+
let ign_loc ~f with_loc = f with_loc.Loc.value
6060

6161
let fmt_verbatim_block s =
6262
let force_break = String.contains s '\n' in
@@ -67,8 +67,9 @@ let fmt_verbatim_block s =
6767
in
6868
hvbox 0 (wrap "{v" "v}" content)
6969

70-
let fmt_code_block conf s =
71-
match conf.fmt_code s with
70+
let fmt_code_block conf _s1 s2 =
71+
let s2 = Odoc_parser.Loc.value s2 in
72+
match conf.fmt_code s2 with
7273
| Ok formatted -> hvbox 0 (wrap "{[@;<1 2>" "@ ]}" formatted)
7374
| Error () ->
7475
let fmt_line ~first ~last:_ l =
@@ -77,7 +78,7 @@ let fmt_code_block conf s =
7778
else if String.length l = 0 then str "\n"
7879
else fmt "@," $ str l
7980
in
80-
let lines = String.split_lines s in
81+
let lines = String.split_lines s2 in
8182
let box = match lines with _ :: _ :: _ -> vbox 0 | _ -> hvbox 0 in
8283
box (wrap "{[@;<1 2>" "@ ]}" (vbox 0 (list_fl lines fmt_line)))
8384

@@ -89,7 +90,7 @@ let fmt_reference = ign_loc ~f:str_normalized
8990
let list_should_use_heavy_syntax items =
9091
let heavy_nestable_block_elements = function
9192
(* More than one element or contains a list *)
92-
| [{Location_.value= `List _; _}] | _ :: _ :: _ -> true
93+
| [{Loc.value= `List _; _}] | _ :: _ :: _ -> true
9394
| [] | [_] -> false
9495
in
9596
List.exists items ~f:heavy_nestable_block_elements
@@ -107,10 +108,10 @@ let block_element_should_break elem next =
107108
depending on [block_element_should_break] *)
108109
let list_block_elem elems f =
109110
list_pn elems (fun ~prev:_ elem ~next ->
110-
let elem = elem.Location_.value in
111+
let elem = elem.Loc.value in
111112
let break =
112113
match next with
113-
| Some {Location_.value= n; _}
114+
| Some {Loc.value= n; _}
114115
when block_element_should_break
115116
(elem :> block_element)
116117
(n :> block_element) ->
@@ -120,8 +121,7 @@ let list_block_elem elems f =
120121
in
121122
f elem $ break )
122123

123-
let space_elt : inline_element with_location =
124-
Location_.(at (span []) (`Space ""))
124+
let space_elt : inline_element with_location = Loc.(at (span []) (`Space ""))
125125

126126
let rec fmt_inline_elements elements =
127127
let wrap_elements opn cls ~always_wrap hd = function
@@ -173,7 +173,7 @@ let rec fmt_inline_elements elements =
173173

174174
and fmt_nestable_block_element c = function
175175
| `Paragraph elems -> fmt_inline_elements elems
176-
| `Code_block s -> fmt_code_block c s
176+
| `Code_block (s1, s2) -> fmt_code_block c s1 s2
177177
| `Verbatim s -> fmt_verbatim_block s
178178
| `Modules mods ->
179179
hovbox 0
@@ -251,7 +251,7 @@ let fmt_block_element c = function
251251
| #nestable_block_element as elm ->
252252
hovbox 0 (fmt_nestable_block_element c elm)
253253

254-
let fmt ~fmt_code (docs : docs) =
254+
let fmt ~fmt_code (docs : t) =
255255
vbox 0 (list_block_elem docs (fmt_block_element {fmt_code}))
256256

257257
let diff c x y =
@@ -262,6 +262,4 @@ let diff c x y =
262262
Set.symmetric_diff (norm x) (norm y)
263263

264264
let is_tag_only =
265-
List.for_all ~f:(function
266-
| {Location_.value= `Tag _; _} -> true
267-
| _ -> false )
265+
List.for_all ~f:(function {Loc.value= `Tag _; _} -> true | _ -> false)

lib/Fmt_odoc.mli

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010
(**************************************************************************)
1111

1212
val fmt :
13-
fmt_code:(string -> (Fmt.t, unit) Result.t)
14-
-> Odoc_parser.Ast.docs
15-
-> Fmt.t
13+
fmt_code:(string -> (Fmt.t, unit) Result.t) -> Odoc_parser.Ast.t -> Fmt.t
1614

1715
val diff :
1816
Conf.t -> Cmt.t list -> Cmt.t list -> (string, string) Either.t Sequence.t
1917
(** Difference between two lists of doc comments. *)
2018

21-
val is_tag_only : Odoc_parser.Ast.docs -> bool
19+
val is_tag_only : Odoc_parser.Ast.t -> bool
2220
(** [true] if the documentation only contains tags *)

lib/Migrate_ast.ml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ module Position = struct
4646
include (val Comparator.make ~compare ~sexp_of_t)
4747

4848
let distance p1 p2 = p2.pos_cnum - p1.pos_cnum
49-
50-
let to_point x = Odoc_model.Location_.{line= x.pos_lnum; column= column x}
5149
end
5250

5351
module Location = struct
@@ -110,12 +108,6 @@ module Location = struct
110108
let min a b = if width a < width b then a else b in
111109
List.reduce_exn (loc :: stack) ~f:min
112110

113-
let to_span loc =
114-
let open Odoc_model.Location_ in
115-
{ file= loc.loc_start.pos_fname
116-
; start= Position.to_point loc.loc_start
117-
; end_= Position.to_point loc.loc_end }
118-
119111
let of_lexbuf (lexbuf : Lexing.lexbuf) =
120112
{ loc_start= lexbuf.lex_start_p
121113
; loc_end= lexbuf.lex_curr_p

lib/Migrate_ast.mli

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ module Location : sig
7878

7979
val is_single_line : t -> int -> bool
8080

81-
val to_span : t -> Odoc_model.Location_.span
82-
8381
val of_lexbuf : Lexing.lexbuf -> t
8482

8583
val print : Format.formatter -> t -> unit

lib/Normalize.ml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,10 @@ let list f fmt l =
5757

5858
let str fmt s = Format.fprintf fmt "%s" (comment s)
5959

60-
let ign_loc f fmt with_loc = f fmt with_loc.Odoc_model.Location_.value
60+
let ign_loc f fmt with_loc = f fmt with_loc.Odoc_parser.Loc.value
6161

6262
let fpf = Format.fprintf
6363

64-
open Odoc_parser.Ast
65-
6664
let odoc_reference = ign_loc str
6765

6866
let odoc_style fmt = function
@@ -101,7 +99,8 @@ let rec odoc_nestable_block_element c fmt = function
10199
only depend on linebreaks, which may be changed by the
102100
formatting. *)
103101
odoc_inline_elements fmt elms
104-
| `Code_block txt ->
102+
| `Code_block (_, txt) ->
103+
let txt = Odoc_parser.Loc.value txt in
105104
let txt =
106105
try
107106
let ({ast; comments; _} : _ Parse_with_comments.with_comments) =
@@ -167,17 +166,17 @@ let odoc_block_element c fmt = function
167166
let lbl = match lbl with Some lbl -> lbl | None -> "" in
168167
fpf fmt "Heading(%s,%a,%a)" lvl str lbl odoc_inline_elements content
169168
| `Tag tag -> fpf fmt "Tag(%a)" (odoc_tag c) tag
170-
| #nestable_block_element as elm -> odoc_nestable_block_element c fmt elm
169+
| #Odoc_parser.Ast.nestable_block_element as elm ->
170+
odoc_nestable_block_element c fmt elm
171171

172172
let odoc_docs c fmt elems = list (ign_loc (odoc_block_element c)) fmt elems
173173

174174
let docstring c text =
175175
if not c.conf.parse_docstrings then comment text
176176
else
177177
let location = Lexing.dummy_pos in
178-
let parsed = Odoc_parser.parse_comment_raw ~location ~text in
179-
Format.asprintf "Docstring(%a)%!" (odoc_docs c)
180-
parsed.Odoc_model.Error.value
178+
let parsed = Odoc_parser.parse_comment ~location ~text in
179+
Format.asprintf "Docstring(%a)%!" (odoc_docs c) (Odoc_parser.ast parsed)
181180

182181
let sort_attributes : attributes -> attributes =
183182
List.sort ~compare:Poly.compare

lib/dune

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
ocaml_413
2323
ocamlformat_stdlib
2424
ocp-indent.lib
25-
odoc.model
26-
odoc.parser
25+
odoc-parser
2726
parse_wyc
2827
re
2928
uuseg

0 commit comments

Comments
 (0)