-- |
-- Module      :  Language.C.Quote
-- Copyright   :  (c) 2006-2011 Harvard University
--                (c) 2011-2013 Geoffrey Mainland
--             :  (c) 2013-2017 Drexel University
-- License     :  BSD-style
-- Maintainer  :  mainland@drexel.edu

{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -w #-}

module Language.C.Quote.Base (
    ToIdent(..),
    ToConst(..),
    ToExp(..),
    qqExp,
    qqPat,
    quasiquote
  ) where

import Control.Monad ((>=>))
import qualified Data.ByteString.Char8 as B
import Data.Char (isAscii, isPrint, ord)
import Data.Data (Data(..))
import Data.Generics (extQ)
import Data.Int
import Data.Loc
import Data.Typeable (Typeable(..))
import Data.Word
#ifdef FULL_HASKELL_ANTIQUOTES
import Language.Haskell.Meta (parseExp,parsePat)
#else
import Language.Haskell.ParseExp (parseExp,parsePat)
#endif
import Language.Haskell.TH as TH
#if MIN_VERSION_template_haskell(2,7,0)
import Language.Haskell.TH.Quote (QuasiQuoter(..),
                                  dataToQa,
                                  dataToExpQ,
                                  dataToPatQ)
#else /* !MIN_VERSION_template_haskell(2,7,0) */
import Language.Haskell.TH.Quote (QuasiQuoter(..))
#endif /* !MIN_VERSION_template_haskell(2,7,0) */
import Language.Haskell.TH.Syntax
import Numeric (showOct, showHex)

import qualified Language.C.Parser as P
import qualified Language.C.Syntax as C

newtype LongDouble = LongDouble Double

-- | An instance of 'ToIndent' can be converted to a 'C.Id'.
class ToIdent a where
    toIdent :: a -> SrcLoc -> C.Id

instance ToIdent C.Id where
    toIdent :: Id -> SrcLoc -> Id
toIdent Id
ident SrcLoc
_ = Id
ident

instance ToIdent (SrcLoc -> C.Id) where
    toIdent :: (SrcLoc -> Id) -> SrcLoc -> Id
toIdent SrcLoc -> Id
ident = SrcLoc -> Id
ident

instance ToIdent String where
    toIdent :: String -> SrcLoc -> Id
toIdent String
s SrcLoc
loc = String -> SrcLoc -> Id
C.Id String
s SrcLoc
loc

-- | An instance of 'ToConst' can be converted to a 'C.Const'.
class ToConst a where
    toConst :: a -> SrcLoc -> C.Const

instance ToConst C.Const where
    toConst :: Const -> SrcLoc -> Const
toConst Const
k SrcLoc
_ = Const
k

instance ToConst Int where
    toConst :: Int -> SrcLoc -> Const
toConst = Integer -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst (Integer -> SrcLoc -> Const)
-> (Int -> Integer) -> Int -> SrcLoc -> Const
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Integer
forall a. Integral a => a -> Integer
toInteger

instance ToConst Int8 where
    toConst :: Int8 -> SrcLoc -> Const
toConst = Integer -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst (Integer -> SrcLoc -> Const)
-> (Int8 -> Integer) -> Int8 -> SrcLoc -> Const
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int8 -> Integer
forall a. Integral a => a -> Integer
toInteger

instance ToConst Int16 where
    toConst :: Int16 -> SrcLoc -> Const
toConst = Integer -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst (Integer -> SrcLoc -> Const)
-> (Int16 -> Integer) -> Int16 -> SrcLoc -> Const
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int16 -> Integer
forall a. Integral a => a -> Integer
toInteger

instance ToConst Int32 where
    toConst :: Int32 -> SrcLoc -> Const
toConst = Integer -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst (Integer -> SrcLoc -> Const)
-> (Int32 -> Integer) -> Int32 -> SrcLoc -> Const
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Integer
forall a. Integral a => a -> Integer
toInteger

instance ToConst Int64 where
    toConst :: Int64 -> SrcLoc -> Const
toConst = Integer -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst (Integer -> SrcLoc -> Const)
-> (Int64 -> Integer) -> Int64 -> SrcLoc -> Const
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger

instance ToConst Word where
    toConst :: Word -> SrcLoc -> Const
toConst Word
n SrcLoc
loc = String -> Signed -> Integer -> SrcLoc -> Const
C.IntConst (Word -> String
forall a. Show a => a -> String
show Word
n) Signed
C.Unsigned (Word -> Integer
forall a. Integral a => a -> Integer
toInteger Word
n) SrcLoc
loc

instance ToConst Word8 where
    toConst :: Word8 -> SrcLoc -> Const
toConst Word8
n SrcLoc
loc = String -> Signed -> Integer -> SrcLoc -> Const
C.IntConst (Word8 -> String
forall a. Show a => a -> String
show Word8
n) Signed
C.Unsigned (Word8 -> Integer
forall a. Integral a => a -> Integer
toInteger Word8
n) SrcLoc
loc

instance ToConst Word16 where
    toConst :: Word16 -> SrcLoc -> Const
toConst Word16
n SrcLoc
loc = String -> Signed -> Integer -> SrcLoc -> Const
C.IntConst (Word16 -> String
forall a. Show a => a -> String
show Word16
n) Signed
C.Unsigned (Word16 -> Integer
forall a. Integral a => a -> Integer
toInteger Word16
n) SrcLoc
loc

instance ToConst Word32 where
    toConst :: Word32 -> SrcLoc -> Const
toConst Word32
n SrcLoc
loc = String -> Signed -> Integer -> SrcLoc -> Const
C.IntConst (Word32 -> String
forall a. Show a => a -> String
show Word32
n) Signed
C.Unsigned (Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger Word32
n) SrcLoc
loc

instance ToConst Word64 where
    toConst :: Word64 -> SrcLoc -> Const
toConst Word64
n SrcLoc
loc = String -> Signed -> Integer -> SrcLoc -> Const
C.IntConst (Word64 -> String
forall a. Show a => a -> String
show Word64
n) Signed
C.Unsigned (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
n) SrcLoc
loc

instance ToConst Integer where
    toConst :: Integer -> SrcLoc -> Const
toConst Integer
n SrcLoc
loc = String -> Signed -> Integer -> SrcLoc -> Const
C.IntConst (Integer -> String
forall a. Show a => a -> String
show Integer
n) Signed
C.Signed Integer
n SrcLoc
loc

instance ToConst Rational where
    toConst :: Rational -> SrcLoc -> Const
toConst Rational
n SrcLoc
loc = Double -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst (Rational -> Double
forall a. Fractional a => Rational -> a
fromRational Rational
n :: Double) SrcLoc
loc

instance ToConst Float where
    toConst :: Float -> SrcLoc -> Const
toConst Float
n SrcLoc
loc = String -> Float -> SrcLoc -> Const
C.FloatConst (Float -> String
forall a. (RealFloat a, Show a) => a -> String
realFloatToString Float
n String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"F") Float
n SrcLoc
loc

instance ToConst Double where
    toConst :: Double -> SrcLoc -> Const
toConst Double
n SrcLoc
loc = String -> Double -> SrcLoc -> Const
C.DoubleConst (Double -> String
forall a. (RealFloat a, Show a) => a -> String
realFloatToString Double
n) Double
n SrcLoc
loc

instance ToConst LongDouble where
    toConst :: LongDouble -> SrcLoc -> Const
toConst (LongDouble Double
n) SrcLoc
loc = String -> Double -> SrcLoc -> Const
C.LongDoubleConst (Double -> String
forall a. (RealFloat a, Show a) => a -> String
realFloatToString Double
n String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"L") Double
n SrcLoc
loc

realFloatToString :: (RealFloat a, Show a) => a -> String
realFloatToString :: a -> String
realFloatToString a
n
  | a -> Bool
forall a. RealFloat a => a -> Bool
isNaN a
n      = String
"NAN"
  | a -> Bool
forall a. RealFloat a => a -> Bool
isInfinite a
n = if a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 then String
"-INFINITY" else String
"INFINITY"
  | Bool
otherwise    = a -> String
forall a. Show a => a -> String
show a
n

instance ToConst Char where
    toConst :: Char -> SrcLoc -> Const
toConst Char
c SrcLoc
loc = String -> Char -> SrcLoc -> Const
C.CharConst (String
"'" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Char -> String
charToString Char
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'") Char
c SrcLoc
loc
      where
        charToString :: Char -> String
        charToString :: Char -> String
charToString Char
'\0' = String
"\\0"
        charToString Char
'\a' = String
"\\a"
        charToString Char
'\b' = String
"\\b"
        charToString Char
'\f' = String
"\\f"
        charToString Char
'\n' = String
"\\n"
        charToString Char
'\r' = String
"\\r"
        charToString Char
'\t' = String
"\\t"
        charToString Char
'\v' = String
"\\v"
        charToString Char
'\\' = String
"\\\\"
        charToString Char
'\"' = String
"\\\""
        charToString Char
c
          | Char -> Bool
isAscii Char
c Bool -> Bool -> Bool
&& Char -> Bool
isPrint Char
c = [Char
c]
          | Char -> Bool
isAscii Char
c              = String
"\\x" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Maybe Int -> Char -> String
hexOf Maybe Int
forall a. Maybe a
Nothing Char
c
          | Char -> Int
ord Char
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0x10000        = String
"\\u" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Maybe Int -> Char -> String
hexOf (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
4) Char
c
          | Bool
otherwise              = String
"\\U" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Maybe Int -> Char -> String
hexOf (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
8) Char
c
          where
            hexOf :: Maybe Int -> Char -> String
            hexOf :: Maybe Int -> Char -> String
hexOf Maybe Int
len Char
c = case Maybe Int
len of
                            Maybe Int
Nothing -> String
hex
                            Just Int
i  -> Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
hex) Char
'0' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
hex
              where
                hex :: String
                hex :: String
hex = Int -> String -> String
forall a. (Integral a, Show a) => a -> String -> String
showHex (Char -> Int
ord Char
c) String
""

instance ToConst String where
    toConst :: String -> SrcLoc -> Const
toConst String
s SrcLoc
loc = [String] -> String -> SrcLoc -> Const
C.StringConst [String -> String
forall a. Show a => a -> String
show String
s] String
s SrcLoc
loc

-- | An instance of 'ToExp' can be converted to a 'C.Exp'.
class ToExp a where
    toExp :: a -> SrcLoc -> C.Exp

instance ToExp C.Exp where
    toExp :: Exp -> SrcLoc -> Exp
toExp Exp
e SrcLoc
_ = Exp
e

instance ToExp Int where
    toExp :: Int -> SrcLoc -> Exp
toExp Int
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Int -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Int
n SrcLoc
loc) SrcLoc
loc

