{-# LANGUAGE TemplateHaskellQuotes #-}
module Css.Selector.QuasiQuoters (
csssel, cssselFile, parseCss
) where
import Css.Selector.Core(SelectorGroup, toPattern)
import Css.Selector.Lexer(alexScanTokens)
import Css.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)
parseCss :: String
-> SelectorGroup
parseCss = cssselector . alexScanTokens . filter ('\r' /=)
liftDataWithText :: Data a => a -> Q Exp
liftDataWithText = dataToExpQ ((((AppE (VarE 'pack) <$>) . lift . unpack) <$>) . cast)
csssel :: QuasiQuoter
csssel = QuasiQuoter {
quoteExp = liftDataWithText . parseCss,
quotePat = pure . toPattern . parseCss,
quoteType = const (reportWarning "The type of the quasiquoter will always use the SelectorGroup type." >> pure (ConT ''SelectorGroup)),
quoteDec = const (reportWarning "The use of this quasiquoter will not make any declarations." >> pure [])
}
cssselFile :: QuasiQuoter
cssselFile = quoteFile csssel