{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TupleSections #-}
{-# OPTIONS_GHC -fno-warn-missing-fields #-} -- QuasiQuoter

module Yesod.Routes.Parse
    ( parseRoutes
    , parseRoutesFile
    , parseRoutesNoCheck
    , parseRoutesFileNoCheck
    , parseType
    , parseTypeM
    , parseTypeTree
    , TypeTree (..)
    , dropBracket
    , dropBracketM
    , nameToType
    , isTvar
    ) where

import Language.Haskell.TH.Syntax
import Data.Char (isUpper, isLower, isSpace)
import Language.Haskell.TH.Quote
import qualified System.IO as SIO
import Yesod.Routes.TH
import Yesod.Routes.Overlap (findOverlapNames)
import Data.List (foldl', isPrefixOf)
import Data.Maybe (mapMaybe)
import qualified Data.Set as Set

-- | A quasi-quoter to parse a string into a list of 'Resource's. Checks for
-- overlapping routes, failing if present; use 'parseRoutesNoCheck' to skip the
-- checking. See documentation site for details on syntax.
parseRoutes :: QuasiQuoter
parseRoutes :: QuasiQuoter
parseRoutes = QuasiQuoter { quoteExp :: String -> Q Exp
quoteExp = String -> Q Exp
forall {m :: * -> *}. (MonadFail m, Quote m) => String -> m Exp
x }
  where
    x :: String -> m Exp
x String
s = do
        res <- String -> m [ResourceTree String]
forall (m :: * -> *).
MonadFail m =>
String -> m [ResourceTree String]
resourcesFromString String
s
        case findOverlapNames res of
            [] -> [ResourceTree String] -> m Exp
forall t (m :: * -> *). (Lift t, Quote m) => t -> m Exp
forall (m :: * -> *). Quote m => [ResourceTree String] -> m Exp
lift [ResourceTree String]
res
            [(String, String)]
z -> String -> m Exp
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m Exp) -> String -> m Exp
forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ String
"Overlapping routes: " String -> [String] -> [String]
forall a. a -> [a] -> [a]
: ((String, String) -> String) -> [(String, String)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String, String) -> String
forall a. Show a => a -> String
show [(String, String)]
z

-- | Same as 'parseRoutes', but uses an external file instead of quasiquotation.
--
-- The recommended file extension is @.yesodroutes@.
parseRoutesFile :: FilePath -> Q Exp
parseRoutesFile :: String -> Q Exp
parseRoutesFile = QuasiQuoter -> String -> Q Exp
parseRoutesFileWith QuasiQuoter
parseRoutes

-- | Same as 'parseRoutesNoCheck', but uses an external file instead of quasiquotation.
--
-- The recommended file extension is @.yesodroutes@.
parseRoutesFileNoCheck :: FilePath -> Q Exp
parseRoutesFileNoCheck :: String -> Q Exp
parseRoutesFileNoCheck = QuasiQuoter -> String -> Q Exp
parseRoutesFileWith QuasiQuoter
parseRoutesNoCheck

parseRoutesFileWith :: QuasiQuoter -> FilePath -> Q Exp
parseRoutesFileWith :: QuasiQuoter -> String -> Q Exp
parseRoutesFileWith QuasiQuoter
qq String
fp = do
    String -> Q ()
forall (m :: * -> *). Quasi m => String -> m ()
qAddDependentFile String
fp
    s <- IO String -> Q String
forall a. IO a -> Q a
forall (m :: * -> *) a. Quasi m => IO a -> m a
qRunIO (IO String -> Q String) -> IO String -> Q String
forall a b. (a -> b) -> a -> b
$ String -> IO String
readUtf8File String
fp
    quoteExp qq s

readUtf8File :: FilePath -> IO String
readUtf8File :: String -> IO String
readUtf8File String
fp = do
    h <- String -> IOMode -> IO Handle
SIO.openFile String
fp IOMode
SIO.ReadMode
    SIO.hSetEncoding h SIO.utf8_bom
    SIO.hGetContents h

-- | Same as 'parseRoutes', but performs no overlap checking.
parseRoutesNoCheck :: QuasiQuoter
parseRoutesNoCheck :: QuasiQuoter
parseRoutesNoCheck = QuasiQuoter
    { quoteExp :: String -> Q Exp
quoteExp = \String
s -> String -> Q [ResourceTree String]
forall (m :: * -> *).
MonadFail m =>
String -> m [ResourceTree String]
resourcesFromString String
s Q [ResourceTree String]
-> ([ResourceTree String] -> Q Exp) -> Q Exp
forall a b. Q a -> (a -> Q b) -> Q b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [ResourceTree String] -> Q Exp
forall t (m :: * -> *). (Lift t, Quote m) => t -> m Exp
forall (m :: * -> *). Quote m => [ResourceTree String] -> m Exp
lift
    }

-- | Converts a multi-line string to a set of resources. See documentation for
-- the format of this string. Runs in any 'MonadFail' (e.g. the 'Q' monad of a
-- splice) and reports malformed input through 'fail', so diagnostics are
-- attributed to the splice rather than surfacing as a raw 'error' call stack.
resourcesFromString :: MonadFail m => String -> m [ResourceTree String]
resourcesFromString :: forall (m :: * -> *).
MonadFail m =>
String -> m [ResourceTree String]
resourcesFromString =
    (([ResourceTree String], [String]) -> [ResourceTree String])
-> m ([ResourceTree String], [String]) -> m [ResourceTree String]
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([ResourceTree String], [String]) -> [ResourceTree String]
forall a b. (a, b) -> a
fst (m ([ResourceTree String], [String]) -> m [ResourceTree String])
-> (String -> m ([ResourceTree String], [String]))
-> String
-> m [ResourceTree String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [String] -> m ([ResourceTree String], [String])
forall {f :: * -> *}.
MonadFail f =>
Int -> [String] -> f ([ResourceTree String], [String])
parse Int
0 ([String] -> m ([ResourceTree String], [String]))
-> (String -> [String])
-> String
-> m ([ResourceTree String], [String])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (String -> Bool) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ')) ([String] -> [String])
-> (String -> [String]) -> String -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> [String] -> [String])
-> [String] -> [String] -> [String]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr String -> [String] -> [String]
lineContinuations [] ([String] -> [String])
-> (String -> [String]) -> String -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
lines (String -> [String]) -> (String -> String) -> String -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
filter (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\r')
  where
    parse :: Int -> [String] -> f ([ResourceTree String], [String])
parse Int
_ [] = ([ResourceTree String], [String])
-> f ([ResourceTree String], [String])
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], [])
    parse Int
indent (String
thisLine:[String]
otherLines)
        | String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
spaces Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
indent = ([ResourceTree String], [String])
-> f ([ResourceTree String], [String])
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], String
thisLine String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
otherLines)
        | Bool