instance ToExp Int8 where
    toExp :: Int8 -> SrcLoc -> Exp
toExp Int8
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Int8 -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Int8
n SrcLoc
loc) SrcLoc
loc

instance ToExp Int16 where
    toExp :: Int16 -> SrcLoc -> Exp
toExp Int16
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Int16 -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Int16
n SrcLoc
loc) SrcLoc
loc

instance ToExp Int32 where
    toExp :: Int32 -> SrcLoc -> Exp
toExp Int32
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Int32 -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Int32
n SrcLoc
loc) SrcLoc
loc

instance ToExp Int64 where
    toExp :: Int64 -> SrcLoc -> Exp
toExp Int64
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Int64 -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Int64
n SrcLoc
loc) SrcLoc
loc

instance ToExp Word where
    toExp :: Word -> SrcLoc -> Exp
toExp Word
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Word -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Word
n SrcLoc
loc) SrcLoc
loc

instance ToExp Word8 where
    toExp :: Word8 -> SrcLoc -> Exp
toExp Word8
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Word8 -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Word8
n SrcLoc
loc) SrcLoc
loc

instance ToExp Word16 where
    toExp :: Word16 -> SrcLoc -> Exp
toExp Word16
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Word16 -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Word16
n SrcLoc
loc) SrcLoc
loc

instance ToExp Word32 where
    toExp :: Word32 -> SrcLoc -> Exp
toExp Word32
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Word32 -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Word32
n SrcLoc
loc) SrcLoc
loc

instance ToExp Word64 where
    toExp :: Word64 -> SrcLoc -> Exp
toExp Word64
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Word64 -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Word64
n SrcLoc
loc) SrcLoc
loc

instance ToExp Integer where
    toExp :: Integer -> SrcLoc -> Exp
toExp Integer
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Integer -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Integer
n SrcLoc
loc) SrcLoc
loc

instance ToExp Rational where
    toExp :: Rational -> SrcLoc -> Exp
toExp Rational
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Rational -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Rational
n SrcLoc
loc) SrcLoc
loc

instance ToExp Float where
    toExp :: Float -> SrcLoc -> Exp
toExp Float
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Float -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Float
n SrcLoc
loc) SrcLoc
loc

instance ToExp Double where
    toExp :: Double -> SrcLoc -> Exp
toExp Double
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Double -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Double
n SrcLoc
loc) SrcLoc
loc

instance ToExp Char where
    toExp :: Char -> SrcLoc -> Exp
toExp Char
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (Char -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst Char
n SrcLoc
loc) SrcLoc
loc

instance ToExp String where
    toExp :: String -> SrcLoc -> Exp
toExp String
n SrcLoc
loc = Const -> SrcLoc -> Exp
C.Const (String -> SrcLoc -> Const
forall a. ToConst a => a -> SrcLoc -> Const
toConst String
n SrcLoc
loc) SrcLoc
loc

antiVarE :: String -> ExpQ
antiVarE :: String -> ExpQ
antiVarE = (String -> ExpQ) -> (Exp -> ExpQ) -> Either String Exp -> ExpQ
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> ExpQ
forall (m :: * -> *) a. MonadFail m => String -> m a
fail Exp -> ExpQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String Exp -> ExpQ)
-> (String -> Either String Exp) -> String -> ExpQ
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Either String Exp
parseExp

qqLocE :: SrcLoc -> ExpQ
qqLocE :: SrcLoc -> ExpQ
qqLocE SrcLoc
loc = (forall b. Data b => b -> Maybe ExpQ) -> SrcLoc -> ExpQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe ExpQ) -> a -> ExpQ
dataToExpQ forall b. Data b => b -> Maybe ExpQ
forall a. Typeable a => a -> Maybe ExpQ
qqExp SrcLoc
loc

qqStringE :: String -> Maybe (Q Exp)
qqStringE :: String -> Maybe ExpQ
qqStringE String
s = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ Lit -> ExpQ
litE (Lit -> ExpQ) -> Lit -> ExpQ
forall a b. (a -> b) -> a -> b
$ String -> Lit
stringL String
s

qqIdE :: C.Id -> Maybe (Q Exp)
qqIdE :: Id -> Maybe ExpQ
qqIdE (C.AntiId String
v SrcLoc
loc)  = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|toIdent $(antiVarE v) $(qqLocE loc)|]
qqIdE Id
_                 = Maybe ExpQ
forall a. Maybe a
Nothing

qqDeclSpecE :: C.DeclSpec -> Maybe (Q Exp)
qqDeclSpecE :: DeclSpec -> Maybe ExpQ
qqDeclSpecE (C.AntiDeclSpec String
v SrcLoc
_) = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
v
qqDeclSpecE (C.AntiTypeDeclSpec [Storage]
extraStorage [TypeQual]
extraTypeQuals String
v SrcLoc
_) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|let C.Type (C.DeclSpec storage typeQuals typeSpec loc) _ _
                   = $(antiVarE v)
           in
             C.DeclSpec (storage ++ $(dataToExpQ qqExp extraStorage))
                        (typeQuals ++ $(dataToExpQ qqExp extraTypeQuals))
                        typeSpec
                        loc
         |]
qqDeclSpecE DeclSpec
_ = Maybe ExpQ
forall a. Maybe a
Nothing

qqDeclE :: C.Decl -> Maybe (Q Exp)
qqDeclE :: Decl -> Maybe ExpQ
qqDeclE (C.AntiTypeDecl String
v SrcLoc
_) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|let C.Type _ decl _ = $(antiVarE v) in decl|]
qqDeclE Decl
_ = Maybe ExpQ
forall a. Maybe a
Nothing

qqTypeQualE :: C.TypeQual -> Maybe (Q Exp)
qqTypeQualE :: TypeQual -> Maybe ExpQ
qqTypeQualE (C.AntiTypeQual String
v SrcLoc
_)  = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
v
qqTypeQualE TypeQual
_                     = Maybe ExpQ
forall a. Maybe a
Nothing

qqTypeQualListE :: [C.TypeQual] -> Maybe (Q Exp)
qqTypeQualListE :: [TypeQual] -> Maybe ExpQ
qqTypeQualListE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqTypeQualListE (C.AntiTypeQuals String
v SrcLoc
_ : [TypeQual]
stms) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE v) ++ $(dataToExpQ qqExp stms)|]
qqTypeQualListE (TypeQual
stm : [TypeQual]
stms) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp stm) : $(dataToExpQ qqExp stms)|]

