|
| 1 | +{- |
| 2 | +
|
| 3 | +PARSER |
| 4 | +====== |
| 5 | +
|
| 6 | +-} |
| 7 | +module Proofread.Parser |
| 8 | + ( Proofread.Parser.parse |
| 9 | + , Result(..) |
| 10 | + ) where |
| 11 | + |
| 12 | +import Control.Monad.Combinators |
| 13 | +import Data.Text (Text) |
| 14 | +import Flow |
| 15 | +import Proofread.Parser.Types |
| 16 | +import Proofread.Parser.Utilities |
| 17 | +import Proofread.Types |
| 18 | +import Protolude hiding (and, one, or, some, try) |
| 19 | +import Text.Megaparsec |
| 20 | +import Text.Megaparsec.Char |
| 21 | + |
| 22 | +import qualified Data.List as List |
| 23 | +import qualified Data.Text as Text |
| 24 | +import qualified Text.Megaparsec as Mega |
| 25 | + |
| 26 | + |
| 27 | +-- 🌳 |
| 28 | + |
| 29 | + |
| 30 | +data Result |
| 31 | + = Ok Document |
| 32 | + | Err Text |
| 33 | + deriving (Show) |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +-- ⚡️ |
| 38 | + |
| 39 | + |
| 40 | +parse :: Text -> Result |
| 41 | +parse contents = |
| 42 | + contents |
| 43 | + |> Text.unpack |
| 44 | + |> Mega.parse document "" |
| 45 | + |> either (parseErrorPretty .> Text.pack .> Err) Ok |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +-- 📮 |
| 50 | + |
| 51 | + |
| 52 | +document :: Parser Document |
| 53 | +document = do |
| 54 | + _ <- maybeSome whitespace |
| 55 | + m <- one docModule |
| 56 | + _ <- maybeSome whitespace |
| 57 | + t <- maybeSome test |
| 58 | + |
| 59 | + return $ Document m t |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +-- MODULE |
| 64 | + |
| 65 | + |
| 66 | +docModule :: Parser Text |
| 67 | +docModule = do |
| 68 | + _ <- one (string "module ") |
| 69 | + moduleName <- some (alphaNumChar `or` char '.' `or` char '_') |
| 70 | + _ <- one spaceCharacter |
| 71 | + _ <- manyTill anyChar (string "\n\n") |
| 72 | + |
| 73 | + return $ Text.pack moduleName |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +-- TESTS |
| 78 | + |
| 79 | + |
| 80 | +test :: Parser Test |
| 81 | +test = |
| 82 | + try (skipManyTill anyChar testInMultiLineComment) |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +{-| Parser for a test in a multiline comment. |
| 87 | +-} |
| 88 | +testInMultiLineComment :: Parser Test |
| 89 | +testInMultiLineComment = do |
| 90 | + _ <- one (string " >>> ") |
| 91 | + startInput <- someTill anyChar eol |
| 92 | + additionalInput <- maybeSome (try mlExtra) |
| 93 | + _ <- maybeSome whitespace |
| 94 | + expectedOutput <- manyTill anyChar (try mlEnd) |
| 95 | + |
| 96 | + return $ Test |
| 97 | + { input = |
| 98 | + additionalInput |
| 99 | + |> (<>) [ startInput, if length additionalInput > 0 then "\n" else "" ] |
| 100 | + |> List.concat |
| 101 | + |> Text.pack |
| 102 | + |
| 103 | + , output = |
| 104 | + Text.pack expectedOutput |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | +mlExtra :: Parser [Char] |
| 109 | +mlExtra = do |
| 110 | + _ <- some whitespace |
| 111 | + _ <- one (string "..> ") |
| 112 | + input <- manyTill anyChar eol |
| 113 | + |
| 114 | + return input |
| 115 | + |
| 116 | + |
| 117 | +mlEnd :: Parser [Char] |
| 118 | +mlEnd = |
| 119 | + eol `andThen` maybeSome spaceCharacter `andThen` (eol `or` string "-}") |
0 commit comments