{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

-- | Preprocessing for input source code.
module Ormolu.Processing.Preprocess
  ( preprocess,
  )
where

import Control.Monad
import Data.Bifunctor (bimap)
import Data.Char (isSpace)
import Data.Function ((&))
import Data.IntMap (IntMap)
import qualified Data.IntMap.Strict as IntMap
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import qualified Data.List as L
import Data.Maybe (isJust)
import Data.Text (Text)
import qualified Data.Text as T
import Ormolu.Config (RegionDeltas (..))
import Ormolu.Processing.Common
import Ormolu.Processing.Cpp

-- | Preprocess the specified region of the input into raw snippets
-- and subregions to be formatted.
preprocess ::
  -- | Whether CPP is enabled
  Bool ->
  RegionDeltas ->
  String ->
  [Either Text RegionDeltas]
preprocess :: Bool -> RegionDeltas -> [Char] -> [Either Text RegionDeltas]
preprocess Bool
cppEnabled RegionDeltas
region [Char]
rawInput = [Either Text RegionDeltas]
rawSnippetsAndRegionsToFormat
  where
    (IntSet
linesNotToFormat', IntMap [Char]
replacementLines) = Bool -> RegionDeltas -> [Char] -> (IntSet, IntMap [Char])
linesNotToFormat Bool
cppEnabled RegionDeltas
region [Char]
rawInput
    regionsToFormat :: [RegionDeltas]
regionsToFormat =
      Key -> IntSet -> [RegionDeltas]
intSetToRegions Key
rawLineLength (IntSet -> [RegionDeltas]) -> IntSet -> [RegionDeltas]
forall a b. (a -> b) -> a -> b
$
        [Key] -> IntSet
IntSet.fromAscList [Key
1 .. Key
rawLineLength] IntSet -> IntSet -> IntSet
IntSet.\\ IntSet
linesNotToFormat'
    regionsNotToFormat :: [RegionDeltas]
regionsNotToFormat = Key -> IntSet -> [RegionDeltas]
intSetToRegions Key
rawLineLength IntSet
linesNotToFormat'
    -- We want to interleave the regionsToFormat and regionsNotToFormat.
    -- If the first non-formattable region starts at the first line, it is
    -- the first interleaved region, otherwise, we start with the first
    -- region to format.
    interleave' :: [a] -> [a] -> [a]
interleave' = case [RegionDeltas]
regionsNotToFormat of
      RegionDeltas
r : [RegionDeltas]
_ | RegionDeltas -> Key
regionPrefixLength RegionDeltas
r Key -> Key -> Bool
forall a. Eq a => a -> a -> Bool
== Key
0 -> [a] -> [a] -> [a]
forall {a}. [a] -> [a] -> [a]
interleave
      [RegionDeltas]
_ -> ([a] -> [a] -> [a]) -> [a] -> [a] -> [a]
forall a b c. (a -> b -> c) -> b -> a -> c
flip [a] -> [a] -> [a]
forall {a}. [a] -> [a] -> [a]
interleave
    rawSnippets :: [Text]
rawSnippets = [Char] -> Text
T.pack ([Char] -> Text)
-> (RegionDeltas -> [Char]) -> RegionDeltas -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RegionDeltas -> [Char] -> [Char])
-> [Char] -> RegionDeltas -> [Char]
forall a b c. (a -> b -> c) -> b -> a -> c
flip RegionDeltas -> [Char] -> [Char]
linesInRegion [Char]
updatedInput (RegionDeltas -> Text) -> [RegionDeltas] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [RegionDeltas]
regionsNotToFormat
      where
        updatedInput :: [Char]
updatedInput = [[Char]] -> [Char]
unlines ([[Char]] -> [Char]) -> ([Char] -> [[Char]]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Key, [Char]) -> [Char]) -> [(Key, [Char])] -> [[Char]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Key, [Char]) -> [Char]
updateLine ([(Key, [Char])] -> [[Char]])
-> ([Char] -> [(Key, [Char])]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Key] -> [[Char]] -> [(Key, [Char])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Key
1 ..] ([[Char]] -> [(Key, [Char])])
-> ([Char] -> [[Char]]) -> [Char] -> [(Key, [Char])]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
lines ([Char] -> [Char]) -> [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ [Char]
rawInput
        updateLine :: (Key, [Char]) -> [Char]
updateLine (Key
i, [Char]
line) = [Char] -> Key -> IntMap [Char] -> [Char]
forall a. a -> Key -> IntMap a -> a
IntMap.findWithDefault [Char]
line Key
i IntMap [Char]
replacementLines
    rawSnippetsAndRegionsToFormat :: [Either Text RegionDeltas]
rawSnippetsAndRegionsToFormat =
      [Either Text RegionDeltas]
-> [Either Text RegionDeltas] -> [Either Text RegionDeltas]
forall {a}. [a] -> [a] -> [a]
interleave' (Text -> Either Text RegionDeltas
forall a b. a -> Either a b
Left (Text -> Either Text RegionDeltas)
-> [Text] -> [Either Text RegionDeltas]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text]
rawSnippets) (RegionDeltas -> Either Text RegionDeltas
forall a b. b -> Either a b
Right (RegionDeltas -> Either Text RegionDeltas)
-> [RegionDeltas] -> [Either Text RegionDeltas]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [RegionDeltas]
regionsToFormat)
        [Either Text RegionDeltas]
