{-# LANGUAGE RecordWildCards, PatternGuards #-}
module Data.GI.GIR.Type
( parseType
, queryCType
, parseCType
, queryElementCType
, parseOptionalType
) where
import Data.Maybe (catMaybes)
#if !MIN_VERSION_base(4,11,0)
import Data.Monoid ((<>))
#endif
import Data.Text (Text)
import qualified Data.Text as T
import Foreign.Storable (sizeOf)
import Foreign.C (CShort, CUShort, CSize)
import System.Posix.Types (CSsize)
import Data.GI.GIR.BasicTypes (Type(..), BasicType(..))
import Data.GI.GIR.Parser
nameToBasicType :: Text -> Maybe BasicType
nameToBasicType :: Text -> Maybe BasicType
nameToBasicType "gpointer" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TPtr
nameToBasicType "gboolean" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TBoolean
nameToBasicType "gchar" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TInt8
nameToBasicType "gint" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TInt
nameToBasicType "guint" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TUInt
nameToBasicType "glong" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TLong
nameToBasicType "gulong" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TULong
nameToBasicType "gint8" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TInt8
nameToBasicType "guint8" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TUInt8
nameToBasicType "gint16" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TInt16
nameToBasicType "guint16" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TUInt16
nameToBasicType "gint32" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TInt32
nameToBasicType "guint32" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TUInt32
nameToBasicType "gint64" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TInt64
nameToBasicType "guint64" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TUInt64
nameToBasicType "gfloat" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TFloat
nameToBasicType "gdouble" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TDouble
nameToBasicType "gunichar" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TUniChar
nameToBasicType "GType" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TGType
nameToBasicType "utf8" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TUTF8
nameToBasicType "filename" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TFileName
nameToBasicType "gintptr" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TIntPtr
nameToBasicType "guintptr" = BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TUIntPtr
nameToBasicType "gshort" = case CShort -> Int
forall a. Storable a => a -> Int
sizeOf (0 :: CShort) of
2 -> BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TInt16
4 -> BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TInt32
8 -> BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TInt64
n :: Int
n -> [Char] -> Maybe BasicType
forall a. HasCallStack => [Char] -> a
error ([Char] -> Maybe BasicType) -> [Char] -> Maybe BasicType
forall a b. (a -> b) -> a -> b
$ "Unexpected short size: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n
nameToBasicType "gushort" = case CUShort -> Int
forall a. Storable a => a -> Int
sizeOf (0 :: CUShort) of
2 -> BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TUInt16
4 -> BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TUInt32
8 -> BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TUInt64
n :: Int
n -> [Char] -> Maybe BasicType
forall a. HasCallStack => [Char] -> a
error ([Char] -> Maybe BasicType) -> [Char] -> Maybe BasicType
forall a b. (a -> b) -> a -> b
$ "Unexpected ushort size: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n
nameToBasicType "gssize" = case CSsize -> Int
forall a. Storable a => a -> Int
sizeOf (0 :: CSsize) of
4 -> BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TInt32
8 -> BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TInt64
n :: Int
n -> [Char] -> Maybe BasicType
forall a. HasCallStack => [Char] -> a
error ([Char] -> Maybe BasicType) -> [Char] -> Maybe BasicType
forall a b. (a -> b) -> a -> b
$ "Unexpected ssize length: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n
nameToBasicType "gsize" = case CSize -> Int
forall a. Storable a => a -> Int
sizeOf (0 :: CSize) of
4 -> BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TUInt32
8 -> BasicType -> Maybe BasicType
forall a. a -> Maybe a
Just BasicType
TUInt64
n :: Int
n -> [Char] -> Maybe BasicType
forall a. HasCallStack => [Char] -> a
error ([Char] -> Maybe BasicType) -> [Char] -> Maybe BasicType
forall a b. (a -> b) -> a -> b
$ "Unexpected size length: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n
nameToBasicType _ = Maybe BasicType
forall a. Maybe a
Nothing
parseArrayInfo :: Parser Type
parseArrayInfo :: Parser Type
parseArrayInfo = Name -> Parser (Maybe Text)
queryAttr "name" Parser (Maybe Text) -> (Maybe Text -> Parser Type) -> Parser Type
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Just "GLib.Array" -> Type -> Type
TGArray (Type -> Type) -> Parser Type -> Parser Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Type
parseType
Just "GLib.PtrArray" -> Type -> Type
TPtrArray (Type -> Type) -> Parser Type -> Parser Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Type
parseType
Just "GLib.ByteArray" -> Type -> Parser Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
TByteArray
Just other :: Text
other -> Text -> Parser Type
forall a. Text -> Parser a
parseError (Text -> Parser Type) -> Text -> Parser Type
forall a b. (a -> b) -> a -> b
$ "Unsupported array type: \"" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
other Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "\""
Nothing -> Parser Type
parseCArrayType
parseCArrayType :: Parser Type
parseCArrayType :: Parser Type
parseCArrayType = do
Bool
zeroTerminated <- Name -> Parser (Maybe Text)
queryAttr "zero-terminated" Parser (Maybe Text)
-> (Maybe Text -> ReaderT ParseContext (Except Text) Bool)
-> ReaderT ParseContext (Except Text) Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Just b :: Text
b -> Text -> ReaderT ParseContext (Except Text) Bool
parseBool Text
b
Nothing -> Bool -> ReaderT ParseContext (Except Text) Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
Int
length <- Name -> Parser (Maybe Text)
queryAttr "length" Parser (Maybe Text)
-> (Maybe Text -> ReaderT ParseContext (Except Text) Int)
-> ReaderT ParseContext (Except Text) Int
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Just l :: Text
l -> Text -> ReaderT ParseContext (Except Text) Int
forall a. Integral a => Text -> Parser a
parseIntegral Text
l
Nothing -> Int -> ReaderT ParseContext (Except Text) Int
forall (m :: * -> *) a. Monad m => a -> m a
return (-1)
Int
fixedSize <- Name -> Parser (Maybe Text)
queryAttr "fixed-size" Parser (Maybe Text)
-> (Maybe Text -> ReaderT ParseContext (Except Text) Int)
-> ReaderT ParseContext (Except Text) Int
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Just s :: Text
s -> Text -> ReaderT ParseContext (Except Text) Int
forall a. Integral a => Text -> Parser a
parseIntegral Text
s
Nothing -> Int -> ReaderT ParseContext (Except Text) Int
forall (m :: * -> *) a. Monad m => a -> m a
return (-1)
Type
elementType <- Parser Type
parseType
Type -> Parser Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> Parser Type) -> Type -> Parser Type
forall a b. (a -> b) -> a -> b
$ Bool -> Int -> Int -> Type -> Type
TCArray Bool
zeroTerminated Int
fixedSize Int
length Type
elementType
parseHashTable :: Parser Type
parseHashTable :: Parser Type
parseHashTable = Parser [Maybe Type]
parseTypeElements Parser [Maybe Type] -> ([Maybe Type] -> Parser Type) -> Parser Type
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
[Just key :: Type
key, Just value :: Type
value] -> Type -> Parser Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> Parser Type) -> Type -> Parser Type
forall a b. (a -> b) -> a -> b
$ Type -> Type -> Type
TGHash Type
key Type
value
other :: [Maybe Type]
other -> Text -> Parser Type
forall a. Text -> Parser a
parseError (Text -> Parser Type) -> Text -> Parser Type
forall a b. (a -> b) -> a -> b
$ "Unsupported hash type: "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack ([Maybe Type] -> [Char]
forall a. Show a => a -> [Char]
show [Maybe Type]
other)
parseClosure :: Parser Type
parseClosure :: Parser Type
parseClosure = Name -> Parser (Maybe Text)
queryAttr "closure-type" Parser (Maybe Text) -> (Maybe Text -> Parser Type) -> Parser Type
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Just t :: Text
t -> (Maybe Type -> Type
TGClosure (Maybe Type -> Type) -> (Type -> Maybe Type) -> Type -> Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type -> Maybe Type
forall a. a -> Maybe a
Just) (Type -> Type) -> Parser Type -> Parser Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Parser Type
parseTypeName Text
t
Nothing -> Type -> Parser Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> Parser Type) -> Type -> Parser Type
forall a b. (a -> b) -> a -> b
$ Maybe Type -> Type
TGClosure Maybe Type
forall a. Maybe a
Nothing
parseListType :: Parser Type
parseListType :: Parser Type
parseListType = Parser (Maybe Type)
queryType Parser (Maybe Type) -> (Maybe Type -> Parser Type) -> Parser Type
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Just t :: Type
t -> Type -> Parser Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
t
Nothing -> Type -> Parser Type
forall (m :: * -> *) a. Monad m => a -> m a
return (BasicType -> Type
TBasicType BasicType
TPtr)
parseFundamentalType :: Text -> Text -> Parser Type
parseFundamentalType :: Text -> Text -> Parser Type
parseFundamentalType "GLib" "List" = Type -> Type
TGList (Type -> Type) -> Parser Type -> Parser Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Type
parseListType
parseFundamentalType "GLib" "SList" = Type -> Type
TGSList (Type -> Type) -> Parser Type -> Parser Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Type
parseListType
parseFundamentalType "GLib" "HashTable" = Parser Type
parseHashTable
parseFundamentalType "GLib" "Error" = Type -> Parser Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
TError
parseFundamentalType "GLib" "Variant" = Type -> Parser Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
TVariant
parseFundamentalType "GObject" "ParamSpec" = Type -> Parser Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
TParamSpec
parseFundamentalType "GObject" "Closure" = Parser Type
parseClosure
parseFundamentalType ns :: Text
ns n :: Text
n = Name -> Parser Type
resolveQualifiedTypeName (Text -> Text -> Name
Name Text
ns Text
n)
parseTypeName :: Text -> Parser Type
parseTypeName :: Text -> Parser Type
parseTypeName typeName :: Text
typeName = case Text -> Maybe BasicType
nameToBasicType Text
typeName of
Just b :: BasicType
b -> Type -> Parser Type
forall (m :: * -> *) a. Monad m => a -> m a
return (BasicType -> Type
TBasicType BasicType
b)
Nothing -> case (Char -> Bool) -> Text -> [Text]
T.split ('.' Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==) Text
typeName of
[ns :: Text
ns, n :: Text
n] -> Text -> Text -> Parser Type
parseFundamentalType Text
ns Text
n
[n :: Text
n] -> do
Text
ns <- Parser Text
currentNamespace
Text -> Text -> Parser Type
parseFundamentalType Text
ns Text
n
_ -> Text -> Parser Type
forall a. Text -> Parser a
parseError (Text -> Parser Type) -> Text -> Parser Type
forall a b. (a -> b) -> a -> b
$ "Unsupported type form: \""
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
typeName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "\""
parseTypeInfo :: Parser (Maybe Type)
parseTypeInfo :: Parser (Maybe Type)
parseTypeInfo = do
Text
typeName <- Name -> Parser Text
getAttr "name"
if Text
typeName Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "none"
then Maybe Type -> Parser (Maybe Type)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Type
forall a. Maybe a
Nothing
else Type -> Maybe Type
forall a. a -> Maybe a
Just (Type -> Maybe Type) -> Parser Type -> Parser (Maybe Type)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Parser Type
parseTypeName Text
typeName
parseTypeElements :: Parser [Maybe Type]
parseTypeElements :: Parser [Maybe Type]
parseTypeElements = do
[Maybe Type]
types <- Text -> Parser (Maybe Type) -> Parser [Maybe Type]
forall a. Text -> Parser a -> Parser [a]
parseChildrenWithLocalName "type" Parser (Maybe Type)
parseTypeInfo
[Type]
arrays <- Text -> Parser Type -> Parser [Type]
forall a. Text -> Parser a -> Parser [a]
parseChildrenWithLocalName "array" Parser Type
parseArrayInfo
[Maybe Type] -> Parser [Maybe Type]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Maybe Type]
types [Maybe Type] -> [Maybe Type] -> [Maybe Type]
forall a. [a] -> [a] -> [a]
++ (Type -> Maybe Type) -> [Type] -> [Maybe Type]
forall a b. (a -> b) -> [a] -> [b]
map Type -> Maybe Type
forall a. a -> Maybe a
Just [Type]
arrays)
queryCType :: Parser (Maybe Text)
queryCType :: Parser (Maybe Text)
queryCType = GIRXMLNamespace -> Name -> Parser (Maybe Text)
queryAttrWithNamespace GIRXMLNamespace
CGIRNS "type"
parseCType :: Parser Text
parseCType :: Parser Text
parseCType = GIRXMLNamespace -> Name -> Parser Text
getAttrWithNamespace GIRXMLNamespace
CGIRNS "type"
parseCTypeNameElements :: Parser [Text]
parseCTypeNameElements :: Parser [Text]
parseCTypeNameElements = do
[Maybe Text]
types <- Text -> Parser (Maybe Text) -> Parser [Maybe Text]
forall a. Text -> Parser a -> Parser [a]
parseChildrenWithLocalName "type" Parser (Maybe Text)
queryCType
[Maybe Text]
arrays <- Text -> Parser (Maybe Text) -> Parser [Maybe Text]
forall a. Text -> Parser a -> Parser [a]
parseChildrenWithLocalName "array" Parser (Maybe Text)
queryCType
[Text] -> Parser [Text]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Maybe Text] -> [Text]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Text]
types [Maybe Text] -> [Maybe Text] -> [Maybe Text]
forall a. [a] -> [a] -> [a]
++ [Maybe Text]
arrays))
queryType :: Parser (Maybe Type)
queryType :: Parser (Maybe Type)
queryType = Parser [Maybe Type]
parseTypeElements Parser [Maybe Type]
-> ([Maybe Type] -> Parser (Maybe Type)) -> Parser (Maybe Type)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
[Just e :: Type
e] -> Maybe Type -> Parser (Maybe Type)
forall (m :: * -> *) a. Monad m => a -> m a
return (Type -> Maybe Type
forall a. a -> Maybe a
Just Type
e)
[] -> Maybe Type -> Parser (Maybe Type)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Type
forall a. Maybe a
Nothing
[Nothing] -> Text -> Parser (Maybe Type)
forall a. Text -> Parser a
parseError (Text -> Parser (Maybe Type)) -> Text -> Parser (Maybe Type)
forall a b. (a -> b) -> a -> b
$ "Unexpected \"none\" type."
_ -> Text -> Parser (Maybe Type)
forall a. Text -> Parser a
parseError (Text -> Parser (Maybe Type)) -> Text -> Parser (Maybe Type)
forall a b. (a -> b) -> a -> b
$ "Found more than one type for the element."
parseType :: Parser Type
parseType :: Parser Type
parseType = Parser [Maybe Type]
parseTypeElements Parser [Maybe Type] -> ([Maybe Type] -> Parser Type) -> Parser Type
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
[Just e :: Type
e] -> Type -> Parser Type
forall (m :: * -> *) a. Monad m => a -> m a
return Type
e
[] -> Text -> Parser Type
forall a. Text -> Parser a
parseError (Text -> Parser Type) -> Text -> Parser Type
forall a b. (a -> b) -> a -> b
$ "Did not find a type for the element."
[Nothing] -> Text -> Parser Type
forall a. Text -> Parser a
parseError (Text -> Parser Type) -> Text -> Parser Type
forall a b. (a -> b) -> a -> b
$ "Unexpected \"none\" type."
_ -> Text -> Parser Type
forall a. Text -> Parser a
parseError (Text -> Parser Type) -> Text -> Parser Type
forall a b. (a -> b) -> a -> b
$ "Found more than one type for the element."
parseOptionalType :: Parser (Maybe Type)
parseOptionalType :: Parser (Maybe Type)
parseOptionalType =
Parser [Maybe Type]
parseTypeElements Parser [Maybe Type]
-> ([Maybe Type] -> Parser (Maybe Type)) -> Parser (Maybe Type)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
[e :: Maybe Type
e] -> Maybe Type -> Parser (Maybe Type)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Type
e
[] -> Text -> Parser (Maybe Type)
forall a. Text -> Parser a
parseError (Text -> Parser (Maybe Type)) -> Text -> Parser (Maybe Type)
forall a b. (a -> b) -> a -> b
$ "Did not find a type for the element."
_ -> Text -> Parser (Maybe Type)
forall a. Text -> Parser a
parseError (Text -> Parser (Maybe Type)) -> Text -> Parser (Maybe Type)
forall a b. (a -> b) -> a -> b
$ "Found more than one type for the element."
queryElementCType :: Parser (Maybe Text)
queryElementCType :: Parser (Maybe Text)
queryElementCType = Parser [Text]
parseCTypeNameElements Parser [Text]
-> ([Text] -> Parser (Maybe Text)) -> Parser (Maybe Text)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
[ctype :: Text
ctype] -> Maybe Text -> Parser (Maybe Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
ctype)
[] -> Maybe Text -> Parser (Maybe Text)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Text
forall a. Maybe a
Nothing
_ -> Text -> Parser (Maybe Text)
forall a. Text -> Parser a
parseError (Text -> Parser (Maybe Text)) -> Text -> Parser (Maybe Text)
forall a b. (a -> b) -> a -> b
$ "Found more than one type for the element."