module Language.Docker.Parser.From
  ( parseFrom,
  )
where

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

parseRegistry :: (?esc :: Char) => Parser Registry
parseRegistry :: (?esc::Char) => Parser Registry
parseRegistry = do
  Text
domain <- (?esc::Char) => String -> (Char -> Bool) -> Parser Text
String -> (Char -> Bool) -> Parser Text
someUnless String
"a domain name" (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.')
  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
'.'
  Text
tld <- (?esc::Char) => String -> (Char -> Bool) -> Parser Text
String -> (Char -> Bool) -> Parser Text
someUnless String
"a TLD" (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/')
  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
'/'
  Registry -> Parser Registry
forall a. a -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (Registry -> Parser Registry) -> Registry -> Parser Registry
forall a b. (a -> b) -> a -> b
$ Text -> Registry
Registry (Text
domain Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
tld)

parsePlatform :: (?esc :: Char) => Parser Platform
parsePlatform :: (?esc::Char) => Parser Text
parsePlatform = do
  ParsecT DockerfileError Text Identity (Tokens Text)
-> ParsecT DockerfileError Text Identity ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT DockerfileError Text Identity (Tokens Text)
 -> ParsecT DockerfileError Text Identity ())
-> ParsecT DockerfileError Text Identity (Tokens Text)
-> ParsecT DockerfileError Text Identity ()
forall a b. (a -> b) -> a -> b
$ Tokens Text -> ParsecT DockerfileError Text Identity (Tokens Text)
forall e s (m :: * -> *).
MonadParsec e s m =>
Tokens s -> m (Tokens s)
string Tokens Text
"--platform="
  Text
p <- (?esc::Char) => String -> (Char -> Bool) -> Parser Text
String -> (Char -> Bool) -> Parser Text
someUnless String
"the platform for the FROM image" (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ')
  ParsecT DockerfileError Text Identity ()
(?esc::Char) => ParsecT DockerfileError Text Identity ()
requiredWhitespace
  Text -> Parser Text
forall a. a -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return Text
p

parseBaseImage :: (?esc :: Char) => (Text -> Parser (Maybe Tag)) -> Parser BaseImage
parseBaseImage :: (?esc::Char) => (Text -> Parser (Maybe Tag)) -> Parser BaseImage
parseBaseImage Text -> Parser (Maybe Tag)
tagParser = do
  Maybe Text
maybePlatform <- (Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text)
-> Parser Text
-> ParsecT DockerfileError Text Identity (Maybe Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text -> Parser Text
forall a.
ParsecT DockerfileError Text Identity a
-> ParsecT DockerfileError Text Identity a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try Parser Text
(?esc::Char) => Parser Text
parsePlatform) ParsecT DockerfileError Text Identity (Maybe Text)
-> ParsecT DockerfileError Text Identity (Maybe Text)
-> ParsecT DockerfileError Text Identity (Maybe 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
<|> Maybe Text -> ParsecT DockerfileError Text Identity (Maybe Text)
forall a. a -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Text
forall a. Maybe a
Nothing
  ParsecT DockerfileError Text Identity (Tokens Text)
-> ParsecT DockerfileError Text Identity ()
forall a.
ParsecT DockerfileError Text Identity a
-> ParsecT DockerfileError Text Identity ()
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m ()
notFollowedBy (Tokens Text -> ParsecT DockerfileError Text Identity (Tokens Text)
forall e s (m :: * -> *).
MonadParsec e s m =>
Tokens s -> m (Tokens s)
string Tokens Text
"--")
  Maybe Registry
regName <- (Registry -> Maybe Registry
forall a. a -> Maybe a
Just (Registry -> Maybe Registry)
-> Parser Registry
-> ParsecT DockerfileError Text Identity (Maybe Registry)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Registry -> Parser Registry
forall a.
ParsecT DockerfileError Text Identity a
-> ParsecT DockerfileError Text Identity a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try Parser Registry
(?esc::Char) => Parser Registry
parseRegistry) ParsecT DockerfileError Text Identity (Maybe Registry)
-> ParsecT DockerfileError Text Identity (Maybe Registry)
-> ParsecT DockerfileError Text Identity (Maybe Registry)
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
<|> Maybe Registry
-> ParsecT DockerfileError Text Identity (Maybe Registry)
forall a. a -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Registry
forall a. Maybe a
Nothing
  Text
name <- (?esc::Char) => String -> (Char -> Bool) -> Parser Text
String -> (Char -> Bool) -> Parser Text
someUnless String
"the image name with a tag" (\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
':')
  Maybe Tag
maybeTag <- Text -> Parser (Maybe Tag)
tagParser Text
name Parser (Maybe Tag) -> Parser (Maybe Tag) -> Parser (Maybe Tag)
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
<|> Maybe Tag -> Parser (Maybe Tag)
forall a. a -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Tag
forall a. Maybe a
Nothing
  Maybe Digest
maybeDigest <- (Digest -> Maybe Digest
forall a. a -> Maybe a
Just (Digest -> Maybe Digest)
-> ParsecT DockerfileError Text Identity Digest
-> ParsecT DockerfileError Text Identity (Maybe Digest)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT DockerfileError Text Identity Digest
-> ParsecT DockerfileError Text Identity Digest
forall a.
ParsecT DockerfileError Text Identity a
-> ParsecT DockerfileError Text Identity a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ParsecT DockerfileError Text Identity Digest
(?esc::Char) => ParsecT DockerfileError Text Identity Digest
parseDigest) ParsecT DockerfileError Text Identity (Maybe Digest)
-> ParsecT DockerfileError Text Identity (Maybe Digest)
-> ParsecT DockerfileError Text Identity (Maybe Digest)
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
<|> Maybe Digest
-> ParsecT DockerfileError Text Identity (Maybe Digest)
forall a. a -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Digest
forall a. Maybe a
Nothing
  Maybe ImageAlias
maybeAlias <- (ImageAlias -> Maybe ImageAlias
forall a. a -> Maybe a
Just (ImageAlias -> Maybe ImageAlias)
-> ParsecT DockerfileError Text Identity ImageAlias
-> ParsecT DockerfileError Text Identity (Maybe ImageAlias)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT DockerfileError Text Identity ImageAlias
-> ParsecT DockerfileError Text Identity ImageAlias
forall a.
ParsecT DockerfileError Text Identity a
-> ParsecT DockerfileError Text Identity a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try (ParsecT DockerfileError Text Identity ()
(?esc::Char) => ParsecT DockerfileError Text Identity ()
requiredWhitespace ParsecT DockerfileError Text Identity ()
-> ParsecT DockerfileError Text Identity ImageAlias
-> ParsecT DockerfileError Text Identity ImageAlias
forall a b.
ParsecT DockerfileError Text Identity a
-> ParsecT DockerfileError Text Identity b
-> ParsecT DockerfileError Text Identity b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT DockerfileError Text Identity ImageAlias
(?esc::Char) => ParsecT DockerfileError Text Identity ImageAlias
imageAlias)) ParsecT DockerfileError Text Identity (Maybe ImageAlias)
-> ParsecT DockerfileError Text Identity (Maybe ImageAlias)
-> ParsecT DockerfileError Text Identity (Maybe ImageAlias)
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
<|> Maybe ImageAlias
-> ParsecT DockerfileError Text Identity (Maybe ImageAlias)
forall a. a -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ImageAlias
forall a. Maybe a
Nothing
  BaseImage -> Parser BaseImage
forall a. a -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (BaseImage -> Parser BaseImage) -> BaseImage -> Parser BaseImage
forall a b. (a -> b) -> a -> b
$ Image
-> Maybe Tag
-> Maybe Digest
-> Maybe ImageAlias
-> Maybe Text
-> BaseImage
BaseImage (Maybe Registry -> Text -> Image
Image Maybe Registry
regName Text
name) Maybe Tag
maybeTag Maybe Digest
maybeDigest Maybe ImageAlias
maybeAlias Maybe Text
maybePlatform

taggedImage :: (?esc :: Char) => Parser BaseImage
taggedImage :: (?esc::Char) => Parser BaseImage
taggedImage = (?esc::Char) => (Text -> Parser (Maybe Tag)) -> Parser BaseImage
(Text -> Parser (Maybe Tag)) -> Parser BaseImage
parseBaseImage Text -> Parser (Maybe Tag)
forall {p}. (?esc::Char) => p -> Parser (Maybe Tag)
tagParser
  where
    tagParser :: p -> Parser (Maybe Tag)
tagParser p
_ = 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
':'
      Text
t <- (?esc::Char) => String -> (Char -> Bool) -> Parser Text
String -> (Char -> Bool) -> Parser Text
someUnless String
"the image tag" (\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
':')
      Maybe Tag -> Parser (Maybe Tag)
forall a. a -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (Tag -> Maybe Tag
forall a. a -> Maybe a
Just (Tag -> Maybe Tag) -> (Text -> Tag) -> Text -> Maybe Tag
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Tag
Tag (Text -> Maybe Tag) -> Text -> Maybe Tag
forall a b. (a -> b) -> a -> b
$ Text
t)

parseDigest :: (?esc :: Char) => Parser Digest
parseDigest :: (?esc::Char) => ParsecT DockerfileError Text Identity Digest
parseDigest = 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
'@'
  Text
d <- (?esc::Char) => String -> (Char -> Bool) -> Parser Text
String -> (Char -> Bool) -> Parser Text
someUnless String
"the image digest" (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'@')
  Digest -> ParsecT DockerfileError Text Identity Digest
forall a. a -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (Digest -> ParsecT DockerfileError Text Identity Digest)
-> Digest -> ParsecT DockerfileError Text Identity Digest
forall a b. (a -> b) -> a -> b
$ Text -> Digest
Digest Text
d

untaggedImage :: (?esc :: Char) => Parser BaseImage
untaggedImage :: (?esc::Char) => Parser BaseImage
untaggedImage = (?esc::Char) => (Text -> Parser (Maybe Tag)) -> Parser BaseImage
(Text -> Parser (Maybe Tag)) -> Parser BaseImage
parseBaseImage Text -> Parser (Maybe Tag)
notInvalidTag
  where
    notInvalidTag :: Text -> Parser (Maybe Tag)
    notInvalidTag :: Text -> Parser (Maybe Tag)
notInvalidTag Text
name = do
      ParsecT DockerfileError Text Identity ()
-> ParsecT DockerfileError Text Identity ()
forall a.
ParsecT DockerfileError Text Identity a
-> ParsecT DockerfileError Text Identity a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try (ParsecT DockerfileError Text Identity (Tokens Text)
-> ParsecT DockerfileError Text Identity ()
forall a.
ParsecT DockerfileError Text Identity a
-> ParsecT DockerfileError Text Identity ()
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m ()
notFollowedBy (ParsecT DockerfileError Text Identity (Tokens Text)
 -> ParsecT DockerfileError Text Identity ())
-> ParsecT DockerfileError Text Identity (Tokens Text)
-> ParsecT DockerfileError Text Identity ()
forall a b. (a -> b) -> a -> b
$ Tokens Text -> ParsecT DockerfileError Text Identity (Tokens Text)
forall e s (m :: * -> *).
MonadParsec e s m =>
Tokens s -> m (Tokens s)
string Tokens Text
":") ParsecT DockerfileError Text Identity ()
-> String -> ParsecT DockerfileError Text Identity ()
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"no ':' or a valid image tag string (example: "
        String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
T.unpack Text
name
        String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
":valid-tag)"
      Maybe Tag -> Parser (Maybe Tag)
forall a. a -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Tag
forall a. Maybe a
Nothing

imageAlias :: (?esc :: Char) => Parser ImageAlias
imageAlias :: (?esc::Char) => ParsecT DockerfileError Text Identity ImageAlias
imageAlias = do
  ParsecT DockerfileError Text Identity ()
-> ParsecT DockerfileError Text Identity ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT DockerfileError Text Identity ()
-> ParsecT DockerfileError Text Identity ()
forall a.
ParsecT DockerfileError Text Identity a
-> ParsecT DockerfileError Text Identity a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try ((?esc::Char) => Text -> ParsecT DockerfileError Text Identity ()
Text -> ParsecT DockerfileError Text Identity ()
reserved Text
"AS") ParsecT DockerfileError Text Identity ()
-> String -> ParsecT DockerfileError Text Identity ()
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"'AS' followed by the image alias")
  Text
aka <- (?esc::Char) => String -> (Char -> Bool) -> Parser Text
String -> (Char -> Bool) -> Parser Text
someUnless String
"the image alias" (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\n')
  ImageAlias -> ParsecT DockerfileError Text Identity ImageAlias
forall a. a -> ParsecT DockerfileError Text Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (ImageAlias -> ParsecT DockerfileError Text Identity ImageAlias)
-> ImageAlias -> ParsecT DockerfileError Text Identity ImageAlias
forall a b. (a -> b) -> a -> b
$ Text -> ImageAlias
ImageAlias Text
aka

baseImage :: (?esc :: Char) => Parser BaseImage
baseImage :: (?esc::Char) => Parser BaseImage
baseImage = Parser BaseImage -> Parser BaseImage
forall a.
ParsecT DockerfileError Text Identity a
-> ParsecT DockerfileError Text Identity a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try Parser BaseImage
(?esc::Char) => Parser BaseImage
taggedImage Parser BaseImage -> Parser BaseImage -> Parser BaseImage
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 BaseImage
(?esc::Char) => Parser BaseImage
untaggedImage

parseFrom :: (?esc :: Char) => Parser (Instruction Text)
parseFrom :: (?esc::Char) => Parser (Instruction Text)
parseFrom = do
  (?esc::Char) => Text -> ParsecT DockerfileError Text Identity ()
Text -> ParsecT DockerfileError Text Identity ()
reserved Text
"FROM"
  BaseImage -> Instruction Text
forall args. BaseImage -> Instruction args
From (BaseImage -> Instruction Text)
-> Parser BaseImage -> Parser (Instruction Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser BaseImage
(?esc::Char) => Parser BaseImage
baseImage