simple-config 1.0.0 → 1.1.0
raw patch · 6 files changed
+50/−4 lines, 6 files
Files
- Text/Config.hs +27/−1
- Text/Config/Lib.hs +8/−0
- Text/Config/Parser.hs +3/−1
- Text/Config/TH.hs +8/−0
- Text/Config/Types.hs +2/−0
- simple-config.cabal +2/−2
Text/Config.hs view
@@ -12,18 +12,44 @@ -- > uri URI -- > text String -- > list [String]+-- > val Int+-- > vals [Int]+-- > bs ByteString -- > |] -- -- The example generates following codes. -- -- > data TestConfig = TestConfig--- > { uri :: String+-- > { uri :: String -- > , text :: String -- > , list :: [String]+-- > , val :: Int+-- > , vals :: [Int]+-- > , bs :: ByteString -- > }+-- > deriving (Show)+-- >+-- > instance Default TestConfig where+-- > def = TestConfig+-- > { uri = "http://localhost/"+-- > , text = ""+-- > , list = []+-- > , val = 0+-- > , vals = []+-- > , bs = ""+-- > } -- > -- > configParser :: Parser TestConfig -- > configParser = ...+--+-- Its parser is able to parse following string.+--+-- > uri: http://example.com/content.html+-- > text: wakaruwa+-- > list: kaede, kirari, momoka+-- > val: 28+-- > vals: 25, 17, 12+-- > bs: chihiro -- module Text.Config ( -- * Types
Text/Config/Lib.hs view
@@ -5,6 +5,8 @@ import Text.Parsec.ByteString import Control.Applicative hiding (many, (<|>)) import Network.URI+import Data.ByteString (ByteString)+import qualified Data.ByteString.Char8 as BC comment :: Parser () comment = () <$ string "--" <* many (noneOf "\n")@@ -30,6 +32,9 @@ cv_string :: Parser String cv_string = many1 (noneOf ", \t\r\n") <* spcs +cv_bytestring :: Parser ByteString+cv_bytestring = BC.pack <$> cv_string+ cv_list :: Parser p -> Parser [p] cv_list p = sepBy p (spcs *> char ',' <* spcs) <* spcs @@ -39,6 +44,9 @@ if isURI str then return str else fail "parse error: URI"++cv_int :: Parser Int+cv_int = read <$> many1 digit <* spcs sep :: Parser () sep = () <$ char ':' *> spcs
Text/Config/Parser.hs view
@@ -36,9 +36,11 @@ <*> (confType <* spcs <* commentLines) confType :: Parser ConfType-confType = choice [typeString, typeUri, typeList]+confType = choice [typeByteString, typeString, typeUri, typeInt, typeList] where typeString = string "String" *> return ConfString typeUri = string "URI" *> return ConfURI+ typeInt = string "Int" *> return ConfInt+ typeByteString = string "ByteString" *> return ConfByteString typeList = ConfList <$> (char '[' *> confType) <* char ']'
Text/Config/TH.hs view
@@ -15,6 +15,8 @@ import Data.Default import Control.Monad.Trans.Class (lift) import Control.Monad.Trans.State+import Data.ByteString (ByteString)+import qualified Data.ByteString as BS import Text.Config.Parser import Text.Config.Types@@ -57,6 +59,8 @@ confTypeQ :: ConfType -> TypeQ confTypeQ ConfString = [t|String|] confTypeQ ConfURI = [t|String|]+ confTypeQ ConfInt = [t|Int|]+ confTypeQ ConfByteString = [t|ByteString|] confTypeQ (ConfList ctype) = [t|[$(confTypeQ ctype)]|] {-@@ -73,6 +77,8 @@ defVal :: ConfLine -> Q (Name, Exp) defVal (n, ConfString) = (,) (mkName n) <$> [|""|] defVal (n, ConfURI) = (,) (mkName n) <$> [|"http://localhost/"|]+ defVal (n, ConfInt) = (,) (mkName n) <$> [|0::Int|]+ defVal (n, ConfByteString) = (,) (mkName n) <$> [|BS.empty|] defVal (n, ConfList _) = (,) (mkName n) <$> [|[]|] mkParsers :: Name -> [ConfLine] -> DecsQ@@ -135,6 +141,8 @@ confParser :: ConfType -> ExpQ confParser ConfString = [|cv_string|] confParser ConfURI = [|cv_uri|]+confParser ConfInt = [|cv_int|]+confParser ConfByteString = [|cv_bytestring|] confParser (ConfList ctype) = [|cv_list $(confParser ctype)|] val :: Parser a -> String -> Parser a
Text/Config/Types.hs view
@@ -4,6 +4,8 @@ data ConfType = ConfString | ConfURI+ | ConfInt+ | ConfByteString | ConfList ConfType deriving (Show)
simple-config.cabal view
@@ -1,11 +1,11 @@ Name: simple-config-Version: 1.0.0+Version: 1.1.0 Synopsis: Simple config file parser generator Description: Simple config file parser generator Homepage: https://github.com/yunomu/simple-config License: BSD3 License-file: LICENSE-Author: YusukeNomura+Author: Yusuke Nomura Maintainer: yunomu@gmail.com -- Copyright: Category: Text