otherwise = do
            (this, otherLines') <- f ([ResourceTree String] -> [ResourceTree String], [String])
classify
            (others, remainder) <- parse indent otherLines'
            pure (this others, remainder)
      where
        parseAttr :: String -> Maybe String
parseAttr (Char
'!':String
x) = String -> Maybe String
forall a. a -> Maybe a
Just String
x
        parseAttr String
_ = Maybe String
forall a. Maybe a
Nothing

        stripColonLast :: [String] -> Maybe [String]
stripColonLast =
            ([String] -> [String]) -> [String] -> Maybe [String]
forall {a}. ([String] -> a) -> [String] -> Maybe a
go [String] -> [String]
forall a. a -> a
id
          where
            go :: ([String] -> a) -> [String] -> Maybe a
go [String] -> a
_ [] = Maybe a
forall a. Maybe a
Nothing
            go [String] -> a
front [String
x]
                | String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
x = Maybe a
forall a. Maybe a
Nothing
                | String -> Char
forall a. HasCallStack => [a] -> a
last String
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
':' = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ [String] -> a
front [String -> String
forall a. HasCallStack => [a] -> [a]
init String
x]
                | Bool
otherwise = Maybe a
forall a. Maybe a
Nothing
            go [String] -> a
front (String
x:[String]
xs) = ([String] -> a) -> [String] -> Maybe a
go ([String] -> a
front ([String] -> a) -> ([String] -> [String]) -> [String] -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
xString -> [String] -> [String]
forall a. a -> [a] -> [a]
:)) [String]
xs

        spaces :: String