qqTypeE :: C.Type -> Maybe (Q Exp)
qqTypeE :: Type -> Maybe ExpQ
qqTypeE (C.AntiType String
v SrcLoc
_)  = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
v
qqTypeE Type
_                 = Maybe ExpQ
forall a. Maybe a
Nothing

qqInitializerE :: C.Initializer -> Maybe (Q Exp)
qqInitializerE :: Initializer -> Maybe ExpQ
qqInitializerE (C.AntiInit String
v SrcLoc
_)  = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
v
qqInitializerE Initializer
_                 = Maybe ExpQ
forall a. Maybe a
Nothing

qqInitializerListE :: [(Maybe C.Designation, C.Initializer)] -> Maybe (Q Exp)
qqInitializerListE :: [(Maybe Designation, Initializer)] -> Maybe ExpQ
qqInitializerListE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqInitializerListE ((Maybe Designation
Nothing, C.AntiInits String
v SrcLoc
_) : [(Maybe Designation, Initializer)]
fields) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[(Nothing, init) | init <- $(antiVarE v)] ++ $(dataToExpQ qqExp fields)|]
qqInitializerListE ((Maybe Designation, Initializer)
field : [(Maybe Designation, Initializer)]
fields) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp field) : $(dataToExpQ qqExp fields)|]

qqInitGroupE :: C.InitGroup -> Maybe (Q Exp)
qqInitGroupE :: InitGroup -> Maybe ExpQ
qqInitGroupE (C.AntiDecl String
v SrcLoc
_)  = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
v
qqInitGroupE InitGroup
_                 = Maybe ExpQ
forall a. Maybe a
Nothing

qqInitGroupListE :: [C.InitGroup] -> Maybe (Q Exp)
qqInitGroupListE :: [InitGroup] -> Maybe ExpQ
qqInitGroupListE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqInitGroupListE (C.AntiDecls String
v SrcLoc
_ : [InitGroup]
inits) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE v) ++ $(dataToExpQ qqExp inits)|]
qqInitGroupListE (InitGroup
ini : [InitGroup]
inis) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp ini) : $(dataToExpQ qqExp inis)|]

qqAttrE :: C.Attr -> Maybe (Q Exp)
qqAttrE :: Attr -> Maybe ExpQ
qqAttrE (C.AntiAttr String
v SrcLoc
_)  = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
v
qqAttrE Attr
_                 = Maybe ExpQ
forall a. Maybe a
Nothing

qqAttrListE :: [C.Attr] -> Maybe (Q Exp)
qqAttrListE :: [Attr] -> Maybe ExpQ
qqAttrListE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqAttrListE (C.AntiAttrs String
v SrcLoc
_ : [Attr]
attrs) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE v) ++ $(dataToExpQ qqExp attrs)|]
qqAttrListE (Attr
field : [Attr]
fields) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp field) : $(dataToExpQ qqExp fields)|]

qqFieldGroupE :: C.FieldGroup -> Maybe (Q Exp)
qqFieldGroupE :: FieldGroup -> Maybe ExpQ
qqFieldGroupE (C.AntiSdecl String
v SrcLoc
_)  = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
v
qqFieldGroupE FieldGroup
_                  = Maybe ExpQ
forall a. Maybe a
Nothing

qqFieldGroupListE :: [C.FieldGroup] -> Maybe (Q Exp)
qqFieldGroupListE :: [FieldGroup] -> Maybe ExpQ
qqFieldGroupListE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqFieldGroupListE (C.AntiSdecls String
v SrcLoc
_ : [FieldGroup]
fields) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE v) ++ $(dataToExpQ qqExp fields)|]
qqFieldGroupListE (FieldGroup
field : [FieldGroup]
fields) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp field) : $(dataToExpQ qqExp fields)|]

qqCEnumE :: C.CEnum -> Maybe (Q Exp)
qqCEnumE :: CEnum -> Maybe ExpQ
qqCEnumE (C.AntiEnum String
v SrcLoc
_)  = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
v
qqCEnumE CEnum
_                 = Maybe ExpQ
forall a. Maybe a
Nothing

qqCEnumListE :: [C.CEnum] -> Maybe (Q Exp)
qqCEnumListE :: [CEnum] -> Maybe ExpQ
qqCEnumListE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqCEnumListE (C.AntiEnums String
v SrcLoc
_ : [CEnum]
fields) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE v) ++ $(dataToExpQ qqExp fields)|]
qqCEnumListE (CEnum
field : [CEnum]
fields) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp field) : $(dataToExpQ qqExp fields)|]

qqParamE :: C.Param -> Maybe (Q Exp)
qqParamE :: Param -> Maybe ExpQ
qqParamE (C.AntiParam String
v SrcLoc
_)  = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
v
qqParamE Param
_                  = Maybe ExpQ
forall a. Maybe a
Nothing

qqParamListE :: [C.Param] -> Maybe (Q Exp)
qqParamListE :: [Param] -> Maybe ExpQ
qqParamListE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqParamListE (C.AntiParams String
v SrcLoc
_ : [Param]
args) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE v) ++ $(dataToExpQ qqExp args)|]
qqParamListE (Param
arg : [Param]
args) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp arg) : $(dataToExpQ qqExp args)|]

qqDefinitionE :: C.Definition -> Maybe (Q Exp)
qqDefinitionE :: Definition -> Maybe ExpQ
qqDefinitionE (C.AntiFunc String
v SrcLoc
loc) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|C.FuncDef $(antiVarE v) $(qqLocE loc)|]
qqDefinitionE (C.AntiEsc String
v SrcLoc
loc) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|C.EscDef $(antiVarE v) $(qqLocE loc)|]
qqDefinitionE (C.AntiEdecl String
v SrcLoc
_) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
v
qqDefinitionE (C.AntiObjCMeth String
m SrcLoc
_) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
m
qqDefinitionE Definition
_ = Maybe ExpQ
forall a. Maybe a
Nothing

qqDefinitionListE :: [C.Definition] -> Maybe (Q Exp)
qqDefinitionListE :: [Definition] -> Maybe ExpQ
qqDefinitionListE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqDefinitionListE (C.AntiEdecls String
v SrcLoc
_ : [Definition]
defs) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE v) ++ $(dataToExpQ qqExp defs)|]
qqDefinitionListE (C.AntiObjCMeths String
m SrcLoc
_ : [Definition]
meths) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE m) ++ $(dataToExpQ qqExp meths)|]
qqDefinitionListE (Definition
def : [Definition]
defs) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp def) : $(dataToExpQ qqExp defs)|]

qqConstE :: C.Const -> Maybe (Q Exp)
qqConstE :: Const -> Maybe ExpQ
qqConstE = Const -> Maybe ExpQ
go
  where
    go :: Const -> Maybe ExpQ
go (C.AntiConst String
v SrcLoc
loc) =
        ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|toConst $(antiVarE v) $(qqLocE loc) :: C.Const|]

    go (C.AntiInt String
v SrcLoc
loc) =
        ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|C.IntConst  $(intConst (antiVarE v)) C.Signed
                           (fromIntegral $(antiVarE v))
                           $(qqLocE loc)|]

    go (C.AntiUInt String
v SrcLoc
loc) =
        ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|C.IntConst  ($(intConst (antiVarE v)) ++ "U") C.Unsigned
                           (fromIntegral $(antiVarE v))
                           $(qqLocE loc)|]

    go (C.AntiLInt String
v SrcLoc
loc) =
        ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|C.LongIntConst  ($(intConst (antiVarE v)) ++ "L") C.Signed
                               (fromIntegral $(antiVarE v))
                               $(qqLocE loc)|]

    go (C.AntiULInt String
v SrcLoc
loc) =
        ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|C.LongIntConst  ($(intConst (antiVarE v)) ++ "UL") C.Unsigned
                               (fromIntegral $(antiVarE v))
                               $(qqLocE loc)|]

    go (C.AntiLLInt String
v SrcLoc
loc) =
        ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|C.LongLongIntConst  ($(intConst (antiVarE v)) ++ "LL") C.Signed
                               (fromIntegral $(antiVarE v))
                               $(qqLocE loc)|]

    go (C.AntiULLInt String
v SrcLoc
loc) =
        ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|C.LongLongIntConst  ($(intConst (antiVarE v)) ++ "ULL") C.Unsigned
                               (fromIntegral $(antiVarE v))
                               $(qqLocE loc)|]

    go (C.AntiFloat String
v SrcLoc
loc) =
        ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|toConst ($(antiVarE v) :: Float) $(qqLocE loc)|]

    go (C.AntiDouble String
v SrcLoc
loc) =
        ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|toConst ($(antiVarE v) :: Double) $(qqLocE loc)|]

    go (C.AntiLongDouble String
v SrcLoc
loc) =
        ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|toConst (LongDouble $(antiVarE v)) $(qqLocE loc)|]

    go (C.AntiChar String
v SrcLoc
loc) =
        ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|toConst $(antiVarE v) $(qqLocE loc)|]

    go (C.AntiString String
v SrcLoc
loc) =
        ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|C.StringConst [show $(antiVarE v)] $(antiVarE v) $(qqLocE loc)|]

    go Const
