-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
-#else
- new_buf <- fillReadBuffer fd True buf
-#endif
- hGetcBuffered fd ref new_buf)
- `catch` \e -> if isEOFError e
- then return '\xFFFF'
- else ioError e
- case ch of
- '\xFFFF' -> return (chunk, I# off)
-#endif
- '\t' -> tabIt c off
- ch -> do writeCharOffPtr chunk (I# off) ch
- let c' | ch == '\n' = 0#
- | otherwise = c +# 1#
- slurp c' (off +# 1#)
-
- tabIt :: Int# -> Int# -> IO (Ptr (), Int)
- -- can't run out of buffer in here, because we reserved an
- -- extra tAB_SIZE bytes at the end earlier.
- tabIt c off = do
- writeCharOffPtr chunk (I# off) ' '
- let c' = c +# 1#
- off' = off +# 1#
- if c' `remInt#` tAB_SIZE ==# 0#
- then slurp c' off'
- else tabIt c' off'
- in do
-
- -- allow space for a full tab at the end of the buffer
- -- (that's what the max_off thing is for),
- -- and add 1 to allow room for the final sentinel \NUL at
- -- the end of the file.
- (chunk', rc) <- slurpFile 0# 0# chunk chunk_sz (chunk_sz -# (tAB_SIZE +# 1#))
- return (chunk', rc+1 {- room for sentinel -})
-
-
-reAllocMem :: Ptr () -> Int -> IO (Ptr ())
-reAllocMem ptr sz = do
- chunk <- c_realloc ptr sz
- if chunk == nullPtr
- then fail "reAllocMem"
- else return chunk
-
-allocMem :: Int -> IO (Ptr ())
-allocMem sz = do
- chunk <- c_malloc sz
- if chunk == nullPtr
-#if __GLASGOW_HASKELL__ < 501
- then constructErrorAndFail "allocMem"
-#else
- then ioException (IOError Nothing ResourceExhausted "malloc"
- "out of memory" Nothing)
-#endif
- else return chunk
-
-#if __GLASGOW_HASKELL__ <= 408
-c_malloc sz = do A# a <- c_malloc' sz; return (Ptr a)
-foreign import ccall "malloc" unsafe
- c_malloc' :: Int -> IO Addr
-
-c_realloc (Ptr a) sz = do A# a <- c_realloc' (A# a) sz; return (Ptr a)
-foreign import ccall "realloc" unsafe
- c_realloc' :: Addr -> Int -> IO Addr
-#else
-foreign import ccall "malloc" unsafe
- c_malloc :: Int -> IO (Ptr a)
-
-foreign import ccall "realloc" unsafe
- c_realloc :: Ptr a -> Int -> IO (Ptr a)
-#endif
-\end{code}
-
-Lookup
-
-\begin{code}
-currentChar :: StringBuffer -> Char
-currentChar sb = case currentChar# sb of c -> C# c
-
-lookAhead :: StringBuffer -> Int -> Char
-lookAhead sb (I# i#) = case lookAhead# sb i# of c -> C# c
-
-indexSBuffer :: StringBuffer -> Int -> Char
-indexSBuffer sb (I# i#) = case indexSBuffer# sb i# of c -> C# c
-
-currentChar# :: StringBuffer -> Char#
-indexSBuffer# :: StringBuffer -> Int# -> Char#
-lookAhead# :: StringBuffer -> Int# -> Char#
-currentChar# (StringBuffer fo# _ _ current#) = indexCharOffAddr# fo# current#
-indexSBuffer# (StringBuffer fo# _ _ _) i# = indexCharOffAddr# fo# i#
-
- -- relative lookup, i.e, currentChar = lookAhead 0
-lookAhead# (StringBuffer fo# _ _ c#) i# = indexCharOffAddr# fo# (c# +# i#)
-
-currentIndex# :: StringBuffer -> Int#
-currentIndex# (StringBuffer fo# _ _ c#) = c#
-
-lexemeIndex :: StringBuffer -> Int#
-lexemeIndex (StringBuffer fo# _ c# _) = c#
-\end{code}
-
- moving the start point of the current lexeme.
-
-\begin{code}
- -- moving the end point of the current lexeme.
-setCurrentPos# :: StringBuffer -> Int# -> StringBuffer
-setCurrentPos# (StringBuffer fo l# s# c#) i# =
- StringBuffer fo l# s# (c# +# i#)
-
--- augmenting the current lexeme by one.
-incLexeme :: StringBuffer -> StringBuffer
-incLexeme (StringBuffer fo l# s# c#) = StringBuffer fo l# s# (c# +# 1#)
-
-decLexeme :: StringBuffer -> StringBuffer
-decLexeme (StringBuffer fo l# s# c#) = StringBuffer fo l# s# (c# -# 1#)
-
-\end{code}
-
--- move the start and end point of the buffer on by
--- x units.
-
-\begin{code}