@@ -5,17 +5,15 @@ License : BSD3
55Maintainer : huffman
66Stability : provisional
77-}
8- {-# LANGUAGE CPP #-}
9-
108module SAWScript.Import
11- ( loadFile
12- , findAndLoadFile
9+ ( findAndLoadFile
1310 ) where
1411
1512import qualified Data.Text.IO as TextIO (readFile )
1613import qualified Data.Text as Text
1714import Control.Exception
1815import System.Directory
16+ import System.FilePath (normalise )
1917
2018import SAWCentral.Position (Pos )
2119import SAWCentral.AST
@@ -24,6 +22,7 @@ import SAWCentral.Options
2422import SAWScript.Parser
2523import SAWScript.Token (Token )
2624
25+ -- | Load the 'Stmt's in a @.saw@ file.
2726loadFile :: Options -> FilePath -> IO [Stmt ]
2827loadFile opts fname = do
2928 printOutLn opts Info $ " Loading file " ++ show fname
@@ -53,6 +52,10 @@ parseFile tokens = do
5352 Left err -> Left err
5453 Right stmts -> Right stmts
5554
55+ -- | Find a file, potentially looking in a list of multiple search paths (as
56+ -- specified via the @SAW_IMPORT_PATH@ environment variable or
57+ -- @-i@/@--import-path@ command-line options). If the file was successfully
58+ -- found, load it. If not, raise an error.
5659findAndLoadFile :: Options -> FilePath -> IO [Stmt ]
5760findAndLoadFile opts fp = do
5861 let paths = importPath opts
@@ -62,17 +65,10 @@ findAndLoadFile opts fp = do
6265 [ " Couldn't find file: " ++ show fp
6366 , " Searched in directories:"
6467 ] ++ map (" " ++ ) paths
65- Just fname -> loadFile opts fname
66-
67- #if __GLASGOW_HASKELL__ < 706
68- findFile :: [FilePath ] -> String -> IO (Maybe FilePath )
69- findFile paths fileName = search paths
70- where
71- search :: [FilePath ] -> IO (Maybe FilePath )
72- search [] = return Nothing
73- search (d: ds) = do
74- let path = d </> fileName
75- b <- doesFileExist path
76- if b then return (Just path)
77- else search ds
78- #endif
68+ Just fname ->
69+ -- NB: Normalise the path name. The default SAW_IMPORT_PATH contains ".",
70+ -- and the behavior of filepath's 'normalise' function is to prepend a
71+ -- search path to the front of the file path that is found, which can
72+ -- cause paths like "./foo.saw" to be returned. This looks ugly in error
73+ -- messages, where we would rather display "foo.saw" instead.
74+ loadFile opts (normalise fname)
0 commit comments