spaces = (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ') String
thisLine
        classify :: f ([ResourceTree String] -> [ResourceTree String], [String])
classify = do
            toks <- String -> f [String]
forall (m :: * -> *). MonadFail m => String -> m [String]
splitSpaces String
thisLine
            case takeWhile (not . isPrefixOf "--") toks of
                (String
pattern:[String]
rest0)
                    | Just (String
constr:[String]
rest) <- [String] -> Maybe [String]
stripColonLast [String]
rest0
                    , Just [String]
attrs <- (String -> Maybe String) -> [String] -> Maybe [String]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM String -> Maybe String
parseAttr [String]
rest -> do
                        (children, otherLines'') <- Int -> [String] -> f ([ResourceTree String], [String])
parse (String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
spaces Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [String]
otherLines
                        let children' = [String] -> [ResourceTree String] -> [ResourceTree String]
addAttrs [String]
attrs [ResourceTree String]
children
                        (pieces, mmulti, check) <- piecesFromStringCheck pattern
                        case mmulti of
                            Maybe String
Nothing -> () -> f ()
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
                            Just String
_ -> String -> f ()
forall a. String -> f a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> f ()) -> String -> f ()
forall a b. (a -> b) -> a -> b
$ String
"Parent routes cannot have a multipiece: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
pattern
                        pure ((ResourceParent constr check (Set.fromList attrs) pieces children' :), otherLines'')
                (String
pattern:String
constr:[String]
rest) -> do
                    (pieces, mmulti, check) <- String -> f ([Piece String], Maybe String, Bool)
forall (m :: * -> *).
MonadFail m =>
String -> m ([Piece String], Maybe String, Bool)
piecesFromStringCheck String
pattern
                    let (attrs, rest') = takeAttrs rest
                    disp <- dispatchFromString rest' mmulti
                    pure ((ResourceLeaf (Resource constr pieces disp attrs check):), otherLines)
                [] -> ([ResourceTree String] -> [ResourceTree String], [String])
-> f ([ResourceTree String] -> [ResourceTree String], [String])
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([ResourceTree String] -> [ResourceTree String]
forall a. a -> a
id, [String]
otherLines)
                [String]
_ -> String
-> f ([ResourceTree String] -> [ResourceTree String], [String])
forall a. String -> f a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
 -> f ([ResourceTree String] -> [ResourceTree String], [String]))
-> String
-> f ([ResourceTree String] -> [ResourceTree String], [String])
forall a b. (a -> b) -> a -> b
$ String
"Invalid resource line: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
thisLine

-- | Splits a string by spaces, as long as the spaces are not enclosed by curly brackets (not recursive).
splitSpaces :: MonadFail m => String -> m [String]
splitSpaces :: forall (m :: * -> *). MonadFail m => String -> m [String]
splitSpaces String
"" = [String] -> m [String]
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
splitSpaces String
str = do
    (rest, piece) <- String -> m (String, String)
forall {m :: * -> *}. MonadFail m => String -> m (String, String)
parse (String -> m (String, String)) -> String -> m (String, String)
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace String
str
    rest' <- splitSpaces rest
    pure (piece : rest')

    where
        parse :: String -> m (String, String)
parse (Char
'{':String
s) = do (r, p) <- String -> m (String, String)
parseBracket String
s; pure (r, '{':p)
        parse (Char
c:String
s) | Char -> Bool
isSpace Char
c = (String, String) -> m (String, String)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String
s, [])
        parse (Char
c:String
s) = do (r, p) <- String -> m (String, String)
parse String
s; pure (r, c:p)
        parse String
"" = (String, String) -> m (String, String)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String
"", String
"")

        parseBracket :: String -> m (String, String)
parseBracket (Char
'{':String
_) = String -> m (String, String)
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m (String, String)) -> String -> m (String, String)
forall a b. (a -> b) -> a -> b
$ String
"Invalid resource line (nested curly bracket): " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
str
        parseBracket (Char
'}':String
s) = do (r, p) <- String -> m (String, String)
parse String
s; pure (r, '}':p)
        parseBracket (Char
c:String
s) = do (r, p) <- String -> m (String, String)
parseBracket String
s; pure (r, c:p)
        parseBracket String
"" = String -> m (String, String)
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m (String, String)) -> String -> m (String, String)
forall a b. (a -> b) -> a -> b
$ String
"Invalid resource line (unclosed curly bracket): " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
str

piecesFromStringCheck :: MonadFail m => String -> m ([Piece String], Maybe String, Bool)
piecesFromStringCheck :: forall (m :: * -> *).
MonadFail m =>
String -> m ([Piece String], Maybe String, Bool)
piecesFromStringCheck String
s0 = do
    let (String
s1, Bool
check1) = String -> (String, Bool)
stripBang String
s0
    (pieces', mmulti') <- String -> m ([(Bool, Piece String)], Maybe (Bool, String))
forall (m :: * -> *).
MonadFail m =>
String -> m ([(Bool, Piece String)], Maybe (Bool, String))
piecesFromString (String -> m ([(Bool, Piece String)], Maybe (Bool, String)))
-> String -> m ([(Bool, Piece String)], Maybe (Bool, String))
forall a b. (a -> b) -> a -> b
$ String -> String
drop1Slash String
s1
    let pieces = ((Bool, Piece String) -> Piece String)
-> [(Bool, Piece String)] -> [Piece String]
forall a b. (a -> b) -> [a] -> [b]
map (Bool, Piece String) -> Piece String
forall a b. (a, b) -> b
snd [(Bool, Piece String)]
pieces'
        mmulti = ((Bool, String) -> String) -> Maybe (Bool, String) -> Maybe String
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Bool, String) -> String
forall a b. (a, b) -> b
snd Maybe (Bool, String)
mmulti'
        check = Bool
check1 Bool -> Bool -> Bool
&& ((Bool, Piece String) -> Bool) -> [(Bool, Piece String)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Bool, Piece String) -> Bool
forall a b. (a, b) -> a
fst [(Bool, Piece String)]
pieces' Bool -> Bool -> Bool
&& Bool -> ((Bool, String) -> Bool) -> Maybe (Bool, String) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True (Bool, String) -> Bool
forall a b. (a, b) -> a
fst Maybe (Bool, String)
mmulti'
    pure (pieces, mmulti, check)
  where
    stripBang :: String -> (String, Bool)
stripBang (Char
'!':String
rest) = (String
rest, Bool
False)
    stripBang String
x = (String
x, Bool
True)

addAttrs :: [String] -> [ResourceTree String] -> [ResourceTree String]
addAttrs :: [String] -> [ResourceTree String] -> [ResourceTree String]
addAttrs [String]
attrs =
    (ResourceTree String -> ResourceTree String)
