module Data.GI.GIR.Alias
( documentListAliases
) where
import qualified Data.Map as M
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import qualified Data.Text as T
import Text.XML (Element(elementAttributes), Document(documentRoot))
import Data.GI.GIR.BasicTypes (Alias(..), Type(..), BasicType(..))
import Data.GI.GIR.Type (parseOptionalType)
import Data.GI.GIR.Parser
import Data.GI.GIR.XMLUtils (childElemsWithLocalName)
namespaceListAliases :: Element -> M.Map Alias Type
namespaceListAliases ns =
case M.lookup "name" (elementAttributes ns) of
Nothing -> error $ "Namespace with no name!"
Just nsName -> case runParser nsName M.empty ns parseAliases of
Left err -> (error . T.unpack) err
Right aliases -> M.fromList (map addNS aliases)
where addNS (n, t) = (Alias (Name nsName n), t)
parseAliases :: Parser [(Text, Type)]
parseAliases = parseChildrenWithLocalName "alias" parseAlias
parseAlias :: Parser (Text, Type)
parseAlias = do
name <- getAttr "name"
t <- parseOptionalType
return (name, fromMaybe (TBasicType TPtr) t)
documentListAliases :: Document -> M.Map Alias Type
documentListAliases doc = M.unions (map namespaceListAliases namespaces)
where namespaces = childElemsWithLocalName "namespace" (documentRoot doc)