_ = Maybe ExpQ
forall a. Maybe a
Nothing

    intConst :: ExpQ -> ExpQ
    intConst :: ExpQ -> ExpQ
intConst ExpQ
e = [|show $(e)|]

qqExpE :: C.Exp -> Maybe (Q Exp)
qqExpE :: Exp -> Maybe ExpQ
qqExpE (C.AntiExp String
v SrcLoc
loc)    = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|toExp $(antiVarE v) $(qqLocE loc) :: C.Exp|]
qqExpE (C.AntiEscExp String
v SrcLoc
loc) = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|C.EscExp $(antiVarE v) $(qqLocE loc) :: C.Exp|]
qqExpE Exp
_                    = Maybe ExpQ
forall a. Maybe a
Nothing

qqExpListE :: [C.Exp] -> Maybe (Q Exp)
qqExpListE :: [Exp] -> Maybe ExpQ
qqExpListE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqExpListE (C.AntiArgs String
v SrcLoc
loc : [Exp]
exps) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[toExp v $(qqLocE loc) | v <- $(antiVarE v)] ++
           $(dataToExpQ qqExp exps)|]
qqExpListE (Exp
exp : [Exp]
exps) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp exp) : $(dataToExpQ qqExp exps)|]

qqStmE :: C.Stm -> Maybe (Q Exp)
qqStmE :: Stm -> Maybe ExpQ
qqStmE (C.AntiEscStm String
v SrcLoc
loc)      = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|C.EscStm $(antiVarE v) $(qqLocE loc)|]
qqStmE (C.AntiPragma String
v SrcLoc
loc)      = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|C.Pragma $(antiVarE v) $(qqLocE loc)|]
qqStmE (C.AntiComment String
v Stm
stm SrcLoc
loc) = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|C.Comment $(antiVarE v) $(dataToExpQ qqExp stm) $(qqLocE loc)|]
qqStmE (C.AntiStm String
v SrcLoc
_)           = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
v
qqStmE Stm
_                         = Maybe ExpQ
forall a. Maybe a
Nothing

qqStmListE :: [C.Stm] -> Maybe (Q Exp)
qqStmListE :: [Stm] -> Maybe ExpQ
qqStmListE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqStmListE (C.AntiStms String
v SrcLoc
_ : [Stm]
stms) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE v) ++ $(dataToExpQ qqExp stms)|]
qqStmListE (Stm
stm : [Stm]
stms) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp stm) : $(dataToExpQ qqExp stms)|]

qqBlockItemE :: C.BlockItem -> Maybe (Q Exp)
qqBlockItemE :: BlockItem -> Maybe ExpQ
qqBlockItemE (C.AntiBlockItem String
v SrcLoc
_) = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
v
qqBlockItemE BlockItem
_                     = Maybe ExpQ
forall a. Maybe a
Nothing

qqBlockItemListE :: [C.BlockItem] -> Maybe (Q Exp)
qqBlockItemListE :: [BlockItem] -> Maybe ExpQ
qqBlockItemListE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqBlockItemListE (C.BlockDecl (C.AntiDecls String
v SrcLoc
_) : [BlockItem]
items) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|map C.BlockDecl $(antiVarE v) ++ $(dataToExpQ qqExp items)|]
qqBlockItemListE (C.BlockStm (C.AntiStms String
v SrcLoc
_) : [BlockItem]
items) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|map C.BlockStm $(antiVarE v) ++ $(dataToExpQ qqExp items)|]
qqBlockItemListE (C.AntiBlockItems String
v SrcLoc
_ : [BlockItem]
items) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE v) ++ $(dataToExpQ qqExp items)|]
qqBlockItemListE (BlockItem
stm : [BlockItem]
stms) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp stm) : $(dataToExpQ qqExp stms)|]

qqObjcIfaceDeclE :: C.ObjCIfaceDecl -> Maybe (Q Exp)
qqObjcIfaceDeclE :: ObjCIfaceDecl -> Maybe ExpQ
qqObjcIfaceDeclE (C.AntiObjCProp String
p SrcLoc
_) = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
p
qqObjcIfaceDeclE ObjCIfaceDecl
_                    = Maybe ExpQ
forall a. Maybe a
Nothing

qqObjcIfaceDeclListE :: [C.ObjCIfaceDecl] -> Maybe (Q Exp)
qqObjcIfaceDeclListE :: [ObjCIfaceDecl] -> Maybe ExpQ
qqObjcIfaceDeclListE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqObjcIfaceDeclListE (C.AntiObjCProps String
p SrcLoc
_ : [ObjCIfaceDecl]
decls) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE p) ++ $(dataToExpQ qqExp decls)|]
qqObjcIfaceDeclListE (C.AntiObjCIfaceDecls String
v SrcLoc
_ : [ObjCIfaceDecl]
decls) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE v) ++ $(dataToExpQ qqExp decls)|]
qqObjcIfaceDeclListE (C.AntiObjCIfaceDecl String
v SrcLoc
_  : [ObjCIfaceDecl]
decls) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE v) : $(dataToExpQ qqExp decls)|]
qqObjcIfaceDeclListE (ObjCIfaceDecl
decl : [ObjCIfaceDecl]
decls) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp decl) : $(dataToExpQ qqExp decls)|]

qqObjCPropAttrE :: C.ObjCPropAttr -> Maybe (Q Exp)
qqObjCPropAttrE :: ObjCPropAttr -> Maybe ExpQ
qqObjCPropAttrE (C.AntiObjCAttr String
pa SrcLoc
_) = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
pa
qqObjCPropAttrE ObjCPropAttr
_                     = Maybe ExpQ
forall a. Maybe a
Nothing

qqObjCPropAttrListE :: [C.ObjCPropAttr] -> Maybe (Q Exp)
qqObjCPropAttrListE :: [ObjCPropAttr] -> Maybe ExpQ
qqObjCPropAttrListE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqObjCPropAttrListE (C.AntiObjCAttrs String
pa SrcLoc
_:[ObjCPropAttr]
attrelems) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE pa) ++ $(dataToExpQ qqExp attrelems)|]
qqObjCPropAttrListE (ObjCPropAttr
pattr : [ObjCPropAttr]
pattrs) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp pattr) : $(dataToExpQ qqExp pattrs)|]

qqObjCDictsE :: [C.ObjCDictElem] -> Maybe (Q Exp)
qqObjCDictsE :: [ObjCDictElem] -> Maybe ExpQ
qqObjCDictsE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqObjCDictsE (C.AntiObjCDictElems String
e SrcLoc
_:[ObjCDictElem]
elems) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE e) ++ $(dataToExpQ qqExp elems)|]
qqObjCDictsE (ObjCDictElem
elem : [ObjCDictElem]
elems) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp elem) : $(dataToExpQ qqExp elems)|]

qqObjCParamE :: C.ObjCParam -> Maybe (Q Exp)
qqObjCParamE :: ObjCParam -> Maybe ExpQ
qqObjCParamE (C.AntiObjCParam String
p SrcLoc
_) = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
p
qqObjCParamE ObjCParam
_                     = Maybe ExpQ
forall a. Maybe a
Nothing

qqObjCParamsE :: [C.ObjCParam] -> Maybe (Q Exp)
qqObjCParamsE :: [ObjCParam] -> Maybe ExpQ
qqObjCParamsE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqObjCParamsE (C.AntiObjCParams String
p SrcLoc
_: [ObjCParam]
props) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE p) ++ $(dataToExpQ qqExp props)|]
qqObjCParamsE (ObjCParam
param : [ObjCParam]
params) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp param) : $(dataToExpQ qqExp params)|]

qqObjCMethodProtoE :: C.ObjCMethodProto -> Maybe (Q Exp)
qqObjCMethodProtoE :: ObjCMethodProto -> Maybe ExpQ
qqObjCMethodProtoE (C.AntiObjCMethodProto String
p SrcLoc
_) = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
p
qqObjCMethodProtoE ObjCMethodProto
_                           = Maybe ExpQ
forall a. Maybe a
Nothing

