Skip to content

Commit e879af1

Browse files
authored
fix: handle case where doc does not lex as ocaml (#2684)
* fix: handle case where doc does not lex as ocaml Fixes #2679 Formatting `mld` files can crash when the input file cannot be lexed as ocaml. This can happen with LaTeX is found in `{m ...}` or C code in `{@c[ ... ]}`, for example. In that case we would crash when building the list of tokens in the file; however it's not used in the `mld` case.
1 parent 1866147 commit e879af1

File tree

7 files changed

+26
-1
lines changed

7 files changed

+26
-1
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ profile. This started with version 0.26.0.
4343

4444
- Fix crash due to edge case with asterisk-prefixed comments (#2674, @Julow)
4545

46+
47+
- Fix crash when formatting `mld` files that cannot be lexed as ocaml (e.g.
48+
containing LaTeX or C code) (#2684, @emillon)
49+
4650
- \* Fix double parens around module constraint in functor application :
4751
`module M = F ((A : T))` becomes `module M = F (A : T)`. (#2678, @EmileTrotignon)
4852

lib/Parse_with_comments.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ let parse ?(disable_w50 = false) ?(disable_deprecated = false) parse fragment
9595
List.map ~f:mk_cmt (Lexer.comments ())
9696
in
9797
let tokens =
98+
(* mld files can not always be lexed using the ocaml lexer *)
9899
let lexbuf, _ = fresh_lexbuf source in
99-
tokens lexbuf
100+
try tokens lexbuf with Lexer.Error _ -> []
100101
in
101102
let source = Source.create ~text:source ~tokens in
102103
{ast; comments; prefix= hash_bang; source} )

test/passing/refs.ahrefs/doc.mld.ref

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,7 @@ For more about [odoc] commands, simply invoke [odoc --help] in your shell.
162162

163163
Preserve the space between a link/reference and its text: {{:foo}bar}
164164
{{:foo} bar} {{!foo}bar} {{!foo} bar}
165+
166+
{1 Some math}
167+
168+
{m \Gamma}

test/passing/refs.default/doc.mld.ref

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,7 @@ For more about [odoc] commands, simply invoke [odoc --help] in your shell.
162162

163163
Preserve the space between a link/reference and its text: {{:foo}bar}
164164
{{:foo} bar} {{!foo}bar} {{!foo} bar}
165+
166+
{1 Some math}
167+
168+
{m \Gamma}

test/passing/refs.janestreet/doc.mld.ref

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,7 @@ For more about [odoc] commands, simply invoke [odoc --help] in your shell.
154154

155155
Preserve the space between a link/reference and its text:
156156
{{:foo}bar} {{:foo} bar} {{!foo}bar} {{!foo} bar}
157+
158+
{1 Some math}
159+
160+
{m \Gamma}

test/passing/refs.ocamlformat/doc.mld.ref

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,7 @@ For more about [odoc] commands, simply invoke [odoc --help] in your shell.
162162

163163
Preserve the space between a link/reference and its text: {{:foo}bar}
164164
{{:foo} bar} {{!foo}bar} {{!foo} bar}
165+
166+
{1 Some math}
167+
168+
{m \Gamma}

test/passing/tests/doc.mld

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,7 @@ For more about [odoc] commands, simply invoke [odoc --help] in your shell.
160160

161161
Preserve the space between a link/reference and its text:
162162
{{:foo}bar} {{:foo} bar} {{!foo}bar} {{!foo} bar}
163+
164+
{1 Some math}
165+
166+
{m \Gamma}

0 commit comments

Comments
 (0)