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
96 hGetStringBuffer :: FilePath -> IO StringBuffer
97 hGetStringBuffer fname =
98 -- trace ("Renamer: opening " ++ fname) $
99 openFile fname ReadMode >>= \ hndl ->
100 hFileSize hndl >>= \ len@(J# _ _ d#) ->
101 let len_i = fromInteger len in
102 -- Allocate an array for system call to store its bytes into.
103 -- ToDo: make it robust
104 -- trace (show ((len_i::Int)+1)) $
105 (_casm_ `` %r=(char *)malloc(sizeof(char)*(int)%0); '' (len_i::Int)) `CCALL_THEN` \ arr@(A# a#) ->
106 if addr2Int# a# ==# 0# then
107 failWith MkIOError(hndl,UserError,("hGetStringBuffer: Could not allocate "++show len_i ++ " bytes"))
110 -- _casm_ `` %r=NULL; '' `thenPrimIO` \ free_p ->
111 -- makeForeignObj arr free_p `thenPrimIO` \ fo@(_ForeignObj fo#) ->
112 _readHandle hndl >>= \ hndl_ ->
113 _writeHandle hndl hndl_ >>
114 let ptr = _filePtr hndl_ in
115 _ccall_ fread arr (1::Int) len_i ptr `CCALL_THEN` \ (I# read#) ->
116 -- trace ("DEBUG: opened " ++ fname ++ show (I# read#)) $
118 if read# ==# 0# then -- EOF or other error
119 failWith MkIOError(hndl,UserError,"hGetStringBuffer: EOF reached or some other error")
121 -- Add a sentinel NUL
122 _casm_ `` ((char *)%0)[(int)%1]=(char)0; '' arr (I# (read# -# 1#)) `CCALL_THEN` \ () ->
123 return (StringBuffer a# read# 0# 0#)
125 freeStringBuffer :: StringBuffer -> IO ()
126 freeStringBuffer (StringBuffer a# _ _ _) =
127 _casm_ `` free((char *)%0); '' (A# a#) `CCALL_THEN` \ () ->
130 unsafeWriteBuffer :: StringBuffer -> Int# -> Char# -> StringBuffer
131 unsafeWriteBuffer s@(StringBuffer a _ _ _) i# ch# =
132 unsafePerformPrimIO (
133 _casm_ `` ((char *)%0)[(int)%1]=(char)%2; '' (A# a) (I# i#) (C# ch#) `thenPrimIO` \ () ->
141 currentChar# :: StringBuffer -> Char#
142 currentChar# (StringBuffer fo# _ _ current#) = indexCharOffAddr# fo# current#
144 currentChar :: StringBuffer -> Char
145 currentChar sb = case currentChar# sb of c -> C# c
147 indexSBuffer# :: StringBuffer -> Int# -> Char#
148 indexSBuffer# (StringBuffer fo# _ _ _) i# = indexCharOffAddr# fo# i#
150 indexSBuffer :: StringBuffer -> Int -> Char
151 indexSBuffer sb (I# i#) = case indexSBuffer# sb i# of c -> C# c
153 -- relative lookup, i.e, currentChar = lookAhead 0
154 lookAhead# :: StringBuffer -> Int# -> Char#
155 lookAhead# (StringBuffer fo# _ _ c#) i# = indexCharOffAddr# fo# (c# +# i#)
157 lookAhead :: StringBuffer -> Int -> Char
158 lookAhead sb (I# i#) = case lookAhead# sb i# of c -> C# c
162 moving the start point of the current lexeme.
165 -- moving the end point of the current lexeme.
166 setCurrentPos# :: StringBuffer -> Int# -> StringBuffer
167 setCurrentPos# (StringBuffer fo l# s# c#) i# =
168 StringBuffer fo l# s# (c# +# i#)
170 -- augmenting the current lexeme by one.
171 incLexeme :: StringBuffer -> StringBuffer
172 incLexeme (StringBuffer fo l# s# c#) = StringBuffer fo l# s# (c# +# 1#)
174 decLexeme :: StringBuffer -> StringBuffer
175 decLexeme (StringBuffer fo l# s# c#) = StringBuffer fo l# s# (c# -# 1#)
179 -- move the start and end point of the buffer on by
183 stepOn :: StringBuffer -> StringBuffer
184 stepOn (StringBuffer fo l# s# c#) = StringBuffer fo l# (s# +# 1#) (s# +# 1#) -- assume they're the same.
186 stepOnBy# :: StringBuffer -> Int# -> StringBuffer
187 stepOnBy# (StringBuffer fo# l# s# c#) i# =
189 new_s# -> StringBuffer fo# l# new_s# new_s#
192 stepOnTo# :: StringBuffer -> Int# -> StringBuffer
193 stepOnTo# (StringBuffer fo l _ _) s# = StringBuffer fo l s# s#
195 stepOnUntil :: (Char -> Bool) -> StringBuffer -> StringBuffer
196 stepOnUntil pred (StringBuffer fo l# s# c#) =
200 case indexCharOffAddr# fo c# of
201 ch# | pred (C# ch#) -> StringBuffer fo l# c# c#
202 | ch# `eqChar#` '\NUL'# && c# >=# l# -> StringBuffer fo l# l# l# -- EOB, return immediately.
203 | otherwise -> loop (c# +# 1#)
205 stepOverLexeme :: StringBuffer -> StringBuffer
206 stepOverLexeme (StringBuffer fo l s# c#) = StringBuffer fo l c# c#
208 expandWhile :: (Char -> Bool) -> StringBuffer -> StringBuffer
209 expandWhile pred (StringBuffer fo l# s# c#) =
213 case indexCharOffAddr# fo c# of
214 ch# | pred (C# ch#) -> loop (c# +# 1#)
215 | ch# `eqChar#` '\NUL'# && c# >=# l# -> StringBuffer fo l# l# l# -- EOB, return immediately.
216 | otherwise -> StringBuffer fo l# s# c#
219 scanNumLit :: Int -> StringBuffer -> (Int,StringBuffer)
220 scanNumLit (I# acc#) (StringBuffer fo l# s# c#) =
224 case indexCharOffAddr# fo c# of
225 ch# | isDigit (C# ch#) -> loop (acc# *# 10# +# (ord# ch# -# ord# '0'#)) (c# +# 1#)
226 | ch# `eqChar#` '\NUL'# && c# >=# l# -> (I# acc#, StringBuffer fo l# l# l#) -- EOB, return immediately.
227 | otherwise -> (I# acc#,StringBuffer fo l# s# c#)
230 expandUntilMatch :: StringBuffer -> String -> StringBuffer
231 expandUntilMatch (StringBuffer fo l# s# c#) str =
234 loop c# [] = StringBuffer fo l# s# c#
235 loop c# ((C# x#):xs) =
236 if indexCharOffAddr# fo c# `eqChar#` x# then
243 -- at or beyond end of buffer?
244 bufferExhausted :: StringBuffer -> Bool
245 bufferExhausted (StringBuffer fo l# _ c#) = c# >=# l#
247 emptyLexeme :: StringBuffer -> Bool
248 emptyLexeme (StringBuffer fo l# s# c#) = s# ==# c#
251 prefixMatch :: StringBuffer -> String -> Maybe StringBuffer
252 prefixMatch (StringBuffer fo l# s# c#) str =
255 loop c# [] = Just (StringBuffer fo l# s# c#)
256 loop c# ((C# x#):xs) =
257 if indexCharOffAddr# fo c# `eqChar#` x# then
262 untilEndOfString# :: StringBuffer -> StringBuffer
263 untilEndOfString# (StringBuffer fo l# s# c#) =
267 case indexCharOffAddr# fo c# of
269 case indexCharOffAddr# fo (c# -# 1#) of
271 -- looks like an escaped something or other to me,
272 -- better count the number of "\\"s that are immediately
273 -- preceeding to decide if the " is escaped.
276 case indexCharOffAddr# fo i# of
277 '\\'# -> odd_slashes (not flg) (i# -# 1#)
280 if odd_slashes True (c# -# 2#) then
281 -- odd number, " is ecaped.
283 else -- a real end of string delimiter after all.
284 StringBuffer fo l# s# c#
285 _ -> StringBuffer fo l# s# c#
287 if c# >=# l# then -- hit sentinel, this doesn't look too good..
288 StringBuffer fo l# l# l#
294 untilEndOfChar# :: StringBuffer -> StringBuffer
295 untilEndOfChar# (StringBuffer fo l# s# c#) =
299 case indexCharOffAddr# fo c# of
301 case indexCharOffAddr# fo (c# -# 1#) of
303 case indexCharOffAddr# fo (c# -# 2#) of
304 '\\'# -> -- end of char
305 StringBuffer fo l# s# c#
306 _ -> loop (c# +# 1#) -- false alarm
307 _ -> StringBuffer fo l# s# c#
309 if c# >=# l# then -- hit sentinel, this doesn't look too good..
310 StringBuffer fo l# l# l#
315 untilChar# :: StringBuffer -> Char# -> StringBuffer
316 untilChar# (StringBuffer fo l# s# c#) x# =
320 if indexCharOffAddr# fo c# `eqChar#` x# then
321 StringBuffer fo l# s# c#
326 lexemeToString :: StringBuffer -> String
327 lexemeToString (StringBuffer fo _ start_pos# current#) =
328 if start_pos# ==# current# then
331 byteArrayToString (copySubStr (A# fo) (I# start_pos#) (I# (current# -# start_pos#)))
334 lexemeToByteArray :: StringBuffer -> _ByteArray Int
335 lexemeToByteArray (StringBuffer fo _ start_pos# current#) =
336 if start_pos# ==# current# then
337 error "lexemeToByteArray"
339 copySubStr (A# fo) (I# start_pos#) (I# (current# -# start_pos#))
341 lexemeToFastString :: StringBuffer -> FastString
342 lexemeToFastString (StringBuffer fo l# start_pos# current#) =
343 if start_pos# ==# current# then
344 mkFastCharString2 (A# fo) (I# 0#)
346 mkFastSubString (A# fo) (I# start_pos#) (I# (current# -# start_pos#))
349 Create a StringBuffer from the current lexeme, and add a sentinel
350 at the end. Know What You're Doing before taking this function
353 lexemeToBuffer :: StringBuffer -> StringBuffer
354 lexemeToBuffer (StringBuffer fo l# start_pos# current#) =
355 if start_pos# ==# current# then
356 StringBuffer fo 0# start_pos# current# -- an error, really.
358 unsafeWriteBuffer (StringBuffer fo (current# -# start_pos#) start_pos# start_pos#)