-- | Parsing of constants in GIR files.
module Data.GI.GIR.Constant
    ( Constant(..)
    , parseConstant
    ) where

import Data.Text (Text)

import Data.GI.GIR.BasicTypes (Type)
import Data.GI.GIR.Type (parseType)
import Data.GI.GIR.Parser

-- | Info about a constant.
data Constant = Constant {
      Constant -> Type
constantType        :: Type,
      Constant -> ParseError
constantValue       :: Text,
      Constant -> ParseError
constantCType       :: Text,
      Constant -> Documentation
constantDocumentation :: Documentation,
      Constant -> Maybe DeprecationInfo
constantDeprecated  :: Maybe DeprecationInfo
    } deriving (Int -> Constant -> ShowS
[Constant] -> ShowS
Constant -> String
(Int -> Constant -> ShowS)
-> (Constant -> String) -> ([Constant] -> ShowS) -> Show Constant
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Constant -> ShowS
showsPrec :: Int -> Constant -> ShowS
$cshow :: Constant -> String
show :: Constant -> String
$cshowList :: [Constant] -> ShowS
showList :: [Constant] -> ShowS
Show)

-- | Parse a "constant" element from the GIR file.
parseConstant :: Parser (Name, Constant)
parseConstant :: Parser (Name, Constant)
parseConstant = do
  name <- Parser Name
parseName
  deprecated <- parseDeprecation
  value <- getAttr "value"
  t <- parseType
  -- This contains the C name for the constant. The C gir generator
  -- call this "c:type", while the vala gir generator calls it
  -- "c:identifier", so try both.
  ctype <- queryAttrWithNamespace CGIRNS "type" >>= \case
    Just ParseError
i -> ParseError -> Parser ParseError
forall a. a -> ReaderT ParseContext (Except ParseError) a
forall (m :: * -> *) a. Monad m => a -> m a
return ParseError
i
    Maybe ParseError
Nothing -> GIRXMLNamespace -> Name -> Parser ParseError
getAttrWithNamespace GIRXMLNamespace
CGIRNS Name
"identifier"
  doc <- parseDocumentation
  return (name, Constant { constantType = t
                         , constantValue = value
                         , constantCType = ctype
                         , constantDocumentation = doc
                         , constantDeprecated = deprecated
                         })