Source

gccxml / Language / Cpp / GccXml / Utils.hs

module Language.Cpp.GccXml.Utils
    ( CodeMap
    , sinkCodeMap
    -- * Searching 
    , search
    , classByName
    -- * Type check
    , isClass
    , isConstructor
    , isDestructor
    , isEnumeration
    , isFunction
    , isMethod
    , isNamespace
    , isType
    )
    where

import Data.Conduit
import Data.Map (Map)
import Data.Text (Text)
import qualified Data.Map as M
import Language.Cpp.GccXml
import Language.Cpp.GccXml.Types


-- | get class (ID,Value) based on its name 
classByName :: Map ID Declaration-> Text -> Maybe (ID, Declaration)
classByName code name = search code f
    where f (Class n) | className n == name = True
          f _                               = False

-- | Generic search by attribute
search :: Map ID Declaration -> (Declaration -> Bool) -> Maybe (ID, Declaration)
search code f = saveHead $ filter (f . snd) (M.toList code)
    where saveHead [] = Nothing
          saveHead (x:_) = Just x



-- | Type for internal representation of XML document
type CodeMap = Map ID Declaration

isClass :: Declaration -> Bool
isClass (Class _) = True
isClass _         = False

isMethod :: Declaration -> Bool
isMethod (Method _ _ _ _ _) = True
isMethod _                  = False

isNamespace :: Declaration -> Bool
isNamespace (Namespace _ _) = True
isNamespace _               = False

isEnumeration :: Declaration -> Bool
isEnumeration (Enumeration _ _) = True
isEnumeration _                 = False

isConstructor :: Declaration -> Bool
isConstructor (Constructor _ _ _) = True
isConstructor _                   = False

isDestructor :: Declaration -> Bool
isDestructor (Destructor _ _) = True
isDestructor _                = False

isFunction :: Declaration -> Bool
isFunction (Function _ _ _ _) = True
isFunction _                  = False

isType :: Declaration -> Bool
isType (FundamentalType _)     = True
isType (PointerType _)         = True
isType (ReferenceType _)       = True
isType (ArrayType _ _)         = True
isType (FunctionType _ _)      = True
isType (CvQualifiedType _ _ _) = True
isType (Typedef _ _)           = True
isType _                       = False


-- | Build internal representation in a conduit way
sinkCodeMap :: (Monad m) => Sink (ID,Declaration) m CodeMap
sinkCodeMap = sinkState M.empty push close
    where
        push state (_,JUNK) = return $ StateProcessing state 
        push state (i,d)    = return $ StateProcessing (M.insert i d state)
        close state = return state