mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-22 11:19:32 -06:00
Some more monadic lifting changes
This commit is contained in:
@@ -98,17 +98,17 @@ compileModule :: Options -- ^ Options from program command line and shell comman
|
|||||||
compileModule opts1 env file = do
|
compileModule opts1 env file = do
|
||||||
file <- getRealFile file
|
file <- getRealFile file
|
||||||
opts0 <- getOptionsFromFile file
|
opts0 <- getOptionsFromFile file
|
||||||
curr_dir <- return $ dropFileName file
|
let curr_dir = dropFileName file
|
||||||
lib_dir <- liftIO $ getLibraryDirectory (addOptions opts0 opts1)
|
lib_dir <- liftIO $ getLibraryDirectory (addOptions opts0 opts1)
|
||||||
let opts = addOptions (fixRelativeLibPaths curr_dir lib_dir opts0) opts1
|
let opts = addOptions (fixRelativeLibPaths curr_dir lib_dir opts0) opts1
|
||||||
ps0 <- liftIO $ extendPathEnv opts
|
ps0 <- extendPathEnv opts
|
||||||
let ps = nub (curr_dir : ps0)
|
let ps = nub (curr_dir : ps0)
|
||||||
liftIO $ putIfVerb opts $ "module search path:" +++ show ps ----
|
putIfVerb opts $ "module search path:" +++ show ps ----
|
||||||
let (_,sgr,rfs) = env
|
let (_,sgr,rfs) = env
|
||||||
files <- getAllFiles opts ps rfs file
|
files <- getAllFiles opts ps rfs file
|
||||||
liftIO $ putIfVerb opts $ "files to read:" +++ show files ----
|
putIfVerb opts $ "files to read:" +++ show files ----
|
||||||
let names = map justModuleName files
|
let names = map justModuleName files
|
||||||
liftIO $ putIfVerb opts $ "modules to include:" +++ show names ----
|
putIfVerb opts $ "modules to include:" +++ show names ----
|
||||||
foldM (compileOne opts) (0,sgr,rfs) files
|
foldM (compileOne opts) (0,sgr,rfs) files
|
||||||
where
|
where
|
||||||
getRealFile file = do
|
getRealFile file = do
|
||||||
|
|||||||
@@ -212,11 +212,11 @@ importsOfModule (m,mi) = (modName m,depModInfo mi [])
|
|||||||
-- | options can be passed to the compiler by comments in @--#@, in the main file
|
-- | options can be passed to the compiler by comments in @--#@, in the main file
|
||||||
getOptionsFromFile :: (MonadIO m,ErrorMonad m) => FilePath -> m Options
|
getOptionsFromFile :: (MonadIO m,ErrorMonad m) => FilePath -> m Options
|
||||||
getOptionsFromFile file = do
|
getOptionsFromFile file = do
|
||||||
s <- handle (liftIO $ BS.readFile file)
|
s <- either (\_ -> raise $ "File " ++ file ++ " does not exist") return =<<
|
||||||
(\_ -> raise $ "File " ++ file ++ " does not exist")
|
liftIO (try $ BS.readFile file)
|
||||||
let ls = filter (BS.isPrefixOf (BS.pack "--#")) $ BS.lines s
|
let ls = filter (BS.isPrefixOf (BS.pack "--#")) $ BS.lines s
|
||||||
fs = map (BS.unpack . BS.unwords . BS.words . BS.drop 3) ls
|
fs = map (BS.unpack . BS.unwords . BS.words . BS.drop 3) ls
|
||||||
liftErr $ parseModuleOptions fs
|
parseModuleOptions fs
|
||||||
|
|
||||||
getFilePath :: MonadIO m => [FilePath] -> String -> m (Maybe FilePath)
|
getFilePath :: MonadIO m => [FilePath] -> String -> m (Maybe FilePath)
|
||||||
getFilePath paths file = liftIO $ get paths
|
getFilePath paths file = liftIO $ get paths
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ import GF.Grammar.Predef
|
|||||||
import System.FilePath
|
import System.FilePath
|
||||||
--import System.IO
|
--import System.IO
|
||||||
|
|
||||||
import GF.Data.ErrM
|
import GF.Data.Operations(Err,ErrorMonad(..),liftErr)
|
||||||
|
|
||||||
import Data.Set (Set)
|
import Data.Set (Set)
|
||||||
import qualified Data.Set as Set
|
import qualified Data.Set as Set
|
||||||
@@ -68,8 +68,8 @@ helpMessage = usageInfo usageHeader optDescr
|
|||||||
|
|
||||||
|
|
||||||
-- FIXME: do we really want multi-line errors?
|
-- FIXME: do we really want multi-line errors?
|
||||||
errors :: [String] -> Err a
|
errors :: ErrorMonad err => [String] -> err a
|
||||||
errors = fail . unlines
|
errors = raise . unlines
|
||||||
|
|
||||||
-- Types
|
-- Types
|
||||||
|
|
||||||
@@ -185,17 +185,19 @@ instance Show Options where
|
|||||||
|
|
||||||
-- Option parsing
|
-- Option parsing
|
||||||
|
|
||||||
parseOptions :: [String] -- ^ list of string arguments
|
parseOptions :: ErrorMonad err =>
|
||||||
-> Err (Options, [FilePath])
|
[String] -- ^ list of string arguments
|
||||||
|
-> err (Options, [FilePath])
|
||||||
parseOptions args
|
parseOptions args
|
||||||
| not (null errs) = errors errs
|
| not (null errs) = errors errs
|
||||||
| otherwise = do opts <- liftM concatOptions $ sequence optss
|
| otherwise = do opts <- concatOptions `fmap` liftErr (sequence optss)
|
||||||
return (opts, files)
|
return (opts, files)
|
||||||
where
|
where
|
||||||
(optss, files, errs) = getOpt RequireOrder optDescr args
|
(optss, files, errs) = getOpt RequireOrder optDescr args
|
||||||
|
|
||||||
parseModuleOptions :: [String] -- ^ list of string arguments
|
parseModuleOptions :: ErrorMonad err =>
|
||||||
-> Err Options
|
[String] -- ^ list of string arguments
|
||||||
|
-> err Options
|
||||||
parseModuleOptions args = do
|
parseModuleOptions args = do
|
||||||
(opts,nonopts) <- parseOptions args
|
(opts,nonopts) <- parseOptions args
|
||||||
if null nonopts
|
if null nonopts
|
||||||
|
|||||||
@@ -38,21 +38,21 @@ import Control.Exception(evaluate)
|
|||||||
--putShow' :: Show a => (c -> a) -> c -> IO ()
|
--putShow' :: Show a => (c -> a) -> c -> IO ()
|
||||||
--putShow' f = putStrLn . show . length . show . f
|
--putShow' f = putStrLn . show . length . show . f
|
||||||
|
|
||||||
putIfVerb :: Options -> String -> IO ()
|
putIfVerb :: MonadIO io => Options -> String -> io ()
|
||||||
putIfVerb opts msg =
|
putIfVerb opts msg =
|
||||||
when (verbAtLeast opts Verbose) $ putStrLn msg
|
when (verbAtLeast opts Verbose) $ liftIO $ putStrLn msg
|
||||||
|
|
||||||
putIfVerbW :: Options -> String -> IO ()
|
putIfVerbW :: MonadIO io => Options -> String -> io ()
|
||||||
putIfVerbW opts msg =
|
putIfVerbW opts msg =
|
||||||
when (verbAtLeast opts Verbose) $ putStr (' ' : msg)
|
when (verbAtLeast opts Verbose) $ liftIO $ putStr (' ' : msg)
|
||||||
|
{-
|
||||||
errOptIO :: Options -> a -> Err a -> IO a
|
errOptIO :: Options -> a -> Err a -> IO a
|
||||||
errOptIO os e m = case m of
|
errOptIO os e m = case m of
|
||||||
Ok x -> return x
|
Ok x -> return x
|
||||||
Bad k -> do
|
Bad k -> do
|
||||||
putIfVerb os k
|
putIfVerb os k
|
||||||
return e
|
return e
|
||||||
|
-}
|
||||||
type FileName = String
|
type FileName = String
|
||||||
type InitPath = String
|
type InitPath = String
|
||||||
type FullPath = String
|
type FullPath = String
|
||||||
@@ -60,13 +60,12 @@ type FullPath = String
|
|||||||
gfLibraryPath = "GF_LIB_PATH"
|
gfLibraryPath = "GF_LIB_PATH"
|
||||||
gfGrammarPathVar = "GF_GRAMMAR_PATH"
|
gfGrammarPathVar = "GF_GRAMMAR_PATH"
|
||||||
|
|
||||||
getLibraryDirectory :: Options -> IO FilePath
|
getLibraryDirectory :: MonadIO io => Options -> io FilePath
|
||||||
getLibraryDirectory opts =
|
getLibraryDirectory opts =
|
||||||
case flag optGFLibPath opts of
|
case flag optGFLibPath opts of
|
||||||
Just path -> return path
|
Just path -> return path
|
||||||
Nothing -> catch
|
Nothing -> liftIO $ catch (getEnv gfLibraryPath)
|
||||||
(getEnv gfLibraryPath)
|
(\ex -> fmap (</> "lib") getDataDir)
|
||||||
(\ex -> getDataDir >>= \path -> return (path </> "lib"))
|
|
||||||
|
|
||||||
getGrammarPath :: FilePath -> IO [FilePath]
|
getGrammarPath :: FilePath -> IO [FilePath]
|
||||||
getGrammarPath lib_dir = do
|
getGrammarPath lib_dir = do
|
||||||
@@ -76,9 +75,9 @@ getGrammarPath lib_dir = do
|
|||||||
-- | extends the search path with the
|
-- | extends the search path with the
|
||||||
-- 'gfLibraryPath' and 'gfGrammarPathVar'
|
-- 'gfLibraryPath' and 'gfGrammarPathVar'
|
||||||
-- environment variables. Returns only existing paths.
|
-- environment variables. Returns only existing paths.
|
||||||
extendPathEnv :: Options -> IO [FilePath]
|
extendPathEnv :: MonadIO io => Options -> io [FilePath]
|
||||||
extendPathEnv opts = do
|
extendPathEnv opts = liftIO $ do
|
||||||
opt_path <- return $ flag optLibraryPath opts -- e.g. paths given as options
|
let opt_path = flag optLibraryPath opts -- e.g. paths given as options
|
||||||
lib_dir <- getLibraryDirectory opts -- e.g. GF_LIB_PATH
|
lib_dir <- getLibraryDirectory opts -- e.g. GF_LIB_PATH
|
||||||
grm_path <- getGrammarPath lib_dir -- e.g. GF_GRAMMAR_PATH
|
grm_path <- getGrammarPath lib_dir -- e.g. GF_GRAMMAR_PATH
|
||||||
let paths = opt_path ++ [lib_dir] ++ grm_path
|
let paths = opt_path ++ [lib_dir] ++ grm_path
|
||||||
|
|||||||
Reference in New Issue
Block a user