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
62 #if __GLASGOW_HASKELL__ <= 200
75 # if __GLASGOW_HASKELL__ == 202
76 import PrelBase ( Char(..) )
88 -- ForeignObj# -- the data
95 instance Text StringBuffer where
96 showsPrec _ s = showString ""
100 hGetStringBuffer :: FilePath -> IO StringBuffer
101 hGetStringBuffer fname =
102 -- trace ("Renamer: opening " ++ fname) $
103 openFile fname ReadMode >>= \ hndl ->
104 hFileSize hndl >>= \ len@(J# _ _ d#) ->
105 let len_i = fromInteger len in
106 -- Allocate an array for system call to store its bytes into.
107 -- ToDo: make it robust
108 -- trace (show ((len_i::Int)+1)) $
109 (_casm_ `` %r=(char *)malloc(sizeof(char)*(int)%0); '' (len_i::Int)) `CCALL_THEN` \ arr@(A# a#) ->
110 if addr2Int# a# ==# 0# then
111 failWith MkIOError(hndl,UserError,("hGetStringBuffer: Could not allocate "++show len_i ++ " bytes"))
114 -- _casm_ `` %r=NULL; '' `thenPrimIO` \ free_p ->
115 -- makeForeignObj arr free_p `thenPrimIO` \ fo@(_ForeignObj fo#) ->
116 _readHandle hndl >>= \ hndl_ ->
117 _writeHandle hndl hndl_ >>
118 let ptr = _filePtr hndl_ in
119 _ccall_ fread arr (1::Int) len_i ptr `CCALL_THEN` \ (I# read#) ->
120 -- trace ("DEBUG: opened " ++ fname ++ show (I# read#)) $
122 if read# ==# 0# then -- EOF or other error
123 failWith MkIOError(hndl,UserError,"hGetStringBuffer: EOF reached or some other error")
125 -- Add a sentinel NUL
126 _casm_ `` ((char *)%0)[(int)%1]=(char)0; '' arr (I# (read# -# 1#)) `CCALL_THEN` \ () ->
127 return (StringBuffer a# read# 0# 0#)
129 freeStringBuffer :: StringBuffer -> IO ()
130 freeStringBuffer (StringBuffer a# _ _ _) =
131 _casm_ `` free((char *)%0); '' (A# a#) `CCALL_THEN` \ () ->
134 unsafeWriteBuffer :: StringBuffer -> Int# -> Char# -> StringBuffer
135 unsafeWriteBuffer s@(StringBuffer a _ _ _) i# ch# =
136 unsafePerformPrimIO (
137 _casm_ `` ((char *)%0)[(int)%1]=(char)%2; '' (A# a) (I# i#) (C# ch#) `thenPrimIO` \ () ->
145 currentChar# :: StringBuffer -> Char#
146 currentChar# (StringBuffer fo# _ _ current#) = indexCharOffAddr# fo# current#
148 currentChar :: StringBuffer -> Char
149 currentChar sb = case currentChar# sb of c -> C# c
151 indexSBuffer# :: StringBuffer -> Int# -> Char#
152 indexSBuffer# (StringBuffer fo# _ _ _) i# = indexCharOffAddr# fo# i#
154 indexSBuffer :: StringBuffer -> Int -> Char
155 indexSBuffer sb (I# i#) = case indexSBuffer# sb i# of c -> C# c
157 -- relative lookup, i.e, currentChar = lookAhead 0
158 lookAhead# :: StringBuffer -> Int# -> Char#
159 lookAhead# (StringBuffer fo# _ _ c#) i# = indexCharOffAddr# fo# (c# +# i#)
161 lookAhead :: StringBuffer -> Int -> Char
162 lookAhead sb (I# i#) = case lookAhead# sb i# of c -> C# c
166 moving the start point of the current lexeme.
169 -- moving the end point of the current lexeme.
170 setCurrentPos# :: StringBuffer -> Int# -> StringBuffer
171 setCurrentPos# (StringBuffer fo l# s# c#) i# =
172 StringBuffer fo l# s# (c# +# i#)
174 -- augmenting the current lexeme by one.
175 incLexeme :: StringBuffer -> StringBuffer
176 incLexeme (StringBuffer fo l# s# c#) = StringBuffer fo l# s# (c# +# 1#)
178 decLexeme :: StringBuffer -> StringBuffer
179 decLexeme (StringBuffer fo l# s# c#) = StringBuffer fo l# s# (c# -# 1#)
183 -- move the start and end point of the buffer on by
187 stepOn :: StringBuffer -> StringBuffer
188 stepOn (StringBuffer fo l# s# c#) = StringBuffer fo l# (s# +# 1#) (s# +# 1#) -- assume they're the same.
190 stepOnBy# :: StringBuffer -> Int# -> StringBuffer
191 stepOnBy# (StringBuffer fo# l# s# c#) i# =
193 new_s# -> StringBuffer fo# l# new_s# new_s#
196 stepOnTo# :: StringBuffer -> Int# -> StringBuffer
197 stepOnTo# (StringBuffer fo l _ _) s# = StringBuffer fo l s# s#
199 stepOnUntil :: (Char -> Bool) -> StringBuffer -> StringBuffer
200 stepOnUntil pred (StringBuffer fo l# s# c#) =
204 case indexCharOffAddr# fo c# of
205 ch# | pred (C# ch#) -> StringBuffer fo l# c# c#
206 | ch# `eqChar#` '\NUL'# && c# >=# l# -> StringBuffer fo l# l# l# -- EOB, return immediately.
207 | otherwise -> loop (c# +# 1#)
209 stepOverLexeme :: StringBuffer -> StringBuffer
210 stepOverLexeme (StringBuffer fo l s# c#) = StringBuffer fo l c# c#
212 expandWhile :: (Char -> Bool) -> StringBuffer -> StringBuffer
213 expandWhile pred (StringBuffer fo l# s# c#) =
217 case indexCharOffAddr# fo c# of
218 ch# | pred (C# ch#) -> loop (c# +# 1#)
219 | ch# `eqChar#` '\NUL'# && c# >=# l# -> StringBuffer fo l# l# l# -- EOB, return immediately.
220 | otherwise -> StringBuffer fo l# s# c#
223 scanNumLit :: Int -> StringBuffer -> (Int,StringBuffer)
224 scanNumLit (I# acc#) (StringBuffer fo l# s# c#) =
228 case indexCharOffAddr# fo c# of
229 ch# | isDigit (C# ch#) -> loop (acc# *# 10# +# (ord# ch# -# ord# '0'#)) (c# +# 1#)
230 | ch# `eqChar#` '\NUL'# && c# >=# l# -> (I# acc#, StringBuffer fo l# l# l#) -- EOB, return immediately.
231 | otherwise -> (I# acc#,StringBuffer fo l# s# c#)
234 expandUntilMatch :: StringBuffer -> String -> StringBuffer
235 expandUntilMatch (StringBuffer fo l# s# c#) str =
238 loop c# [] = StringBuffer fo l# s# c#
239 loop c# ((C# x#):xs) =
240 if indexCharOffAddr# fo c# `eqChar#` x# then
247 -- at or beyond end of buffer?
248 bufferExhausted :: StringBuffer -> Bool
249 bufferExhausted (StringBuffer fo l# _ c#) = c# >=# l#
251 emptyLexeme :: StringBuffer -> Bool
252 emptyLexeme (StringBuffer fo l# s# c#) = s# ==# c#
255 prefixMatch :: StringBuffer -> String -> Maybe StringBuffer
256 prefixMatch (StringBuffer fo l# s# c#) str =
259 loop c# [] = Just (StringBuffer fo l# s# c#)
260 loop c# ((C# x#):xs) =
261 if indexCharOffAddr# fo c# `eqChar#` x# then
266 untilEndOfString# :: StringBuffer -> StringBuffer
267 untilEndOfString# (StringBuffer fo l# s# c#) =
271 case indexCharOffAddr# fo c# of
273 case indexCharOffAddr# fo (c# -# 1#) of
275 -- looks like an escaped something or other to me,
276 -- better count the number of "\\"s that are immediately
277 -- preceeding to decide if the " is escaped.
280 case indexCharOffAddr# fo i# of
281 '\\'# -> odd_slashes (not flg) (i# -# 1#)
284 if odd_slashes True (c# -# 2#) then
285 -- odd number, " is ecaped.
287 else -- a real end of string delimiter after all.
288 StringBuffer fo l# s# c#
289 _ -> StringBuffer fo l# s# c#
291 if c# >=# l# then -- hit sentinel, this doesn't look too good..
292 StringBuffer fo l# l# l#
298 untilEndOfChar# :: StringBuffer -> StringBuffer
299 untilEndOfChar# (StringBuffer fo l# s# c#) =
303 case indexCharOffAddr# fo c# of
305 case indexCharOffAddr# fo (c# -# 1#) of
307 case indexCharOffAddr# fo (c# -# 2#) of
308 '\\'# -> -- end of char
309 StringBuffer fo l# s# c#
310 _ -> loop (c# +# 1#) -- false alarm
311 _ -> StringBuffer fo l# s# c#
313 if c# >=# l# then -- hit sentinel, this doesn't look too good..
314 StringBuffer fo l# l# l#
319 untilChar# :: StringBuffer -> Char# -> StringBuffer
320 untilChar# (StringBuffer fo l# s# c#) x# =
324 if indexCharOffAddr# fo c# `eqChar#` x# then
325 StringBuffer fo l# s# c#
330 lexemeToString :: StringBuffer -> String
331 lexemeToString (StringBuffer fo _ start_pos# current#) =
332 if start_pos# ==# current# then
335 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 205
336 byteArrayToString (copySubStr (A# fo) (I# start_pos#) (I# (current# -# start_pos#)))
338 unpackCStringBA (copySubStr (A# fo) (I# start_pos#) (I# (current# -# start_pos#)))
341 lexemeToByteArray :: StringBuffer -> _ByteArray Int
342 lexemeToByteArray (StringBuffer fo _ start_pos# current#) =
343 if start_pos# ==# current# then
344 error "lexemeToByteArray"
346 copySubStr (A# fo) (I# start_pos#) (I# (current# -# start_pos#))
348 lexemeToFastString :: StringBuffer -> FastString
349 lexemeToFastString (StringBuffer fo l# start_pos# current#) =
350 if start_pos# ==# current# then
351 mkFastCharString2 (A# fo) (I# 0#)
353 mkFastSubString (A# fo) (I# start_pos#) (I# (current# -# start_pos#))
356 Create a StringBuffer from the current lexeme, and add a sentinel
357 at the end. Know What You're Doing before taking this function
360 lexemeToBuffer :: StringBuffer -> StringBuffer
361 lexemeToBuffer (StringBuffer fo l# start_pos# current#) =
362 if start_pos# ==# current# then
363 StringBuffer fo 0# start_pos# current# -- an error, really.
365 unsafeWriteBuffer (StringBuffer fo (current# -# start_pos#) start_pos# start_pos#)