-> [ResourceTree String] -> [ResourceTree String]
forall a b. (a -> b) -> [a] -> [b]
map ResourceTree String -> ResourceTree String
forall {typ}. ResourceTree typ -> ResourceTree typ
goTree
  where
    goTree :: ResourceTree typ -> ResourceTree typ
goTree (ResourceLeaf Resource typ
res) = Resource typ -> ResourceTree typ
forall typ. Resource typ -> ResourceTree typ
ResourceLeaf (Resource typ -> Resource typ
forall {typ}. Resource typ -> Resource typ
goRes Resource typ
res)
    goTree (ResourceParent String
v Bool
w Set String
x [Piece typ]
y [ResourceTree typ]
z) = String
-> Bool
-> Set String
-> [Piece typ]
-> [ResourceTree typ]
-> ResourceTree typ
forall typ.
String
-> Bool
-> Set String
-> [Piece typ]
-> [ResourceTree typ]
-> ResourceTree typ
ResourceParent String
v Bool
w Set String
x [Piece typ]
y ((ResourceTree typ -> ResourceTree typ)
-> [ResourceTree typ] -> [ResourceTree typ]
forall a b. (a -> b) -> [a] -> [b]
map ResourceTree typ -> ResourceTree typ
goTree [ResourceTree typ]
z)

    goRes :: Resource typ -> Resource typ
goRes Resource typ
res =
        Resource typ
res { resourceAttrs = noDupes ++ resourceAttrs res }
      where
        usedKeys :: Set String
usedKeys = [String] -> Set String
forall a. Ord a => [a] -> Set a
Set.fromList ([String] -> Set String) -> [String] -> Set String
forall a b. (a -> b) -> a -> b
$ ((String, String) -> String) -> [(String, String)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String, String) -> String
forall a b. (a, b) -> a
fst ([(String, String)] -> [String]) -> [(String, String)] -> [String]
forall a b. (a -> b) -> a -> b
$ (String -> Maybe (String, String))
-> [String] -> [(String, String)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe String -> Maybe (String, String)
toPair ([String] -> [(String, String)]) -> [String] -> [(String, String)]
forall a b. (a -> b) -> a -> b
$ Resource typ -> [String]
forall typ. Resource typ -> [String]
resourceAttrs Resource typ
res
        used :: String -> Bool
used String
attr =
            case String -> Maybe (String, String)
toPair String
attr of
                Maybe (String, String)
Nothing -> Bool
False
                Just (String
key, String
_) -> String
key String -> Set String -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set String
usedKeys
        noDupes :: [String]
noDupes = (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (String -> Bool) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Bool
used) [String]
attrs

    toPair :: String -> Maybe (String, String)
toPair String
s =
        case (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'=') String
s of
            (String
x, Char
'=':String
y) -> (String, String) -> Maybe (String, String)
forall a. a -> Maybe a
Just (String
x, String
y)
            (String, String)
_ -> Maybe (String, String)
forall a. Maybe a
Nothing

-- | Take attributes out of the list and put them in the first slot in the
-- result tuple.
takeAttrs :: [String] -> ([String], [String])
takeAttrs :: [String] -> ([String], [String])
takeAttrs =
    ([String] -> [String])
-> ([String] -> [String]) -> [String] -> ([String], [String])
forall {c} {b}.
([String] -> c) -> ([String] -> b) -> [String] -> (c, b)
go [String] -> [String]
forall a. a -> a
id [String] -> [String]
forall a. a -> a
id
  where
    go :: ([String] -> c) -> ([String] -> b) -> [String] -> (c, b)
go [String] -> c
x [String] -> b
y [] = ([String] -> c
x [], [String] -> b
y [])
    go [String] -> c
x [String] -> b
y ((Char
'!':String
attr):[String]
rest) = ([String] -> c) -> ([String] -> b) -> [String] -> (c, b)
go ([String] -> c
x ([String] -> c) -> ([String] -> [String]) -> [String] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
attrString -> [String] -> [String]
forall a. a -> [a] -> [a]
:)) [String] -> b
y [String]
rest
    go [String] -> c
x [String] -> b
y (String
z:[String]
rest) = ([String] -> c) -> ([String] -> b) -> [String] -> (c, b)
go [String] -> c
x ([String] -> b
y ([String] -> b) -> ([String] -> [String]) -> [String] -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
zString -> [String] -> [String]
forall a. a -> [a] -> [a]
:)) [String]
rest

dispatchFromString :: MonadFail m => [String] -> Maybe String -> m (Dispatch String)
dispatchFromString :: forall (m :: * -> *).
MonadFail m =>
[String] -> Maybe String -> m (Dispatch String)
dispatchFromString [String]
rest Maybe String
mmulti
    | [String] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
rest = Dispatch String -> m (Dispatch String)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Dispatch String -> m (Dispatch String))
-> Dispatch String -> m (Dispatch String)
forall a b. (a -> b) -> a -> b
$ Maybe String -> [String] -> Dispatch String
forall typ. Maybe typ -> [String] -> Dispatch typ
Methods Maybe String
mmulti []
    | (String -> Bool) -> [String] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isUpper) [String]
