- (a, read) <- slurpFileExpandTabs fname
-
- -- urk! slurpFile gives us a buffer that doesn't have room for
- -- the sentinel. Assume it has a final newline for now, and overwrite
- -- that with the sentinel. slurpFileExpandTabs (below) leaves room
- -- for the sentinel.
- let (Ptr a#) = a;
- (I# read#) = read;
- end# = read# -# 1#
-
- -- add sentinel '\NUL'
- writeCharOffPtr a (I# end#) '\0'
-
- return (StringBuffer a# end# 0# 0#)
-\end{code}
-
------------------------------------------------------------------------------
--- Turn a String into a StringBuffer
-
-\begin{code}
-stringToStringBuffer :: String -> IO StringBuffer
-freeStringBuffer :: StringBuffer -> IO ()
-
-#if __GLASGOW_HASKELL__ >= 502
-stringToStringBuffer str = do
- let sz@(I# sz#) = length str
- Ptr a# <- newCString str
- return (StringBuffer a# sz# 0# 0#)
-
-freeStringBuffer (StringBuffer a# _ _ _) = Foreign.free (Ptr a#)
-#else
-stringToStringBuffer = panic "stringToStringBuffer: not implemented"
-freeStringBuffer sb = return ()
-#endif
-
-\end{code}
-
------------------------------------------------------------------------------
-This very disturbing bit of code is used for expanding the tabs in a
-file before we start parsing it. Expanding the tabs early makes the
-lexer a lot simpler: we only have to record the beginning of the line
-in order to be able to calculate the column offset of the current
-token.
-
-We guess the size of the buffer required as 20% extra for
-expanded tabs, and enlarge it if necessary.
-
-\begin{code}
-#if __GLASGOW_HASKELL__ < 501
-getErrType :: IO Int
-getErrType = _ccall_ getErrType__
-#endif
-
-slurpFileExpandTabs :: FilePath -> IO (Ptr (),Int)
-slurpFileExpandTabs fname = do
- bracket (openFile fname ReadMode) (hClose)
- (\ handle ->
- do sz <- hFileSize handle
- if sz > toInteger (maxBound::Int)
- then ioError (userError "slurpFile: file too big")
- else do
- let sz_i = fromInteger sz
- if sz_i == 0
- -- empty file: just allocate a buffer containing '\0'
- then do chunk <- allocMem 1
- writeCharOffPtr chunk 0 '\0'
- return (chunk, 0)
- else do let sz_i' = (sz_i * 12) `div` 10 -- add 20% for tabs
- chunk <- allocMem sz_i'
- trySlurp handle sz_i' chunk
- )
-
-trySlurp :: Handle -> Int -> Ptr () -> IO (Ptr (), Int)
-trySlurp handle sz_i chunk =
-#if __GLASGOW_HASKELL__ < 501
- wantReadableHandle "hGetChar" handle $ \ handle_ ->
- let fo = haFO__ handle_ in
-#else
- wantReadableHandle "hGetChar" handle $
- \ handle_@Handle__{ haFD=fd, haBuffer=ref, haBufferMode=mode } ->
-#endif
- let
- (I# chunk_sz) = sz_i
-
- tAB_SIZE = 8#
-
- slurpFile :: Int# -> Int# -> Ptr () -> Int# -> Int# -> IO (Ptr (), Int)
- slurpFile c off chunk chunk_sz max_off = slurp c off
- where
-
- slurp :: Int# -> Int# -> IO (Ptr (), Int)
- slurp c off | off >=# max_off = do
- let new_sz = chunk_sz *# 2#
- chunk' <- reAllocMem chunk (I# new_sz)
- slurpFile c off chunk' new_sz (new_sz -# (tAB_SIZE +# 1#))
- slurp c off = do
-#if __GLASGOW_HASKELL__ < 501
- intc <- mayBlock fo (_ccall_ fileGetc fo)
- if intc == ((-1)::Int)
- then do errtype <- getErrType
- if errtype == (19{-ERR_EOF-} :: Int)
- then return (chunk, I# off)
- else constructErrorAndFail "slurpFile"
- else case chr intc of
-#else
- buf <- readIORef ref
- ch <- (if not (bufferEmpty buf)
- then hGetcBuffered fd ref buf
- else do
-#if __GLASGOW_HASKELL__ >= 503
- new_buf <- fillReadBuffer fd True False buf