on master help sign in
move getNChar and getLen to IdeSlave
a0ccc57Hannes Mehnert
src/Idris/IdeSlave.hs+13−1
@@ -1,6 +1,6 @@
1{-# LANGUAGE FlexibleInstances, IncoherentInstances, PatternGuards #-}
1{-# LANGUAGE FlexibleInstances, IncoherentInstances, PatternGuards #-}
2
2
3−module Idris.IdeSlave(parseMessage, convSExp, IdeSlaveCommand(..), sexpToCommand, toSExp, SExp(..), SExpable, Opt(..), ideSlaveEpoch) where
3+module Idris.IdeSlave(parseMessage, convSExp, IdeSlaveCommand(..), sexpToCommand, toSExp, SExp(..), SExpable, Opt(..), ideSlaveEpoch, getLen, getNChar) where
4
4
5import Text.Printf
5import Text.Printf
6import Numeric
6import Numeric
@@ -12,12 +12,24 @@ import qualified Data.ByteString.UTF8 as UTF8
12-- import qualified Data.Text as T
12-- import qualified Data.Text as T
13import Text.Trifecta hiding (Err)
13import Text.Trifecta hiding (Err)
14import Text.Trifecta.Delta
14import Text.Trifecta.Delta
15+import System.IO
15
16
16import Idris.Core.TT
17import Idris.Core.TT
17import Idris.Core.Binary
18import Idris.Core.Binary
18
19
19import Control.Applicative hiding (Const)
20import Control.Applicative hiding (Const)
20
21
22+getNChar :: Handle -> Int -> String -> IO (String)
23+getNChar _ 0 s = return (reverse s)
24+getNChar h n s = do c <- hGetChar h
25+ getNChar h (n - 1) (c : s)
26+
27+getLen :: Handle -> IO (Either Err Int)
28+getLen h = do s <- getNChar h 6 ""
29+ case readHex s of
30+ ((num, ""):_) -> return $ Right num
31+ _ -> return $ Left . Msg $ "Couldn't read length " ++ s
32+
21data SExp = SexpList [SExp]
33data SExp = SexpList [SExp]
22 | StringAtom String
34 | StringAtom String
23 | BoolAtom Bool
35 | BoolAtom Bool
src/Idris/REPL.hs+5−13
@@ -236,23 +236,15 @@ ideslaveStart s orig mods
236 _ -> return ()
236 _ -> return ()
237 ideslave h orig mods
237 ideslave h orig mods
238
238
239−getNChar :: Handle -> Int -> String -> IO (String)
240−getNChar _ 0 s = return (reverse s)
241−getNChar h n s = do c <- hGetChar h
242− getNChar h (n - 1) (c : s)
243−
244−getLen :: Handle -> Idris (Int)
245−getLen h = do s <- runIO $ getNChar h 6 ""
246− case readHex s of
247− ((num, ""):_) -> return num
248− _ -> ierror (Msg $ "Couldn't read length " ++ s)
249−
250ideslave :: Handle -> IState -> [FilePath] -> Idris ()
239ideslave :: Handle -> IState -> [FilePath] -> Idris ()
251ideslave h orig mods
240ideslave h orig mods
252 = do idrisCatch
241 = do idrisCatch
253 (do let inh = if h == stdout then stdin else h
242 (do let inh = if h == stdout then stdin else h
254− len <- getLen inh
243+ len' <- runIO $ IdeSlave.getLen inh
244+ len <- case len' of
245+ Left err -> ierror err
246+ Right n -> return n
255− l <- runIO $ getNChar inh len ""
247+ l <- runIO $ IdeSlave.getNChar inh len ""
256 (sexp, id) <- case IdeSlave.parseMessage l of
248 (sexp, id) <- case IdeSlave.parseMessage l of
257 Left err -> ierror err
249 Left err -> ierror err
258 Right (sexp, id) -> return (sexp, id)
250 Right (sexp, id) -> return (sexp, id)