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 | ch# `eqChar#` '\NUL'# && c# >=# l# -> StringBuffer fo l# l# l# -- EOB, return immediately.
200 | otherwise -> loop (c# +# 1#)
202 stepOverLexeme :: StringBuffer -> StringBuffer
203 stepOverLexeme (StringBuffer fo l s# c#) = StringBuffer fo l c# c#
205 expandWhile :: (Char -> Bool) -> StringBuffer -> StringBuffer
206 expandWhile pred (StringBuffer fo l# s# c#) =
210 case indexCharOffAddr# fo c# of
211 ch# | pred (C# ch#) -> loop (c# +# 1#)
212 | ch# `eqChar#` '\NUL'# && c# >=# l# -> StringBuffer fo l# l# l# -- EOB, return immediately.
213 | otherwise -> StringBuffer fo l# s# c#
216 scanNumLit :: Int -> StringBuffer -> (Int,StringBuffer)
217 scanNumLit (I# acc#) (StringBuffer fo l# s# c#) =
221 case indexCharOffAddr# fo c# of
222 ch# | isDigit (C# ch#) -> loop (acc# *# 10# +# (ord# ch# -# ord# '0'#)) (c# +# 1#)
223 | ch# `eqChar#` '\NUL'# && c# >=# l# -> (I# acc#, StringBuffer fo l# l# l#) -- EOB, return immediately.
224 | otherwise -> (I# acc#,StringBuffer fo l# s# c#)
227 expandUntilMatch :: StringBuffer -> String -> StringBuffer
228 expandUntilMatch (StringBuffer fo l# s# c#) str =
231 loop c# [] = StringBuffer fo l# s# c#
232 loop c# ((C# x#):xs) =
233 if indexCharOffAddr# fo c# `eqChar#` x# then
240 -- at or beyond end of buffer?
241 bufferExhausted :: StringBuffer -> Bool
242 bufferExhausted (StringBuffer fo l# _ c#) = c# >=# l#
244 emptyLexeme :: StringBuffer -> Bool
245 emptyLexeme (StringBuffer fo l# s# c#) = s# ==# c#
248 prefixMatch :: StringBuffer -> String -> Maybe StringBuffer
249 prefixMatch (StringBuffer fo l# s# c#) str =
252 loop c# [] = Just (StringBuffer fo l# s# c#)
253 loop c# ((C# x#):xs) =
254 if indexCharOffAddr# fo c# `eqChar#` x# then
259 untilEndOfString# :: StringBuffer -> StringBuffer
260 untilEndOfString# (StringBuffer fo l# s# c#) =
264 case indexCharOffAddr# fo c# of
266 case indexCharOffAddr# fo (c# -# 1#) of
268 -- looks like an escaped something or other to me,
269 -- better count the number of "\\"s that are immediately
270 -- preceeding to decide if the " is escaped.
273 case indexCharOffAddr# fo i# of
274 '\\'# -> odd_slashes (not flg) (i# -# 1#)
277 if odd_slashes True (c# -# 2#) then
278 -- odd number, " is ecaped.
280 else -- a real end of string delimiter after all.
281 StringBuffer fo l# s# c#
282 _ -> StringBuffer fo l# s# c#
284 if c# >=# l# then -- hit sentinel, this doesn't look too good..
285 StringBuffer fo l# l# l#
291 untilEndOfChar# :: StringBuffer -> StringBuffer
292 untilEndOfChar# (StringBuffer fo l# s# c#) =
296 case indexCharOffAddr# fo c# of
298 case indexCharOffAddr# fo (c# -# 1#) of
300 case indexCharOffAddr# fo (c# -# 2#) of
301 '\\'# -> -- end of char
302 StringBuffer fo l# s# c#
303 _ -> loop (c# +# 1#) -- false alarm
304 _ -> StringBuffer fo l# s# c#
306 if c# >=# l# then -- hit sentinel, this doesn't look too good..
307 StringBuffer fo l# l# l#
312 untilChar# :: StringBuffer -> Char# -> StringBuffer
313 untilChar# (StringBuffer fo l# s# c#) x# =
317 if indexCharOffAddr# fo c# `eqChar#` x# then
318 StringBuffer fo l# s# c#
323 lexemeToString :: StringBuffer -> String
324 lexemeToString (StringBuffer fo _ start_pos# current#) =
325 if start_pos# ==# current# then
328 byteArrayToString (copySubStr (A# fo) (I# start_pos#) (I# (current# -# start_pos#)))
331 lexemeToByteArray :: StringBuffer -> _ByteArray Int
332 lexemeToByteArray (StringBuffer fo _ start_pos# current#) =
333 if start_pos# ==# current# then
334 error "lexemeToByteArray"
336 copySubStr (A# fo) (I# start_pos#) (I# (current# -# start_pos#))
338 lexemeToFastString :: StringBuffer -> FastString
339 lexemeToFastString (StringBuffer fo l# start_pos# current#) =
340 if start_pos# ==# current# then
341 mkFastCharString2 (A# fo) (I# 0#)
343 mkFastSubString (A# fo) (I# start_pos#) (I# (current# -# start_pos#))
346 Create a StringBuffer from the current lexeme, and add a sentinel
347 at the end. Know What You're Doing before taking this function
350 lexemeToBuffer :: StringBuffer -> StringBuffer
351 lexemeToBuffer (StringBuffer fo l# start_pos# current#) =
352 if start_pos# ==# current# then
353 StringBuffer fo 0# start_pos# current# -- an error, really.
355 unsafeWriteBuffer (StringBuffer fo (current# -# start_pos#) start_pos# start_pos#)