realworldocaml / mdx

Execute code blocks inside your documentation
ISC License
265 stars 45 forks source link

Multiline toplevel block do not work in `.mli` files #394

Closed panglesd closed 1 year ago

panglesd commented 2 years ago

If I have a markdown file

```ocaml
# let f = function
   | "" -> false
   | _ -> true
val f : string -> bool = <fun>
it gets accepted by `ocaml-mdx test`.

However, for the same file in `.mli`:
```ocaml
(** {[
# let f = function
   | "" -> false
   | _ -> true
val f : string -> bool = <fun>
]} *)

then ocaml-mdx test will complain that the first line is not ending with a ;; and report a syntax error, as the first line is considered as a complete block.

$ cat blibli.mli.corrected 
(** {[
      # let f = function;;
      Line 1, characters 17-19:
      Error: Syntax error
    ]} *)
Leonidas-from-XIV commented 2 years ago

I've debugged this today and the issue is that somehow Toplevel.of_lines when set to parse with ~syntax:Syntax.Mli only parses the first line as a Toplevel.command thus it fails to execute.

The code of of_lines is a bit strange, with all kinds of ad-hoc fixes for mli syntax, but maybe it can be cleaned up a little because the adjustments feel a bit strange and unexplained, so I need to understand why these are there.

panglesd commented 2 years ago

I need to understand why these are there.

I also started to have a patch and faced the same problem. For instance, I am puzzled by this! However, since multiline toplevel for mli file never worked, I would say it is fine to discard the current adjustments and come up with our own. (I could give it a go if you want!)