Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow doctests to span multiple lines with a trailing \ like the REPL already supports #1757

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/Cryptol/REPL/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2062,10 +2062,12 @@ data SubcommandResult = SubcommandResult
checkBlock ::
[T.Text] {- ^ lines of the code block -} ->
REPL [SubcommandResult]
checkBlock = isolated . go
checkBlock = isolated . go . continuedLines
where
go [] = pure []
go (line:block) =
go (line:block)
| T.all isSpace line = go block
| otherwise =
case parseCommand (findNbCommand True) (T.unpack line) of
Nothing -> do
pure [SubcommandResult
Expand Down Expand Up @@ -2104,6 +2106,19 @@ checkBlock = isolated . go
subresults <- checkBlock block
pure (subresult : subresults)

-- | Combine lines ending in a backslash with the next line.
continuedLines :: [T.Text] -> [T.Text]
continuedLines xs =
case span ("\\" `T.isSuffixOf`) xs of
([], []) -> []
(a, []) -> [concat' (map T.init a)] -- permissive
(a, b:bs) -> concat' (map T.init a ++ [b]) : continuedLines bs
where
-- concat that eats leading whitespace between elements
concat' [] = T.empty
concat' (y:ys) = T.concat (y : map (T.dropWhile isSpace) ys)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you have an example like this?

/**
 ** ```repl
 ** :check f 1\
 ** 0
 ** ``` */

Does this get interpreted as :check f 10 or :check f 1 0?

Copy link
Member Author

@glguy glguy Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gets interpreted as 10 which is consistent with C (as an example). This will be useful for long literals, too:

x = 0x0123456789abcdef\
      0123456789abcdef\
      0123456789abcdef

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense—thanks for the explanation. It might be worth testing this property in the T02 test case.



captureLog :: REPL a -> REPL (String, a)
captureLog m = do
previousLogger <- getLogger
Expand Down
4 changes: 3 additions & 1 deletion tests/docstrings/T02.cry
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ submodule F7 = submodule F where
/**
** ```repl
** let y = 20
** :check f 10 == 20
**
** :check f 10 \
** == 20
** ``` */
f : Integer -> Integer
f x = x + x
Loading