rest = Dispatch String -> m (Dispatch String)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Dispatch String -> m (Dispatch String))
-> Dispatch String -> m (Dispatch String)
forall a b. (a -> b) -> a -> b
$ Maybe String -> [String] -> Dispatch String
forall typ. Maybe typ -> [String] -> Dispatch typ
Methods Maybe String
mmulti [String]
rest
dispatchFromString [String
subTyp, String
subFun] Maybe String
Nothing =
    Dispatch String -> m (Dispatch String)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Dispatch String -> m (Dispatch String))
-> Dispatch String -> m (Dispatch String)
forall a b. (a -> b) -> a -> b
$ String -> String -> Dispatch String
forall typ. typ -> String -> Dispatch typ
Subsite String
subTyp String
subFun
dispatchFromString [String
_, String
_] Just{} =
    String -> m (Dispatch String)
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Subsites cannot have a multipiece"
dispatchFromString [String]
rest Maybe String
_ = String -> m (Dispatch String)
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m (Dispatch String)) -> String -> m (Dispatch String)
forall a b. (a -> b) -> a -> b
$ String
"Invalid list of methods: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
forall a. Show a => a -> String
show [String]
rest

drop1Slash :: String -> String
drop1Slash :: String -> String
drop1Slash (Char
'/':String
x) = String
x
drop1Slash String
x = String
x

piecesFromString :: MonadFail m => String -> m ([(CheckOverlap, Piece String)], Maybe (CheckOverlap, String))
piecesFromString :: forall (m :: * -> *).
MonadFail m =>
String -> m ([(Bool, Piece String)], Maybe (Bool, String))
piecesFromString String
"" = ([(Bool, Piece String)], Maybe (Bool, String))
-> m ([(Bool, Piece String)], Maybe (Bool, String))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], Maybe (Bool, String)
forall a. Maybe a
Nothing)
piecesFromString String
x = do
    rest <- String -> m ([(Bool, Piece String)], Maybe (Bool, String))
forall (m :: * -> *).
MonadFail m =>
String -> m ([(Bool, Piece String)], Maybe (Bool, String))
piecesFromString (String -> m ([(Bool, Piece String)], Maybe (Bool, String)))
-> String -> m ([(Bool, Piece String)], Maybe (Bool, String))
forall a b. (a -> b) -> a -> b
$ Int -> String -> String
forall a. Int -> [a] -> [a]
drop Int
1 String
z
    this <- pieceFromString y
    case (this, rest) of
        (Left (Bool, String)
typ, ([], Maybe (Bool, String)
Nothing)) -> ([(Bool, Piece String)], Maybe (Bool, String))
-> m ([(Bool, Piece String)], Maybe (Bool, String))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], (Bool, String) -> Maybe (Bool, String)
forall a. a -> Maybe a
Just (Bool, String)
typ)
        (Left (Bool, String)
_, ([(Bool, Piece String)], Maybe (Bool, String))
_) -> String -> m ([(Bool, Piece String)], Maybe (Bool, String))
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Multipiece must be last piece"
        (Right (Bool, Piece String)
piece, ([(Bool, Piece String)]
pieces, Maybe (Bool, String)
mtyp)) -> ([(Bool, Piece String)], Maybe (Bool, String))
-> m ([(Bool, Piece String)], Maybe (Bool, String))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Bool, Piece String)
piece(Bool, Piece String)
-> [(Bool, Piece String)] -> [(Bool, Piece String)]
forall a. a -> [a] -> [a]
:[(Bool, Piece String)]
pieces, Maybe (Bool, String)
mtyp)
  where
    (String
y, String
z) = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/') String
x

parseType :: String -> Type
parseType :: String -> Type
parseType String
orig =
    Type -> (TypeTree -> Type) -> Maybe TypeTree -> Type
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Type
forall a. HasCallStack => String -> a
error (String -> Type) -> String -> Type
forall a b. (a -> b) -> a -> b
$ String
"Invalid type: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
forall a. Show a => a -> String
show String
orig) TypeTree -> Type
ttToType (Maybe TypeTree -> Type) -> Maybe TypeTree -> Type
forall a b. (a -> b) -> a -> b
$ String -> Maybe TypeTree
parseTypeTree String
orig

-- | 'parseType' in 'MonadFail': a malformed type surfaces via 'fail' instead of
-- a raw 'error'. Used at the 'Language.Haskell.TH.Q' splice sites so a bad type
-- in a route definition becomes an attributed compile error. The pure
-- 'parseType' is retained for callers (e.g. tests) supplying known-good input.
--
-- @since 1.7.0.0
parseTypeM :: MonadFail m => String -> m Type
parseTypeM :: forall (m :: * -> *). MonadFail m => String -> m Type
parseTypeM String
orig =
    m Type -> (TypeTree -> m Type) -> Maybe TypeTree -> m Type
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> m Type
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m Type) -> String -> m Type
forall a b. (a -> b) -> a -> b
$ String
"Invalid type: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
forall a. Show a => a -> String
show String
orig) (Type -> m Type
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Type -> m Type) -> (TypeTree -> Type) -> TypeTree -> m Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TypeTree -> Type
ttToType) (Maybe TypeTree -> m Type) -> Maybe TypeTree -> m Type
forall a b. (a -> b) -> a -> b
$ String -> Maybe TypeTree
parseTypeTree String
orig

