| 1 |
{-# LANGUAGE OverloadedStrings #-}
|
| 2 |
{-# LANGUAGE ScopedTypeVariables #-}
|
| 3 |
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
| 4 |
{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
|
| 5 |
|
| 6 |
module Data.Ini.Config
|
| 7 |
(
|
| 8 |
-- $main
|
| 9 |
-- * Running Parsers
|
| 10 |
parseIniFile
|
| 11 |
-- * Parser Types
|
| 12 |
, IniParser
|
| 13 |
, SectionParser
|
| 14 |
-- * Section-Level Parsing
|
| 15 |
, section
|
| 16 |
, sectionMb
|
| 17 |
, sectionDef
|
| 18 |
-- * Field-Level Parsing
|
| 19 |
, field
|
| 20 |
, fieldOf
|
| 21 |
, fieldMb
|
| 22 |
, fieldMbOf
|
| 23 |
, fieldDef
|
| 24 |
, fieldDefOf
|
| 25 |
, fieldFlag
|
| 26 |
, fieldFlagDef
|
| 27 |
-- * Reader Functions
|
| 28 |
, readable
|
| 29 |
, number
|
| 30 |
, string
|
| 31 |
, flag
|
| 32 |
) where
|
| 33 |
|
| 34 |
import Control.Monad.Trans.Except
|
| 35 |
import qualified Data.HashMap.Strict as HM
|
| 36 |
import Data.Ini.Raw
|
| 37 |
import Data.String (IsString(..))
|
| 38 |
import Data.Text (Text)
|
| 39 |
import qualified Data.Text as T
|
| 40 |
import Data.Typeable (Typeable, Proxy(..), typeRep)
|
| 41 |
import Text.Read (readMaybe)
|
| 42 |
|
| 43 |
addLineInformation :: Int -> Text -> StParser s a -> StParser s a
|
| 44 |
addLineInformation lineNo sec = withExceptT go
|
| 45 |
where go e = "Line " ++ show lineNo ++
|
| 46 |
", in section " ++ show sec ++
|
| 47 |
": " ++ e
|
| 48 |
|
| 49 |
type StParser s a = ExceptT String ((->) s) a
|
| 50 |
|
| 51 |
-- | An 'IniParser' value represents a computation for parsing entire
|
| 52 |
-- INI-format files.
|
| 53 |
newtype IniParser a = IniParser (StParser Ini a)
|
| 54 |
deriving (Functor, Applicative, Monad)
|
| 55 |
|
| 56 |
-- | A 'SectionParser' value represents a computation for parsing a single
|
| 57 |
-- section of an INI-format file.
|
| 58 |
newtype SectionParser a = SectionParser (StParser IniSection a)
|
| 59 |
deriving (Functor, Applicative, Monad)
|
| 60 |
|
| 61 |
-- | Parse a 'Text' value as an INI file and run an 'IniParser' over it
|
| 62 |
parseIniFile :: Text -> IniParser a -> Either String a
|
| 63 |
parseIniFile text (IniParser mote) = do
|
| 64 |
ini <- parseIni text
|
| 65 |
runExceptT mote ini
|
| 66 |
|
| 67 |
-- | Find a named section in the INI file and parse it with the provided
|
| 68 |
-- section parser, failing if the section does not exist.
|
| 69 |
--
|
| 70 |
-- >>> parseIniFile "[ONE]\nx = hello\n" $ section "ONE" (field "x")
|
| 71 |
-- Right "hello"
|
| 72 |
-- >>> parseIniFile "[ONE]\nx = hello\n" $ section "TWO" (field "y")
|
| 73 |
-- Left "No top-level section named \"TWO\""
|
| 74 |
section :: Text -> SectionParser a -> IniParser a
|
| 75 |
section name (SectionParser thunk) = IniParser $ ExceptT $ \(Ini ini) ->
|
| 76 |
case HM.lookup (T.toLower name) ini of
|
| 77 |
Nothing -> Left ("No top-level section named " ++ show name)
|
| 78 |
Just sec -> runExceptT thunk sec
|
| 79 |
|
| 80 |
-- | Find a named section in the INI file and parse it with the provided
|
| 81 |
-- section parser, returning 'Nothing' if the section does not exist.
|
| 82 |
--
|
| 83 |
-- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionMb "ONE" (field "x")
|
| 84 |
-- Right (Just "hello")
|
| 85 |
-- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionMb "TWO" (field "y")
|
| 86 |
-- Right Nothing
|
| 87 |
sectionMb :: Text -> SectionParser a -> IniParser (Maybe a)
|
| 88 |
sectionMb name (SectionParser thunk) = IniParser $ ExceptT $ \(Ini ini) ->
|
| 89 |
case HM.lookup (T.toLower name) ini of
|
| 90 |
Nothing -> return Nothing
|
| 91 |
Just sec -> Just `fmap` runExceptT thunk sec
|
| 92 |
|
| 93 |
-- | Find a named section in the INI file and parse it with the provided
|
| 94 |
-- section parser, returning a default value if the section does not exist.
|
| 95 |
--
|
| 96 |
-- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionDef "ONE" "def" (field "x")
|
| 97 |
-- Right "hello"
|
| 98 |
-- >>> parseIniFile "[ONE]\nx = hello\n" $ sectionDef "TWO" "def" (field "y")
|
| 99 |
-- Right "def"
|
| 100 |
sectionDef :: Text -> a -> SectionParser a -> IniParser a
|
| 101 |
sectionDef name def (SectionParser thunk) = IniParser $ ExceptT $ \(Ini ini) ->
|
| 102 |
case HM.lookup (T.toLower name) ini of
|
| 103 |
Nothing -> return def
|
| 104 |
Just sec -> runExceptT thunk sec
|
| 105 |
|
| 106 |
---
|
| 107 |
|
| 108 |
throw :: String -> StParser s a
|
| 109 |
throw msg = ExceptT $ (\ _ -> Left msg)
|
| 110 |
|
| 111 |
getSectionName :: StParser IniSection Text
|
| 112 |
getSectionName = ExceptT $ (\ m -> return (isName m))
|
| 113 |
|
| 114 |
rawFieldMb :: Text -> StParser IniSection (Maybe IniValue)
|
| 115 |
rawFieldMb name = ExceptT $ \m ->
|
| 116 |
return (HM.lookup name (isVals m))
|
| 117 |
|
| 118 |
rawField :: Text -> StParser IniSection IniValue
|
| 119 |
rawField name = do
|
| 120 |
sec <- getSectionName
|
| 121 |
valMb <- rawFieldMb name
|
| 122 |
case valMb of
|
| 123 |
Nothing -> throw ("Missing field " ++ show name ++
|
| 124 |
" in section " ++ show sec)
|
| 125 |
Just x -> return x
|
| 126 |
|
| 127 |
-- | Retrieve a field, failing if it doesn't exist, and return its raw value.
|
| 128 |
--
|
| 129 |
-- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (field "x")
|
| 130 |
-- Right "hello"
|
| 131 |
-- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (field "y")
|
| 132 |
-- Left "Missing field \"y\" in section \"main\""
|
| 133 |
field :: Text -> SectionParser Text
|
| 134 |
field name = SectionParser $ vValue `fmap` rawField name
|
| 135 |
|
| 136 |
-- | Retrieve a field and use the supplied parser to parse it as a value,
|
| 137 |
-- failing if the field does not exist, or if the parser fails to
|
| 138 |
-- produce a value.
|
| 139 |
--
|
| 140 |
-- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldOf "x" number)
|
| 141 |
-- Right 72
|
| 142 |
-- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldOf "x" number)
|
| 143 |
-- Left "Line 2, in section \"main\": Unable to parse \"hello\" as a value of type Integer"
|
| 144 |
-- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldOf "y" number)
|
| 145 |
-- Left "Missing field \"y\" in section \"main\""
|
| 146 |
fieldOf :: Text -> (Text -> Either String a) -> SectionParser a
|
| 147 |
fieldOf name parse = SectionParser $ do
|
| 148 |
sec <- getSectionName
|
| 149 |
val <- rawField name
|
| 150 |
case parse (vValue val) of
|
| 151 |
Left err -> addLineInformation (vLineNo val) sec (throw err)
|
| 152 |
Right x -> return x
|
| 153 |
|
| 154 |
-- | Retrieve a field, returning a @Nothing@ value if it does not exist.
|
| 155 |
--
|
| 156 |
-- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldMb "x")
|
| 157 |
-- Right (Just "hello")
|
| 158 |
-- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldMb "y")
|
| 159 |
-- Right Nothing
|
| 160 |
fieldMb :: Text -> SectionParser (Maybe Text)
|
| 161 |
fieldMb name = SectionParser $ fmap vValue `fmap` rawFieldMb name
|
| 162 |
|
| 163 |
-- | Retrieve a field and parse it according to the given parser, returning
|
| 164 |
-- @Nothing@ if it does not exist. If the parser fails, then this will
|
| 165 |
-- fail.
|
| 166 |
--
|
| 167 |
-- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldMbOf "x" number)
|
| 168 |
-- Right 72
|
| 169 |
-- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldMbOf "x" number)
|
| 170 |
-- Left "Line 2, in section \"main\": Unable to parse \"hello\" as a value of type Integer"
|
| 171 |
-- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldMbOf "y" number)
|
| 172 |
-- Right Nothing
|
| 173 |
fieldMbOf :: Text -> (Text -> Either String a) -> SectionParser (Maybe a)
|
| 174 |
fieldMbOf name parse = SectionParser $ do
|
| 175 |
sec <- getSectionName
|
| 176 |
mb <- rawFieldMb name
|
| 177 |
case mb of
|
| 178 |
Nothing -> return Nothing
|
| 179 |
Just v -> case parse (vValue v) of
|
| 180 |
Left err -> addLineInformation (vLineNo v) sec (throw err)
|
| 181 |
Right x -> return (Just x)
|
| 182 |
|
| 183 |
-- | Retrieve a field and supply a default value for if it doesn't exist.
|
| 184 |
--
|
| 185 |
-- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldDef "x" "def")
|
| 186 |
-- Right "hello"
|
| 187 |
-- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldDef "y" "def")
|
| 188 |
-- Right "def"
|
| 189 |
fieldDef :: Text -> Text -> SectionParser Text
|
| 190 |
fieldDef name def = SectionParser $ ExceptT $ \m ->
|
| 191 |
case HM.lookup name (isVals m) of
|
| 192 |
Nothing -> return def
|
| 193 |
Just x -> return (vValue x)
|
| 194 |
|
| 195 |
-- | Retrieve a field, parsing it according to the given parser, and returning
|
| 196 |
-- a default value if it does not exist. If the parser fails, then this will
|
| 197 |
-- fail.
|
| 198 |
--
|
| 199 |
-- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldDefOf "x" number 99)
|
| 200 |
-- Right 72
|
| 201 |
-- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldDefOf "x" number 99)
|
| 202 |
-- Left "Line 2, in section \"main\": Unable to parse \"hello\" as a value of type Integer"
|
| 203 |
-- >>> parseIniFile "[MAIN]\nx = 72\n" $ section "MAIN" (fieldDefOf "y" number 99)
|
| 204 |
-- Right 99
|
| 205 |
fieldDefOf :: Text -> (Text -> Either String a) -> a -> SectionParser a
|
| 206 |
fieldDefOf name parse def = SectionParser $ do
|
| 207 |
sec <- getSectionName
|
| 208 |
mb <- rawFieldMb name
|
| 209 |
case mb of
|
| 210 |
Nothing -> return def
|
| 211 |
Just v -> case parse (vValue v) of
|
| 212 |
Left err -> addLineInformation (vLineNo v) sec (throw err)
|
| 213 |
Right x -> return x
|
| 214 |
|
| 215 |
-- | Retrieve a field and treat it as a boolean, failing if it
|
| 216 |
-- does not exist.
|
| 217 |
--
|
| 218 |
-- >>> parseIniFile "[MAIN]\nx = yes\n" $ section "MAIN" (fieldFlag "x")
|
| 219 |
-- Right True
|
| 220 |
-- >>> parseIniFile "[MAIN]\nx = yes\n" $ section "MAIN" (fieldFlag "y")
|
| 221 |
-- Left "Missing field \"y\" in section \"main\""
|
| 222 |
fieldFlag :: Text -> SectionParser Bool
|
| 223 |
fieldFlag name = fieldOf name flag
|
| 224 |
|
| 225 |
-- | Retrieve a field and treat it as a boolean, subsituting
|
| 226 |
-- a default value if it doesn't exist.
|
| 227 |
--
|
| 228 |
-- >>> parseIniFile "[MAIN]\nx = yes\n" $ section "MAIN" (fieldFlagDef "x" False)
|
| 229 |
-- Right True
|
| 230 |
-- >>> parseIniFile "[MAIN]\nx = hello\n" $ section "MAIN" (fieldFlagDef "x" False)
|
| 231 |
-- Left "Line 2, in section \"main\": Unable to parse \"hello\" as a boolean"
|
| 232 |
-- >>> parseIniFile "[MAIN]\nx = yes\n" $ section "MAIN" (fieldFlagDef "y" False)
|
| 233 |
-- Right False
|
| 234 |
fieldFlagDef :: Text -> Bool -> SectionParser Bool
|
| 235 |
fieldFlagDef name def = fieldDefOf name flag def
|
| 236 |
|
| 237 |
---
|
| 238 |
|
| 239 |
-- | Try to use the "Read" instance for a type to parse a value, failing
|
| 240 |
-- with a human-readable error message if reading fails.
|
| 241 |
--
|
| 242 |
-- >>> readable "(5, 7)" :: Either String (Int, Int)
|
| 243 |
-- Right (5, 7)
|
| 244 |
-- >>> readable "hello" :: Either String (Int, Int)
|
| 245 |
-- Left "Unable to parse \"hello\" as a value of type (Int,Int)"
|
| 246 |
readable :: forall a. (Read a, Typeable a) => Text -> Either String a
|
| 247 |
readable t = case readMaybe str of
|
| 248 |
Just v -> Right v
|
| 249 |
Nothing -> Left ("Unable to parse " ++ show str ++
|
| 250 |
" as a value of type " ++ show typ)
|
| 251 |
where str = T.unpack t
|
| 252 |
typ = typeRep prx
|
| 253 |
prx :: Proxy a
|
| 254 |
prx = Proxy
|
| 255 |
|
| 256 |
-- | Try to use the "Read" instance for a numeric type to parse a value,
|
| 257 |
-- failing with a human-readable error message if reading fails.
|
| 258 |
--
|
| 259 |
-- >>> number "5" :: Either String Int
|
| 260 |
-- Right 5
|
| 261 |
-- >>> number "hello" :: Either String Int
|
| 262 |
-- Left "Unable to parse \"hello\" as a value of type Int"
|
| 263 |
number :: (Num a, Read a, Typeable a) => Text -> Either String a
|
| 264 |
number = readable
|
| 265 |
|
| 266 |
-- | Convert a textua value to the appropriate string type. This will
|
| 267 |
-- never fail.
|
| 268 |
--
|
| 269 |
-- >>> string "foo" :: Either String String
|
| 270 |
-- Right "foo"
|
| 271 |
string :: (IsString a) => Text -> Either String a
|
| 272 |
string = return . fromString . T.unpack
|
| 273 |
|
| 274 |
-- | Convert a string that represents a boolean to a proper boolean. This
|
| 275 |
-- is case-insensitive, and matches the words @true@, @false@, @yes@,
|
| 276 |
-- @no@, as well as single-letter abbreviations for all of the above.
|
| 277 |
-- If the input does not match, then this will fail with a human-readable
|
| 278 |
-- error message.
|
| 279 |
--
|
| 280 |
-- >>> flag "TRUE"
|
| 281 |
-- Right True
|
| 282 |
-- >>> flag "y"
|
| 283 |
-- Right True
|
| 284 |
-- >>> flag "no"
|
| 285 |
-- Right False
|
| 286 |
-- >>> flag "F"
|
| 287 |
-- Right False
|
| 288 |
-- >>> flag "That's a secret!"
|
| 289 |
-- Left "Unable to parse \"that's a secret!\" as a boolean"
|
| 290 |
flag :: Text -> Either String Bool
|
| 291 |
flag s = case T.toLower s of
|
| 292 |
"true" -> Right True
|
| 293 |
"yes" -> Right True
|
| 294 |
"t" -> Right True
|
| 295 |
"y" -> Right True
|
| 296 |
"false" -> Right False
|
| 297 |
"no" -> Right False
|
| 298 |
"f" -> Right False
|
| 299 |
"n" -> Right False
|
| 300 |
_ -> Left ("Unable to parse " ++ show s ++ " as a boolean")
|
| 301 |
|
| 302 |
|
| 303 |
-- $main
|
| 304 |
-- The 'config-ini' library exports some simple monadic functions to
|
| 305 |
-- make parsing INI-like configuration easier. INI files have a
|
| 306 |
-- two-level structure: the top-level named chunks of configuration,
|
| 307 |
-- and the individual key-value pairs contained within those chunks.
|
| 308 |
-- For example, the following INI file has two sections, @NETWORK@
|
| 309 |
-- and @LOCAL@, and each contains its own key-value pairs. Comments,
|
| 310 |
-- which begin with @#@ or @;@, are ignored:
|
| 311 |
--
|
| 312 |
-- > [NETWORK]
|
| 313 |
-- > host = example.com
|
| 314 |
-- > port = 7878
|
| 315 |
-- >
|
| 316 |
-- > # here is a comment
|
| 317 |
-- > [LOCAL]
|
| 318 |
-- > user = terry
|
| 319 |
--
|
| 320 |
-- The combinators provided here are designed to write quick and
|
| 321 |
-- idiomatic parsers for files of this form. Sections are parsed by
|
| 322 |
-- 'IniParser' computations, like 'section' and its variations,
|
| 323 |
-- while the fields within sections are parsed by 'SectionParser'
|
| 324 |
-- computations, like 'field' and its variations. If we want to
|
| 325 |
-- parse an INI file like the one above, treating the entire
|
| 326 |
-- `LOCAL` section as optional, we can write it like this:
|
| 327 |
--
|
| 328 |
-- > data Config = Config
|
| 329 |
-- > { cfNetwork :: NetworkConfig, cfLocal :: Maybe LocalConfig }
|
| 330 |
-- > deriving (Eq, Show)
|
| 331 |
-- >
|
| 332 |
-- > data NetworkConfig = NetworkConfig
|
| 333 |
-- > { netHost :: String, netPort :: Int }
|
| 334 |
-- > deriving (Eq, Show)
|
| 335 |
-- >
|
| 336 |
-- > data LocalConfig = LocalConfig
|
| 337 |
-- > { localUser :: Text }
|
| 338 |
-- > deriving (Eq, Show)
|
| 339 |
-- >
|
| 340 |
-- > configParser :: IniParser Config
|
| 341 |
-- > configParser = do
|
| 342 |
-- > netCf <- section "NETWORK" $ do
|
| 343 |
-- > host <- fieldOf "host" string
|
| 344 |
-- > port <- fieldOf "port" number
|
| 345 |
-- > return NetworkConfig { netHost = host, netPort = port }
|
| 346 |
-- > locCf <- sectionMb "LOCAL" $
|
| 347 |
-- > LocalConfig <$> field "user"
|
| 348 |
-- > return Config { cfNetwork = netCf, cfLocal = locCf }
|
| 349 |
--
|
| 350 |
-- We can run our computation with 'parseIniFile', which,
|
| 351 |
-- when run on our example file above, would produce the
|
| 352 |
-- following:
|
| 353 |
--
|
| 354 |
-- >>> parseIniFile example configParser
|
| 355 |
-- Right (Config {cfNetwork = NetworkConfig {netHost = "example.com", netPort = 7878}, cfLocal = Just (LocalConfig {localUser = "terry"})})
|