module Configuration.Dotenv.Parse (configParser) where
import Configuration.Dotenv.ParsedVariable
import Control.Applicative
import Control.Monad
import Data.Void (Void)
import Text.Megaparsec
import Text.Megaparsec.Char
import qualified Text.Megaparsec.Char.Lexer as L
type Parser = Parsec Void String
data QuoteType = SingleQuote | DoubleQuote
configParser :: Parser [ParsedVariable]
configParser = between scn eof (sepEndBy1 envLine (eol <* scn))
envLine :: Parser ParsedVariable
envLine = ParsedVariable <$> (lexeme variableName <* lexeme (char '=')) <*> lexeme value
variableName :: Parser VarName
variableName = ((:) <$> firstChar <*> many otherChar) <?> "variable name"
where
firstChar = char '_' <|> letterChar
otherChar = firstChar <|> digitChar
value :: Parser VarValue
value = (quotedValue <|> unquotedValue) <?> "variable value"
where
quotedValue = quotedWith SingleQuote <|> quotedWith DoubleQuote
unquotedValue = Unquoted <$> (many $ fragment "\'\" \t\n\r")
quotedWith :: QuoteType -> Parser VarValue
quotedWith SingleQuote = SingleQuoted <$> (between (char '\'') (char '\'') $ many (literalValueFragment "\'\\"))
quotedWith DoubleQuote = DoubleQuoted <$> (between (char '\"') (char '\"') $ many (fragment "\""))
fragment :: [Char] -> Parser VarFragment
fragment charsToEscape = interpolatedValueFragment <|> literalValueFragment ('$' : '\\' : charsToEscape)
interpolatedValueFragment :: Parser VarFragment
interpolatedValueFragment = VarInterpolation <$>
((between (symbol "${") (symbol "}") variableName) <|>
(char '$' >> variableName))
where
symbol = L.symbol sc
literalValueFragment :: [Char] -> Parser VarFragment
literalValueFragment charsToEscape = VarLiteral <$> (some $ escapedChar <|> normalChar)
where
escapedChar = (char '\\' *> anyChar) <?> "escaped character"
normalChar = noneOf charsToEscape <?> "unescaped character"
lexeme :: Parser a -> Parser a
lexeme = L.lexeme sc
sc :: Parser ()
sc = L.space (void spaceChar') skipLineComment empty
scn :: Parser ()
scn = L.space (void spaceChar) skipLineComment empty
spaceChar' :: Parser Char
spaceChar' = oneOf (" \t" :: String)
skipLineComment :: Parser ()
#if MIN_VERSION_megaparsec(5,1,0)
skipLineComment = L.skipLineComment "#"
#else
skipLineComment = p >> void (manyTill anyChar n)
where p = string "#"
n = lookAhead (void newline) <|> eof
#endif