parseTypeTree :: String -> Maybe TypeTree
parseTypeTree :: String -> Maybe TypeTree
parseTypeTree String
orig =
    [String] -> Maybe TypeTree
toTypeTree [String]
pieces
  where
    pieces :: [String]
pieces = (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (String -> Bool) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> String -> [String]
forall {a}. (a -> Bool) -> [a] -> [[a]]
splitOn (\Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ') (String -> [String]) -> String -> [String]
forall a b. (a -> b) -> a -> b
$ String -> String
addDashes String
orig
    addDashes :: String -> String
addDashes [] = []
    addDashes (Char
x:String
xs) =
        String -> String
front (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String -> String
addDashes String
xs
      where
        front :: String -> String
front String
rest
            | Char
x Char -> String -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
"()[]" = Char
'-' Char -> String -> String
forall a. a -> [a] -> [a]
: Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: Char
'-' Char -> String -> String
forall a. a -> [a] -> [a]
: String
rest
            | Bool
otherwise = Char
x Char -> String -> String
forall a. a -> [a] -> [a]
: String
rest
    splitOn :: (a -> Bool) -> [a] -> [[a]]
splitOn a -> Bool
c [a]
s =
        case [a]
y' of
            a
_:[a]
y -> [a]
x [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: (a -> Bool) -> [a] -> [[a]]
splitOn a -> Bool
c [a]
y
            [] -> [[a]
x]
      where
        ([a]
x, [a]
y') = (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break a -> Bool
c [a]
s

data TypeTree = TTTerm String
              | TTApp TypeTree TypeTree
              | TTList TypeTree
    deriving (Int -> TypeTree -> String -> String
[TypeTree] -> String -> String
TypeTree -> String
(Int -> TypeTree -> String -> String)
-> (TypeTree -> String)
-> ([TypeTree] -> String -> String)
-> Show TypeTree
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
$cshowsPrec :: Int -> TypeTree -> String -> String
showsPrec :: Int -> TypeTree -> String -> String
$cshow :: TypeTree -> String
show :: TypeTree -> String
$cshowList :: [TypeTree] -> String -> String
showList :: [TypeTree] -> String -> String
Show, TypeTree -> TypeTree -> Bool
(TypeTree -> TypeTree -> Bool)
-> (TypeTree -> TypeTree -> Bool) -> Eq TypeTree
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TypeTree -> TypeTree -> Bool
== :: TypeTree -> TypeTree -> Bool
$c/= :: TypeTree -> TypeTree -> Bool
/= :: TypeTree -> TypeTree -> Bool
Eq)

toTypeTree :: [String] -> Maybe TypeTree
toTypeTree :: [String] -> Maybe TypeTree
toTypeTree [String]
orig = do
    (x, []) <- [String] -> Maybe (TypeTree, [String])
gos [String]
orig
    return x
  where
    go :: [String] -> Maybe (TypeTree, [String])
go [] = Maybe (TypeTree, [String])
forall a. Maybe a
Nothing
    go (String
"(":[String]
xs) = do
        (x, rest) <- [String] -> Maybe (TypeTree, [String])
gos [String]
xs
        case rest of
            String
")":[String]
rest' -> (TypeTree, [String]) -> Maybe (TypeTree, [String])
forall a. a -> Maybe a
Just (TypeTree
x, [String]
rest')
            [String]
_ -> Maybe (TypeTree, [String])
forall a. Maybe a
Nothing
    go (String
"[":[String]
xs) = do
        (x, rest) <- [String] -> Maybe (TypeTree, [String])
gos [String]
xs
        case rest of
            String
"]":[String]
rest' -> (TypeTree, [String]) -> Maybe (TypeTree, [String])
forall a. a -> Maybe a
Just (TypeTree -> TypeTree
TTList TypeTree
x, [String]
rest')
            [String]
_ -> Maybe (TypeTree, [String])
forall a. Maybe a
Nothing
    go (String
x:[String]
xs) = (TypeTree, [String]) -> Maybe (TypeTree, [String])
forall a. a -> Maybe a
Just (String -> TypeTree
TTTerm String
x, [String]
xs)

    gos :: [String] -> Maybe (TypeTree, [String])
gos [String]
xs1 = do
        (t, xs2) <- [String] -> Maybe (TypeTree, [String])
go [String]
xs1
        (ts, xs3) <- gos' id xs2
        Just (foldl' TTApp t ts, xs3)

    gos' :: ([TypeTree] -> [TypeTree])
-> [String] -> Maybe ([TypeTree], [String])
gos' [TypeTree] -> [TypeTree]
front [] = ([TypeTree], [String]) -> Maybe ([TypeTree], [String])
forall a. a -> Maybe a
Just ([TypeTree] -> [TypeTree]
front [], [])
    gos' [TypeTree] -> [TypeTree]
front (String
x:[String]
xs)
        | String
x String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String -> [String]
words String
") ]" = ([TypeTree], [String]) -> Maybe ([TypeTree], [String])
forall a. a -> Maybe a
Just ([TypeTree] -> [TypeTree]
front [], String
xString -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
xs)
        | Bool
otherwise = do
            (t, xs') <- [String] -> Maybe (TypeTree, [String])
go ([String] -> Maybe (TypeTree, [String]))
-> [String] -> Maybe (TypeTree, [String])
forall a b. (a -> b) -> a -> b
$ String
xString -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
xs
            gos' (front . (t:)) xs'

ttToType :: TypeTree -> Type
ttToType :: TypeTree -> Type
ttToType (TTTerm String
s) = (Type, Name) -> Type
forall a b. (a, b) -> a
fst ((Type, Name) -> Type) -> (Type, Name) -> Type
forall a b. (a -> b) -> a -> b
$ String -> (Type, Name)
nameToType String
s
ttToType (TTApp TypeTree
x TypeTree
y) = TypeTree -> Type
ttToType TypeTree
x Type -> Type -> Type
`AppT` TypeTree -> Type
ttToType TypeTree
y
ttToType (TTList TypeTree
t) = Type
ListT Type -> Type -> Type
`AppT` TypeTree -> Type
ttToType TypeTree
t

nameToType :: String -> (Type, Name)
nameToType :: String -> (Type, Name)
nameToType String
t = (, Name
nm) (Type -> (Type, Name)) -> Type -> (Type, Name)
forall a b. (a -> b) -> a -> b
$ if String -> Bool
isTvar String
t
               then Name -> Type
VarT Name
nm
               else Name -> Type
ConT Name
nm
    where nm :: Name
nm = String -> Name
mkName String
t

isTvar :: String -> Bool
isTvar :: String -> Bool
isTvar (Char
h:String
_) = Char -> Bool
isLower Char
h
isTvar String
_     = Bool
False

-- | 'MonadFail' so that an unclosed bracket in a 'Dynamic' piece (via
-- 'dropBracketM') surfaces as an attributed splice error rather than a raw
-- 'error' (the pure 'dropBracket' would otherwise bottom out deep in codegen).
pieceFromString :: MonadFail m => String -> m (Either (CheckOverlap, String) (CheckOverlap, Piece String))
pieceFromString :: forall (m :: * -> *).
MonadFail m =>
String -> m (Either (Bool, String) (Bool, Piece String))
pieceFromString (Char
'#':Char
'!':String
x) = (Bool, Piece String) -> Either (Bool, String) (Bool, Piece String)
forall a b. b -> Either a b
Right ((Bool, Piece String)
 -> Either (Bool, String) (Bool, Piece String))
-> (String -> (Bool, Piece String))
-> String
-> Either (Bool, String) (Bool, Piece String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool
False,) (Piece String -> (Bool, Piece String))
-> (String -> Piece String) -> String -> (Bool, Piece String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Piece String
forall typ. typ -> Piece typ
Dynamic (String -> Either (Bool, String) (Bool, Piece String))
-> m String -> m (Either (Bool, String) (Bool, Piece String))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> m String
forall (m :: * -> *). MonadFail m => String -> m String
dropBracketM String
x
pieceFromString (Char
'!':Char
'#':String
x) = (Bool, Piece String) -> Either (Bool, String) (Bool, Piece String)
forall a b. b -> Either a b
Right ((Bool, Piece String)
 -> Either (Bool, String) (Bool, Piece String))
-> (String -> (Bool, Piece String))
-> String
-> Either (Bool, String) (Bool, Piece String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool
False,) (Piece String -> (Bool, Piece String))
-> (String -> Piece String) -> String -> (Bool, Piece String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Piece String
forall typ. typ -> Piece typ
Dynamic (String -> Either (Bool, String) (Bool, Piece String))
-> m String -> m (Either (Bool, String) (Bool, Piece String))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> m String
forall (m :: * -> *). MonadFail m => String -> m String
dropBracketM String
x -- https://github.com/yesodweb/yesod/issues/652
pieceFromString (Char
'#':String
x) = (Bool, Piece String) -> Either (Bool, String) (Bool, Piece String)
forall a b. b -> Either a b
Right ((Bool, Piece String)
 -> Either (Bool, String) (Bool, Piece String))
-> (String -> (Bool, Piece String))
-> String
-> Either (Bool, String) (Bool, Piece String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool
True,) (Piece String -> (Bool, Piece String))
-> (String -> Piece String) -> String -> (Bool, Piece String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Piece String
forall typ. typ -> Piece typ
Dynamic (String -> Either (Bool, String) (Bool, Piece String))
-> m String -> m (Either (Bool, String) (Bool, Piece String))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> m String
forall (m :: * -> *). MonadFail m => String -> m String
dropBracketM String
x

pieceFromString (Char
'*':Char
'!':String
x) = Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either (Bool, String) (Bool, Piece String)
 -> m (Either (Bool, String) (Bool, Piece String)))
-> Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a b. (a -> b) -> a -> b
$ (Bool, String) -> Either (Bool, String) (Bool, Piece String)
forall a b. a -> Either a b
Left (Bool
False, String
x)
pieceFromString (Char
'+':Char
'!':String
x) = Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either (Bool, String) (Bool, Piece String)
 -> m (Either (Bool, String) (Bool, Piece String)))
-> Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a b. (a -> b) -> a -> b
$ (Bool, String) -> Either (Bool, String) (Bool, Piece String)
forall a b. a -> Either a b
Left (Bool
False, String
x)

pieceFromString (Char
'!':Char
'*':String
x) = Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either (Bool, String) (Bool, Piece String)
 -> m (Either (Bool, String) (Bool, Piece String)))
-> Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a b. (a -> b) -> a -> b
$ (Bool, String) -> Either (Bool, String) (Bool, Piece String)
forall a b. a -> Either a b
Left (Bool
False, String
x)
pieceFromString (Char
'!':Char
'+':String
x) = Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either (Bool, String) (Bool, Piece String)
 -> m (Either (Bool, String) (Bool, Piece String)))
-> Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a b. (a -> b) -> a -> b
$ (Bool, String) -> Either (Bool, String) (Bool, Piece String)
forall a b. a -> Either a b
Left (Bool
False, String
x)

pieceFromString (Char
'*':String
x) = Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either (Bool, String) (Bool, Piece String)
 -> m (Either (Bool, String) (Bool, Piece String)))
-> Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a b. (a -> b) -> a -> b
$ (Bool, String) -> Either (Bool, String) (Bool, Piece String)
forall a b. a -> Either a b
Left (Bool
True, String
x)
pieceFromString (Char
'+':String
x) = Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either (Bool, String) (Bool, Piece String)
 -> m (Either (Bool, String) (Bool, Piece String)))
-> Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a b. (a -> b) -> a -> b
$ (Bool, String) -> Either (Bool, String) (Bool, Piece String)
forall a b. a -> Either a b
Left (Bool
True, String
x)

pieceFromString (Char
'!':String
x) = Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either (Bool, String) (Bool, Piece String)
 -> m (Either (Bool, String) (Bool, Piece String)))
-> Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a b. (a -> b) -> a -> b
$ (Bool, Piece String) -> Either (Bool, String) (Bool, Piece String)
forall a b. b -> Either a b
Right (Bool
False, String -> Piece String
forall typ. String -> Piece typ
Static String
x)
pieceFromString String
x = Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either (Bool, String) (Bool, Piece String)
 -> m (Either (Bool, String) (Bool, Piece String)))
-> Either (Bool, String) (Bool, Piece String)
-> m (Either (Bool, String) (Bool, Piece String))
forall a b. (a -> b) -> a -> b
$ (Bool, Piece String) -> Either (Bool, String) (Bool, Piece String)
forall a b. b -> Either a b
Right (Bool
True, String -> Piece String
forall typ. String -> Piece typ
Static String
x)

dropBracket :: String -> String
dropBracket :: String -> String
dropBracket str :: String
str@(Char
'{':String
x) = case (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'}') String
x of
    (String
s, String
"}") -> String
s
    (String, String)
_ -> String -> String
forall a. HasCallStack => String -> a
error (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String
"Unclosed bracket ('{'): " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
str
dropBracket String
x = String
x

-- | 'dropBracket' in 'MonadFail': an unclosed @{@ surfaces via 'fail' instead
-- of a raw 'error', so it becomes an attributed compile error at a splice site.
--
-- @since 1.7.0.0
dropBracketM :: MonadFail m => String -> m String
dropBracketM :: forall (m :: * -> *). MonadFail m => String -> m String
dropBracketM str :: String
str@(Char
'{':String
x) = case (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'}') String
x of
    (String
s, String
"}") -> String -> m String
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure String
s
    (String, String)
_ -> String -> m String
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m String) -> String -> m String
forall a b. (a -> b) -> a -> b
$ String
"Unclosed bracket ('{'): " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
str
dropBracketM String
x = String -> m String
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure String
x

-- | If this line ends with a backslash, concatenate it together with the next line.
--
-- @since 1.6.8
lineContinuations :: String -> [String] -> [String]
lineContinuations :: String -> [String] -> [String]
lineContinuations String
this [] = [String
this]
lineContinuations String
this below :: [String]
below@(String
next:[String]
rest) = case String -> Maybe (String, Char)
forall {b}. [b] -> Maybe ([b], b)
unsnoc String
this of
    Just (String
this', Char
'\\') -> (String
this' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
next)String -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
rest
    Maybe (String, Char)
_ -> String
thisString -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
below
  where unsnoc :: [b] -> Maybe ([b], b)
unsnoc [b]
s = if [b] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [b]
s then Maybe ([b], b)
forall a. Maybe a
Nothing else ([b], b) -> Maybe ([b], b)
forall a. a -> Maybe a
Just ([b] -> [b]
forall a. HasCallStack => [a] -> [a]
init [b]
s, [b] -> b
forall a. HasCallStack => [a] -> a
last [b]
s)