Add support for literat Cryptol with RST

This commit is contained in:
Iavor Diatchki 2022-09-27 22:41:13 +03:00
parent 6ab24582e1
commit 42b56f6e8b
7 changed files with 103 additions and 3 deletions

View File

@ -8,7 +8,7 @@
--
-- Convert a literate source file into an ordinary source file.
{-# LANGUAGE OverloadedStrings, Safe, PatternGuards #-}
{-# LANGUAGE OverloadedStrings, PatternGuards #-}
module Cryptol.Parser.Unlit
( unLit, PreProc(..), guessPreProc, knownExts
) where
@ -20,7 +20,7 @@ import System.FilePath(takeExtension)
import Cryptol.Utils.Panic
data PreProc = None | Markdown | LaTeX
data PreProc = None | Markdown | LaTeX | RST
knownExts :: [String]
knownExts =
@ -28,6 +28,7 @@ knownExts =
, "tex"
, "markdown"
, "md"
, "rst"
]
guessPreProc :: FilePath -> PreProc
@ -35,6 +36,7 @@ guessPreProc file = case takeExtension file of
".tex" -> LaTeX
".markdown" -> Markdown
".md" -> Markdown
".rst" -> RST
_ -> None
unLit :: PreProc -> Text -> Text
@ -47,6 +49,7 @@ preProc p =
None -> return . Code
Markdown -> markdown
LaTeX -> latex
RST -> rst
data Block = Code [Text] | Comment [Text]
@ -134,6 +137,34 @@ latex = comment []
isBeginCode l = "\\begin{code}" `Text.isPrefixOf` l
isEndCode l = "\\end{code}" `Text.isPrefixOf` l
rst :: [Text] -> [Block]
rst = comment []
where
isBeginCode l = case filter (not . Text.null) (Text.splitOn " " l) of
["..", "code::", "cryptol"] -> True
_ -> False
isEmpty = Text.all isSpace
isCode l = case Text.uncons l of
Just (c, _) -> isSpace c
Nothing -> True
comment acc ls =
case ls of
[] -> mk Comment acc
l : ls1 | isBeginCode l -> codeOptoins (l : acc) ls1
| otherwise -> comment (l : acc) ls1
codeOptoins acc ls =
case ls of
[] -> mk Comment acc
l : ls1 | isEmpty l -> mk Comment (l : acc) ++ code [] ls1
| otherwise -> codeOptoins (l : acc) ls1
code acc ls =
case ls of
[] -> mk Code acc
l : ls1 | isCode l -> code (l : acc) ls1
| otherwise -> mk Code acc ++ comment [] ls

View File

@ -0,0 +1,2 @@
:load issue1441_a.rst
(f1,f2,f3,f4)

View File

@ -0,0 +1,4 @@
Loading module Cryptol
Loading module Cryptol
Loading module Main
(0x01, 0x02, 0x03, 0x4)

View File

@ -0,0 +1,24 @@
This is an RTS document. Here is some code:
.. code:: cryptol
f1 = 0x01
Now we are back to text. Here is some more code, this one with options:
.. code:: cryptol
:linenos:
f2 = 0x02
Back to text. Now two code blocks together:
.. code:: cryptol
f3 = 0x03
.. code:: cryptol
:linenos:
f4 = 0x4

View File

@ -0,0 +1 @@
:load issue1441_b.rst

View File

@ -0,0 +1,10 @@
Loading module Cryptol
Loading module Cryptol
Loading module Main
[error] at issue1441_b.rst:28:14--28:18:
Type mismatch:
Expected type: 4
Inferred type: 8
Context: [ERROR] _
When checking type of function argument

View File

@ -0,0 +1,28 @@
This is an RTS document. Here is some code:
.. code:: cryptol
f1 = 0x01
Now we are back to text. Here is some more code, this one with options:
.. code:: cryptol
:linenos:
f2 = 0x02
Back to text. Now two code blocks together:
.. code:: cryptol
f3 = 0x03
.. code:: cryptol
:linenos:
f4 = 0x4
Now we are going to make an error and we want to get the correct location:
.. code:: cryptol
f5 = 0x1 + 0x02