From c6c01d7c248bca1078285c4afeaa25493e9974b3 Mon Sep 17 00:00:00 2001 From: Joris Date: Wed, 16 Mar 2016 21:09:44 +0100 Subject: Add import expression --- Data/ConfigManager/Parser.hs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'Data/ConfigManager/Parser.hs') diff --git a/Data/ConfigManager/Parser.hs b/Data/ConfigManager/Parser.hs index e2329b6..856ccfd 100644 --- a/Data/ConfigManager/Parser.hs +++ b/Data/ConfigManager/Parser.hs @@ -6,31 +6,41 @@ module Data.ConfigManager.Parser import Data.Text (Text) import qualified Data.Text as T -import qualified Data.HashMap.Strict as M import Data.Char (isSpace, isAlphaNum) import Text.ParserCombinators.Parsec -import Data.ConfigManager.Config (Config(..)) -import Data.ConfigManager.Types +import Data.ConfigManager.Expr -parseConfig :: Text -> Either Text Config +parseConfig :: Text -> Either Text [Expr] parseConfig input = - case parse bindingsParser "" (T.unpack (T.concat [input, "\n"])) of - Right bindings -> Right . Config . M.fromList $ bindings + case parse exprsParser "" (T.unpack (T.concat [input, "\n"])) of + Right exprs -> Right exprs Left parserError -> Left . T.pack . show $ parserError -bindingsParser :: Parser [Binding] -bindingsParser = skip *> many (bindingParser <* restOfLine <* skip) <* eof +exprsParser :: Parser [Expr] +exprsParser = skip *> many (exprParser <* restOfLine <* skip) <* eof -bindingParser :: Parser Binding +exprParser :: Parser Expr +exprParser = (try bindingParser) <|> (try importParser) + +bindingParser :: Parser Expr bindingParser = do name <- nameParser _ <- spaces _ <- char '=' _ <- spaces value <- valueParser - return (name, value) + return $ Binding name value + +importParser :: Parser Expr +importParser = do + _ <- string "import" + _ <- spaces + _ <- char '"' + path <- many (noneOf "\"") + _ <- char '"' + return $ Import path nameParser :: Parser Name nameParser = do -- cgit v1.2.3