{-# 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 :: [Char] -> Q Exp
quoteExp = [Char] -> Q Exp
forall {m :: * -> *}. (MonadFail m, Quote m) => [Char] -> m Exp
x }
  where
    x :: [Char] -> m Exp
x [Char]
s = do
        res <- [Char] -> m [ResourceTree [Char]]
forall (m :: * -> *).
MonadFail m =>
[Char] -> m [ResourceTree [Char]]
resourcesFromString [Char]
s
        case findOverlapNames res of
            [] -> [ResourceTree [Char]] -> m Exp
forall t (m :: * -> *). (Lift t, Quote m) => t -> m Exp
forall (m :: * -> *). Quote m => [ResourceTree [Char]] -> m Exp
lift [ResourceTree [Char]]
res
            [([Char], [Char])]
z -> [Char] -> m Exp
forall a. HasCallStack => [Char] -> m a
forall (m :: * -> *) a.
(MonadFail m, HasCallStack) =>
[Char] -> m a
fail ([Char] -> m Exp) -> [Char] -> m Exp
forall a b. (a -> b) -> a -> b
$ [[Char]] -> [Char]
unlines ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ [Char]
"Overlapping routes: " [Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
: (([Char], [Char]) -> [Char]) -> [([Char], [Char])] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ([Char], [Char]) -> [Char]
forall a. Show a => a -> [Char]
show [([Char], [Char])]
z

-- | Same as 'parseRoutes', but uses an external file instead of quasiquotation.
--
-- The recommended file extension is @.yesodroutes@.
parseRoutesFile :: FilePath -> Q Exp
parseRoutesFile :: [Char] -> Q Exp
parseRoutesFile = QuasiQuoter -> [Char] -> 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 :: [Char] -> Q Exp
parseRoutesFileNoCheck = QuasiQuoter -> [Char] -> Q Exp
parseRoutesFileWith QuasiQuoter
parseRoutesNoCheck

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

readUtf8File :: FilePath -> IO String
readUtf8File :: [Char] -> IO [Char]
readUtf8File [Char]
fp = do
    h <- [Char] -> IOMode -> IO Handle
SIO.openFile [Char]
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 :: [Char] -> Q Exp
quoteExp = \[Char]
s -> [Char] -> Q [ResourceTree [Char]]
forall (m :: * -> *).
MonadFail m =>
[Char] -> m [ResourceTree [Char]]
resourcesFromString [Char]
s Q [ResourceTree [Char]]
-> ([ResourceTree [Char]] -> 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 [Char]] -> Q Exp
forall t (m :: * -> *). (Lift t, Quote m) => t -> m Exp
forall (m :: * -> *). Quote m => [ResourceTree [Char]] -> 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 =>
[Char] -> m [ResourceTree [Char]]
resourcesFromString =
    (([ResourceTree [Char]], [[Char]]) -> [ResourceTree [Char]])
-> m ([ResourceTree [Char]], [[Char]]) -> m [ResourceTree [Char]]
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([ResourceTree [Char]], [[Char]]) -> [ResourceTree [Char]]
forall a b. (a, b) -> a
fst (m ([ResourceTree [Char]], [[Char]]) -> m [ResourceTree [Char]])
-> ([Char] -> m ([ResourceTree [Char]], [[Char]]))
-> [Char]
-> m [ResourceTree [Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [[Char]] -> m ([ResourceTree [Char]], [[Char]])
forall {f :: * -> *}.
MonadFail f =>
Int -> [[Char]] -> f ([ResourceTree [Char]], [[Char]])
parse Int
0 ([[Char]] -> m ([ResourceTree [Char]], [[Char]]))
-> ([Char] -> [[Char]])
-> [Char]
-> m ([ResourceTree [Char]], [[Char]])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([Char] -> Bool) -> [Char] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ')) ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> [[Char]] -> [[Char]])
-> [[Char]] -> [[Char]] -> [[Char]]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr [Char] -> [[Char]] -> [[Char]]
lineContinuations [] ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
lines ([Char] -> [[Char]]) -> ([Char] -> [Char]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> [Char] -> [Char]
forall a. (a -> Bool) -> [a] -> [a]
filter (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\r')
  where
    parse :: Int -> [[Char]] -> f ([ResourceTree [Char]], [[Char]])
parse Int
_ [] = ([ResourceTree [Char]], [[Char]])
-> f ([ResourceTree [Char]], [[Char]])
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], [])
    parse Int
indent ([Char]
thisLine:[[Char]]
otherLines)
        | [Char] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Char]
spaces Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
indent = ([ResourceTree [Char]], [[Char]])
-> f ([ResourceTree [Char]], [[Char]])
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], [Char]
thisLine [Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
: [[Char]]
otherLines)
        | Bool
otherwise = do
            (this, otherLines') <- f ([ResourceTree [Char]] -> [ResourceTree [Char]], [[Char]])
classify
            (others, remainder) <- parse indent otherLines'
            pure (this others, remainder)
      where
        parseAttr :: [Char] -> Maybe [Char]
parseAttr (Char
'!':[Char]
x) = [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
x
        parseAttr [Char]
_ = Maybe [Char]
forall a. Maybe a
Nothing

        stripColonLast :: [[Char]] -> Maybe [[Char]]
stripColonLast =
            ([[Char]] -> [[Char]]) -> [[Char]] -> Maybe [[Char]]
forall {a}. ([[Char]] -> a) -> [[Char]] -> Maybe a
go [[Char]] -> [[Char]]
forall a. a -> a
id
          where
            go :: ([[Char]] -> a) -> [[Char]] -> Maybe a
go [[Char]] -> a
_ [] = Maybe a
forall a. Maybe a
Nothing
            go [[Char]] -> a
front [[Char]
x]
                | [Char] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Char]
x = Maybe a
forall a. Maybe a
Nothing
                | [Char] -> Char
forall a. HasCallStack => [a] -> a
last [Char]
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
$ [[Char]] -> a
front [[Char] -> [Char]
forall a. HasCallStack => [a] -> [a]
init [Char]
x]
                | Bool
otherwise = Maybe a
forall a. Maybe a
Nothing
            go [[Char]] -> a
front ([Char]
x:[[Char]]
xs) = ([[Char]] -> a) -> [[Char]] -> Maybe a
go ([[Char]] -> a
front ([[Char]] -> a) -> ([[Char]] -> [[Char]]) -> [[Char]] -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char]
x[Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
:)) [[Char]]
xs

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

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

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

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

addAttrs :: [String] -> [ResourceTree String] -> [ResourceTree String]
addAttrs :: [[Char]] -> [ResourceTree [Char]] -> [ResourceTree [Char]]
addAttrs [[Char]]
attrs =
    (ResourceTree [Char] -> ResourceTree [Char])
-> [ResourceTree [Char]] -> [ResourceTree [Char]]
forall a b. (a -> b) -> [a] -> [b]
map ResourceTree [Char] -> ResourceTree [Char]
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 [Char]
v Bool
w Set [Char]
x [Piece typ]
y [ResourceTree typ]
z) = [Char]
-> Bool
-> Set [Char]
-> [Piece typ]
-> [ResourceTree typ]
-> ResourceTree typ
forall typ.
[Char]
-> Bool
-> Set [Char]
-> [Piece typ]
-> [ResourceTree typ]
-> ResourceTree typ
ResourceParent [Char]
v Bool
w Set [Char]
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 [Char]
usedKeys = [[Char]] -> Set [Char]
forall a. Ord a => [a] -> Set a
Set.fromList ([[Char]] -> Set [Char]) -> [[Char]] -> Set [Char]
forall a b. (a -> b) -> a -> b
$ (([Char], [Char]) -> [Char]) -> [([Char], [Char])] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ([Char], [Char]) -> [Char]
forall a b. (a, b) -> a
fst ([([Char], [Char])] -> [[Char]]) -> [([Char], [Char])] -> [[Char]]
forall a b. (a -> b) -> a -> b
$ ([Char] -> Maybe ([Char], [Char]))
-> [[Char]] -> [([Char], [Char])]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe [Char] -> Maybe ([Char], [Char])
toPair ([[Char]] -> [([Char], [Char])]) -> [[Char]] -> [([Char], [Char])]
forall a b. (a -> b) -> a -> b
$ Resource typ -> [[Char]]
forall typ. Resource typ -> [[Char]]
resourceAttrs Resource typ
res
        used :: [Char] -> Bool
