module Curry.Files.Filenames
(
FilePath, takeBaseName, dropExtension, takeExtension, takeFileName
, moduleNameToFile, fileNameToModule, splitModuleFileName, isCurryFilePath
, defaultOutDir, hasOutDir, addOutDir, addOutDirModule
, ensureOutDir
, curryExt, lcurryExt, icurryExt
, annotatedFlatExt, typedFlatExt, flatExt, flatIntExt
, acyExt, uacyExt
, sourceRepExt, sourceExts, moduleExts
, interfName, typedFlatName, annotatedFlatName, flatName, flatIntName
, acyName, uacyName, sourceRepName, tokensName, commentsName
, astName, shortASTName, htmlName
) where
import System.FilePath
import Curry.Base.Ident
moduleNameToFile :: ModuleIdent -> FilePath
moduleNameToFile :: ModuleIdent -> FilePath
moduleNameToFile = (FilePath -> FilePath -> FilePath) -> [FilePath] -> FilePath
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 FilePath -> FilePath -> FilePath
(</>) ([FilePath] -> FilePath)
-> (ModuleIdent -> [FilePath]) -> ModuleIdent -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ModuleIdent -> [FilePath]
midQualifiers
fileNameToModule :: FilePath -> ModuleIdent
fileNameToModule :: FilePath -> ModuleIdent
fileNameToModule = [FilePath] -> ModuleIdent
mkMIdent ([FilePath] -> ModuleIdent)
-> (FilePath -> [FilePath]) -> FilePath -> ModuleIdent
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> [FilePath]
splitDirectories (FilePath -> [FilePath])
-> (FilePath -> FilePath) -> FilePath -> [FilePath]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath
dropExtension (FilePath -> FilePath)
-> (FilePath -> FilePath) -> FilePath -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath
dropDrive
splitModuleFileName :: ModuleIdent -> FilePath -> (FilePath, FilePath)
splitModuleFileName :: ModuleIdent -> FilePath -> (FilePath, FilePath)
splitModuleFileName m :: ModuleIdent
m fn :: FilePath
fn = case ModuleIdent -> [FilePath]
midQualifiers ModuleIdent
m of
[_] -> FilePath -> (FilePath, FilePath)
splitFileName FilePath
fn
ms :: [FilePath]
ms -> let (base :: FilePath
base, ext :: FilePath
ext) = FilePath -> (FilePath, FilePath)
splitExtension FilePath
fn
dirs :: [FilePath]
dirs = FilePath -> [FilePath]
splitDirectories FilePath
base
(pre :: [FilePath]
pre, suf :: [FilePath]
suf) = Int -> [FilePath] -> ([FilePath], [FilePath])
forall a. Int -> [a] -> ([a], [a])
splitAt ([FilePath] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [FilePath]
dirs Int -> Int -> Int
forall a. Num a => a -> a -> a
- [FilePath] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [FilePath]
ms) [FilePath]
dirs
path :: FilePath
path = if [FilePath] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FilePath]
pre then ""
else FilePath -> FilePath
addTrailingPathSeparator ([FilePath] -> FilePath
joinPath [FilePath]
pre)
in (FilePath
path, [FilePath] -> FilePath
joinPath [FilePath]
suf FilePath -> FilePath -> FilePath
<.> FilePath
ext)
isCurryFilePath :: String -> Bool
isCurryFilePath :: FilePath -> Bool
isCurryFilePath str :: FilePath
str = FilePath -> Bool
isValid FilePath
str
Bool -> Bool -> Bool
&& FilePath -> FilePath
takeExtension FilePath
str FilePath -> [FilePath] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ("" FilePath -> [FilePath] -> [FilePath]
forall a. a -> [a] -> [a]
: [FilePath]
moduleExts)
defaultOutDir :: String
defaultOutDir :: FilePath
defaultOutDir = ".curry"
hasOutDir :: String -> FilePath -> Bool
hasOutDir :: FilePath -> FilePath -> Bool
hasOutDir outDir :: FilePath
outDir f :: FilePath
f = Bool -> Bool
not ([FilePath] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FilePath]
dirs) Bool -> Bool -> Bool
&& [FilePath] -> FilePath
forall a. [a] -> a
last [FilePath]
dirs FilePath -> FilePath -> Bool
forall a. Eq a => a -> a -> Bool
== FilePath
outDir
where dirs :: [FilePath]
dirs = FilePath -> [FilePath]
splitDirectories (FilePath -> [FilePath]) -> FilePath -> [FilePath]
forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath
takeDirectory FilePath
f
addOutDir :: Bool -> String -> FilePath -> FilePath
addOutDir :: Bool -> FilePath -> FilePath -> FilePath
addOutDir b :: Bool
b outDir :: FilePath
outDir fn :: FilePath
fn = if Bool
b then FilePath -> FilePath -> FilePath
ensureOutDir FilePath
outDir FilePath
fn else FilePath
fn
addOutDirModule :: Bool -> String -> ModuleIdent -> FilePath -> FilePath
addOutDirModule :: Bool -> FilePath -> ModuleIdent -> FilePath -> FilePath
addOutDirModule b :: Bool
b outDir :: FilePath
outDir m :: ModuleIdent
m fn :: FilePath
fn
| Bool
b = let (pre :: FilePath
pre, file :: FilePath
file) = ModuleIdent -> FilePath -> (FilePath, FilePath)
splitModuleFileName ModuleIdent
m FilePath
fn
in FilePath -> FilePath -> FilePath
ensureOutDir FilePath
outDir FilePath
pre FilePath -> FilePath -> FilePath
</> FilePath
file
| Bool
otherwise = FilePath
fn
ensureOutDir :: String
-> FilePath
-> FilePath
ensureOutDir :: FilePath -> FilePath -> FilePath
ensureOutDir outDir :: FilePath
outDir fn :: FilePath
fn = FilePath -> FilePath
normalise (FilePath -> FilePath) -> FilePath -> FilePath
forall a b. (a -> b) -> a -> b
$ [FilePath] -> FilePath
addSub (FilePath -> [FilePath]
splitDirectories FilePath
d) FilePath -> FilePath -> FilePath
</> FilePath
f
where
(d :: FilePath
d, f :: FilePath
f) = FilePath -> (FilePath, FilePath)
splitFileName FilePath
fn
addSub :: [FilePath] -> FilePath
addSub dirs :: [FilePath]
dirs | [FilePath] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FilePath]
dirs = FilePath
outDir
| [FilePath] -> FilePath
forall a. [a] -> a
last [FilePath]
dirs FilePath -> FilePath -> Bool
forall a. Eq a => a -> a -> Bool
== FilePath
outDir = [FilePath] -> FilePath
joinPath [FilePath]
dirs
| Bool
otherwise = [FilePath] -> FilePath
joinPath [FilePath]
dirs FilePath -> FilePath -> FilePath
</> FilePath
outDir
curryExt :: String
curryExt :: FilePath
curryExt = ".curry"
lcurryExt :: String
lcurryExt :: FilePath
lcurryExt = ".lcurry"
icurryExt :: String
icurryExt :: FilePath
icurryExt = ".icurry"
sourceExts :: [String]
sourceExts :: [FilePath]
sourceExts = [FilePath
curryExt, FilePath
lcurryExt]
moduleExts :: [String]
moduleExts :: [FilePath]
moduleExts = [FilePath]
sourceExts [FilePath] -> [FilePath] -> [FilePath]
forall a. [a] -> [a] -> [a]
++ [FilePath
icurryExt]
typedFlatExt :: String
typedFlatExt :: FilePath
typedFlatExt = ".tfcy"
annotatedFlatExt :: String
annotatedFlatExt :: FilePath
annotatedFlatExt = ".tafcy"
flatExt :: String
flatExt :: FilePath
flatExt = ".fcy"
flatIntExt :: String
flatIntExt :: FilePath
flatIntExt = ".fint"
acyExt :: String
acyExt :: FilePath
acyExt = ".acy"
uacyExt :: String
uacyExt :: FilePath
uacyExt = ".uacy"
sourceRepExt :: String
sourceRepExt :: FilePath
sourceRepExt = ".cy"
tokensExt :: String
tokensExt :: FilePath
tokensExt = ".tokens"
commentsExt :: String
= ".cycom"
astExt :: String
astExt :: FilePath
astExt = ".ast"
shortASTExt :: String
shortASTExt :: FilePath
shortASTExt = ".sast"
interfName :: FilePath -> FilePath
interfName :: FilePath -> FilePath
interfName = FilePath -> FilePath -> FilePath
replaceExtensionWith FilePath
icurryExt
typedFlatName :: FilePath -> FilePath
typedFlatName :: FilePath -> FilePath
typedFlatName = FilePath -> FilePath -> FilePath
replaceExtensionWith FilePath
typedFlatExt
annotatedFlatName :: FilePath -> FilePath
annotatedFlatName :: FilePath -> FilePath
annotatedFlatName = FilePath -> FilePath -> FilePath
replaceExtensionWith FilePath
annotatedFlatExt
flatName :: FilePath -> FilePath
flatName :: FilePath -> FilePath
flatName = FilePath -> FilePath -> FilePath
replaceExtensionWith FilePath
flatExt
flatIntName :: FilePath -> FilePath
flatIntName :: FilePath -> FilePath
flatIntName = FilePath -> FilePath -> FilePath
replaceExtensionWith FilePath
flatIntExt
acyName :: FilePath -> FilePath
acyName :: FilePath -> FilePath
acyName = FilePath -> FilePath -> FilePath
replaceExtensionWith FilePath
acyExt
uacyName :: FilePath -> FilePath
uacyName :: FilePath -> FilePath
uacyName = FilePath -> FilePath -> FilePath
replaceExtensionWith FilePath
uacyExt
sourceRepName :: FilePath -> FilePath
sourceRepName :: FilePath -> FilePath
sourceRepName = FilePath -> FilePath -> FilePath
replaceExtensionWith FilePath
sourceRepExt
tokensName :: FilePath -> FilePath
tokensName :: FilePath -> FilePath
tokensName = FilePath -> FilePath -> FilePath
replaceExtensionWith FilePath
tokensExt
commentsName :: FilePath -> FilePath
= FilePath -> FilePath -> FilePath
replaceExtensionWith FilePath
commentsExt
astName :: FilePath -> FilePath
astName :: FilePath -> FilePath
astName = FilePath -> FilePath -> FilePath
replaceExtensionWith FilePath
astExt
shortASTName :: FilePath -> FilePath
shortASTName :: FilePath -> FilePath
shortASTName = FilePath -> FilePath -> FilePath
replaceExtensionWith FilePath
shortASTExt
htmlName :: ModuleIdent -> String
htmlName :: ModuleIdent -> FilePath
htmlName m :: ModuleIdent
m = ModuleIdent -> FilePath
moduleName ModuleIdent
m FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ "_curry.html"
replaceExtensionWith :: String -> FilePath -> FilePath
replaceExtensionWith :: FilePath -> FilePath -> FilePath
replaceExtensionWith = (FilePath -> FilePath -> FilePath)
-> FilePath -> FilePath -> FilePath
forall a b c. (a -> b -> c) -> b -> a -> c
flip FilePath -> FilePath -> FilePath
replaceExtension