{-# LANGUAGE Safe #-}

{- arch-tag: Parser for Debian control file
Copyright (c) 2004-2011 John Goerzen <jgoerzen@complete.org>

All rights reserved.

For license and copyright information, see the file LICENSE
-}

{- |
   Module     : System.Debian.ControlParser
   Copyright  : Copyright (C) 2004-2011 John Goerzen
   SPDX-License-Identifier: BSD-3-Clause

   Stability  : stable
   Portability: portable

This module provides various helpful utilities for dealing with Debian
files and programs.

Written by John Goerzen, jgoerzen\@complete.org
-}

module System.Debian.ControlParser(control, depPart)
    where

import safe Data.List.Utils ( split )
import safe Text.ParserCombinators.Parsec
    ( char,
      noneOf,
      oneOf,
      string,
      many1,
      manyTill,
      (<?>),
      (<|>),
      many,
      try,
      GenParser,
      CharParser )

eol, extline :: GenParser Char st String
eol :: forall st. GenParser Char st String
eol = (forall tok st a. GenParser tok st a -> GenParser tok st a
try (forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"\r\n"))
      forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"\n" forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
<?> String
"EOL"

extline :: forall st. GenParser Char st String
extline = forall tok st a. GenParser tok st a -> GenParser tok st a
try (do Char
_ <- forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
' '
                  String
content <- forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
"\r\n")
                  String
_ <- forall st. GenParser Char st String
eol
                  forall (m :: * -> *) a. Monad m => a -> m a
return String
content )

entry :: GenParser Char st (String, String)
entry :: forall st. GenParser Char st (String, String)
entry = do String
key <- forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
":\r\n")
           Char
_ <- forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
':'
           String
val <- forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
"\r\n")
           String
_ <- forall st. GenParser Char st String
eol
           [String]
exts <- forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many forall st. GenParser Char st String
extline
           forall (m :: * -> *) a. Monad m => a -> m a
return (String
key, [String] -> String
unlines ([String
val] forall a. [a] -> [a] -> [a]
++ [String]
exts))

{- | Main parser for the control file -}
control :: CharParser a [(String, String)]
control :: forall a. CharParser a [(String, String)]
control = do [()]
_ <- forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many forall st. GenParser Char st ()
header
             [(String, String)]
retval <- forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many forall st. GenParser Char st (String, String)
entry
             forall (m :: * -> *) a. Monad m => a -> m a
return [(String, String)]
retval

headerPGP, blankLine, header, headerHash :: GenParser Char st ()
headerPGP :: forall st. GenParser Char st ()
headerPGP = do String
_ <- forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"-----BEGIN PGP"
               String
_ <- forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill (forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
"\r\n") forall st. GenParser Char st String
eol
               forall (m :: * -> *) a. Monad m => a -> m a
return ()
blankLine :: forall st. GenParser Char st ()
blankLine = do String
_ <- forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
oneOf String
" \t")
               String
_ <- forall st. GenParser Char st String
eol
               forall (m :: * -> *) a. Monad m => a -> m a
return ()
headerHash :: forall st. GenParser Char st ()
headerHash = do String
_ <- forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
string String
"Hash: "
                String
_ <- forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill (forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
"\r\n") forall st. GenParser Char st String
eol
                forall (m :: * -> *) a. Monad m => a -> m a
return ()
header :: forall st. GenParser Char st ()
header = (forall tok st a. GenParser tok st a -> GenParser tok st a
try forall st. GenParser Char st ()
headerPGP) forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> (forall tok st a. GenParser tok st a -> GenParser tok st a
try forall st. GenParser Char st ()
blankLine) forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> (forall tok st a. GenParser tok st a -> GenParser tok st a
try forall st. GenParser Char st ()
headerHash)

{- | Dependency parser.

Returns (package name, Maybe version, arch list)

version is (operator, operand) -}
depPart :: CharParser a (String, (Maybe (String, String)), [String])
depPart :: forall a. CharParser a (String, Maybe (String, String), [String])
depPart = do String
packagename <- forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
" (")
             String
_ <- forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
' ')
             Maybe (String, String)
version <- (do Char
_ <- forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'('
                            String
op <- forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
oneOf String
"<>=")
                            String
_ <- forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
' ')
                            String
vers <- forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
") ")
                            String
_ <- forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
' ')
                            Char
_ <- forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
')'
                            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (String
op, String
vers)
                        ) forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
             String
_ <- forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
' ')
             [String]
archs <- (do Char
_ <- forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'['
                          String
t <- forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
"]")
                          String
_ <- forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
' ')
                          Char
_ <- forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
']'
                          forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Eq a => [a] -> [a] -> [[a]]
split String
" " String
t)
                      ) forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> forall (m :: * -> *) a. Monad m => a -> m a
return []
             forall (m :: * -> *) a. Monad m => a -> m a
return (String
packagename, Maybe (String, String)
version, [String]
archs)