-> (Either Text RegionDeltas -> [Either Text RegionDeltas])
-> [Either Text RegionDeltas]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Either Text RegionDeltas -> [Either Text RegionDeltas]
patchSeparatingBlankLines
        [Either Text RegionDeltas]
-> ([Either Text RegionDeltas] -> [Either Text RegionDeltas])
-> [Either Text RegionDeltas]
forall a b. a -> (a -> b) -> b
& (Either Text RegionDeltas -> Bool)
-> [Either Text RegionDeltas] -> [Either Text RegionDeltas]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Either Text RegionDeltas -> Bool
forall {b}. Either Text b -> Bool
isBlankRawSnippet
        [Either Text RegionDeltas]
-> ([Either Text RegionDeltas] -> [Either Text RegionDeltas])
-> [Either Text RegionDeltas]
forall a b. a -> (a -> b) -> b
& (Either Text RegionDeltas -> Bool)
-> [Either Text RegionDeltas] -> [Either Text RegionDeltas]
forall a. (a -> Bool) -> [a] -> [a]
L.dropWhileEnd Either Text RegionDeltas -> Bool
forall {b}. Either Text b -> Bool
isBlankRawSnippet
    -- For every formattable region, we want to ensure that it is separated by
    -- a blank line from preceding/succeeding raw snippets if it starts/ends
    -- with a blank line.
    -- Empty formattable regions are replaced by a blank line instead.
    -- Extraneous raw snippets at the start/end are dropped afterwards.
    patchSeparatingBlankLines :: Either Text RegionDeltas -> [Either Text RegionDeltas]
patchSeparatingBlankLines = \case
      Right r :: RegionDeltas
