[project @ 2001-03-01 15:59:51 by simonmar]
[ghc-hetmet.git] / ghc / compiler / utils / StringBuffer.lhs
index 1294556..713dc77 100644 (file)
@@ -6,13 +6,15 @@
 Buffers for scanning string input stored in external arrays.
 
 \begin{code}
-{-# OPTIONS -fno-prune-tydecls #-}
+
 module StringBuffer
        (
         StringBuffer,
 
-        -- creation
-        hGetStringBuffer,  -- :: FilePath       -> IO StringBuffer
+        -- creation/destruction
+        hGetStringBuffer,     -- :: FilePath     -> IO StringBuffer
+       stringToStringBuffer, -- :: String       -> IO StringBuffer
+       freeStringBuffer,     -- :: StringBuffer -> IO ()
 
          -- Lookup
        currentChar,      -- :: StringBuffer -> Char
@@ -37,6 +39,7 @@ module StringBuffer
         stepOnBy#,        -- :: StringBuffer -> Int# -> StringBuffer
         stepOnTo#,        -- :: StringBuffer -> Int# -> StringBuffer
         stepOnUntil,      -- :: (Char -> Bool) -> StringBuffer -> StringBuffer
+       stepOnUntilChar#, -- :: StringBuffer -> Char# -> StringBuffer
         stepOverLexeme,   -- :: StringBuffer   -> StringBuffer
        scanNumLit,       -- :: Int -> StringBuffer -> (Int, StringBuffer)
        squeezeLexeme,    -- :: StringBuffer -> Int# -> StringBuffer
@@ -51,7 +54,6 @@ module StringBuffer
         -- matching
         prefixMatch,       -- :: StringBuffer -> String -> Bool
        untilEndOfString#, -- :: StringBuffer -> Int#
-       untilChar#,        -- :: StringBuffer -> Char# -> Int#
 
          -- conversion
         lexemeToString,     -- :: StringBuffer -> String
@@ -66,16 +68,24 @@ module StringBuffer
 #include "HsVersions.h"
 
 import GlaExts
-import Addr            ( Addr(..) )
+#if __GLASGOW_HASKELL__ < 411
+import PrelAddr        ( Addr(..) )
+#else
+import Addr            ( Addr(..) )
+#endif
 import Foreign
-import ST
 import Char            ( chr )
+import Panic           ( panic )
 
 -- urk!
-#include "../lib/std/cbits/error.h"
+#include "../lib/std/cbits/stgerror.h"
 
 #if __GLASGOW_HASKELL__ >= 303
-import IO              ( openFile, slurpFile )
+import IO              ( openFile
+#if __GLASGOW_HASKELL__ < 407
+                        , slurpFile   -- comes from PrelHandle or IOExts now
+#endif
+                        )
 import PrelIOBase
 import PrelHandle
 import Addr
@@ -83,9 +93,13 @@ import Addr
 import IO              ( openFile, hFileSize, hClose, IOMode(..) )
 import Addr
 #endif
+#if __GLASGOW_HASKELL__ >= 411
+import Ptr             ( Ptr(..) )
+#endif
 
 #if __GLASGOW_HASKELL__ < 301
-import IOBase          ( IOError(..), IOErrorType(..) )
+import IOBase          ( Handle, IOError(..), IOErrorType(..),
+                         constructErrorAndFail )
 import IOHandle                ( readHandle, writeHandle, filePtr )
 import PackBase        ( unpackCStringBA )
 #else
@@ -118,7 +132,7 @@ data StringBuffer
 \end{code}
 
 \begin{code}
-instance Text StringBuffer where
+instance Show StringBuffer where
        showsPrec _ s = showString ""
 \end{code}
 
@@ -127,7 +141,13 @@ hGetStringBuffer :: Bool -> FilePath -> IO StringBuffer
 hGetStringBuffer expand_tabs fname = do
    (a, read) <- if expand_tabs 
                                then slurpFileExpandTabs fname 
+#if __GLASGOW_HASKELL__ < 411
                                else slurpFile fname
+#else
+                               else do
+                                   (Ptr a#, read) <- slurpFile fname
+                                   return (A# a#, read)
+#endif
 
    let (A# a#) = a;  (I# read#) = read
 
@@ -171,6 +191,34 @@ unsafeWriteBuffer s@(StringBuffer a _ _ _) i# ch# =
 \end{code}
 
 -----------------------------------------------------------------------------
+-- Turn a String into a StringBuffer
+
+\begin{code}
+stringToStringBuffer :: String -> IO StringBuffer
+freeStringBuffer :: StringBuffer -> IO ()
+
+#if __GLASGOW_HASKELL__ >= 411
+stringToStringBuffer str =
+  do let sz@(I# sz#) = length str + 1
+     (Ptr a#) <- mallocBytes sz
+     fill_in str (A# a#)
+     writeCharOffAddr (A# a#) (sz-1) '\0'              -- sentinel
+     return (StringBuffer a# sz# 0# 0#)
+ where
+  fill_in [] _ = return ()
+  fill_in (c:cs) a = do
+    writeCharOffAddr a 0 c 
+    fill_in cs (a `plusAddr` 1)
+
+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
@@ -182,7 +230,6 @@ expanded tabs, and enlarge it if necessary.
 
 \begin{code}
 #if __GLASGOW_HASKELL__ < 303
-ioError = fail
 mayBlock fo thing = thing
 
 writeCharOffAddr :: Addr -> Int -> Char -> IO ()
@@ -203,7 +250,7 @@ slurpFileExpandTabs fname = do
    (\ handle ->
      do sz <- hFileSize handle
         if sz > toInteger (maxBound::Int) 
-         then ioError (userError "slurpFile: file too big")
+         then IOERROR (userError "slurpFile: file too big")
           else do
            let sz_i = fromInteger sz
                sz_i' = (sz_i * 12) `div` 10            -- add 20% for tabs
@@ -213,7 +260,10 @@ slurpFileExpandTabs fname = do
 
 trySlurp :: Handle -> Int -> Addr -> IO (Addr, Int)
 trySlurp handle sz_i chunk =
-#if __GLASGOW_HASKELL__ >= 303
+#if __GLASGOW_HASKELL__ == 303
+  wantReadableHandle "hGetChar" handle >>= \ handle_ ->
+  let fo = haFO__ handle_ in
+#elif __GLASGOW_HASKELL__ > 303
   wantReadableHandle "hGetChar" handle $ \ handle_ ->
   let fo = haFO__ handle_ in
 #else
@@ -225,21 +275,21 @@ trySlurp handle sz_i chunk =
 
        tAB_SIZE = 8#
 
-       slurpFile :: Int# -> Int# -> Addr -> Int# -> Int# -> IO Int
+       slurpFile :: Int# -> Int# -> Addr -> Int# -> Int# -> IO (Addr, Int)
        slurpFile c off chunk chunk_sz max_off = slurp c off
         where
 
-         slurp :: Int# -> Int# -> IO Int
+         slurp :: Int# -> Int# -> IO (Addr, 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)
+               slurpFile c off chunk' new_sz (new_sz -# (tAB_SIZE +# 1#))
          slurp c off = do
                intc <- mayBlock fo (_ccall_ fileGetc fo)
                if intc == ((-1)::Int)
                  then do errtype <- getErrType
                          if errtype == (ERR_EOF :: Int)
-                           then return (I# off)
+                           then return (chunk, I# off)
                            else constructErrorAndFail "slurpFile"
                  else case chr intc of
                         '\t' -> tabIt c off
@@ -248,7 +298,7 @@ trySlurp handle sz_i chunk =
                                            | otherwise  = c +# 1#
                                     slurp c' (off +# 1#)
 
-         tabIt :: Int# -> Int# -> IO Int
+         tabIt :: Int# -> Int# -> IO (Addr, 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
@@ -261,34 +311,37 @@ trySlurp handle sz_i chunk =
   in do
 
        -- allow space for a full tab at the end of the buffer
-       -- (that's what the max_off thing is for)
-  rc <- slurpFile 0# 0# chunk chunk_sz (chunk_sz -# tAB_SIZE)
+       -- (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#))
+#if __GLASGOW_HASKELL__ < 404
   writeHandle handle handle_
+#endif
   if rc < (0::Int)
        then constructErrorAndFail "slurpFile"
-       else return (chunk, rc)
+       else return (chunk', rc+1 {-room for sentinel-})
 
 
 reAllocMem :: Addr -> Int -> IO Addr
 reAllocMem ptr sz = do
    chunk <- _ccall_ realloc ptr sz
    if chunk == nullAddr 
-#if __GLASGOW_HASKELL__ < 303
-      then fail (userError "reAllocMem")
-#else
+#if __GLASGOW_HASKELL__ >= 400
       then fail "reAllocMem"
+#else
+      then fail (userError "reAllocMem")
 #endif
       else return chunk
 
 allocMem :: Int -> IO Addr
 allocMem sz = do
-#if __GLASGOW_HASKELL__ < 303
    chunk <- _ccall_ malloc sz
+#if __GLASGOW_HASKELL__ < 303
    if chunk == nullAddr 
       then fail (userError "allocMem")
       else return chunk
 #else
-   chunk <- _ccall_ allocMemory__ sz
    if chunk == nullAddr 
       then constructErrorAndFail "allocMem"
       else return chunk
@@ -408,16 +461,16 @@ scanNumLit acc (StringBuffer fo l# s# c#) =
          | otherwise        -> (acc,StringBuffer fo l# s# c#)
 
 
-expandUntilMatch :: StringBuffer -> String -> StringBuffer
+expandUntilMatch :: StringBuffer -> String -> Maybe StringBuffer
 expandUntilMatch (StringBuffer fo l# s# c#) str =
   loop c# str
   where
-   loop c# [] = StringBuffer fo l# s# c#
-   loop c# ((C# x#):xs)
-      | indexCharOffAddr# fo c# `eqChar#` x#
-      = loop (c# +# 1#) xs
-      | otherwise 
-      = loop (c# +# 1#) str
+   loop c# [] = Just (StringBuffer fo l# s# c#)
+   loop c# ((C# x#):xs) =
+    case indexCharOffAddr# fo c# of
+      ch# | ch# `eqChar#` '\NUL'# && c# >=# l# -> Nothing
+         | ch# `eqChar#` x# -> loop (c# +# 1#) xs
+          | otherwise        -> loop (c# +# 1#) str
        
 \end{code}
 
@@ -475,13 +528,13 @@ untilEndOfString# (StringBuffer fo l# s# c#) =
     _ -> loop (c# +# 1#)
 
 
-untilChar# :: StringBuffer -> Char# -> StringBuffer
-untilChar# (StringBuffer fo l# s# c#) x# = 
+stepOnUntilChar# :: StringBuffer -> Char# -> StringBuffer
+stepOnUntilChar# (StringBuffer fo l# s# c#) x# = 
  loop c# 
  where
   loop c#
    | c# >=# l# || indexCharOffAddr# fo c# `eqChar#` x#
-   = StringBuffer fo l# s# c#
+   = StringBuffer fo l# c# c#
    | otherwise
    = loop (c# +# 1#)
 
@@ -493,7 +546,7 @@ lexemeToString (StringBuffer fo _ start_pos# current#) =
  else
     unpackCStringBA (copySubStr (A# fo) (I# start_pos#) (I# (current# -# start_pos#)))
     
-lexemeToByteArray :: StringBuffer -> _ByteArray Int
+lexemeToByteArray :: StringBuffer -> ByteArray Int
 lexemeToByteArray (StringBuffer fo _ start_pos# current#) = 
  if start_pos# ==# current# then
     error "lexemeToByteArray"