1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
------------------------------------------------------------------------------
--- This library defines operations to read Curry interfaces.
------------------------------------------------------------------------------

module CurryInterface.Files where

import System.CurryPath     ( getLoadPathForModule, inCurrySubdir
                            , lookupModuleSourceInLoadPath, stripCurrySuffix )
import System.Directory     ( doesFileExist )
import System.FilePath      ( takeFileName, (</>), (<.>) )
import System.FrontendExec

import CurryInterface.Parser
import CurryInterface.Types

--- I/O action which parses a Curry interface and returns it as a data structure.
--- The argument is the module name (without suffix ".curry" or ".lcurry").
readCurryInterface :: String -> IO Interface
readCurryInterface modname =
  readCurryInterfaceWithParseOptions modname (setQuiet True defaultParams)

--- I/O action which reads a Curry interface from a file (with extension
--- `.icurry`) with respect to some parser options.
readCurryInterfaceWithParseOptions :: String -> FrontendParams -> IO Interface
readCurryInterfaceWithParseOptions modname options = do
  mbsrc <- lookupModuleSourceInLoadPath modname
  case mbsrc of
    Nothing -> error $ "Module '" ++ modname ++ "' not found in load path!"
    Just (dir,_) -> do
      -- read FlatCurry to generate interface file
      callFrontendWithParams FCY options modname
      readCurryInterfaceFile (curryInterfaceFileName (dir </> modname))

--- Transforms a name of a Curry program (with or without suffix ".curry"
--- or ".lcurry") into the name of the file containing the Curry interface.
curryInterfaceFileName :: String -> String
curryInterfaceFileName prog = inCurrySubdir (stripCurrySuffix prog) <.> "icurry"

--- I/O action which reads a Curry interface from a file in `.icurry` format.
readCurryInterfaceFile :: String -> IO Interface
readCurryInterfaceFile filename = do
  exacy <- doesFileExist filename
  if exacy
   then readExistingICURRY filename
   else error $ "EXISTENCE ERROR: Curry interface file '" ++ filename ++
                "' does not exist"
 where
   readExistingICURRY fname = do
     icurrystring <- readFile fname
     -- for testing:
     --putStrLn icurrystring
     return $ parseCurryInterface icurrystring