r@RegionDeltas {Key
regionSuffixLength :: RegionDeltas -> Key
regionSuffixLength :: Key
regionPrefixLength :: Key
regionPrefixLength :: RegionDeltas -> Key
..} ->
        if (Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isSpace (RegionDeltas -> [Char] -> [Char]
linesInRegion RegionDeltas
r [Char]
rawInput)
          then [Either Text RegionDeltas
forall {b}. Either Text b
blankRawSnippet]
          else
            [Either Text RegionDeltas
forall {b}. Either Text b
blankRawSnippet | Key -> Bool
isBlankLine Key
regionPrefixLength] [Either Text RegionDeltas]
-> [Either Text RegionDeltas] -> [Either Text RegionDeltas]
forall a. Semigroup a => a -> a -> a
<> [RegionDeltas -> Either Text RegionDeltas
forall a b. b -> Either a b
Right RegionDeltas
r]
              [Either Text RegionDeltas]
-> [Either Text RegionDeltas] -> [Either Text RegionDeltas]
forall a. Semigroup a => a -> a -> a
<> [Either Text RegionDeltas
forall {b}. Either Text b
blankRawSnippet | Key -> Bool
isBlankLine (Key
rawLineLength Key -> Key -> Key
forall a. Num a => a -> a -> a
- Key
regionSuffixLength Key -> Key -> Key
forall a. Num a => a -> a -> a
- Key
1)]
      Left Text
r -> [Text -> Either Text RegionDeltas
forall a b. a -> Either a b
Left Text
r]
      where
        blankRawSnippet :: Either Text b
blankRawSnippet = Text -> Either Text b
forall a b. a -> Either a b
Left Text
"\n"
        isBlankLine :: Key -> Bool
isBlankLine Key
i = Maybe [Char] -> Bool
forall a. Maybe a -> Bool
isJust (Maybe [Char] -> Bool)
-> (Maybe [Char] -> Maybe [Char]) -> Maybe [Char] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> Bool) -> Maybe [Char] -> Maybe [Char]
forall (m :: * -> *) a. MonadPlus m => (a -> Bool) -> m a -> m a
mfilter ((Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isSpace) (Maybe [Char] -> Bool) -> Maybe [Char] -> Bool
forall a b. (a -> b) -> a -> b
$ [[Char]]
rawLines [[Char]] -> Key -> Maybe [Char]
forall {a}. [a] -> Key -> Maybe a
!!? Key
i
    isBlankRawSnippet :: Either Text b -> Bool
isBlankRawSnippet = \case
      Left Text
r | (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isSpace Text
r -> Bool
True
      Either Text b
_ -> Bool
False

    rawLines :: [[Char]]
rawLines = [Char] -> [[Char]]
lines [Char]
rawInput
    rawLineLength :: Key
rawLineLength = [[Char]] -> Key
forall (t :: * -> *) a. Foldable t => t a -> Key
length [[Char]]
rawLines

    interleave :: [a] -> [a] -> [a]
interleave [] [a]
bs = [a]
bs
    interleave (a
a : [a]
as) [a]
bs = a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a] -> [a]
interleave [a]
bs [a]
as

    [a]
xs !!? :: [a] -> Key -> Maybe a
!!? Key
i = if Key
i Key -> Key -> Bool
forall a. Ord a => a -> a -> Bool
>= Key
0 Bool -> Bool -> Bool
&& Key
i Key -> Key -> Bool
forall a. Ord a => a -> a -> Bool
< [a] -> Key
forall (t :: * -> *) a. Foldable t => t a -> Key
length [a]
xs then a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ [a]
xs [a] -> Key -> a
forall a. [a] -> Key -> a
!! Key
i else Maybe a
forall a. Maybe a
Nothing

-- | All lines we are not supposed to format, and a set of replacements
-- for specific lines.
linesNotToFormat ::
  -- | Whether CPP is enabled
  Bool ->
  RegionDeltas ->
  String ->
  (IntSet, IntMap String)
linesNotToFormat :: Bool -> RegionDeltas -> [Char] -> (IntSet, IntMap [Char])
linesNotToFormat Bool
cppEnabled region :: RegionDeltas
region@RegionDeltas {Key
regionSuffixLength :: Key
regionPrefixLength :: Key
regionSuffixLength :: RegionDeltas -> Key
regionPrefixLength :: RegionDeltas -> Key
..} [Char]
input =
  (IntSet
unconsidered IntSet -> IntSet -> IntSet
forall a. Semigroup a => a -> a -> a
<> IntSet
magicDisabled IntSet -> IntSet -> IntSet
forall a. Semigroup a => a -> a -> a
<> IntSet
otherDisabled, IntMap [Char]
lineUpdates)
  where
    unconsidered :: IntSet
unconsidered =
      [Key] -> IntSet
IntSet.fromAscList ([Key] -> IntSet) -> [Key] -> IntSet
forall a b. (a -> b) -> a -> b
$
        [Key
1 .. Key
regionPrefixLength] [Key] -> [Key] -> [Key]
forall a. Semigroup a => a -> a -> a
<> [Key
totalLines Key -> Key -> Key
forall a. Num a => a -> a -> a
- Key
regionSuffixLength Key -> Key -> Key
forall a. Num a => a -> a -> a
+ Key
1 .. Key
totalLines]
    totalLines :: Key
totalLines = [[Char]] -> Key
forall (t :: * -> *) a. Foldable t => t a -> Key
length ([Char] -> [[Char]]
lines [Char]
input)
    regionLines :: [Char]
regionLines = RegionDeltas -> [Char] -> [Char]
linesInRegion RegionDeltas
region [Char]
input
    (IntSet
magicDisabled, IntMap [Char]
lineUpdates) = [Char] -> (IntSet, IntMap [Char])
magicDisabledLines [Char]
regionLines
    otherDisabled :: IntSet
otherDisabled = ([[Char] -> IntSet] -> [Char] -> IntSet
forall a. Monoid a => [a] -> a
mconcat [[Char] -> IntSet]
allLines) [Char]
regionLines
      where
        allLines :: [[Char] -> IntSet]
allLines = [[Char] -> IntSet
shebangLines, [Char] -> IntSet
linePragmaLines] [[Char] -> IntSet] -> [[Char] -> IntSet] -> [[Char] -> IntSet]
forall a. Semigroup a => a -> a -> a
<> [[Char] -> IntSet
cppLines | Bool
cppEnabled]

-- | Ormolu state.
data OrmoluState
  = -- | Enabled
    OrmoluEnabled
  | -- | Disabled
    OrmoluDisabled
  deriving (OrmoluState -> OrmoluState -> Bool
(OrmoluState -> OrmoluState -> Bool)
-> (OrmoluState -> OrmoluState -> Bool) -> Eq OrmoluState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OrmoluState -> OrmoluState -> Bool
$c/= :: OrmoluState -> OrmoluState -> Bool
== :: OrmoluState -> OrmoluState -> Bool
$c== :: OrmoluState -> OrmoluState -> Bool
Eq, Key -> OrmoluState -> [Char] -> [Char]
[OrmoluState] -> [Char] -> [Char]
OrmoluState -> [Char]
(Key -> OrmoluState -> [Char] -> [Char])
-> (OrmoluState -> [Char])
-> ([OrmoluState] -> [Char] -> [Char])
-> Show OrmoluState
forall a.
(Key -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
showList :: [OrmoluState] -> [Char] -> [Char]
$cshowList :: [OrmoluState] -> [Char] -> [Char]
show :: OrmoluState -> [Char]
$cshow :: OrmoluState -> [Char]
showsPrec :: Key -> OrmoluState -> [Char] -> [Char]
$cshowsPrec :: Key -> OrmoluState -> [Char] -> [Char]
Show)

-- | All lines which are disabled by Ormolu's magic comments,
-- as well as normalizing replacements.
magicDisabledLines :: String -> (IntSet, IntMap String)
magicDisabledLines :: [Char] -> (IntSet, IntMap [Char])
magicDisabledLines [Char]
input =
  ([Key] -> IntSet)
-> ([(Key, [Char])] -> IntMap [Char])
-> ([Key], [(Key, [Char])])
-> (IntSet, IntMap [Char])
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap [Key] -> IntSet
IntSet.fromAscList [(Key, [Char])] -> IntMap [Char]
forall a. [(Key, a)] -> IntMap a
IntMap.fromAscList (([Key], [(Key, [Char])]) -> (IntSet, IntMap [Char]))
-> ([([Key], [(Key, [Char])])] -> ([Key], [(Key, [Char])]))
-> [([Key], [(Key, [Char])])]
-> (IntSet, IntMap [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [([Key], [(Key, [Char])])] -> ([Key], [(Key, [Char])])
forall a. Monoid a => [a] -> a
mconcat ([([Key], [(Key, [Char])])] -> (IntSet, IntMap [Char]))
-> [([Key], [(Key, [Char])])] -> (IntSet, IntMap [Char])
forall a b. (a -> b) -> a -> b
$
    OrmoluState -> [([Char], Key)] -> [([Key], [(Key, [Char])])]
forall {a}. OrmoluState -> [([Char], a)] -> [([a], [(a, [Char])])]
go OrmoluState
OrmoluEnabled ([Char] -> [[Char]]
lines [Char]
input [[Char]] -> [Key] -> [([Char], Key)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Key
1 ..])
  where
    go :: OrmoluState -> [([Char], a)] -> [([a], [(a, [Char])])]
go OrmoluState
_ [] = []
    go OrmoluState
state (([Char]
line, a
i) : [([Char], a)]
ls)
      | [Char] -> [Char] -> Bool
isMagicComment [Char]
ormoluDisable [Char]
line,
        OrmoluState
state OrmoluState -> OrmoluState -> Bool
forall a. Eq a => a -> a -> Bool
== OrmoluState
OrmoluEnabled =
        ([a
i], [(a
i, [Char] -> [Char]
magicComment [Char]
ormoluDisable)]) ([a], [(a, [Char])])
-> [([a], [(a, [Char])])] -> [([a], [(a, [Char])])]
forall a. a -> [a] -> [a]
: OrmoluState -> [([Char], a)] -> [([a], [(a, [Char])])]
go OrmoluState
OrmoluDisabled [([Char], a)]
ls
      | [Char] -> [Char] -> Bool
isMagicComment [Char]
ormoluEnable [Char]
line,
        OrmoluState
state OrmoluState -> OrmoluState -> Bool
forall a. Eq a => a -> a -> Bool
== OrmoluState
OrmoluDisabled =
        ([a
i], [(a
i, [Char] -> [Char]
magicComment [Char]
ormoluEnable)]) ([a], [(a, [Char])])
-> [([a], [(a, [Char])])] -> [([a], [(a, [Char])])]
forall a. a -> [a] -> [a]
: OrmoluState -> [([Char], a)] -> [([a], [(a, [Char])])]
go OrmoluState
OrmoluEnabled [([Char], a)]
ls
      | Bool
otherwise = ([a], [(a, [Char])])
forall {a}. ([a], [a])
iIfDisabled ([a], [(a, [Char])])
-> [([a], [(a, [Char])])] -> [([a], [(a, [Char])])]
forall a. a -> [a] -> [a]
: OrmoluState -> [([Char], a)] -> [([a], [(a, [Char])])]
go OrmoluState
state [([Char], a)]
ls
      where
        iIfDisabled :: ([a], [a])
iIfDisabled = case OrmoluState
state of
          OrmoluState
OrmoluDisabled -> ([a
i], [])
          OrmoluState
OrmoluEnabled -> ([], [])

-- | All lines which satisfy a predicate.
linesFiltered :: (String -> Bool) -> String -> IntSet
linesFiltered :: ([Char] -> Bool) -> [Char] -> IntSet
linesFiltered [Char] -> Bool
p =
  [Key] -> IntSet
IntSet.fromAscList ([Key] -> IntSet) -> ([Char] -> [Key]) -> [Char] -> IntSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (([Char], Key) -> Key) -> [([Char], Key)] -> [Key]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([Char], Key) -> Key
forall a b. (a, b) -> b
snd ([([Char], Key)] -> [Key])
-> ([Char] -> [([Char], Key)]) -> [Char] -> [Key]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (([Char], Key) -> Bool) -> [([Char], Key)] -> [([Char], Key)]
forall a. (a -> Bool) -> [a] -> [a]
filter ([Char] -> Bool
p ([Char] -> Bool)
-> (([Char], Key) -> [Char]) -> ([Char], Key) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char], Key) -> [Char]
forall a b. (a, b) -> a
fst) ([([Char], Key)] -> [([Char], Key)])
-> ([Char] -> [([Char], Key)]) -> [Char] -> [([Char], Key)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([[Char]] -> [Key] -> [([Char], Key)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Key
1 ..]) ([[Char]] -> [([Char], Key)])
-> ([Char] -> [[Char]]) -> [Char] -> [([Char], Key)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
lines

-- | Lines which contain a shebang.
shebangLines :: String -> IntSet
shebangLines :: [Char] -> IntSet
shebangLines = ([Char] -> Bool) -> [Char] -> IntSet
linesFiltered ([Char]
"#!" [Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`L.isPrefixOf`)

-- | Lines which contain a LINE pragma.
linePragmaLines :: String -> IntSet
linePragmaLines :: [Char] -> IntSet
linePragmaLines = ([Char] -> Bool) -> [Char] -> IntSet
linesFiltered ([Char]
"{-# LINE" [Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`L.isPrefixOf`)

-- | Inner text of a magic enabling marker.
ormoluEnable :: String
ormoluEnable :: [Char]
ormoluEnable = [Char]
"ORMOLU_ENABLE"

-- | Inner text of a magic disabling marker.
ormoluDisable :: String
ormoluDisable :: [Char]
ormoluDisable = [Char]
"ORMOLU_DISABLE"

-- | Creates a magic comment with the given inner text.
magicComment :: String -> String
magicComment :: [Char] -> [Char]
magicComment [Char]
t = [Char]
"{- " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
t [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
" -}"

-- | Construct a function for whitespace-insensitive matching of string.
isMagicComment ::
  -- | What to expect
  String ->
  -- | String to test
  String ->
  -- | Whether or not the two strings watch
  Bool
isMagicComment :: [Char] -> [Char] -> Bool
isMagicComment [Char]
expected [Char]
s0 = Maybe () -> Bool
forall a. Maybe a -> Bool
isJust (Maybe () -> Bool) -> Maybe () -> Bool
forall a b. (a -> b) -> a -> b
$ do
  let trim :: [Char] -> [Char]
trim = (Char -> Bool) -> [Char] -> [Char]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace
  [Char]
s1 <- [Char] -> [Char]
trim ([Char] -> [Char]) -> Maybe [Char] -> Maybe [Char]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> [Char] -> Maybe [Char]
forall a. Eq a => [a] -> [a] -> Maybe [a]
L.stripPrefix [Char]
"{-" ([Char] -> [Char]
trim [Char]
s0)
  [Char]
s2 <- [Char] -> [Char]
trim ([Char] -> [Char]) -> Maybe [Char] -> Maybe [Char]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> [Char] -> Maybe [Char]
forall a. Eq a => [a] -> [a] -> Maybe [a]
L.stripPrefix [Char]
expected [Char]
s1
  [Char]
s3 <- [Char] -> [Char] -> Maybe [Char]
forall a. Eq a => [a] -> [a] -> Maybe [a]
L.stripPrefix [Char]
"-}" [Char]
s2
  Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard ((Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isSpace [Char]
s3)