module Language.Docker.Parser.Pairs
  ( parseEnv,
    parseLabel,
  )
where

import qualified Data.Text as T
import Language.Docker.Parser.Prelude
import Language.Docker.Syntax


unquotedString :: (?esc :: Char) => (Char -> Bool) -> Parser Text
unquotedString :: (?esc::Char) => (Char -> Bool) -> Parser Text
unquotedString Char -> Bool
acceptCondition = do
  Text
str <- (?esc::Char) => [Char] -> Maybe (Char -> Bool) -> Parser Text
[Char] -> Maybe (Char -> Bool) -> Parser Text
stringWithEscaped [Char
' ', Char
'\t'] ((Char -> Bool) -> Maybe (Char -> Bool)
forall a. a -> Maybe a
Just (\Char
c -> Char -> Bool
acceptCondition Char
c Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'"' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\''))
  Text -> Parser Text
checkFaults Text
str
  where
    checkFaults :: Text -> Parser Text
checkFaults Text
str
      | Text -> Bool
T.null Text
str = [Char] -> Parser Text
forall a. [Char] -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"a non empty string"
      | HasCallStack => Text -> Char
Text -> Char
T.head Text
str Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\'' = DockerfileError -> Parser Text
forall a. DockerfileError -> Parser a
customError (DockerfileError -> Parser Text) -> DockerfileError -> Parser Text
forall a b. (a -> b) -> a -> b
$ [Char] -> [Char] -> DockerfileError
QuoteError [Char]
"single" (Text -> [Char]
T.unpack Text
str)
      | HasCallStack => Text -> Char
Text -> Char
T.head Text
str Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\"' = DockerfileError -> Parser Text
forall a. DockerfileError -> Parser a
customError (DockerfileError -> Parser Text) -> DockerfileError -> Parser Text
forall a b. (a -> b) -> a -> b
$ [Char] -> [Char] -> DockerfileError
QuoteError [Char]
"double" (Text -> [Char]
T.unpack Text
str)
      | Bool
otherwise = Text -> Parser Text
forall a. a -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return Text
str

singleValue :: (?esc :: Char) => (Char -> Bool) -> Parser Text
singleValue :: (?esc::Char) => (Char -> Bool) -> Parser Text
singleValue Char -> Bool
acceptCondition = [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat ([Text] -> Text)
-> ParsecT DockerfileError Text Identity [Text] -> Parser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT DockerfileError Text Identity [Text]
variants
  where
    variants :: ParsecT DockerfileError Text Identity [Text]
variants =
      Parser Text -> ParsecT DockerfileError Text Identity [Text]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many (Parser Text -> ParsecT DockerfileError Text Identity [Text])
-> Parser Text -> ParsecT DockerfileError Text Identity [Text]
forall a b. (a -> b) -> a -> b
$
        [Parser Text] -> Parser Text
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
          [ Parser Text
(?esc::Char) => Parser Text
doubleQuotedStringEscaped Parser Text -> [Char] -> Parser Text
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"a string inside double quotes",
            Parser Text
(?esc::Char) => Parser Text
singleQuotedStringEscaped Parser Text -> [Char] -> Parser Text
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"a string inside single quotes",
            (?esc::Char) => (Char -> Bool) -> Parser Text
(Char -> Bool) -> Parser Text
unquotedString Char -> Bool
acceptCondition Parser Text -> [Char] -> Parser Text
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"a string with no quotes"
          ]

pair :: (?esc :: Char) => Parser (Text, Text)
pair :: (?esc::Char) => Parser (Text, Text)
pair = do
  Text
key <- (?esc::Char) => (Char -> Bool) -> Parser Text
(Char -> Bool) -> Parser Text
singleValue (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'=')
  Text
value <- Parser Text
withEqualSign Parser Text -> Parser Text -> Parser Text
forall a.
ParsecT DockerfileError Text Identity a
-> ParsecT DockerfileError Text Identity a
-> ParsecT DockerfileError Text Identity a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Text
withoutEqualSign
  (Text, Text) -> Parser (Text, Text)
forall a. a -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
key, Text
value)
  where
    withEqualSign :: Parser Text
withEqualSign = do
      ParsecT DockerfileError Text Identity Char
-> ParsecT DockerfileError Text Identity ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT DockerfileError Text Identity Char
 -> ParsecT DockerfileError Text Identity ())
-> ParsecT DockerfileError Text Identity Char
-> ParsecT DockerfileError Text Identity ()
forall a b. (a -> b) -> a -> b
$ Token Text -> ParsecT DockerfileError Text Identity (Token Text)
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
Token s -> m (Token s)
char Char
Token Text
'='
      (?esc::Char) => (Char -> Bool) -> Parser Text
(Char -> Bool) -> Parser Text
singleValue (\Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
' ' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\t')
    withoutEqualSign :: Parser Text
withoutEqualSign = do
      ParsecT DockerfileError Text Identity ()
(?esc::Char) => ParsecT DockerfileError Text Identity ()
requiredWhitespace
      (?esc::Char) => [Char] -> Parser Text
[Char] -> Parser Text
untilEol [Char]
"value"

pairs :: (?esc :: Char) => Parser Pairs
pairs :: (?esc::Char) => Parser Pairs
pairs = (Parser (Text, Text)
(?esc::Char) => Parser (Text, Text)
pair Parser (Text, Text) -> [Char] -> Parser (Text, Text)
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"a key value pair (key=value)") Parser (Text, Text)
-> ParsecT DockerfileError Text Identity () -> Parser Pairs
forall (m :: * -> *) a sep. MonadPlus m => m a -> m sep -> m [a]
`sepEndBy1` ParsecT DockerfileError Text Identity ()
(?esc::Char) => ParsecT DockerfileError Text Identity ()
requiredWhitespace

parseLabel :: (?esc :: Char) => Parser (Instruction Text)
parseLabel :: (?esc::Char) => Parser (Instruction Text)
parseLabel = do
  (?esc::Char) => Text -> ParsecT DockerfileError Text Identity ()
Text -> ParsecT DockerfileError Text Identity ()
reserved Text
"LABEL"
  Pairs -> Instruction Text
forall args. Pairs -> Instruction args
Label (Pairs -> Instruction Text)
-> Parser Pairs -> Parser (Instruction Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Pairs
(?esc::Char) => Parser Pairs
pairs

parseEnv :: (?esc :: Char) => Parser (Instruction Text)
parseEnv :: (?esc::Char) => Parser (Instruction Text)
parseEnv = do
  (?esc::Char) => Text -> ParsecT DockerfileError Text Identity ()
Text -> ParsecT DockerfileError Text Identity ()
reserved Text
"ENV"
  Pairs -> Instruction Text
forall args. Pairs -> Instruction args
Env (Pairs -> Instruction Text)
-> Parser Pairs -> Parser (Instruction Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Pairs
(?esc::Char) => Parser Pairs
pairs