2 % (c) The GRASP/AQUA Project, Glasgow University, 1997
4 \section{String buffers}
6 Buffers for scanning string input stored in external arrays.
9 #include "HsVersions.h"
16 hGetStringBuffer, -- :: FilePath -> IO StringBuffer
17 freeStringBuffer, -- :: StringBuffer -> IO ()
20 currentChar, -- :: StringBuffer -> Char
21 currentChar#, -- :: StringBuffer -> Char#
22 indexSBuffer, -- :: StringBuffer -> Int -> Char
23 indexSBuffer#, -- :: StringBuffer -> Int# -> Char#
24 -- relative lookup, i.e, currentChar = lookAhead 0
25 lookAhead, -- :: StringBuffer -> Int -> Char
26 lookAhead#, -- :: StringBuffer -> Int# -> Char#
28 -- moving the end point of the current lexeme.
29 setCurrentPos#, -- :: StringBuffer -> Int# -> StringBuffer
30 incLexeme, -- :: StringBuffer -> StringBuffer
31 decLexeme, -- :: StringBuffer -> StringBuffer
33 -- move the start and end lexeme pointer on by x units.
34 stepOn, -- :: StringBuffer -> StringBuffer
35 stepOnBy#, -- :: StringBuffer -> Int# -> StringBuffer
36 stepOnTo#, -- :: StringBuffer -> Int# -> StringBuffer
37 stepOnUntil, -- :: (Char -> Bool) -> StringBuffer -> StringBuffer
38 stepOverLexeme, -- :: StringBuffer -> StringBuffer
39 scanNumLit, -- :: Int -> StringBuffer -> (Int, StringBuffer)
40 expandWhile, -- :: (Char -> Bool) -> StringBuffer -> StringBuffer
41 expandUntilMatch, -- :: StrinBuffer -> String -> StringBuffer
42 -- at or beyond end of buffer?
43 bufferExhausted, -- :: StringBuffer -> Bool
44 emptyLexeme, -- :: StringBuffer -> Bool
47 prefixMatch, -- :: StringBuffer -> String -> Bool
48 untilEndOfString#, -- :: StringBuffer -> Int#
49 untilEndOfChar#, -- :: StringBuffer -> Int#
50 untilChar#, -- :: StringBuffer -> Char# -> Int#
53 lexemeToString, -- :: StringBuffer -> String
54 lexemeToByteArray, -- :: StringBuffer -> _ByteArray Int
55 lexemeToFastString, -- :: StringBuffer -> FastString
56 lexemeToBuffer, -- :: StringBuffer -> StringBuffer
63 #if __GLASGOW_HASKELL__ <= 200
85 -- ForeignObj# -- the data
93 hGetStringBuffer :: FilePath -> IO StringBuffer
94 hGetStringBuffer fname =
95 -- _trace ("Renamer: opening " ++ fname)
96 openFile fname ReadMode >>= \ hndl ->
97 hFileSize hndl >>= \ len@(J# _ _ d#) ->
98 let len_i = fromInteger len in
99 -- Allocate an array for system call to store its bytes into.
100 -- ToDo: make it robust
101 -- _trace (show (len_i::Int)+1)
102 (_casm_ `` %r=(char *)malloc(sizeof(char)*(int)%0); '' (len_i::Int)) `CCALL_THEN` \ arr@(A# a#) ->
103 if addr2Int# a# ==# 0# then
104 failWith MkIOError(hndl,UserError,("hGetStringBuffer: Could not allocate "++show len_i ++ " bytes"))
107 -- _casm_ `` %r=NULL; '' `thenPrimIO` \ free_p ->
108 -- makeForeignObj arr free_p `thenPrimIO` \ fo@(_ForeignObj fo#) ->
109 _readHandle hndl >>= \ hndl_ ->
110 _writeHandle hndl hndl_ >>
111 let ptr = _filePtr hndl_ in
112 _ccall_ fread arr (1::Int) len_i ptr `CCALL_THEN` \ (I# read#) ->
113 -- _trace ("DEBUG: opened " ++ fname ++ show (I# read#)) $
115 if read# ==# 0# then -- EOF or other error
116 failWith MkIOError(hndl,UserError,"hGetStringBuffer: EOF reached or some other error")
118 -- Add a sentinel NUL
119 _casm_ `` ((char *)%0)[(int)%1]=(char)0; '' arr (I# (read# -# 1#)) `CCALL_THEN` \ () ->
120 return (StringBuffer a# read# 0# 0#)
122 freeStringBuffer :: StringBuffer -> IO ()
123 freeStringBuffer (StringBuffer a# _ _ _) =
124 _casm_ `` free((char *)%0); '' (A# a#) `CCALL_THEN` \ () ->
127 unsafeWriteBuffer :: StringBuffer -> Int# -> Char# -> StringBuffer
128 unsafeWriteBuffer s@(StringBuffer a _ _ _) i# ch# =
129 unsafePerformPrimIO (
130 _casm_ `` ((char *)%0)[(int)%1]=(char)%2; '' (A# a) (I# i#) (C# ch#) `thenPrimIO` \ () ->
138 currentChar# :: StringBuffer -> Char#
139 currentChar# (StringBuffer fo# _ _ current#) = indexCharOffAddr# fo# current#
141 currentChar :: StringBuffer -> Char
142 currentChar sb = case currentChar# sb of c -> C# c
144 indexSBuffer# :: StringBuffer -> Int# -> Char#
145 indexSBuffer# (StringBuffer fo# _ _ _) i# = indexCharOffAddr# fo# i#
147 indexSBuffer :: StringBuffer -> Int -> Char
148 indexSBuffer sb (I# i#) = case indexSBuffer# sb i# of c -> C# c
150 -- relative lookup, i.e, currentChar = lookAhead 0
151 lookAhead# :: StringBuffer -> Int# -> Char#
152 lookAhead# (StringBuffer fo# _ _ c#) i# = indexCharOffAddr# fo# (c# +# i#)
154 lookAhead :: StringBuffer -> Int -> Char
155 lookAhead sb (I# i#) = case lookAhead# sb i# of c -> C# c
159 moving the start point of the current lexeme.
162 -- moving the end point of the current lexeme.
163 setCurrentPos# :: StringBuffer -> Int# -> StringBuffer
164 setCurrentPos# (StringBuffer fo l# s# c#) i# =
165 StringBuffer fo l# s# (c# +# i#)
167 -- augmenting the current lexeme by one.
168 incLexeme :: StringBuffer -> StringBuffer
169 incLexeme (StringBuffer fo l# s# c#) = StringBuffer fo l# s# (c# +# 1#)
171 decLexeme :: StringBuffer -> StringBuffer
172 decLexeme (StringBuffer fo l# s# c#) = StringBuffer fo l# s# (c# -# 1#)
176 -- move the start and end point of the buffer on by
180 stepOn :: StringBuffer -> StringBuffer
181 stepOn (StringBuffer fo l# s# c#) = StringBuffer fo l# (s# +# 1#) (s# +# 1#) -- assume they're the same.
183 stepOnBy# :: StringBuffer -> Int# -> StringBuffer
184 stepOnBy# (StringBuffer fo# l# s# c#) i# =
186 new_s# -> StringBuffer fo# l# new_s# new_s#
189 stepOnTo# :: StringBuffer -> Int# -> StringBuffer
190 stepOnTo# (StringBuffer fo l _ _) s# = StringBuffer fo l s# s#
192 stepOnUntil :: (Char -> Bool) -> StringBuffer -> StringBuffer
193 stepOnUntil pred (StringBuffer fo l# s# c#) =
197 case indexCharOffAddr# fo c# of
198 ch# | pred (C# ch#) -> StringBuffer fo l# c# c#
199 | otherwise -> loop (c# +# 1#)
201 stepOverLexeme :: StringBuffer -> StringBuffer
202 stepOverLexeme (StringBuffer fo l s# c#) = StringBuffer fo l c# c#
204 expandWhile :: (Char -> Bool) -> StringBuffer -> StringBuffer
205 expandWhile pred (StringBuffer fo l# s# c#) =
209 case indexCharOffAddr# fo c# of
210 ch# | pred (C# ch#) -> loop (c# +# 1#)
211 | otherwise -> StringBuffer fo l# s# c#
214 scanNumLit :: Int -> StringBuffer -> (Int,StringBuffer)
215 scanNumLit (I# acc#) (StringBuffer fo l# s# c#) =
219 case indexCharOffAddr# fo c# of
220 ch# | isDigit (C# ch#) -> loop (acc# *# 10# +# (ord# ch# -# ord# '0'#)) (c# +# 1#)
221 | otherwise -> (I# acc#,StringBuffer fo l# s# c#)
224 expandUntilMatch :: StringBuffer -> String -> StringBuffer
225 expandUntilMatch (StringBuffer fo l# s# c#) str =
228 loop c# [] = StringBuffer fo l# s# c#
229 loop c# ((C# x#):xs) =
230 if indexCharOffAddr# fo c# `eqChar#` x# then
237 -- at or beyond end of buffer?
238 bufferExhausted :: StringBuffer -> Bool
239 bufferExhausted (StringBuffer fo l# _ c#) = c# >=# l#
241 emptyLexeme :: StringBuffer -> Bool
242 emptyLexeme (StringBuffer fo l# s# c#) = s# ==# c#
245 prefixMatch :: StringBuffer -> String -> Maybe StringBuffer
246 prefixMatch (StringBuffer fo l# s# c#) str =
249 loop c# [] = Just (StringBuffer fo l# s# c#)
250 loop c# ((C# x#):xs) =
251 if indexCharOffAddr# fo c# `eqChar#` x# then
256 untilEndOfString# :: StringBuffer -> StringBuffer
257 untilEndOfString# (StringBuffer fo l# s# c#) =
261 case indexCharOffAddr# fo c# of
263 case indexCharOffAddr# fo (c# -# 1#) of
265 -- looks like an escaped something or other to me,
266 -- better count the number of "\\"s that are immediately
267 -- preceeding to decide if the " is escaped.
270 case indexCharOffAddr# fo i# of
271 '\\'# -> odd_slashes (not flg) (i# -# 1#)
274 if odd_slashes True (c# -# 1#) then
275 -- odd number, " is ecaped.
277 else -- a real end of string delimiter after all.
278 StringBuffer fo l# s# c#
279 _ -> StringBuffer fo l# s# c#
283 untilEndOfChar# :: StringBuffer -> StringBuffer
284 untilEndOfChar# (StringBuffer fo l# s# c#) =
288 case indexCharOffAddr# fo c# of
290 case indexCharOffAddr# fo (c# -# 1#) of
292 case indexCharOffAddr# fo (c# -# 2#) of
293 '\\'# -> -- end of char
294 StringBuffer fo l# s# c#
295 _ -> loop (c# +# 1#) -- false alarm
296 _ -> StringBuffer fo l# s# c#
299 untilChar# :: StringBuffer -> Char# -> StringBuffer
300 untilChar# (StringBuffer fo l# s# c#) x# =
304 if indexCharOffAddr# fo c# `eqChar#` x# then
305 StringBuffer fo l# s# c#
310 lexemeToString :: StringBuffer -> String
311 lexemeToString (StringBuffer fo _ start_pos# current#) =
312 if start_pos# ==# current# then
315 byteArrayToString (copySubStr (A# fo) (I# start_pos#) (I# (current# -# start_pos#)))
318 lexemeToByteArray :: StringBuffer -> _ByteArray Int
319 lexemeToByteArray (StringBuffer fo _ start_pos# current#) =
320 if start_pos# ==# current# then
321 error "lexemeToByteArray"
323 copySubStr (A# fo) (I# start_pos#) (I# (current# -# start_pos#))
325 lexemeToFastString :: StringBuffer -> FastString
326 lexemeToFastString (StringBuffer fo l# start_pos# current#) =
327 if start_pos# ==# current# then
328 mkFastCharString2 (A# fo) (I# 0#)
330 mkFastSubString (A# fo) (I# start_pos#) (I# (current# -# start_pos#))
333 Create a StringBuffer from the current lexeme, and add a sentinel
334 at the end. Know What You're Doing before taking this function
337 lexemeToBuffer :: StringBuffer -> StringBuffer
338 lexemeToBuffer (StringBuffer fo l# start_pos# current#) =
339 if start_pos# ==# current# then
340 StringBuffer fo 0# start_pos# current# -- an error, really.
342 unsafeWriteBuffer (StringBuffer fo (current# -# start_pos#) start_pos# start_pos#)