used [Char]
attr =
            case [Char] -> Maybe ([Char], [Char])
toPair [Char]
attr of
                Maybe ([Char], [Char])
Nothing -> Bool
False
                Just ([Char]
key, [Char]
_) -> [Char]
key [Char] -> Set [Char] -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set [Char]
usedKeys
        noDupes :: [[Char]]
noDupes = ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([Char] -> Bool) -> [Char] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Bool
used) [[Char]]
attrs

    toPair :: [Char] -> Maybe ([Char], [Char])
toPair [Char]
s =
        case (Char -> Bool) -> [Char] -> ([Char], [Char])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'=') [Char]
s of
            ([Char]
x, Char
'=':[Char]
y) -> ([Char], [Char]) -> Maybe ([Char], [Char])
forall a. a -> Maybe a
Just ([Char]
x, [Char]
y)
            ([Char], [Char])
_ -> Maybe ([Char], [Char])
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 :: [[Char]] -> ([[Char]], [[Char]])
takeAttrs =
    ([[Char]] -> [[Char]])
-> ([[Char]] -> [[Char]]) -> [[Char]] -> ([[Char]], [[Char]])
forall {t} {t}.
([[Char]] -> t) -> ([[Char]] -> t) -> [[Char]] -> (t, t)
go [[Char]] -> [[Char]]
forall a. a -> a
id [[Char]] -> [[Char]]
forall a. a -> a
id
  where
    go :: ([[Char]] -> t) -> ([[Char]] -> t) -> [[Char]] -> (t, t)
