fix: make directives usable in the ocaml mode#443
Open
sorawee wants to merge 1 commit intorealworldocaml:mainfrom
Open
fix: make directives usable in the ocaml mode#443sorawee wants to merge 1 commit intorealworldocaml:mainfrom
ocaml mode#443sorawee wants to merge 1 commit intorealworldocaml:mainfrom
Conversation
Prior this PR, the following code fails:
```
{@ocaml ocaml[
#require "astring";;
let x = Astring.strf;;
]}
```
because MDX incorrectly infers that the code block is a toplevel
interaction (from the fact that the block starts with `#`),
resulting in an error:
incomplete toplevel entry: unexpected character '#'.
Did you forget a space after the '#' at the start of the line
It is possible to workaround this issue as suggested in realworldocaml#421 by adding a
comment as a first line.
```
{@ocaml ocaml[
(* This works! *)
#require "astring";;
let x = Astring.strf;;
]}
```
but ideally the workaround should not be needed.
One may wonder why the inference is needed, since the above code block
is already specified to be in the `ocaml` mode.
The answer appears to be that we are expected to use the inference heuristics
for a light sanity check, as the existing tests
("invalid ocaml" and "invalid toplevel" in `test_block.ml`) require:
"let x = 2;;" in the toplevel mode should error with
invalid toplevel syntax in toplevel blocks
"# let x = 2;;" in the ocaml mode should error with
toplevel syntax is not allowed in OCaml blocks
As a result, this PR keeps the light sanity check intact, but
adjusts the inference heuristics to be more conservative.
A block is now considered a toplevel interaction when
it starts with `#` followed by a space.
This fixes the issue, making it possible to use directives.
As a bonus, directives will now also work even when
the mode is not specified at all. But one disadvantage is that
this kind of code will no longer be considered invalid.
realworldocaml#1+1;;
...
realworldocaml#2+2;;
...
88bff95 to
3fd9e80
Compare
Julow
approved these changes
Dec 4, 2023
Collaborator
Julow
left a comment
There was a problem hiding this comment.
I always use directives with Mdx and always stumble upon this issue.
The more limited toplevel recognission is not an issue in my opinion as this is not a valid block:
```ocaml
#1+1;;
```
File "test.md", lines 1-3: Error in the toplevel code block
File "test.md", line 2, character 0: incomplete toplevel entry: unexpected character '#'. Did you forget a space after the '#' at the start of the line?
| let h = String.trim h in | ||
| if h = "" then aux t | ||
| else if String.length h > 1 && h.[0] = '#' then `Toplevel | ||
| else if String.length h > 2 && h.[0] = '#' && h.[1] = ' ' then `Toplevel |
Collaborator
There was a problem hiding this comment.
If needed, the check could be more strict as top-level directives necessarily start with a lowercase ASCII letter (or the sequence \# which is never going to be used).
Author
|
@Julow Here's the actual result of This is because |
Collaborator
|
Do you mind rebasing your changes? Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prior this PR, the following code fails:
because MDX incorrectly infers that the code block is a toplevel interaction (from the fact that the block starts with
#), resulting in an error:It is possible to workaround this issue as suggested in #421 by adding a comment as a first line.
but ideally the workaround should not be needed.
One may wonder why the inference is needed, since the above code block is already specified to be in the
ocamlmode.The answer appears to be that we are expected to use the inference heuristics for a light sanity check, as the existing tests
("invalid ocaml" and "invalid toplevel" in
test_block.ml) require:As a result, this PR keeps the light sanity check intact, but adjusts the inference heuristics to be more conservative. A block is now considered a toplevel interaction when it starts with
#followed by a space.This fixes the issue, making it possible to use directives. As a bonus, directives will now also work even when the mode is not specified at all. But one disadvantage is that this kind of code will no longer be considered invalid.