module Distribution.Client.Utils.Parsec
  ( renderParseError
  ) where

import Distribution.Client.Compat.Prelude
import System.FilePath (normalise)
import Prelude ()

import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8

import Distribution.Parsec (PError (..), PWarning (..), Position (..), showPos, zeroPos)
import Distribution.Simple.Utils (fromUTF8BS)

-- | Render parse error highlighting the part of the input file.
renderParseError
  :: FilePath
  -> BS.ByteString
  -> NonEmpty PError
  -> [PWarning]
  -> String
renderParseError :: [Char] -> ByteString -> NonEmpty PError -> [PWarning] -> [Char]
renderParseError [Char]
filepath ByteString
contents NonEmpty PError
errors [PWarning]
warnings =
  [[Char]] -> [Char]
unlines ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$
    [ [Char]
"Errors encountered when parsing cabal file " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
filepath [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
":"
    ]
      [[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [[Char]]
renderedErrors
      [[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [[Char]]
renderedWarnings
  where
    filepath' :: [Char]
filepath' = [Char] -> [Char]
normalise [Char]
filepath

    -- lines of the input file. 'lines' is taken, so they are called rows
    -- contents, line number, whether it's empty line
    rows :: [(String, Int, Bool)]
    rows :: [([Char], Int, Bool)]
rows = (ByteString -> Int -> ([Char], Int, Bool))
-> [ByteString] -> [Int] -> [([Char], Int, Bool)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith ByteString -> Int -> ([Char], Int, Bool)
forall {b}. ByteString -> b -> ([Char], b, Bool)
f (ByteString -> [ByteString]
BS8.lines ByteString
contents) [Int
1 ..]
      where
        f :: ByteString -> b -> ([Char], b, Bool)
f ByteString
bs b
i = let s :: [Char]
s = ByteString -> [Char]
fromUTF8BS ByteString
bs in ([Char]
s, b
i, [Char] -> Bool
isEmptyOrComment [Char]
s)

    rowsZipper :: Zipper ([Char], Int, Bool)
rowsZipper = [([Char], Int, Bool)] -> Zipper ([Char], Int, Bool)
forall a. [a] -> Zipper a
listToZipper [([Char], Int, Bool)]
rows

    isEmptyOrComment :: String -> Bool
    isEmptyOrComment :: [Char] -> Bool
isEmptyOrComment [Char]
s = case (Char -> Bool) -> [Char] -> [Char]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ') [Char]
s of
      [Char]
"" -> Bool
True -- empty
      (Char
'-' : Char
'-' : [Char]
_) -> Bool
True -- comment
      [Char]
_ -> Bool
False

    renderedErrors :: [[Char]]
renderedErrors = (PError -> [[Char]]) -> NonEmpty PError -> [[Char]]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap PError -> [[Char]]
renderError NonEmpty PError
errors
    renderedWarnings :: [[Char]]
renderedWarnings = (PWarning -> [[Char]]) -> [PWarning] -> [[Char]]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap PWarning -> [[Char]]
renderWarning [PWarning]
warnings

    renderError :: PError -> [String]
    renderError :: PError -> [[Char]]
renderError (PError pos :: Position
pos@(Position Int
row Int
col) [Char]
msg)
      -- if position is 0:0, then it doesn't make sense to show input
      -- looks like, Parsec errors have line-feed in them
      | Position
pos Position -> Position -> Bool
forall a. Eq a => a -> a -> Bool
== Position
zeroPos = [[Char]]
msgs
      | Bool
otherwise = [[Char]]
msgs [[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ Int -> Int -> [[Char]]
formatInput Int
row Int
col
      where
        msgs :: [[Char]]
msgs = [[Char]
"", [Char]
filepath' [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
":" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Position -> [Char]
showPos Position
pos [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": error:", [Char] -> [Char]
trimLF [Char]
msg, [Char]
""]

    renderWarning :: PWarning -> [String]
    renderWarning :: PWarning -> [[Char]]
renderWarning (PWarning PWarnType
_ pos :: Position
pos@(Position Int
row Int
col) [Char]
msg)
      | Position
pos Position -> Position -> Bool
forall a. Eq a => a -> a -> Bool
== Position
zeroPos = [[Char]]
msgs
      | Bool
otherwise = [[Char]]
msgs [[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ Int -> Int -> [[Char]]
formatInput Int
row Int
col
      where
        msgs :: [[Char]]
msgs = [[Char]
"", [Char]
filepath' [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
":" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Position -> [Char]
showPos Position
pos [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": warning:", [Char] -> [Char]
trimLF [Char]
msg, [Char]
""]

    -- sometimes there are (especially trailing) newlines.
    trimLF :: String -> String
    trimLF :: [Char] -> [Char]
trimLF = (Char -> Bool) -> [Char] -> [Char]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\n') ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char]
forall a. [a] -> [a]
reverse ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> [Char] -> [Char]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\n') ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char]
forall a. [a] -> [a]
reverse

    -- format line: prepend the given line number
    formatInput :: Int -> Int -> [String]
    formatInput :: Int -> Int -> [[Char]]
formatInput Int
row Int
col = case Int -> Zipper ([Char], Int, Bool) -> Zipper ([Char], Int, Bool)
forall a. Int -> Zipper a -> Zipper a
advance (Int
row Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Zipper ([Char], Int, Bool)
rowsZipper of
      Zipper [([Char], Int, Bool)]
xs [([Char], Int, Bool)]
ys -> [[Char]]
before [[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [[Char]]
after
        where
          before :: [[Char]]
before = case (([Char], Int, Bool) -> Bool)
-> [([Char], Int, Bool)]
-> ([([Char], Int, Bool)], [([Char], Int, Bool)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span (\([Char]
_, Int
_, Bool
b) -> Bool
b) [([Char], Int, Bool)]
xs of
            ([([Char], Int, Bool)]
_, []) -> []
            ([([Char], Int, Bool)]
zs, ([Char], Int, Bool)
z : [([Char], Int, Bool)]
_) -> (([Char], Int, Bool) -> [Char])
-> [([Char], Int, Bool)] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ([Char], Int, Bool) -> [Char]
formatInputLine ([([Char], Int, Bool)] -> [[Char]])
-> [([Char], Int, Bool)] -> [[Char]]
forall a b. (a -> b) -> a -> b
$ ([Char], Int, Bool)
z ([Char], Int, Bool)
-> [([Char], Int, Bool)] -> [([Char], Int, Bool)]
forall a. a -> [a] -> [a]
: [([Char], Int, Bool)] -> [([Char], Int, Bool)]
forall a. [a] -> [a]
reverse [([Char], Int, Bool)]
zs

          after :: [[Char]]
after = case [([Char], Int, Bool)]
ys of
            [] -> []
            (([Char], Int, Bool)
z : [([Char], Int, Bool)]
_zs) ->
              [ ([Char], Int, Bool) -> [Char]
formatInputLine ([Char], Int, Bool)
z -- error line
              , [Char]
"      | " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> Char -> [Char]
forall a. Int -> a -> [a]
replicate (Int
col Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Char
' ' [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"^" -- pointer: ^
              ]
    -- do we need rows after?
    -- ++ map formatInputLine (take 1 zs)           -- one row after

    formatInputLine :: (String, Int, Bool) -> String
    formatInputLine :: ([Char], Int, Bool) -> [Char]
formatInputLine ([Char]
str, Int
row, Bool
_) = Int -> [Char]
leftPadShow Int
row [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" | " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
str

    -- hopefully we don't need to work with over 99999 lines .cabal files
    -- at that point small glitches in error messages are hopefully fine.
    leftPadShow :: Int -> String
    leftPadShow :: Int -> [Char]
leftPadShow Int
n = let s :: [Char]
s = Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n in Int -> Char -> [Char]
forall a. Int -> a -> [a]
replicate (Int
5 Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Char] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Char]
s) Char
' ' [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
s

data Zipper a = Zipper [a] [a]

listToZipper :: [a] -> Zipper a
listToZipper :: forall a. [a] -> Zipper a
listToZipper = [a] -> [a] -> Zipper a
forall a. [a] -> [a] -> Zipper a
Zipper []

advance :: Int -> Zipper a -> Zipper a
advance :: forall a. Int -> Zipper a -> Zipper a
advance Int
n z :: Zipper a
z@(Zipper [a]
xs [a]
ys)
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Zipper a
z
  | Bool
otherwise = case [a]
ys of
      [] -> Zipper a
z
      (a
y : [a]
ys') -> Int -> Zipper a -> Zipper a
forall a. Int -> Zipper a -> Zipper a
advance (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (Zipper a -> Zipper a) -> Zipper a -> Zipper a
forall a b. (a -> b) -> a -> b
$ [a] -> [a] -> Zipper a
forall a. [a] -> [a] -> Zipper a
Zipper (a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs) [a]
ys'