{-# LANGUAGE TemplateHaskellQuotes #-}

{-|
Module      : Css3.Selector.QuasiQuoters
Description : A module that defines a quasiquoter to parse a string to a css selector.
Maintainer  : hapytexeu+gh@gmail.com
Stability   : experimental
Portability : POSIX

A module that defines a quasiquoter to parse a string to a css selector.
-}
module Css3.Selector.QuasiQuoters (
    csssel, cssselFile, parseCss
  ) where

import Css3.Selector.Core(SelectorGroup, toPattern)
import Css3.Selector.Lexer(alexScanTokens)
import Css3.Selector.Parser(cssselector)

import Data.Data(Data, cast)
import Data.Text(pack, unpack)

import Language.Haskell.TH.Quote(QuasiQuoter(QuasiQuoter, quoteExp, quotePat, quoteType, quoteDec), quoteFile)
import Language.Haskell.TH.Syntax(Exp(AppE, VarE), Q, Type(ConT), dataToExpQ, lift, reportWarning)

-- | Parse the string to a 'SelectorGroup'.
parseCss :: String -- ^ The string to be parsed to a 'SelectorGroup'
    -> SelectorGroup -- ^ The selectorgroup that is the equivalent of the given 'String'.
parseCss :: String -> SelectorGroup
parseCss String
st = Either String [TokenLoc] -> SelectorGroup
al (String -> Either String [TokenLoc]
alexScanTokens String
st')
  where st' :: String
st' = forall a. (a -> Bool) -> [a] -> [a]
filter (Char
'\r' forall a. Eq a => a -> a -> Bool
/=) String
st
        al :: Either String [TokenLoc] -> SelectorGroup
al (Left String
er) = forall a. HasCallStack => String -> a
error String
er
        al (Right [TokenLoc]
val) = [TokenLoc] -> SelectorGroup
cssselector [TokenLoc]
val

liftDataWithText :: Data a => a -> Q Exp
liftDataWithText :: forall a. Data a => a -> Q Exp
liftDataWithText = forall (m :: * -> *) a.
(Quote m, Data a) =>
(forall b. Data b => b -> Maybe (m Exp)) -> a -> m Exp
dataToExpQ ((((Exp -> Exp -> Exp
AppE (Name -> Exp
VarE 'pack) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t (m :: * -> *). (Lift t, Quote m) => t -> m Exp
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
unpack) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast)

-- | A quasiquoter that can be used to construct a 'SelectorGroup' for the given
-- css selector. In case the css selector is invalid. A compiler error will be
-- thrown (at compile time).
csssel :: QuasiQuoter
csssel :: QuasiQuoter
csssel = QuasiQuoter {
    quoteExp :: String -> Q Exp
quoteExp = forall a. Data a => a -> Q Exp
liftDataWithText forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> SelectorGroup
parseCss,
    quotePat :: String -> Q Pat
quotePat = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToCssSelector a => a -> Pat
toPattern forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> SelectorGroup
parseCss,
    quoteType :: String -> Q Type
quoteType = forall a b. a -> b -> a
const (String -> Q ()
reportWarning String
"The type of the quasiquoter will always use the SelectorGroup type." forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Applicative f => a -> f a
pure (Name -> Type
ConT ''SelectorGroup)),
    quoteDec :: String -> Q [Dec]
quoteDec = forall a b. a -> b -> a
const (String -> Q ()
reportWarning String
"The use of this quasiquoter will not make any declarations." forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Applicative f => a -> f a
pure [])
  }

-- | A quasiquoter that takes the content from the file, and then runs the
-- content of that file as a 'csssel' quasiquote.
cssselFile :: QuasiQuoter
cssselFile :: QuasiQuoter
cssselFile = QuasiQuoter -> QuasiQuoter
quoteFile QuasiQuoter
csssel