| 1 |
{-# LANGUAGE FlexibleInstances #-}
|
1 | 2 |
{-|
|
2 | 3 |
Module : Data.Ini.Config.Raw
|
3 | 4 |
Copyright : (c) Getty Ritter, 2017
|
|
40 | 41 |
import Text.Megaparsec.Char
|
41 | 42 |
|
42 | 43 |
type Parser = Parsec (ErrorFancy Void) Text
|
| 44 |
|
| 45 |
instance ShowErrorComponent (ErrorFancy Void) where
|
| 46 |
showErrorComponent = show
|
43 | 47 |
|
44 | 48 |
-- | The 'NormalizedText' type is an abstract representation of text
|
45 | 49 |
-- which has had leading and trailing whitespace removed and been
|
|
146 | 150 |
-- amount of structure as needed to reconstruct the original INI file.
|
147 | 151 |
parseRawIni :: Text -> Either String RawIni
|
148 | 152 |
parseRawIni t = case runParser pIni "ini file" t of
|
149 | |
Left err -> Left (parseErrorPretty err)
|
| 153 |
Left err -> Left (errorBundlePretty err)
|
150 | 154 |
Right v -> Right v
|
151 | 155 |
|
152 | 156 |
pIni :: Parser RawIni
|
|
159 | 163 |
|
160 | 164 |
sComment :: Parser BlankLine
|
161 | 165 |
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
|
164 | 168 |
return (CommentLine c txt)
|
165 | 169 |
|
166 | 170 |
pSections :: Seq BlankLine -> Seq (NormalizedText, IniSection) -> Parser RawIni
|
|
171 | 175 |
pSection leading prevs = do
|
172 | 176 |
start <- getCurrentLine
|
173 | 177 |
void (char '[')
|
174 | |
name <- T.pack `fmap` some (noneOf "[]")
|
| 178 |
name <- T.pack `fmap` some (noneOf ("[]" :: String))
|
175 | 179 |
void (char ']')
|
176 | 180 |
void eol
|
177 | 181 |
comments <- sBlanks
|
|
204 | 208 |
pPair :: Seq BlankLine -> Parser (NormalizedText, IniValue)
|
205 | 209 |
pPair leading = do
|
206 | 210 |
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
|
210 | 214 |
return ( normalize key
|
211 | 215 |
, IniValue
|
212 | 216 |
{ vLineNo = pos
|
|
218 | 222 |
} )
|
219 | 223 |
|
220 | 224 |
getCurrentLine :: Parser Int
|
221 | |
getCurrentLine = (fromIntegral . unPos . sourceLine) `fmap` getPosition
|
| 225 |
getCurrentLine = (fromIntegral . unPos . sourceLine) `fmap` getSourcePos
|
222 | 226 |
|
223 | 227 |
|
224 | 228 |
-- | Serialize an INI file to text, complete with any comments which
|