go [[Char]] -> t
x [[Char]] -> t
y [] = ([[Char]] -> t
x [], [[Char]] -> t
y [])
    go [[Char]] -> t
x [[Char]] -> t
y ((Char
'!':[Char]
attr):[[Char]]
rest) = ([[Char]] -> t) -> ([[Char]] -> t) -> [[Char]] -> (t, t)
go ([[Char]] -> t
x ([[Char]] -> t) -> ([[Char]] -> [[Char]]) -> [[Char]] -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char]
attr[Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
:)) [[Char]] -> t
y [[Char]]
rest
    go [[Char]] -> t
x [[Char]] -> t
y ([Char]
z:[[Char]]
rest) = ([[Char]] -> t) -> ([[Char]] -> t) -> [[Char]] -> (t, t)
go [[Char]] -> t
x ([[Char]] -> t
y ([[Char]] -> t) -> ([[Char]] -> [[Char]]) -> [[Char]] -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char]
z[Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
:)) [[Char]]
rest

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

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

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

parseType :: String -> Type
parseType :: [Char] -> Type
parseType [Char]
orig =
    Type -> (TypeTree -> Type) -> Maybe TypeTree -> Type
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Char] -> Type
forall a. HasCallStack => [Char] -> a
error ([Char] -> Type) -> [Char] -> Type
forall a b. (a -> b) -> a -> b
$ [Char]
"Invalid type: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
forall a. Show a => a -> [Char]
show [Char]
orig) TypeTree -> Type
ttToType (Maybe TypeTree -> Type) -> Maybe TypeTree -> Type
forall a b. (a -> b) -> a -> b
$ [Char] -> Maybe TypeTree
parseTypeTree [Char]
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 => [Char] -> m Type
parseTypeM [Char]
orig =
    m Type -> (TypeTree -> m Type) -> Maybe TypeTree -> m Type
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([Char] -> m Type
forall a. HasCallStack => [Char] -> m a
forall (m :: * -> *) a.
(MonadFail m, HasCallStack) =>
[Char] -> m a
fail ([Char] -> m Type) -> [Char] -> m Type
forall a b. (a -> b) -> a -> b
$ [Char]
"Invalid type: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
forall a. Show a => a -> [Char]
show [Char]
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
$ [Char] -> Maybe TypeTree
parseTypeTree [Char]
orig

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

data TypeTree = TTTerm String
              | TTApp TypeTree TypeTree
              | TTList TypeTree
    deriving (Int -> TypeTree -> [Char] -> [Char]
[TypeTree] -> [Char] -> [Char]
TypeTree -> [Char]
(Int -> TypeTree -> [Char] -> [Char])
-> (TypeTree -> [Char])
-> ([TypeTree] -> [Char] -> [Char])
-> Show TypeTree
forall a.
(Int -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
$cshowsPrec :: Int -> TypeTree -> [Char] -> [Char]
showsPrec :: Int -> TypeTree -> [Char] -> [Char]
$cshow :: TypeTree -> [Char]
show :: TypeTree -> [Char]
$cshowList :: [TypeTree] -> [Char] -> [Char]
showList :: [TypeTree] -> [Char] -> [Char]
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 :: [[Char]] -> Maybe TypeTree
toTypeTree [[Char]]
orig = do
    (x, []) <- [[Char]] -> Maybe (TypeTree, [[Char]])
gos [[Char]]
orig
    return x
  where
    go :: [[Char]] -> Maybe (TypeTree, [[Char]])
go [] = Maybe (TypeTree, [[Char]])
forall a. Maybe a
Nothing
    go ([Char]
"(":[[Char]]
xs) = do
        (x, rest) <- [[Char]] -> Maybe (TypeTree, [[Char]])
gos [[Char]]
xs
        case rest of
            [Char]
")":[[Char]]
rest' -> (TypeTree, [[Char]]) -> Maybe (TypeTree, [[Char]])
forall a. a -> Maybe a
Just (TypeTree
x, [[Char]]
rest')
            [[Char]]
_ -> Maybe (TypeTree, [[Char]])
forall a. Maybe a
Nothing
    go ([Char]
"[":[[Char]]
xs) = do
        (x, rest) <- [[Char]] -> Maybe (TypeTree, [[Char]])
gos [[Char]]
xs
        case rest of
            [Char]
"]":[[Char]]
rest' -> (TypeTree, [[Char]]) -> Maybe (TypeTree, [[Char]])
forall a. a -> Maybe a
Just (TypeTree -> TypeTree
TTList TypeTree
x, [[Char]]
rest')
            [[Char]]
_ -> Maybe (TypeTree, [[Char]])
forall a. Maybe a
Nothing
    go ([Char]
x:[[Char]]
xs) = (TypeTree, [[Char]]) -> Maybe (TypeTree, [[Char]])
forall a. a -> Maybe a
Just ([Char] -> TypeTree
TTTerm [Char]
x, [[Char]]
xs)

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

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

ttToType :: TypeTree -> Type
ttToType :: TypeTree -> Type
ttToType (TTTerm [Char]
s) = (Type, Name) -> Type
forall a b. (a, b) -> a
fst ((Type, Name) -> Type) -> (Type, Name) -> Type
forall a b. (a -> b) -> a -> b
$ [Char] -> (Type, Name)
nameToType [Char]
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 :: [Char] -> (Type, Name)
nameToType [Char]
t = (, Name
nm) (Type -> (Type, Name)) -> Type -> (Type, Name)
forall a b. (a -> b) -> a -> b
$ if [Char] -> Bool
isTvar [Char]
t
               then Name -> Type
VarT Name
nm
               else Name -> Type
ConT Name
nm
    where nm :: Name
nm = [Char] -> Name
mkName [Char]
t

isTvar :: String -> Bool
isTvar :: [Char] -> Bool
isTvar (Char
h:[Char]
_) = Char -> Bool
isLower Char
h
isTvar [Char]
_     = 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 =>
[Char] -> m (Either (Bool, [Char]) (Bool, Piece [Char]))
pieceFromString (Char
'#':Char
'!':[Char]
x) = (Bool, Piece [Char]) -> Either (Bool, [Char]) (Bool, Piece [Char])
forall a b. b -> Either a b
Right ((Bool, Piece [Char])
 -> Either (Bool, [Char]) (Bool, Piece [Char]))
-> ([Char] -> (Bool, Piece [Char]))
-> [Char]
-> Either (Bool, [Char]) (Bool, Piece [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool
False,) (Piece [Char] -> (Bool, Piece [Char]))
-> ([Char] -> Piece [Char]) -> [Char] -> (Bool, Piece [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Piece [Char]
forall typ. typ -> Piece typ
Dynamic ([Char] -> Either (Bool, [Char]) (Bool, Piece [Char]))
-> m [Char] -> m (Either (Bool, [Char]) (Bool, Piece [Char]))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> m [Char]
forall (m :: * -> *). MonadFail m => [Char] -> m [Char]
dropBracketM [Char]
x
pieceFromString (Char
'!':Char
'#':[Char]
x) = (Bool, Piece [Char]) -> Either (Bool, [Char]) (Bool, Piece [Char])
forall a b. b -> Either a b
Right ((Bool, Piece [Char])
 -> Either (Bool, [Char]) (Bool, Piece [Char]))
-> ([Char] -> (Bool, Piece [Char]))
-> [Char]
-> Either (Bool, [Char]) (Bool, Piece [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool
False,) (Piece [Char] -> (Bool, Piece [Char]))
-> ([Char] -> Piece [Char]) -> [Char] -> (Bool, Piece [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Piece [Char]
forall typ. typ -> Piece typ
Dynamic ([Char] -> Either (Bool, [Char]) (Bool, Piece [Char]))
-> m [Char] -> m (Either (Bool, [Char]) (Bool, Piece [Char]))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> m [Char]
forall (m :: * -> *). MonadFail m => [Char] -> m [Char]
dropBracketM [Char]
x -- https://github.com/yesodweb/yesod/issues/652
pieceFromString (Char
'#':[Char]
x) = (Bool, Piece [Char]) -> Either (Bool, [Char]) (Bool, Piece [Char])
forall a b. b -> Either a b
Right ((Bool, Piece [Char])
 -> Either (Bool, [Char]) (Bool, Piece [Char]))
-> ([Char] -> (Bool, Piece [Char]))
-> [Char]
-> Either (Bool, [Char]) (Bool, Piece [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool
True,) (Piece [Char] -> (Bool, Piece [Char]))
-> ([Char] -> Piece [Char]) -> [Char] -> (Bool, Piece [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Piece [Char]
forall typ. typ -> Piece typ
Dynamic ([Char] -> Either (Bool, [Char]) (Bool, Piece [Char]))
-> m [Char] -> m (Either (Bool, [Char]) (Bool, Piece [Char]))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> m [Char]
forall (m :: * -> *). MonadFail m => [Char] -> m [Char]
dropBracketM [Char]
x

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

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

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

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

dropBracket :: String -> String
dropBracket :: [Char] -> [Char]
dropBracket str :: [Char]
str@(Char
'{':[Char]
x) = case (Char -> Bool) -> [Char] -> ([Char], [Char])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'}') [Char]
x of
    ([Char]
s, [Char]
"}") -> [Char]
s
    ([Char], [Char])
_ -> [Char] -> [Char]
forall a. HasCallStack => [Char] -> a
error ([Char] -> [Char]) -> [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ [Char]
"Unclosed bracket ('{'): " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
str
dropBracket [Char]
x = [Char]
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 => [Char] -> m [Char]
dropBracketM str :: [Char]
str@(Char
'{':[Char]
x) = case (Char -> Bool) -> [Char] -> ([Char], [Char])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'}') [Char]
x of
    ([Char]
s, [Char]
"}") -> [Char] -> m [Char]
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Char]
s
    ([Char], [Char])
_ -> [Char] -> m [Char]
forall a. HasCallStack => [Char] -> m a
forall (m :: * -> *) a.
(MonadFail m, HasCallStack) =>
[Char] -> m a
fail ([Char] -> m [Char]) -> [Char] -> m [Char]
forall a b. (a -> b) -> a -> b
$ [Char]
"Unclosed bracket ('{'): " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
str
dropBracketM [Char]
x = [Char] -> m [Char]
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Char]
x

-- | If this line ends with a backslash, concatenate it together with the next line.
--
-- @since 1.6.8
lineContinuations :: String -> [String] -> [String]
lineContinuations :: [Char] -> [[Char]] -> [[Char]]
lineContinuations [Char]
this [] = [[Char]
this]
lineContinuations [Char]
this below :: [[Char]]
below@([Char]
next:[[Char]]
rest) = case [Char] -> Maybe ([Char], Char)
forall {b}. [b] -> Maybe ([b], b)
unsnoc [Char]
this of
    Just ([Char]
this', Char
'\\') -> ([Char]
this' [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
next)[Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
:[[Char]]
rest
    Maybe ([Char], Char)
_ -> [Char]
this[Char] -> [[Char]] -> [[Char]]
forall a. a -> [a] -> [a]
:[[Char]]
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)