2 % (c) The GRASP/AQUA Project, Glasgow University, 1997
4 \section{String buffers}
6 Buffers for scanning string input stored in external arrays.
9 {-# OPTIONS -fno-prune-tydecls #-}
15 hGetStringBuffer, -- :: FilePath -> IO StringBuffer
18 currentChar, -- :: StringBuffer -> Char
19 currentChar#, -- :: StringBuffer -> Char#
20 indexSBuffer, -- :: StringBuffer -> Int -> Char
21 indexSBuffer#, -- :: StringBuffer -> Int# -> Char#
22 -- relative lookup, i.e, currentChar = lookAhead 0
23 lookAhead, -- :: StringBuffer -> Int -> Char
24 lookAhead#, -- :: StringBuffer -> Int# -> Char#
26 -- moving the end point of the current lexeme.
27 setCurrentPos#, -- :: StringBuffer -> Int# -> StringBuffer
28 incLexeme, -- :: StringBuffer -> StringBuffer
29 decLexeme, -- :: StringBuffer -> StringBuffer
31 -- move the start and end lexeme pointer on by x units.
32 stepOn, -- :: StringBuffer -> StringBuffer
33 stepOnBy#, -- :: StringBuffer -> Int# -> StringBuffer
34 stepOnTo#, -- :: StringBuffer -> Int# -> StringBuffer
35 stepOnUntil, -- :: (Char -> Bool) -> StringBuffer -> StringBuffer
36 stepOverLexeme, -- :: StringBuffer -> StringBuffer
37 scanNumLit, -- :: Int -> StringBuffer -> (Int, StringBuffer)
38 expandWhile, -- :: (Char -> Bool) -> StringBuffer -> StringBuffer
39 expandUntilMatch, -- :: StrinBuffer -> String -> StringBuffer
40 -- at or beyond end of buffer?
41 bufferExhausted, -- :: StringBuffer -> Bool
42 emptyLexeme, -- :: StringBuffer -> Bool
45 prefixMatch, -- :: StringBuffer -> String -> Bool
46 untilEndOfString#, -- :: StringBuffer -> Int#
47 untilEndOfChar#, -- :: StringBuffer -> Int#
48 untilChar#, -- :: StringBuffer -> Char# -> Int#
51 lexemeToString, -- :: StringBuffer -> String
52 lexemeToByteArray, -- :: StringBuffer -> _ByteArray Int
53 lexemeToFastString, -- :: StringBuffer -> FastString
54 lexemeToBuffer, -- :: StringBuffer -> StringBuffer
60 #include "HsVersions.h"
63 import Addr ( Addr(..) )
67 #if __GLASGOW_HASKELL__ >= 303
68 import IO ( slurpFile )
70 import IO ( openFile, hFileSize, hClose, IOMode(..) )
73 #if __GLASGOW_HASKELL__ < 301
74 import IOBase ( IOError(..), IOErrorType(..) )
75 import IOHandle ( readHandle, writeHandle, filePtr )
76 import PackBase ( unpackCStringBA )
78 # if __GLASGOW_HASKELL__ <= 302
79 import PrelIOBase ( IOError(..), IOErrorType(..) )
80 import PrelHandle ( readHandle, writeHandle, filePtr )
82 import PrelPack ( unpackCStringBA )
100 instance Text StringBuffer where
101 showsPrec _ s = showString ""
105 hGetStringBuffer :: FilePath -> IO StringBuffer
106 hGetStringBuffer fname =
107 #if __GLASGOW_HASKELL__ >= 303
108 slurpFile fname >>= \ (a , read) ->
112 _casm_ `` ((char *)%0)[(int)%1]=(char)0; '' a (I# (read# -# 1#)) >>= \ () ->
113 return (StringBuffer a# read# 0# 0#)
115 openFile fname ReadMode >>= \ hndl ->
116 hFileSize hndl >>= \ len@(J# _ _ d#) ->
117 let len_i = fromInteger len in
118 -- Allocate an array for system call to store its bytes into.
119 -- ToDo: make it robust
120 -- trace (show ((len_i::Int)+1)) $
121 _casm_ `` %r=(char *)malloc(sizeof(char)*(int)%0); '' (len_i::Int) >>= \ arr@(A# a#) ->
122 if addr2Int# a# ==# 0# then
123 fail (userError ("hGetStringBuffer: Could not allocate "++show len_i ++ " bytes"))
125 readHandle hndl >>= \ hndl_ ->
126 writeHandle hndl hndl_ >>
127 let ptr = filePtr hndl_ in
128 #if __GLASGOW_HASKELL__ <= 302
129 _ccall_ fread arr (1::Int) len_i (ptr::ForeignObj) >>= \ (I# read#) ->
131 _ccall_ fread arr (1::Int) len_i (ptr::Addr) >>= \ (I# read#) ->
134 if read# ==# 0# then -- EOF or some other error
135 fail (userError ("hGetStringBuffer: failed to slurp in interface file "++fname))
137 -- Add a sentinel NUL
138 _casm_ `` ((char *)%0)[(int)%1]=(char)0; '' arr (I# (read# -# 1#)) >>= \ () ->
139 return (StringBuffer a# read# 0# 0#)
143 unsafeWriteBuffer :: StringBuffer -> Int# -> Char# -> StringBuffer
144 unsafeWriteBuffer s@(StringBuffer a _ _ _) i# ch# =
146 _casm_ `` ((char *)%0)[(int)%1]=(char)%2; '' (A# a) (I# i#) (C# ch#) >>= \ () ->
154 currentChar :: StringBuffer -> Char
155 currentChar sb = case currentChar# sb of c -> C# c
157 lookAhead :: StringBuffer -> Int -> Char
158 lookAhead sb (I# i#) = case lookAhead# sb i# of c -> C# c
160 indexSBuffer :: StringBuffer -> Int -> Char
161 indexSBuffer sb (I# i#) = case indexSBuffer# sb i# of c -> C# c
163 currentChar# :: StringBuffer -> Char#
164 indexSBuffer# :: StringBuffer -> Int# -> Char#
165 lookAhead# :: StringBuffer -> Int# -> Char#
166 currentChar# (StringBuffer fo# _ _ current#) = indexCharOffAddr# fo# current#
167 indexSBuffer# (StringBuffer fo# _ _ _) i# = indexCharOffAddr# fo# i#
169 -- relative lookup, i.e, currentChar = lookAhead 0
170 lookAhead# (StringBuffer fo# _ _ c#) i# = indexCharOffAddr# fo# (c# +# i#)
174 moving the start point of the current lexeme.
177 -- moving the end point of the current lexeme.
178 setCurrentPos# :: StringBuffer -> Int# -> StringBuffer
179 setCurrentPos# (StringBuffer fo l# s# c#) i# =
180 StringBuffer fo l# s# (c# +# i#)
182 -- augmenting the current lexeme by one.
183 incLexeme :: StringBuffer -> StringBuffer
184 incLexeme (StringBuffer fo l# s# c#) = StringBuffer fo l# s# (c# +# 1#)
186 decLexeme :: StringBuffer -> StringBuffer
187 decLexeme (StringBuffer fo l# s# c#) = StringBuffer fo l# s# (c# -# 1#)
191 -- move the start and end point of the buffer on by
195 stepOn :: StringBuffer -> StringBuffer
196 stepOn (StringBuffer fo l# s# c#) = StringBuffer fo l# (s# +# 1#) (s# +# 1#) -- assume they're the same.
198 stepOnBy# :: StringBuffer -> Int# -> StringBuffer
199 stepOnBy# (StringBuffer fo# l# s# c#) i# =
201 new_s# -> StringBuffer fo# l# new_s# new_s#
204 stepOnTo# :: StringBuffer -> Int# -> StringBuffer
205 stepOnTo# (StringBuffer fo l _ _) s# = StringBuffer fo l s# s#
207 stepOnUntil :: (Char -> Bool) -> StringBuffer -> StringBuffer
209 stepOnUntil pred (StringBuffer fo l# s# c#) =
213 case indexCharOffAddr# fo c# of
214 ch# | pred (C# ch#) -> StringBuffer fo l# c# c#
215 | ch# `eqChar#` '\NUL'# && c# >=# l# -> StringBuffer fo l# l# l# -- EOB, return immediately.
216 | otherwise -> loop (c# +# 1#)
218 stepOverLexeme :: StringBuffer -> StringBuffer
219 stepOverLexeme (StringBuffer fo l s# c#) = StringBuffer fo l c# c#
221 expandWhile :: (Char -> Bool) -> StringBuffer -> StringBuffer
222 expandWhile pred (StringBuffer fo l# s# c#) =
226 case indexCharOffAddr# fo c# of
227 ch# | pred (C# ch#) -> loop (c# +# 1#)
228 | ch# `eqChar#` '\NUL'# && c# >=# l# -> StringBuffer fo l# l# l# -- EOB, return immediately.
229 | otherwise -> StringBuffer fo l# s# c#
232 scanNumLit :: Int -> StringBuffer -> (Int,StringBuffer)
233 scanNumLit (I# acc#) (StringBuffer fo l# s# c#) =
237 case indexCharOffAddr# fo c# of
238 ch# | isDigit (C# ch#) -> loop (acc# *# 10# +# (ord# ch# -# ord# '0'#)) (c# +# 1#)
239 | ch# `eqChar#` '\NUL'# && c# >=# l# -> (I# acc#, StringBuffer fo l# l# l#) -- EOB, return immediately.
240 | otherwise -> (I# acc#,StringBuffer fo l# s# c#)
243 expandUntilMatch :: StringBuffer -> String -> StringBuffer
244 expandUntilMatch (StringBuffer fo l# s# c#) str =
247 loop c# [] = StringBuffer fo l# s# c#
249 | indexCharOffAddr# fo c# `eqChar#` x#
252 = loop (c# +# 1#) str
257 -- at or beyond end of buffer?
258 bufferExhausted :: StringBuffer -> Bool
259 bufferExhausted (StringBuffer fo l# _ c#) = c# >=# l#
261 emptyLexeme :: StringBuffer -> Bool
262 emptyLexeme (StringBuffer fo l# s# c#) = s# ==# c#
265 prefixMatch :: StringBuffer -> String -> Maybe StringBuffer
266 prefixMatch (StringBuffer fo l# s# c#) str =
269 loop c# [] = Just (StringBuffer fo l# s# c#)
271 | indexCharOffAddr# fo c# `eqChar#` x#
276 untilEndOfString# :: StringBuffer -> StringBuffer
277 untilEndOfString# (StringBuffer fo l# s# c#) =
280 getch# i# = indexCharOffAddr# fo i#
285 case getch# (c# -# 1#) of
287 -- looks like an escaped something or other to me,
288 -- better count the number of "\\"s that are immediately
289 -- preceeding to decide if the " is escaped.
293 '\\'# -> odd_slashes (not flg) (i# -# 1#)
296 if odd_slashes True (c# -# 2#) then
297 -- odd number, " is ecaped.
299 else -- a real end of string delimiter after all.
300 StringBuffer fo l# s# c#
301 _ -> StringBuffer fo l# s# c#
303 if c# >=# l# then -- hit sentinel, this doesn't look too good..
304 StringBuffer fo l# l# l#
310 untilEndOfChar# :: StringBuffer -> StringBuffer
311 untilEndOfChar# (StringBuffer fo l# s# c#) =
314 getch# i# = indexCharOffAddr# fo i#
319 case getch# (c# -# 1#) of
321 case getch# (c# -# 2#) of
322 '\\'# -> -- end of char
323 StringBuffer fo l# s# c#
324 _ -> loop (c# +# 1#) -- false alarm
325 _ -> StringBuffer fo l# s# c#
327 if c# >=# l# then -- hit sentinel, this doesn't look too good..
328 StringBuffer fo l# l# l#
333 untilChar# :: StringBuffer -> Char# -> StringBuffer
334 untilChar# (StringBuffer fo l# s# c#) x# =
338 | indexCharOffAddr# fo c# `eqChar#` x#
339 = StringBuffer fo l# s# c#
344 lexemeToString :: StringBuffer -> String
345 lexemeToString (StringBuffer fo _ start_pos# current#) =
346 if start_pos# ==# current# then
349 unpackCStringBA (copySubStr (A# fo) (I# start_pos#) (I# (current# -# start_pos#)))
351 lexemeToByteArray :: StringBuffer -> _ByteArray Int
352 lexemeToByteArray (StringBuffer fo _ start_pos# current#) =
353 if start_pos# ==# current# then
354 error "lexemeToByteArray"
356 copySubStr (A# fo) (I# start_pos#) (I# (current# -# start_pos#))
358 lexemeToFastString :: StringBuffer -> FastString
359 lexemeToFastString (StringBuffer fo l# start_pos# current#) =
360 if start_pos# ==# current# then
361 mkFastCharString2 (A# fo) (I# 0#)
363 mkFastSubString (A# fo) (I# start_pos#) (I# (current# -# start_pos#))
366 Create a StringBuffer from the current lexeme, and add a sentinel
367 at the end. Know What You're Doing before taking this function
370 lexemeToBuffer :: StringBuffer -> StringBuffer
371 lexemeToBuffer (StringBuffer fo l# start_pos# current#) =
372 if start_pos# ==# current# then
373 StringBuffer fo 0# start_pos# current# -- an error, really.
375 unsafeWriteBuffer (StringBuffer fo (current# -# start_pos#) start_pos# start_pos#)