ghc-mod/core/Language/Haskell/GhcMod/Convert.hs

141 lines
4.9 KiB
Haskell
Raw Normal View History

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
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
convert' :: (ToString a, IOish m, GmEnv m) => a -> m String
convert' x = flip convert x . optOutput <$> options
2014-05-11 22:40:00 +00:00
convert :: ToString a => OutputOpts -> a -> String
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
toLisp :: OutputOpts -> a -> Builder
toPlain :: OutputOpts -> a -> Builder
2014-05-11 22:40:00 +00:00
lineSep :: OutputOpts -> String
lineSep oopts = interpret lsep
2014-05-11 22:40:00 +00:00
where
interpret s = read $ "\"" ++ s ++ "\""
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
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
toLisp oopts = toSexp1 oopts
toPlain oopts = inter '\n' . map (toPlain oopts)
2014-05-11 22:40:00 +00:00
instance ToString [ModuleString] where
toLisp oopts = toLisp oopts . map getModuleString
toPlain oopts = toPlain oopts . map getModuleString
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
toLisp oopts = toSexp2 . map toS
2015-06-01 14:54:50 +00:00
where
toS x = ('(' :) . tupToString oopts x . (')' :)
toPlain oopts = inter '\n' . map (tupToString oopts)
2014-05-11 22:40:00 +00:00
instance ToString ((Int,Int,Int,Int),String) where
toLisp oopts x = ('(' :) . tupToString oopts x . (')' :)
toPlain oopts x = tupToString oopts x
2014-08-02 07:52:36 +00:00
instance ToString ((Int,Int,Int,Int),[String]) where
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
instance ToString (String, (Int,Int,Int,Int),[String]) where
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]
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
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
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
----------------------------------------------------------------
-- Empty result to be returned when no info can be gathered
emptyResult :: Monad m => OutputOpts -> m String
emptyResult oopts = return $ convert oopts ([] :: [String])
-- Return an emptyResult when Nothing
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
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