{-# LANGUAGE FlexibleInstances #-}
module BNFC.Backend.Base
( Backend
, MkFiles
, GeneratedFile(..)
, MakeComment
, execBackend
, mkfile
, liftIO
, writeFiles
) where
import Control.Arrow ( (&&&) )
import Control.Monad.IO.Class ( liftIO )
import Control.Monad.Writer ( WriterT, execWriterT, tell )
import Data.Char ( isSpace )
import Data.Foldable ( forM_ )
import Data.Function ( on )
import qualified Data.List as List
import System.Directory ( createDirectoryIfMissing )
import System.FilePath ( dropFileName, takeExtension, (</>) )
import BNFC.Options ( versionString )
import BNFC.PrettyPrint
import BNFC.Utils ( writeFileRep )
msgGenerated :: String
msgGenerated :: [Char]
msgGenerated = [Char]
"File generated by the BNF Converter (bnfc " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
versionString [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
")."
type MkFiles a = WriterT [GeneratedFile] IO a
type Backend = MkFiles ()
data GeneratedFile = GeneratedFile
{ GeneratedFile -> [Char]
fileName :: FilePath
, :: MakeComment
, GeneratedFile -> [Char]
fileContent :: String
}
instance Show GeneratedFile where
show :: GeneratedFile -> [Char]
show (GeneratedFile [Char]
x [Char] -> [Char]
_ [Char]
y) = [[Char]] -> [Char]
unwords [ [Char]
"GeneratedFile", [Char] -> [Char]
forall a. Show a => a -> [Char]
show [Char]
x, [Char]
"_", [Char] -> [Char]
forall a. Show a => a -> [Char]
show [Char]
y ]
instance Eq GeneratedFile where
== :: GeneratedFile -> GeneratedFile -> Bool
(==) = ([Char], [Char]) -> ([Char], [Char]) -> Bool
forall a. Eq a => a -> a -> Bool
(==) (([Char], [Char]) -> ([Char], [Char]) -> Bool)
-> (GeneratedFile -> ([Char], [Char]))
-> GeneratedFile
-> GeneratedFile
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` GeneratedFile -> [Char]
fileName (GeneratedFile -> [Char])
-> (GeneratedFile -> [Char]) -> GeneratedFile -> ([Char], [Char])
forall b c c'. (b -> c) -> (b -> c') -> b -> (c, c')
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& GeneratedFile -> [Char]
fileContent
type = String -> String
execBackend :: MkFiles () -> IO [GeneratedFile]
execBackend :: MkFiles () -> IO [GeneratedFile]
execBackend = MkFiles () -> IO [GeneratedFile]
forall (m :: * -> *) w a. Monad m => WriterT w m a -> m w
execWriterT
mkfile :: FileContent c => FilePath -> MakeComment -> c -> MkFiles ()
mkfile :: forall c.
FileContent c =>
[Char] -> ([Char] -> [Char]) -> c -> MkFiles ()
mkfile [Char]
path [Char] -> [Char]
f c
content = [GeneratedFile] -> MkFiles ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [[Char] -> ([Char] -> [Char]) -> [Char] -> GeneratedFile
GeneratedFile [Char]
path [Char] -> [Char]
f (c -> [Char]
forall a. FileContent a => a -> [Char]
fileContentToString c
content)]
class FileContent a where
fileContentToString :: a -> String
instance FileContent [Char] where
fileContentToString :: [Char] -> [Char]
fileContentToString = [Char] -> [Char]
deleteTrailingWhiteSpace
instance FileContent Doc where
fileContentToString :: Doc -> [Char]
fileContentToString = [Char] -> [Char]
deleteTrailingWhiteSpace ([Char] -> [Char]) -> (Doc -> [Char]) -> Doc -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> [Char]
render
deleteTrailingWhiteSpace :: String -> String
deleteTrailingWhiteSpace :: [Char] -> [Char]
deleteTrailingWhiteSpace = [[Char]] -> [Char]
unlines ([[Char]] -> [Char]) -> ([Char] -> [[Char]]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ((Char -> Bool) -> [Char] -> [Char]
forall a. (a -> Bool) -> [a] -> [a]
List.dropWhileEnd Char -> Bool
isSpace) ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
lines
writeFiles :: FilePath -> MkFiles () -> IO ()
writeFiles :: [Char] -> MkFiles () -> IO ()
writeFiles [Char]
root MkFiles ()
fw = do
[GeneratedFile]
fb <- MkFiles () -> IO [GeneratedFile]
execBackend MkFiles ()
fw
Bool -> [Char] -> IO ()
createDirectoryIfMissing Bool
True [Char]
root
[GeneratedFile] -> (GeneratedFile -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [GeneratedFile]
fb ((GeneratedFile -> IO ()) -> IO ())
-> (GeneratedFile -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ (GeneratedFile [Char]
path [Char] -> [Char]
mkComment [Char]
content) -> do
Bool -> [Char] -> IO ()
createDirectoryIfMissing Bool
True ([Char]
root [Char] -> [Char] -> [Char]
</> [Char] -> [Char]
dropFileName [Char]
path)
[Char] -> [Char] -> IO ()
writeFileRep ([Char]
root [Char] -> [Char] -> [Char]
</> [Char]
path) ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$
if [Char] -> [Char]
takeExtension [Char]
path [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
".txt" then
[[Char]] -> [Char]
unlines [ [Char]
content, [Char] -> [Char]
mkComment [Char]
msgGenerated ]
else
[Char] -> [Char]
mkComment [Char]
msgGenerated [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"\n\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
content