module Language.Haskell.Ghcid.Parser(
parseShowModules, parseLoad
) where
import System.FilePath
import Data.Char
import Data.List.Extra
import Data.Tuple.Extra
import Control.Applicative
import Prelude
import Language.Haskell.Ghcid.Types
parseShowModules :: [String] -> [(String, FilePath)]
parseShowModules xs =
[ (takeWhile (not . isSpace) $ trimStart a, takeWhile (/= ',') b)
| x <- xs, (a,'(':' ':b) <- [break (== '(') x]]
parseLoad :: [String] -> [Load]
parseLoad = nubOrd . f
where
f :: [String] -> [Load]
f (('[':xs):rest) =
map (uncurry Loading) (parseShowModules [drop 11 $ dropWhile (/= ']') xs]) ++
f rest
f (x:xs)
| not $ " " `isPrefixOf` x
, Just (file,rest) <- breakFileColon x
, takeExtension file `elem` [".hs",".lhs",".hs-boot",".lhs-boot"]
, (pos,rest) <- span (\c -> c == ':' || c == '-' || isSpan c || isDigit c) rest
, [p1,p2] <- map read $ wordsBy (\c -> c == ':' || isSpan c) $ takeWhile (\c->c /= '-') pos
, (msg,las) <- span (isPrefixOf " ") xs
, rest <- trimStart $ unwords $ rest : xs
, sev <- if "warning:" `isPrefixOf` lower rest then Warning else Error
= Message sev file (p1,p2) (x:msg) : f las
f (x:xs)
| Just file <- stripPrefix "<no location info>: can't find file: " x
= Message Error file (0,0) [file ++ ": Can't find file"] : f xs
f (x:xs)
| x == "Module imports form a cycle:"
, (xs,rest) <- span (isPrefixOf " ") xs
, let ms = [takeWhile (/= ')') x | x <- xs, '(':x <- [dropWhile (/= '(') x]]
= Message Error "" (0,0) (x:xs) :
[Message Error m (0,0) [] | m <- nubOrd ms] ++ f rest
f (_:xs) = f xs
f [] = []
isSpan c = c== ',' || c == '(' || c == ')'
breakFileColon :: String -> Maybe (FilePath, String)
breakFileColon (x:':':xs) | isLetter x = first ([x,':']++) <$> stripInfix ":" xs
breakFileColon xs = stripInfix ":" xs