@@ -2,22 +2,28 @@ module Language.Egison.Parser
2
2
( readTopExprs
3
3
, readTopExpr
4
4
, readExprs
5
- , readExpr
5
+ , readExpr
6
+ , loadFile
7
+ , loadLibraryFile
6
8
, parseTopExprs
7
9
, parseTopExpr
8
10
, parseExprs
9
11
, parseExpr ) where
10
12
11
- import Control.Monad.Identity
12
- import Control.Monad.Error
13
- import Control.Monad.State
13
+ import Prelude hiding (mapM )
14
+ import Control.Monad.Identity hiding (mapM )
15
+ import Control.Monad.Error hiding (mapM )
16
+ import Control.Monad.State hiding (mapM )
14
17
import Control.Applicative ((<$>) , (<*>) , (*>) , (<*) , pure )
15
18
19
+ import System.Directory (doesFileExist )
20
+
16
21
import qualified Data.Sequence as Sq
17
22
import Data.Either
18
23
import Data.Set (Set )
19
24
import Data.Char (isLower , isUpper )
20
25
import qualified Data.Set as Set
26
+ import Data.Traversable (mapM )
21
27
22
28
import Data.ByteString.Lazy (ByteString )
23
29
import Data.ByteString.Lazy.Char8 ()
@@ -29,24 +35,40 @@ import qualified Text.Parsec.Token as P
29
35
30
36
import Language.Egison.Types
31
37
import Language.Egison.Desugar
38
+ import Paths_egison (getDataFileName )
32
39
33
40
doParse :: Parser a -> String -> Either EgisonError a
34
41
doParse p input = either (throwError . fromParsecError) return $ parse p " egison" $ B. pack input
35
42
where
36
43
fromParsecError :: ParseError -> EgisonError
37
44
fromParsecError = Parser . show
38
45
39
- readTopExprs :: String -> Fresh (Either EgisonError [EgisonTopExpr ])
40
- readTopExprs = runDesugarM . either throwError (mapM desugarTopExpr) . parseTopExprs
46
+ readTopExprs :: String -> EgisonM [EgisonTopExpr ]
47
+ readTopExprs = liftEgisonM . runDesugarM . either throwError (mapM desugarTopExpr) . parseTopExprs
48
+
49
+ readTopExpr :: String -> EgisonM EgisonTopExpr
50
+ readTopExpr = liftEgisonM . runDesugarM . either throwError desugarTopExpr . parseTopExpr
41
51
42
- readTopExpr :: String -> Fresh ( Either EgisonError EgisonTopExpr )
43
- readTopExpr = runDesugarM . either throwError desugarTopExpr . parseTopExpr
52
+ readExprs :: String -> EgisonM [ EgisonExpr ]
53
+ readExprs = liftEgisonM . runDesugarM . either throwError ( mapM desugar) . parseExprs
44
54
45
- readExprs :: String -> Fresh (Either EgisonError [EgisonExpr ])
46
- readExprs = runDesugarM . either throwError (mapM desugar) . parseExprs
55
+ readExpr :: String -> EgisonM EgisonExpr
56
+ readExpr = liftEgisonM . runDesugarM . either throwError desugar . parseExpr
57
+
58
+ loadFile :: FilePath -> EgisonM [EgisonTopExpr ]
59
+ loadFile file = do
60
+ doesExist <- liftIO $ doesFileExist file
61
+ unless doesExist $ throwError $ strMsg (" file does not exist: " ++ file)
62
+ input <- liftIO $ readFile file
63
+ exprs <- readTopExprs input
64
+ concat <$> mapM recursiveLoad exprs
65
+ where
66
+ recursiveLoad (Load file) = loadLibraryFile file
67
+ recursiveLoad (LoadFile file) = loadFile file
68
+ recursiveLoad expr = return [expr]
47
69
48
- readExpr :: String -> Fresh ( Either EgisonError EgisonExpr )
49
- readExpr = runDesugarM . either throwError desugar . parseExpr
70
+ loadLibraryFile :: FilePath -> EgisonM [ EgisonTopExpr ]
71
+ loadLibraryFile file = liftIO (getDataFileName file) >>= loadFile
50
72
51
73
parseTopExprs :: String -> Either EgisonError [EgisonTopExpr ]
52
74
parseTopExprs = doParse $ whiteSpace >> endBy topExpr whiteSpace
0 commit comments