{-# LANGUAGE FlexibleInstances #-}
module BNFC.Backend.Base
( Backend
, MkFiles
, execBackend
, mkfile
, liftIO
, writeFiles
) where
import Control.Monad.Writer
import Data.Char (isSpace)
import qualified Data.List as List
import System.Directory (createDirectoryIfMissing)
import System.FilePath (dropFileName, (</>))
import BNFC.PrettyPrint
import BNFC.Utils (writeFileRep)
type MkFiles a = WriterT [(FilePath, String)] IO a
type Backend = MkFiles ()
execBackend :: MkFiles () -> IO [(FilePath, String)]
execBackend :: MkFiles () -> IO [(FilePath, FilePath)]
execBackend = MkFiles () -> IO [(FilePath, FilePath)]
forall (m :: * -> *) w a. Monad m => WriterT w m a -> m w
execWriterT
mkfile :: (FileContent c) => FilePath -> c -> MkFiles ()
mkfile :: forall c. FileContent c => FilePath -> c -> MkFiles ()
mkfile FilePath
path c
content = [(FilePath, FilePath)] -> MkFiles ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [(FilePath
path, c -> FilePath
forall a. FileContent a => a -> FilePath
fileContentToString c
content)]
class FileContent a where
fileContentToString :: a -> String
instance FileContent [Char] where
fileContentToString :: FilePath -> FilePath
fileContentToString = FilePath -> FilePath
deleteTrailingWhiteSpace
instance FileContent Doc where
fileContentToString :: Doc -> FilePath
fileContentToString = FilePath -> FilePath
deleteTrailingWhiteSpace (FilePath -> FilePath) -> (Doc -> FilePath) -> Doc -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> FilePath
render
deleteTrailingWhiteSpace :: String -> String
deleteTrailingWhiteSpace :: FilePath -> FilePath
deleteTrailingWhiteSpace = [FilePath] -> FilePath
unlines ([FilePath] -> FilePath)
-> (FilePath -> [FilePath]) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FilePath -> FilePath) -> [FilePath] -> [FilePath]
forall a b. (a -> b) -> [a] -> [b]
map ((Char -> Bool) -> FilePath -> FilePath
forall a. (a -> Bool) -> [a] -> [a]
List.dropWhileEnd Char -> Bool
isSpace) ([FilePath] -> [FilePath])
-> (FilePath -> [FilePath]) -> FilePath -> [FilePath]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> [FilePath]
lines
writeFiles :: FilePath -> MkFiles () -> IO ()
writeFiles :: FilePath -> MkFiles () -> IO ()
writeFiles FilePath
root MkFiles ()
fw = do
[(FilePath, FilePath)]
fb <- MkFiles () -> IO [(FilePath, FilePath)]
execBackend MkFiles ()
fw
Bool -> FilePath -> IO ()
createDirectoryIfMissing Bool
True FilePath
root
((FilePath, FilePath) -> IO ()) -> [(FilePath, FilePath)] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ((FilePath -> FilePath -> IO ()) -> (FilePath, FilePath) -> IO ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry FilePath -> FilePath -> IO ()
writeFile') [(FilePath, FilePath)]
fb
where
writeFile' :: FilePath -> String -> IO ()
writeFile' :: FilePath -> FilePath -> IO ()
writeFile' FilePath
path FilePath
content = do
Bool -> FilePath -> IO ()
createDirectoryIfMissing Bool
True (FilePath
root FilePath -> FilePath -> FilePath
</> FilePath -> FilePath
dropFileName FilePath
path)
FilePath -> FilePath -> IO ()
writeFileRep (FilePath
root FilePath -> FilePath -> FilePath
</> FilePath
path) FilePath
content