qqObjCRecvE :: C.ObjCRecv -> Maybe (Q Exp)
qqObjCRecvE :: ObjCRecv -> Maybe ExpQ
qqObjCRecvE (C.AntiObjCRecv String
p SrcLoc
_) = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
p
qqObjCRecvE ObjCRecv
_                  = Maybe ExpQ
forall a. Maybe a
Nothing

qqObjCArgE :: C.ObjCArg -> Maybe (Q Exp)
qqObjCArgE :: ObjCArg -> Maybe ExpQ
qqObjCArgE (C.AntiObjCArg String
p SrcLoc
_) = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just (ExpQ -> Maybe ExpQ) -> ExpQ -> Maybe ExpQ
forall a b. (a -> b) -> a -> b
$ String -> ExpQ
antiVarE String
p
qqObjCArgE ObjCArg
_                  = Maybe ExpQ
forall a. Maybe a
Nothing

qqObjCArgsE :: [C.ObjCArg] -> Maybe (Q Exp)
qqObjCArgsE :: [ObjCArg] -> Maybe ExpQ
qqObjCArgsE [] = ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|[]|]
qqObjCArgsE (C.AntiObjCArgs String
a SrcLoc
_: [ObjCArg]
args) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(antiVarE a) ++ $(dataToExpQ qqExp args)|]
qqObjCArgsE (ObjCArg
arg : [ObjCArg]
args) =
    ExpQ -> Maybe ExpQ
forall a. a -> Maybe a
Just [|$(dataToExpQ qqExp arg) : $(dataToExpQ qqExp args)|]

