2015-01-16 14:47:56 +00:00
|
|
|
{-# LANGUAGE CPP, FlexibleInstances, FlexibleContexts #-}
|
2014-05-11 22:40:00 +00:00
|
|
|
|
2014-06-27 17:06:20 +00:00
|
|
|
module Language.Haskell.GhcMod.Convert (convert, convert', emptyResult, whenFound, whenFound') where
|
2014-05-11 22:40:00 +00:00
|
|
|
|
2015-03-03 20:12:43 +00:00
|
|
|
import Language.Haskell.GhcMod.Monad.Types
|
2014-05-11 22:40:00 +00:00
|
|
|
import Language.Haskell.GhcMod.Types
|
|
|
|
|
2015-08-03 01:09:56 +00:00
|
|
|
import Control.Applicative
|
|
|
|
import Prelude
|
2014-05-11 22:40:00 +00:00
|
|
|
|
|
|
|
type Builder = String -> String
|
|
|
|
|
|
|
|
-- |
|
|
|
|
--
|
|
|
|
-- >>> replace '"' "\\\"" "foo\"bar" ""
|
|
|
|
-- "foo\\\"bar"
|
|
|
|
replace :: Char -> String -> String -> Builder
|
|
|
|
replace _ _ [] = id
|
|
|
|
replace c cs (x:xs)
|
|
|
|
| x == c = (cs ++) . replace c cs xs
|
|
|
|
| otherwise = (x :) . replace c cs xs
|
|
|
|
|
|
|
|
inter :: Char -> [Builder] -> Builder
|
|
|
|
inter _ [] = id
|
|
|
|
inter c bs = foldr1 (\x y -> x . (c:) . y) bs
|
|
|
|
|
2015-03-03 20:12:43 +00:00
|
|
|
convert' :: (ToString a, IOish m, GmEnv m) => a -> m String
|
2015-09-01 08:27:12 +00:00
|
|
|
convert' x = flip convert x . optOutput <$> options
|
2014-05-11 22:40:00 +00:00
|
|
|
|
2015-08-31 05:33:36 +00:00
|
|
|
convert :: ToString a => OutputOpts -> a -> String
|
2015-09-01 08:27:12 +00:00
|
|
|
convert opt@OutputOpts { ooptStyle = LispStyle } x = toLisp opt x "\n"
|
|
|
|
convert opt@OutputOpts { ooptStyle = PlainStyle } x
|
2014-05-11 22:40:00 +00:00
|
|
|
| str == "\n" = ""
|
|
|
|
| otherwise = str
|
|
|
|
where
|
|
|
|
str = toPlain opt x "\n"
|
|
|
|
|
|
|
|
class ToString a where
|
2015-08-31 05:33:36 +00:00
|
|
|
toLisp :: OutputOpts -> a -> Builder
|
|
|
|
toPlain :: OutputOpts -> a -> Builder
|
2014-05-11 22:40:00 +00:00
|
|
|
|
2015-08-31 05:33:36 +00:00
|
|
|
lineSep :: OutputOpts -> String
|
|
|
|
lineSep oopts = interpret lsep
|
2014-05-11 22:40:00 +00:00
|
|
|
where
|
2014-07-30 03:03:58 +00:00
|
|
|
interpret s = read $ "\"" ++ s ++ "\""
|
2015-09-01 08:27:12 +00:00
|
|
|
LineSeparator lsep = ooptLineSeparator oopts
|
2014-05-11 22:40:00 +00:00
|
|
|
|
|
|
|
-- |
|
|
|
|
--
|
2015-09-02 02:00:30 +00:00
|
|
|
-- >>> toLisp (optOutput defaultOptions) "fo\"o" ""
|
2014-05-11 22:40:00 +00:00
|
|
|
-- "\"fo\\\"o\""
|
2015-09-02 02:00:30 +00:00
|
|
|
-- >>> toPlain (optOutput defaultOptions) "foo" ""
|
2014-05-11 22:40:00 +00:00
|
|
|
-- "foo"
|
|
|
|
instance ToString String where
|
2015-08-31 05:33:36 +00:00
|
|
|
toLisp oopts = quote oopts
|
|
|
|
toPlain oopts = replace '\n' (lineSep oopts)
|
2014-05-11 22:40:00 +00:00
|
|
|
|
|
|
|
-- |
|
|
|
|
--
|
2015-09-02 02:00:30 +00:00
|
|
|
-- >>> toLisp (optOutput defaultOptions) ["foo", "bar", "ba\"z"] ""
|
2014-05-11 22:40:00 +00:00
|
|
|
-- "(\"foo\" \"bar\" \"ba\\\"z\")"
|
2015-09-02 02:00:30 +00:00
|
|
|
-- >>> toPlain (optOutput defaultOptions) ["foo", "bar", "baz"] ""
|
2014-05-11 22:40:00 +00:00
|
|
|
-- "foo\nbar\nbaz"
|
|
|
|
instance ToString [String] where
|
2015-08-31 05:33:36 +00:00
|
|
|
toLisp oopts = toSexp1 oopts
|
|
|
|
toPlain oopts = inter '\n' . map (toPlain oopts)
|
2014-05-11 22:40:00 +00:00
|
|
|
|
2015-06-01 15:10:37 +00:00
|
|
|
instance ToString [ModuleString] where
|
2015-08-31 05:33:36 +00:00
|
|
|
toLisp oopts = toLisp oopts . map getModuleString
|
|
|
|
toPlain oopts = toPlain oopts . map getModuleString
|
2015-06-01 15:10:37 +00:00
|
|
|
|
2014-05-11 22:40:00 +00:00
|
|
|
-- |
|
|
|
|
--
|
|
|
|
-- >>> let inp = [((1,2,3,4),"foo"),((5,6,7,8),"bar")] :: [((Int,Int,Int,Int),String)]
|
2015-09-02 02:00:30 +00:00
|
|
|
-- >>> toLisp (optOutput defaultOptions) inp ""
|
2014-05-11 22:40:00 +00:00
|
|
|
-- "((1 2 3 4 \"foo\") (5 6 7 8 \"bar\"))"
|
2015-09-02 02:00:30 +00:00
|
|
|
-- >>> toPlain (optOutput defaultOptions) inp ""
|
2014-05-11 22:40:00 +00:00
|
|
|
-- "1 2 3 4 \"foo\"\n5 6 7 8 \"bar\""
|
|
|
|
instance ToString [((Int,Int,Int,Int),String)] where
|
2015-08-31 05:33:36 +00:00
|
|
|
toLisp oopts = toSexp2 . map toS
|
2015-06-01 14:54:50 +00:00
|
|
|
where
|
2015-08-31 05:33:36 +00:00
|
|
|
toS x = ('(' :) . tupToString oopts x . (')' :)
|
|
|
|
toPlain oopts = inter '\n' . map (tupToString oopts)
|
2014-05-11 22:40:00 +00:00
|
|
|
|
2014-06-08 10:33:13 +00:00
|
|
|
instance ToString ((Int,Int,Int,Int),String) where
|
2015-08-31 05:33:36 +00:00
|
|
|
toLisp oopts x = ('(' :) . tupToString oopts x . (')' :)
|
|
|
|
toPlain oopts x = tupToString oopts x
|
2014-06-08 10:33:13 +00:00
|
|
|
|
2014-08-02 07:52:36 +00:00
|
|
|
instance ToString ((Int,Int,Int,Int),[String]) where
|
2015-08-31 05:33:36 +00:00
|
|
|
toLisp oopts (x,s) = ('(' :) . fourIntsToString x .
|
|
|
|
(' ' :) . toLisp oopts s . (')' :)
|
|
|
|
toPlain oopts (x,s) = fourIntsToString x . ('\n' :) . toPlain oopts s
|
2014-08-02 07:52:36 +00:00
|
|
|
|
2014-06-22 16:03:34 +00:00
|
|
|
instance ToString (String, (Int,Int,Int,Int),[String]) where
|
2015-08-31 05:33:36 +00:00
|
|
|
toLisp oopts (s,x,y) = toSexp2 [toLisp oopts s, ('(' :) . fourIntsToString x . (')' :), toLisp oopts y]
|
|
|
|
toPlain oopts (s,x,y) = inter '\n' [toPlain oopts s, fourIntsToString x, toPlain oopts y]
|
2014-06-18 19:01:22 +00:00
|
|
|
|
2015-08-31 05:33:36 +00:00
|
|
|
toSexp1 :: OutputOpts -> [String] -> Builder
|
|
|
|
toSexp1 oopts ss = ('(' :) . inter ' ' (map (quote oopts) ss) . (')' :)
|
2014-05-11 22:40:00 +00:00
|
|
|
|
|
|
|
toSexp2 :: [Builder] -> Builder
|
2014-07-17 08:16:44 +00:00
|
|
|
toSexp2 ss = ('(' :) . inter ' ' ss . (')' :)
|
2014-05-11 22:40:00 +00:00
|
|
|
|
2015-08-31 05:33:36 +00:00
|
|
|
fourIntsToString :: (Int,Int,Int,Int) -> Builder
|
|
|
|
fourIntsToString (a,b,c,d) = (show a ++) . (' ' :)
|
|
|
|
. (show b ++) . (' ' :)
|
|
|
|
. (show c ++) . (' ' :)
|
|
|
|
. (show d ++)
|
|
|
|
|
|
|
|
tupToString :: OutputOpts -> ((Int,Int,Int,Int),String) -> Builder
|
|
|
|
tupToString oopts ((a,b,c,d),s) = (show a ++) . (' ' :)
|
|
|
|
. (show b ++) . (' ' :)
|
|
|
|
. (show c ++) . (' ' :)
|
|
|
|
. (show d ++) . (' ' :)
|
|
|
|
. quote oopts s -- fixme: quote is not necessary
|
|
|
|
|
|
|
|
quote :: OutputOpts -> String -> Builder
|
|
|
|
quote oopts str = ("\"" ++) . (quote' str ++) . ("\"" ++)
|
2014-05-11 22:40:00 +00:00
|
|
|
where
|
2015-08-31 05:33:36 +00:00
|
|
|
lsep = lineSep oopts
|
2014-05-11 22:40:00 +00:00
|
|
|
quote' [] = []
|
|
|
|
quote' (x:xs)
|
|
|
|
| x == '\n' = lsep ++ quote' xs
|
|
|
|
| x == '\\' = "\\\\" ++ quote' xs
|
|
|
|
| x == '"' = "\\\"" ++ quote' xs
|
|
|
|
| otherwise = x : quote' xs
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
2014-06-27 16:38:15 +00:00
|
|
|
|
|
|
|
-- Empty result to be returned when no info can be gathered
|
2015-08-31 05:33:36 +00:00
|
|
|
emptyResult :: Monad m => OutputOpts -> m String
|
|
|
|
emptyResult oopts = return $ convert oopts ([] :: [String])
|
2014-06-27 16:38:15 +00:00
|
|
|
|
|
|
|
-- Return an emptyResult when Nothing
|
2015-08-31 05:33:36 +00:00
|
|
|
whenFound :: (Monad m, ToString b) => OutputOpts -> m (Maybe a) -> (a -> b) -> m String
|
|
|
|
whenFound oopts from f = maybe (emptyResult oopts) (return . convert oopts . f) =<< from
|
2014-06-27 17:06:20 +00:00
|
|
|
|
|
|
|
-- Return an emptyResult when Nothing, inside a monad
|
2015-08-31 05:33:36 +00:00
|
|
|
whenFound' :: (Monad m, ToString b) => OutputOpts -> m (Maybe a) -> (a -> m b) -> m String
|
|
|
|
whenFound' oopts from f = maybe (emptyResult oopts) (\x -> do y <- f x ; return (convert oopts y)) =<< from
|