Commits

Alexander Vershilov committed 5b7b3f8

utils function added

Comments (0)

Files changed (1)

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
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.