qqExp :: Typeable a => a -> Maybe (Q Exp)
qqExp :: a -> Maybe ExpQ
qqExp = Maybe ExpQ -> a -> Maybe ExpQ
forall a b. a -> b -> a
const Maybe ExpQ
forall a. Maybe a
Nothing  (a -> Maybe ExpQ) -> (String -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` String -> Maybe ExpQ
qqStringE
                       (a -> Maybe ExpQ) -> (Id -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Id -> Maybe ExpQ
qqIdE
                       (a -> Maybe ExpQ) -> (DeclSpec -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` DeclSpec -> Maybe ExpQ
qqDeclSpecE
                       (a -> Maybe ExpQ) -> (Decl -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Decl -> Maybe ExpQ
qqDeclE
                       (a -> Maybe ExpQ) -> (TypeQual -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` TypeQual -> Maybe ExpQ
qqTypeQualE
                       (a -> Maybe ExpQ) -> ([TypeQual] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [TypeQual] -> Maybe ExpQ
qqTypeQualListE
                       (a -> Maybe ExpQ) -> (Type -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Type -> Maybe ExpQ
qqTypeE
                       (a -> Maybe ExpQ) -> (Initializer -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Initializer -> Maybe ExpQ
qqInitializerE
                       (a -> Maybe ExpQ)
-> ([(Maybe Designation, Initializer)] -> Maybe ExpQ)
-> a
-> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [(Maybe Designation, Initializer)] -> Maybe ExpQ
qqInitializerListE
                       (a -> Maybe ExpQ) -> (InitGroup -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` InitGroup -> Maybe ExpQ
qqInitGroupE
                       (a -> Maybe ExpQ) -> ([InitGroup] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [InitGroup] -> Maybe ExpQ
qqInitGroupListE
                       (a -> Maybe ExpQ) -> (Attr -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Attr -> Maybe ExpQ
qqAttrE
                       (a -> Maybe ExpQ) -> ([Attr] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [Attr] -> Maybe ExpQ
qqAttrListE
                       (a -> Maybe ExpQ) -> (FieldGroup -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` FieldGroup -> Maybe ExpQ
qqFieldGroupE
                       (a -> Maybe ExpQ)
-> ([FieldGroup] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [FieldGroup] -> Maybe ExpQ
qqFieldGroupListE
                       (a -> Maybe ExpQ) -> (CEnum -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` CEnum -> Maybe ExpQ
qqCEnumE
                       (a -> Maybe ExpQ) -> ([CEnum] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [CEnum] -> Maybe ExpQ
qqCEnumListE
                       (a -> Maybe ExpQ) -> (Param -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Param -> Maybe ExpQ
qqParamE
                       (a -> Maybe ExpQ) -> ([Param] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [Param] -> Maybe ExpQ
qqParamListE
                       (a -> Maybe ExpQ) -> (Definition -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Definition -> Maybe ExpQ
qqDefinitionE
                       (a -> Maybe ExpQ)
-> ([Definition] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [Definition] -> Maybe ExpQ
qqDefinitionListE
                       (a -> Maybe ExpQ) -> (Const -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Const -> Maybe ExpQ
qqConstE
                       (a -> Maybe ExpQ) -> (Exp -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Exp -> Maybe ExpQ
qqExpE
                       (a -> Maybe ExpQ) -> ([Exp] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [Exp] -> Maybe ExpQ
qqExpListE
                       (a -> Maybe ExpQ) -> (Stm -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Stm -> Maybe ExpQ
qqStmE
                       (a -> Maybe ExpQ) -> ([Stm] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [Stm] -> Maybe ExpQ
qqStmListE
                       (a -> Maybe ExpQ) -> (BlockItem -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` BlockItem -> Maybe ExpQ
qqBlockItemE
                       (a -> Maybe ExpQ) -> ([BlockItem] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [BlockItem] -> Maybe ExpQ
qqBlockItemListE
                       (a -> Maybe ExpQ)
-> (ObjCIfaceDecl -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` ObjCIfaceDecl -> Maybe ExpQ
qqObjcIfaceDeclE
                       (a -> Maybe ExpQ)
-> ([ObjCIfaceDecl] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [ObjCIfaceDecl] -> Maybe ExpQ
qqObjcIfaceDeclListE
                       (a -> Maybe ExpQ)
-> (ObjCPropAttr -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` ObjCPropAttr -> Maybe ExpQ
qqObjCPropAttrE
                       (a -> Maybe ExpQ)
-> ([ObjCPropAttr] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [ObjCPropAttr] -> Maybe ExpQ
qqObjCPropAttrListE
                       (a -> Maybe ExpQ)
-> ([ObjCDictElem] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [ObjCDictElem] -> Maybe ExpQ
qqObjCDictsE
                       (a -> Maybe ExpQ) -> (ObjCParam -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` ObjCParam -> Maybe ExpQ
qqObjCParamE
                       (a -> Maybe ExpQ) -> ([ObjCParam] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [ObjCParam] -> Maybe ExpQ
qqObjCParamsE
                       (a -> Maybe ExpQ)
-> (ObjCMethodProto -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` ObjCMethodProto -> Maybe ExpQ
qqObjCMethodProtoE
                       (a -> Maybe ExpQ) -> (ObjCRecv -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` ObjCRecv -> Maybe ExpQ
qqObjCRecvE
                       (a -> Maybe ExpQ) -> (ObjCArg -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` ObjCArg -> Maybe ExpQ
qqObjCArgE
                       (a -> Maybe ExpQ) -> ([ObjCArg] -> Maybe ExpQ) -> a -> Maybe ExpQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [ObjCArg] -> Maybe ExpQ
qqObjCArgsE

antiVarP :: String -> PatQ
antiVarP :: String -> PatQ
antiVarP = (String -> PatQ) -> (Pat -> PatQ) -> Either String Pat -> PatQ
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> PatQ
forall (m :: * -> *) a. MonadFail m => String -> m a
fail Pat -> PatQ
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String Pat -> PatQ)
-> (String -> Either String Pat) -> String -> PatQ
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Either String Pat
parsePat

qqStringP :: String -> Maybe (Q Pat)
qqStringP :: String -> Maybe PatQ
qqStringP String
s = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Lit -> PatQ
litP (Lit -> PatQ) -> Lit -> PatQ
forall a b. (a -> b) -> a -> b
$ String -> Lit
stringL String
s

qqLocP :: Data.Loc.Loc -> Maybe (Q Pat)
qqLocP :: Loc -> Maybe PatQ
qqLocP Loc
_ = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just PatQ
wildP

qqIdP :: C.Id -> Maybe (Q Pat)
qqIdP :: Id -> Maybe PatQ
qqIdP (C.AntiId String
v SrcLoc
_) = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
"C.Id") [String -> PatQ
antiVarP String
v, PatQ
wildP]
qqIdP Id
_              = Maybe PatQ
forall a. Maybe a
Nothing

qqDeclSpecP :: C.DeclSpec -> Maybe (Q Pat)
qqDeclSpecP :: DeclSpec -> Maybe PatQ
qqDeclSpecP (C.AntiDeclSpec String
v SrcLoc
_) = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqDeclSpecP C.AntiTypeDeclSpec{} =
    String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Illegal antiquoted type in pattern"
qqDeclSpecP DeclSpec
_ = Maybe PatQ
forall a. Maybe a
Nothing

qqDeclP :: C.Decl -> Maybe (Q Pat)
qqDeclP :: Decl -> Maybe PatQ
qqDeclP C.AntiTypeDecl{} =
    String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Illegal antiquoted type in pattern"
qqDeclP Decl
_ = Maybe PatQ
forall a. Maybe a
Nothing

qqTypeQualP :: C.TypeQual -> Maybe (Q Pat)
qqTypeQualP :: TypeQual -> Maybe PatQ
qqTypeQualP (C.AntiTypeQual String
v SrcLoc
_) = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqTypeQualP TypeQual
_                    = Maybe PatQ
forall a. Maybe a
Nothing

qqTypeQualListP :: [C.TypeQual] -> Maybe (Q Pat)
qqTypeQualListP :: [TypeQual] -> Maybe PatQ
qqTypeQualListP [] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ [PatQ] -> PatQ
listP []
qqTypeQualListP [C.AntiTypeQuals String
v SrcLoc
_] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqTypeQualListP (C.AntiTypeQuals{} : TypeQual
_ : [TypeQual]
_) =
    String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Antiquoted list of type qualifiers must be last item in quoted list"
qqTypeQualListP (TypeQual
arg : [TypeQual]
args) =
    PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
":") [(forall b. Data b => b -> Maybe PatQ) -> TypeQual -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat TypeQual
arg, (forall b. Data b => b -> Maybe PatQ) -> [TypeQual] -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat [TypeQual]
args]

qqTypeP :: C.Type -> Maybe (Q Pat)
qqTypeP :: Type -> Maybe PatQ
qqTypeP (C.AntiType String
v SrcLoc
_)  = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqTypeP Type
_                 = Maybe PatQ
forall a. Maybe a
Nothing

qqInitializerP :: C.Initializer -> Maybe (Q Pat)
qqInitializerP :: Initializer -> Maybe PatQ
qqInitializerP (C.AntiInit String
v SrcLoc
_)  = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqInitializerP Initializer
_                 = Maybe PatQ
forall a. Maybe a
Nothing

qqInitializerListP :: [C.Initializer] -> Maybe (Q Pat)
qqInitializerListP :: [Initializer] -> Maybe PatQ
qqInitializerListP [] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ [PatQ] -> PatQ
listP []
qqInitializerListP [C.AntiInits String
v SrcLoc
_] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqInitializerListP (C.AntiInits{} : Initializer
_ : [Initializer]
_) =
    String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Antiquoted list of initializers must be last item in quoted list"
qqInitializerListP (Initializer
ini : [Initializer]
inis) =
    PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
":") [(forall b. Data b => b -> Maybe PatQ) -> Initializer -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat Initializer
ini,  (forall b. Data b => b -> Maybe PatQ) -> [Initializer] -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat [Initializer]
inis]

qqInitGroupP :: C.InitGroup -> Maybe (Q Pat)
qqInitGroupP :: InitGroup -> Maybe PatQ
qqInitGroupP (C.AntiDecl String
v SrcLoc
_) = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqInitGroupP InitGroup
_                = Maybe PatQ
forall a. Maybe a
Nothing

qqInitGroupListP :: [C.InitGroup] -> Maybe (Q Pat)
qqInitGroupListP :: [InitGroup] -> Maybe PatQ
qqInitGroupListP [] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ [PatQ] -> PatQ
listP []
qqInitGroupListP [C.AntiDecls String
v SrcLoc
_] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqInitGroupListP (C.AntiDecls{} : InitGroup
_ : [InitGroup]
_) =
    String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Antiquoted list of initialization groups must be last item in quoted list"
qqInitGroupListP (InitGroup
ini : [InitGroup]
inis) =
    PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
":") [(forall b. Data b => b -> Maybe PatQ) -> InitGroup -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat InitGroup
ini,  (forall b. Data b => b -> Maybe PatQ) -> [InitGroup] -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat [InitGroup]
inis]

qqAttrP :: C.Attr -> Maybe (Q Pat)
qqAttrP :: Attr -> Maybe PatQ
qqAttrP (C.AntiAttr String
v SrcLoc
_)  = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqAttrP Attr
_                 = Maybe PatQ
forall a. Maybe a
Nothing

qqAttrListP :: [C.Attr] -> Maybe (Q Pat)
qqAttrListP :: [Attr] -> Maybe PatQ
qqAttrListP [] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ [PatQ] -> PatQ
listP []
qqAttrListP [C.AntiAttrs String
v SrcLoc
_] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqAttrListP (C.AntiAttrs{} : Attr
_ : [Attr]
_) =
   String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Antiquoted list of attrs must be last item in quoted list"
qqAttrListP (Attr
ini : [Attr]
inis) =
    PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
":") [(forall b. Data b => b -> Maybe PatQ) -> Attr -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat Attr
ini,  (forall b. Data b => b -> Maybe PatQ) -> [Attr] -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat [Attr]
inis]

qqFieldGroupP :: C.FieldGroup -> Maybe (Q Pat)
qqFieldGroupP :: FieldGroup -> Maybe PatQ
qqFieldGroupP (C.AntiSdecl String
v SrcLoc
_) = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqFieldGroupP FieldGroup
_                 = Maybe PatQ
forall a. Maybe a
Nothing

qqFieldGroupListP :: [C.FieldGroup] -> Maybe (Q Pat)
qqFieldGroupListP :: [FieldGroup] -> Maybe PatQ
qqFieldGroupListP [] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ [PatQ] -> PatQ
listP []
qqFieldGroupListP [C.AntiSdecls String
v SrcLoc
_] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqFieldGroupListP (C.AntiSdecls{} : FieldGroup
_ : [FieldGroup]
_) =
    String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Antiquoted list of struct/union fields must be last item in quoted list"
qqFieldGroupListP (FieldGroup
ini : [FieldGroup]
inis) =
    PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
":") [(forall b. Data b => b -> Maybe PatQ) -> FieldGroup -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat FieldGroup
ini,  (forall b. Data b => b -> Maybe PatQ) -> [FieldGroup] -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat [FieldGroup]
inis]

qqCEnumP :: C.CEnum -> Maybe (Q Pat)
qqCEnumP :: CEnum -> Maybe PatQ
qqCEnumP (C.AntiEnum String
v SrcLoc
_) = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqCEnumP CEnum
_                = Maybe PatQ
forall a. Maybe a
Nothing

qqCEnumListP :: [C.CEnum] -> Maybe (Q Pat)
qqCEnumListP :: [CEnum] -> Maybe PatQ
qqCEnumListP [] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ [PatQ] -> PatQ
listP []
qqCEnumListP [C.AntiEnums String
v SrcLoc
_] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqCEnumListP (C.AntiEnums{} : CEnum
_ : [CEnum]
_) =
    String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Antiquoted list of enumerations must be last item in quoted list"
qqCEnumListP (CEnum
ini : [CEnum]
inis) =
    PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
":") [(forall b. Data b => b -> Maybe PatQ) -> CEnum -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat CEnum
ini,  (forall b. Data b => b -> Maybe PatQ) -> [CEnum] -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat [CEnum]
inis]

qqParamP :: C.Param -> Maybe (Q Pat)
qqParamP :: Param -> Maybe PatQ
qqParamP (C.AntiParam String
v SrcLoc
_) = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqParamP Param
_                 = Maybe PatQ
forall a. Maybe a
Nothing

qqParamListP :: [C.Param] -> Maybe (Q Pat)
qqParamListP :: [Param] -> Maybe PatQ
qqParamListP [] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ [PatQ] -> PatQ
listP []
qqParamListP [C.AntiParams String
v SrcLoc
_] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqParamListP (C.AntiParams{} : Param
_ : [Param]
_) =
    String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Antiquoted list of parameters must be last item in quoted list"
qqParamListP (Param
arg : [Param]
args) =
    PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
":") [(forall b. Data b => b -> Maybe PatQ) -> Param -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat Param
arg,  (forall b. Data b => b -> Maybe PatQ) -> [Param] -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat [Param]
args]

qqDefinitionP :: C.Definition -> Maybe (Q Pat)
qqDefinitionP :: Definition -> Maybe PatQ
qqDefinitionP (C.AntiFunc String
v SrcLoc
_)  = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
"C.FuncDef") [String -> PatQ
antiVarP String
v, PatQ
wildP]
qqDefinitionP (C.AntiEsc String
v SrcLoc
_)   = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
"C.EscDef") [String -> PatQ
antiVarP String
v, PatQ
wildP]
qqDefinitionP (C.AntiEdecl String
v SrcLoc
_) = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqDefinitionP Definition
_                 = Maybe PatQ
forall a. Maybe a
Nothing

qqDefinitionListP :: [C.Definition] -> Maybe (Q Pat)
qqDefinitionListP :: [Definition] -> Maybe PatQ
qqDefinitionListP [] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ [PatQ] -> PatQ
listP []
qqDefinitionListP [C.AntiEdecls String
v SrcLoc
_] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqDefinitionListP (C.AntiEdecls{} : Definition
_ : [Definition]
_) =
    String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Antiquoted list of definitions must be last item in quoted list"
qqDefinitionListP (Definition
arg : [Definition]
args) =
    PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
":") [(forall b. Data b => b -> Maybe PatQ) -> Definition -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat Definition
arg,  (forall b. Data b => b -> Maybe PatQ) -> [Definition] -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat [Definition]
args]

qqConstP :: C.Const -> Maybe (Q Pat)
qqConstP :: Const -> Maybe PatQ
qqConstP = Const -> Maybe PatQ
go
  where
    go :: Const -> Maybe PatQ
go (C.AntiInt String
v SrcLoc
_) =
        PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> [PatQ] -> PatQ
con String
"C.IntConst" [PatQ
wildP, PatQ
signed, String -> PatQ
antiVarP String
v, PatQ
wildP]
    go (C.AntiUInt String
v SrcLoc
_) =
        PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> [PatQ] -> PatQ
con String
"C.IntConst" [PatQ
wildP, PatQ
unsigned, String -> PatQ
antiVarP String
v, PatQ
wildP]
    go (C.AntiLInt String
v SrcLoc
_) =
        PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> [PatQ] -> PatQ
con String
"C.LongIntConst" [PatQ
wildP, PatQ
signed, String -> PatQ
antiVarP String
v, PatQ
wildP]
    go (C.AntiULInt String
v SrcLoc
_) =
        PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> [PatQ] -> PatQ
con String
"C.LongIntConst" [PatQ
wildP, PatQ
unsigned, String -> PatQ
antiVarP String
v, PatQ
wildP]
    go (C.AntiFloat String
v SrcLoc
_) =
        PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> [PatQ] -> PatQ
con String
"C.FloatConst" [PatQ
wildP, String -> PatQ
antiVarP String
v, PatQ
wildP]
    go (C.AntiDouble String
v SrcLoc
_) =
        PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> [PatQ] -> PatQ
con String
"C.DoubleConst" [PatQ
wildP, String -> PatQ
antiVarP String
v, PatQ
wildP]
    go (C.AntiLongDouble String
v SrcLoc
_) =
        PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> [PatQ] -> PatQ
con String
"C.LongDoubleConst" [PatQ
wildP, String -> PatQ
antiVarP String
v, PatQ
wildP]
    go (C.AntiChar String
v SrcLoc
_) =
        PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> [PatQ] -> PatQ
con String
"C.CharConst" [PatQ
wildP, String -> PatQ
antiVarP String
v, PatQ
wildP]
    go (C.AntiString String
v SrcLoc
_) =
        PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> [PatQ] -> PatQ
con String
"C.StringConst" [PatQ
wildP, String -> PatQ
antiVarP String
v, PatQ
wildP]
    go Const
_ =
        Maybe PatQ
forall a. Maybe a
Nothing

    con :: String -> [PatQ] -> PatQ
con String
n = Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
n)

    signed :: PatQ
signed   = Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
"C.Signed") []
    unsigned :: PatQ
unsigned = Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
"C.Unsigned") []

qqExpP :: C.Exp -> Maybe (Q Pat)
qqExpP :: Exp -> Maybe PatQ
qqExpP (C.AntiExp String
v SrcLoc
_)    = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqExpP (C.AntiEscExp String
v SrcLoc
_) = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
"C.EscExp") [String -> PatQ
antiVarP String
v, PatQ
wildP]
qqExpP Exp
_                  = Maybe PatQ
forall a. Maybe a
Nothing

qqExpListP :: [C.Exp] -> Maybe (Q Pat)
qqExpListP :: [Exp] -> Maybe PatQ
qqExpListP [] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ [PatQ] -> PatQ
listP []
qqExpListP [C.AntiArgs String
v SrcLoc
_] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqExpListP (C.AntiArgs{} : Exp
_ : [Exp]
_) =
    String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Antiquoted list of arguments must be last item in quoted list"
qqExpListP (Exp
arg : [Exp]
args) =
    PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
":") [(forall b. Data b => b -> Maybe PatQ) -> Exp -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat Exp
arg,  (forall b. Data b => b -> Maybe PatQ) -> [Exp] -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat [Exp]
args]

qqStmP :: C.Stm -> Maybe (Q Pat)
qqStmP :: Stm -> Maybe PatQ
qqStmP (C.AntiStm String
v SrcLoc
_)    = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqStmP (C.AntiEscStm String
v SrcLoc
_) = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
"C.EscStm") [String -> PatQ
antiVarP String
v, PatQ
wildP]
qqStmP Stm
_                  = Maybe PatQ
forall a. Maybe a
Nothing

qqStmListP :: [C.Stm] -> Maybe (Q Pat)
qqStmListP :: [Stm] -> Maybe PatQ
qqStmListP [] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ [PatQ] -> PatQ
listP []
qqStmListP [C.AntiStms String
v SrcLoc
_] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqStmListP (C.AntiStms{} : Stm
_ : [Stm]
_) =
    String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Antiquoted list of statements must be last item in quoted list"
qqStmListP (Stm
arg : [Stm]
args) =
    PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
":") [(forall b. Data b => b -> Maybe PatQ) -> Stm -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat Stm
arg,  (forall b. Data b => b -> Maybe PatQ) -> [Stm] -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat [Stm]
args]

qqBlockItemP :: C.BlockItem -> Maybe (Q Pat)
qqBlockItemP :: BlockItem -> Maybe PatQ
qqBlockItemP (C.AntiBlockItem String
v SrcLoc
_) = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqBlockItemP BlockItem
_                     = Maybe PatQ
forall a. Maybe a
Nothing

qqBlockItemListP :: [C.BlockItem] -> Maybe (Q Pat)
qqBlockItemListP :: [BlockItem] -> Maybe PatQ
qqBlockItemListP [] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ [PatQ] -> PatQ
listP []
qqBlockItemListP (C.BlockDecl C.AntiDecls{} : [BlockItem]
_) =
    String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Antiquoted list of declarations cannot appear in block"
qqBlockItemListP (C.BlockStm C.AntiStms{} : [BlockItem]
_) =
    String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Antiquoted list of statements cannot appear in block"
qqBlockItemListP [C.AntiBlockItems String
v SrcLoc
_] = PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ String -> PatQ
antiVarP String
v
qqBlockItemListP (C.AntiBlockItems{} : BlockItem
_ : [BlockItem]
_) =
    String -> Maybe PatQ
forall a. HasCallStack => String -> a
error String
"Antiquoted list of block items must be last item in quoted list"
qqBlockItemListP (BlockItem
arg : [BlockItem]
args) =
    PatQ -> Maybe PatQ
forall a. a -> Maybe a
Just (PatQ -> Maybe PatQ) -> PatQ -> Maybe PatQ
forall a b. (a -> b) -> a -> b
$ Name -> [PatQ] -> PatQ
conP (String -> Name
mkName String
":") [(forall b. Data b => b -> Maybe PatQ) -> BlockItem -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat BlockItem
arg,  (forall b. Data b => b -> Maybe PatQ) -> [BlockItem] -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat [BlockItem]
args]

qqPat :: Typeable a => a -> Maybe (Q Pat)
qqPat :: a -> Maybe PatQ
qqPat = Maybe PatQ -> a -> Maybe PatQ
forall a b. a -> b -> a
const Maybe PatQ
forall a. Maybe a
Nothing (a -> Maybe PatQ) -> (String -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` String -> Maybe PatQ
qqStringP
                      (a -> Maybe PatQ) -> (Loc -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Loc -> Maybe PatQ
qqLocP
                      (a -> Maybe PatQ) -> (Id -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Id -> Maybe PatQ
qqIdP
                      (a -> Maybe PatQ) -> (DeclSpec -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` DeclSpec -> Maybe PatQ
qqDeclSpecP
                      (a -> Maybe PatQ) -> (Decl -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Decl -> Maybe PatQ
qqDeclP
                      (a -> Maybe PatQ) -> (TypeQual -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` TypeQual -> Maybe PatQ
qqTypeQualP
                      (a -> Maybe PatQ) -> ([TypeQual] -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [TypeQual] -> Maybe PatQ
qqTypeQualListP
                      (a -> Maybe PatQ) -> (Type -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Type -> Maybe PatQ
qqTypeP
                      (a -> Maybe PatQ) -> (Initializer -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Initializer -> Maybe PatQ
qqInitializerP
                      (a -> Maybe PatQ)
-> ([Initializer] -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [Initializer] -> Maybe PatQ
qqInitializerListP
                      (a -> Maybe PatQ) -> (InitGroup -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` InitGroup -> Maybe PatQ
qqInitGroupP
                      (a -> Maybe PatQ) -> ([InitGroup] -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [InitGroup] -> Maybe PatQ
qqInitGroupListP
                      (a -> Maybe PatQ) -> (Attr -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Attr -> Maybe PatQ
qqAttrP
                      (a -> Maybe PatQ) -> ([Attr] -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [Attr] -> Maybe PatQ
qqAttrListP
                      (a -> Maybe PatQ) -> (FieldGroup -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` FieldGroup -> Maybe PatQ
qqFieldGroupP
                      (a -> Maybe PatQ) -> (CEnum -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` CEnum -> Maybe PatQ
qqCEnumP
                      (a -> Maybe PatQ) -> ([CEnum] -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [CEnum] -> Maybe PatQ
qqCEnumListP
                      (a -> Maybe PatQ) -> (Param -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Param -> Maybe PatQ
qqParamP
                      (a -> Maybe PatQ) -> ([Param] -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [Param] -> Maybe PatQ
qqParamListP
                      (a -> Maybe PatQ) -> (Definition -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Definition -> Maybe PatQ
qqDefinitionP
                      (a -> Maybe PatQ)
-> ([Definition] -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [Definition] -> Maybe PatQ
qqDefinitionListP
                      (a -> Maybe PatQ) -> (Const -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Const -> Maybe PatQ
qqConstP
                      (a -> Maybe PatQ) -> (Exp -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Exp -> Maybe PatQ
qqExpP
                      (a -> Maybe PatQ) -> ([Exp] -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [Exp] -> Maybe PatQ
qqExpListP
                      (a -> Maybe PatQ) -> (Stm -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` Stm -> Maybe PatQ
qqStmP
                      (a -> Maybe PatQ) -> ([Stm] -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [Stm] -> Maybe PatQ
qqStmListP
                      (a -> Maybe PatQ) -> (BlockItem -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` BlockItem -> Maybe PatQ
qqBlockItemP
                      (a -> Maybe PatQ) -> ([BlockItem] -> Maybe PatQ) -> a -> Maybe PatQ
forall a b q.
(Typeable a, Typeable b) =>
(a -> q) -> (b -> q) -> a -> q
`extQ` [BlockItem] -> Maybe PatQ
qqBlockItemListP

parse :: [C.Extensions]
      -> [String]
      -> P.P a
      -> String
      -> Q a
parse :: [Extensions] -> [String] -> P a -> String -> Q a
parse [Extensions]
exts [String]
typenames P a
p String
s = do
    Loc
loc <- Q Loc
location
    case [Extensions]
-> [String]
-> P a
-> ByteString
-> Maybe Pos
-> Either SomeException a
forall a.
[Extensions]
-> [String]
-> P a
-> ByteString
-> Maybe Pos
-> Either SomeException a
P.parse (Extensions
C.Antiquotation Extensions -> [Extensions] -> [Extensions]
forall a. a -> [a] -> [a]
: [Extensions]
exts) [String]
typenames P a
p (String -> ByteString
B.pack String
s) (Pos -> Maybe Pos
forall a. a -> Maybe a
Just (Loc -> Pos
locToPos Loc
loc)) of
      Left SomeException
err -> String -> Q a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (SomeException -> String
forall a. Show a => a -> String
show SomeException
err)
      Right a
x  -> a -> Q a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
  where
    locToPos :: TH.Loc -> Pos
    locToPos :: Loc -> Pos
locToPos TH.Loc {loc_filename :: Loc -> String
loc_filename = String
filename, loc_start :: Loc -> CharPos
loc_start = (Int
line, Int
col)} =
        String -> Int -> Int -> Int -> Pos
Pos String
filename Int
line Int
col Int
0

quasiquote :: Data a
           => [C.Extensions]
           -> [String]
           -> P.P a
           -> QuasiQuoter
quasiquote :: [Extensions] -> [String] -> P a -> QuasiQuoter
quasiquote [Extensions]
exts [String]
typenames P a
p =
    QuasiQuoter :: (String -> ExpQ)
-> (String -> PatQ)
-> (String -> Q Type)
-> (String -> Q [Dec])
-> QuasiQuoter
QuasiQuoter { quoteExp :: String -> ExpQ
quoteExp  = [Extensions] -> [String] -> P a -> String -> Q a
forall a. [Extensions] -> [String] -> P a -> String -> Q a
parse [Extensions]
exts [String]
typenames P a
p (String -> Q a) -> (a -> ExpQ) -> String -> ExpQ
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (forall b. Data b => b -> Maybe ExpQ) -> a -> ExpQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe ExpQ) -> a -> ExpQ
dataToExpQ forall b. Data b => b -> Maybe ExpQ
forall a. Typeable a => a -> Maybe ExpQ
qqExp
                , quotePat :: String -> PatQ
quotePat  = [Extensions] -> [String] -> P a -> String -> Q a
forall a. [Extensions] -> [String] -> P a -> String -> Q a
parse [Extensions]
exts [String]
typenames P a
p (String -> Q a) -> (a -> PatQ) -> String -> PatQ
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
forall a.
Data a =>
(forall b. Data b => b -> Maybe PatQ) -> a -> PatQ
dataToPatQ forall b. Data b => b -> Maybe PatQ
forall a. Typeable a => a -> Maybe PatQ
qqPat
                , quoteType :: String -> Q Type
quoteType = String -> String -> Q Type
forall a. HasCallStack => String -> a
error String
"C type quasiquoter undefined"
                , quoteDec :: String -> Q [Dec]
quoteDec  = String -> String -> Q [Dec]
forall a. HasCallStack => String -> a
error String
"C declaration quasiquoter undefined"
                }

#if !MIN_VERSION_template_haskell(2,7,0)
dataToQa  ::  forall a k q. Data a
          =>  (Name -> k)
          ->  (Lit -> Q q)
          ->  (k -> [Q q] -> Q q)
          ->  (forall b . Data b => b -> Maybe (Q q))
          ->  a
          ->  Q q
dataToQa mkCon mkLit appCon antiQ t =
    case antiQ t of
      Nothing ->
          case constrRep constr of
            AlgConstr _  ->
                appCon con conArgs
            IntConstr n ->
                mkLit $ integerL n
            FloatConstr n ->
                mkLit $ rationalL (toRational n)
            CharConstr c ->
                mkLit $ charL c
        where
          constr :: Constr
          constr = toConstr t

          con :: k
          con = mkCon (mkConName mod occ)
            where
              mod :: String
              mod = (tyconModule . dataTypeName . dataTypeOf) t

              occ :: String
              occ = showConstr constr

              mkConName :: String -> String -> Name
              mkConName "Prelude" "(:)" = Name (mkOccName ":") NameS
              mkConName "Prelude" "[]"  = Name (mkOccName "[]") NameS
              mkConName "Prelude" "()"  = Name (mkOccName "()") NameS

              mkConName "Prelude" s@('(' : ',' : rest) = go rest
                where
                  go :: String -> Name
                  go (',' : rest) = go rest
                  go ")"          = Name (mkOccName s) NameS
                  go _            = Name (mkOccName occ) (NameQ (mkModName mod))

              mkConName "GHC.Real" ":%" = mkNameG_d "base" "GHC.Real" ":%"

              mkConName mod occ = Name (mkOccName occ) (NameQ (mkModName mod))

          conArgs :: [Q q]
          conArgs = gmapQ (dataToQa mkCon mkLit appCon antiQ) t

      Just y -> y

-- | 'dataToExpQ' converts a value to a 'Q Exp' representation of the same
-- value. It takes a function to handle type-specific cases.
dataToExpQ  ::  Data a
            =>  (forall b . Data b => b -> Maybe (Q Exp))
            ->  a
            ->  Q Exp
dataToExpQ = dataToQa conE litE (foldl appE)

-- | 'dataToPatQ' converts a value to a 'Q Pat' representation of the same
-- value. It takes a function to handle type-specific cases.
dataToPatQ  ::  Data a
            =>  (forall b . Data b => b -> Maybe (Q Pat))
            ->  a
            ->  Q Pat
dataToPatQ = dataToQa id litP conP
#endif /* !MIN_VERSION_template_haskell(2,7,0) */