gdritter repos config-ini / 1e8fa09
Merge pull request #17 from kquick/megaparsec7 Update to support megaparsec 7.x. G. D. Ritter authored 6 years ago GitHub committed 6 years ago
2 changed file(s) with 13 addition(s) and 9 deletion(s). Collapse all Expand all
4747 , text >=1.2.2 && <1.3
4848 , unordered-containers >=0.2.7 && <0.3
4949 , transformers >=0.4.1 && <0.6
50 , megaparsec >=6 && <7
50 , megaparsec >=7 && <8
5151 default-language: Haskell2010
5252
5353 test-suite test-ini-compat
1 {-# LANGUAGE FlexibleInstances #-}
12 {-|
23 Module : Data.Ini.Config.Raw
34 Copyright : (c) Getty Ritter, 2017
4041 import Text.Megaparsec.Char
4142
4243 type Parser = Parsec (ErrorFancy Void) Text
44
45 instance ShowErrorComponent (ErrorFancy Void) where
46 showErrorComponent = show
4347
4448 -- | The 'NormalizedText' type is an abstract representation of text
4549 -- which has had leading and trailing whitespace removed and been
146150 -- amount of structure as needed to reconstruct the original INI file.
147151 parseRawIni :: Text -> Either String RawIni
148152 parseRawIni t = case runParser pIni "ini file" t of
149 Left err -> Left (parseErrorPretty err)
153 Left err -> Left (errorBundlePretty err)
150154 Right v -> Right v
151155
152156 pIni :: Parser RawIni
159163
160164 sComment :: Parser BlankLine
161165 sComment = do
162 c <- oneOf ";#"
163 txt <- T.pack `fmap` manyTill anyChar eol
166 c <- oneOf (";#" :: String)
167 txt <- T.pack `fmap` manyTill anySingle eol
164168 return (CommentLine c txt)
165169
166170 pSections :: Seq BlankLine -> Seq (NormalizedText, IniSection) -> Parser RawIni
171175 pSection leading prevs = do
172176 start <- getCurrentLine
173177 void (char '[')
174 name <- T.pack `fmap` some (noneOf "[]")
178 name <- T.pack `fmap` some (noneOf ("[]" :: String))
175179 void (char ']')
176180 void eol
177181 comments <- sBlanks
204208 pPair :: Seq BlankLine -> Parser (NormalizedText, IniValue)
205209 pPair leading = do
206210 pos <- getCurrentLine
207 key <- T.pack `fmap` some (noneOf "[]=:")
208 delim <- oneOf ":="
209 val <- T.pack `fmap` manyTill anyChar eol
211 key <- T.pack `fmap` some (noneOf ("[]=:" :: String))
212 delim <- oneOf (":=" :: String)
213 val <- T.pack `fmap` manyTill anySingle eol
210214 return ( normalize key
211215 , IniValue
212216 { vLineNo = pos
218222 } )
219223
220224 getCurrentLine :: Parser Int
221 getCurrentLine = (fromIntegral . unPos . sourceLine) `fmap` getPosition
225 getCurrentLine = (fromIntegral . unPos . sourceLine) `fmap` getSourcePos
222226
223227
224228 -- | Serialize an INI file to text, complete with any comments which