module ConfigParser
( OptParser
, parseConfig
, parseConfigFile
, option
, customOption
, parserDefault
, parserExample
, ConfParseError
, OParser
, Option
, OptionArgument()
) where
import Control.Applicative
import Control.Applicative.Free
import Control.Monad
import Data.Functor.Identity
import Data.Monoid
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Text.Megaparsec as P
import Text.Megaparsec.Compat hiding (option)
import Data.Maybe
data ConfParseError = UnknownOption Text
| TypeError Text Text
deriving (Eq, Ord, Show)
instance ShowErrorComponent ConfParseError where
showErrorComponent (UnknownOption name) = "Unknown option " ++ T.unpack name
showErrorComponent (TypeError typ name) =
"in " ++ T.unpack typ ++ " argument for option " ++ T.unpack name
type OParser = Parsec (CustomError ConfParseError) Text
type CustomParseError = ParseError Char (CustomError ConfParseError)
parseConfig :: FilePath
-> Text
-> OptParser a
-> Either CustomParseError a
parseConfig path input parser = case parse (assignmentList <* eof) path input of
Left err -> Left err
Right res -> runOptionParser res parser
parseConfigFile :: FilePath
-> OptParser a
-> IO (Either CustomParseError a)
parseConfigFile path parser = do
input <- T.readFile path
return $ parseConfig path input parser
data Option a = Option
{ optParser :: OParser a
, optType :: Text
, optName :: Text
, optHelp :: Text
, optDefault :: a
, optDefaultTxt :: Text
} deriving (Functor)
type OptParser a = Ap Option a
class OptionArgument a where
mkParser :: (Text, OParser a)
printArgument :: a -> Text
option :: OptionArgument a
=> Text
-> a
-> Text
-> OptParser a
option name def help = liftAp $ Option parser typename name help def (printArgument def)
where (typename, parser) = mkParser
customOption :: Text
-> a
-> Text
-> Text
-> Text
-> OParser a
-> OptParser a
customOption optName optDefault optDefaultTxt optHelp optType optParser = liftAp $ Option {..}
instance OptionArgument Int where
mkParser = ("integer", parseNumber)
printArgument = T.pack . show
instance OptionArgument Integer where
mkParser = ("integer", parseNumber)
printArgument = T.pack . show
instance OptionArgument String where
mkParser = ("string", many anyChar)
printArgument = quote . T.pack
instance OptionArgument Text where
mkParser = ("string", T.pack <$> many anyChar)
printArgument = quote
quote :: Text -> Text
quote x = "\"" <> escape x <> "\""
where
escape = T.replace "\"" "\\\"" . T.replace "\\" "\\\\"
runOptionParser :: [Assignment] -> OptParser a -> Either CustomParseError a
runOptionParser (a:as) parser = parseOption parser a >>= runOptionParser as
runOptionParser [] parser = Right $ parserDefault parser
parserDefault :: OptParser a -> a
parserDefault = runIdentity . runAp (Identity . optDefault)
parserExample :: OptParser a -> Text
parserExample = T.strip . runAp_ example1
where example1 a = commentify (optHelp a) <> optName a <> " = " <> optDefaultTxt a <> "\n\n"
commentify = T.unlines . map ("# " <>) . T.lines
parseOption :: OptParser a -> Assignment -> Either CustomParseError (OptParser a)
parseOption (Pure _) ass =
Left $ mkCustomError (assignmentPosition ass) (UnknownOption (assignmentKey ass))
parseOption (Ap opt rest) ass
| optName opt == assignmentKey ass =
let content = (valueContent $ assignmentValue ass)
pos = (valuePosition $ assignmentValue ass)
in case parseWithStart (optParser opt <* eof) pos content of
Left e -> Left $ addCustomError e $ TypeError (optType opt) (assignmentKey ass)
Right res -> Right $ fmap ($ res) rest
| otherwise = fmap (Ap opt) $ parseOption rest ass
data Assignment = Assignment
{ assignmentPosition :: SourcePos
, assignmentKey :: Text
, assignmentValue :: AssignmentValue
} deriving (Show)
data AssignmentValue = AssignmentValue
{ valuePosition :: SourcePos
, valueContent :: Text
} deriving (Show)
assignmentList :: OParser [Assignment]
assignmentList = whitespace *> many (assignment <* whitespace)
assignment :: OParser Assignment
assignment = do
Assignment
<$> getPosition <*> key <* whitespaceNoComment
<* char '=' <* whitespaceNoComment
<*> value
key :: OParser Text
key = T.pack <$> some (alphaNumChar <|> char '_' <|> char '-')
value :: OParser AssignmentValue
value = AssignmentValue <$> getPosition <*> content <* whitespaceNoEOL <* (void eol <|> eof)
content :: OParser Text
content = escapedString
<|> bareString
bareString :: OParser Text
bareString = (T.strip . T.pack <$> some (noneOf ("#\n" :: String)))
<?> "bare string"
escapedString :: OParser Text
escapedString = (T.pack <$> (char '"' *> many escapedChar <* char '"'))
<?> "quoted string"
where escapedChar = char '\\' *> anyChar
<|> noneOf ("\"" :: String)
whitespace :: OParser ()
whitespace = skipMany $ (void $ oneOf (" \t\n" :: String)) <|> comment
whitespaceNoEOL :: OParser ()
whitespaceNoEOL = skipMany $ (void $ oneOf (" \t" :: String)) <|> comment
whitespaceNoComment :: OParser ()
whitespaceNoComment = skipMany $ oneOf (" \t" :: String)
comment :: OParser ()
comment = char '#' >> skipMany (noneOf ("\n" :: String))
parseNumber :: Read a => OParser a
parseNumber = read <$> ((<>) <$> (maybeToList <$> optional (char '-')) <*> some digitChar)