6
6
7
7
#if defined(ALEX_MONAD) || defined(ALEX_MONAD_BYTESTRING)
8
8
import Control.Applicative as App (Applicative (.. ))
9
- import qualified Control.Monad (ap )
10
9
#endif
11
10
12
11
import Data.Word (Word8 )
@@ -160,14 +159,20 @@ alexMove (AlexPn a l c) _ = AlexPn (a+1) l (c+1)
160
159
#endif
161
160
162
161
-- -----------------------------------------------------------------------------
163
- -- Default monad
162
+ -- Monad (default and with ByteString input)
164
163
165
- #ifdef ALEX_MONAD
164
+ #if defined( ALEX_MONAD) || defined(ALEX_MONAD_BYTESTRING)
166
165
data AlexState = AlexState {
167
166
alex_pos :: ! AlexPosn , -- position at current input location
167
+ # ifndef ALEX_MONAD_BYTESTRING
168
168
alex_inp :: String , -- the current input
169
169
alex_chr :: ! Char , -- the character before the input
170
170
alex_bytes :: [Byte ],
171
+ # else /* ALEX_MONAD_BYTESTRING */
172
+ alex_bpos :: ! Int64 , -- bytes consumed so far
173
+ alex_inp :: ByteString. ByteString , -- the current input
174
+ alex_chr :: ! Char , -- the character before the input
175
+ # endif /* ALEX_MONAD_BYTESTRING */
171
176
alex_scd :: ! Int -- the current startcode
172
177
# ifdef ALEX_MONAD_USER_STATE
173
178
, alex_ust :: AlexUserState -- AlexUserState will be defined in the user program
@@ -176,12 +181,18 @@ data AlexState = AlexState {
176
181
177
182
-- Compile with -funbox-strict-fields for best results!
178
183
184
+ #ifndef ALEX_MONAD_BYTESTRING
179
185
runAlex :: String -> Alex a -> Either String a
180
186
runAlex input__ (Alex f)
181
- = case f (AlexState {alex_pos = alexStartPos,
187
+ = case f (AlexState {alex_bytes = [] ,
188
+ #else /* ALEX_MONAD_BYTESTRING */
189
+ runAlex :: ByteString. ByteString -> Alex a -> Either String a
190
+ runAlex input__ (Alex f)
191
+ = case f (AlexState {alex_bpos = 0 ,
192
+ #endif /* ALEX_MONAD_BYTESTRING */
193
+ alex_pos = alexStartPos,
182
194
alex_inp = input__,
183
195
alex_chr = ' \n ' ,
184
- alex_bytes = [] ,
185
196
#ifdef ALEX_MONAD_USER_STATE
186
197
alex_ust = alexInitUserState,
187
198
#endif
@@ -211,12 +222,25 @@ instance Monad Alex where
211
222
212
223
alexGetInput :: Alex AlexInput
213
224
alexGetInput
225
+ #ifndef ALEX_MONAD_BYTESTRING
214
226
= Alex $ \ s@ AlexState {alex_pos= pos,alex_chr= c,alex_bytes= bs,alex_inp= inp__} ->
215
227
Right (s, (pos,c,bs,inp__))
228
+ #else /* ALEX_MONAD_BYTESTRING */
229
+ = Alex $ \ s@ AlexState {alex_pos= pos,alex_bpos= bpos,alex_chr= c,alex_inp= inp__} ->
230
+ Right (s, (pos,c,inp__,bpos))
231
+ #endif /* ALEX_MONAD_BYTESTRING */
216
232
217
233
alexSetInput :: AlexInput -> Alex ()
234
+ #ifndef ALEX_MONAD_BYTESTRING
218
235
alexSetInput (pos,c,bs,inp__)
219
236
= Alex $ \ s -> case s{alex_pos= pos,alex_chr= c,alex_bytes= bs,alex_inp= inp__} of
237
+ #else /* ALEX_MONAD_BYTESTRING */
238
+ alexSetInput (pos,c,inp__,bpos)
239
+ = Alex $ \ s -> case s{alex_pos= pos,
240
+ alex_bpos= bpos,
241
+ alex_chr= c,
242
+ alex_inp= inp__} of
243
+ #endif /* ALEX_MONAD_BYTESTRING */
220
244
state__@ (AlexState {}) -> Right (state__, () )
221
245
222
246
alexError :: String -> Alex a
@@ -228,136 +252,43 @@ alexGetStartCode = Alex $ \s@AlexState{alex_scd=sc} -> Right (s, sc)
228
252
alexSetStartCode :: Int -> Alex ()
229
253
alexSetStartCode sc = Alex $ \ s -> Right (s{alex_scd= sc}, () )
230
254
231
- #ifdef ALEX_MONAD_USER_STATE
255
+ #if !defined(ALEX_MONAD_BYTESTRING) && defined( ALEX_MONAD_USER_STATE)
232
256
alexGetUserState :: Alex AlexUserState
233
257
alexGetUserState = Alex $ \ s@ AlexState {alex_ust= ust} -> Right (s,ust)
234
258
235
259
alexSetUserState :: AlexUserState -> Alex ()
236
260
alexSetUserState ss = Alex $ \ s -> Right (s{alex_ust= ss}, () )
237
- #endif
261
+ #endif /* !defined(ALEX_MONAD_BYTESTRING) && defined(ALEX_MONAD_USER_STATE) */
238
262
239
263
alexMonadScan = do
264
+ #ifndef ALEX_MONAD_BYTESTRING
240
265
inp__ <- alexGetInput
266
+ #else /* ALEX_MONAD_BYTESTRING */
267
+ inp__@ (_,_,_,n) <- alexGetInput
268
+ #endif /* ALEX_MONAD_BYTESTRING */
241
269
sc <- alexGetStartCode
242
270
case alexScan inp__ sc of
243
271
AlexEOF -> alexEOF
244
272
AlexError ((AlexPn _ line column),_,_,_) -> alexError $ " lexical error at line " ++ (show line) ++ " , column " ++ (show column)
245
273
AlexSkip inp__' _len -> do
246
274
alexSetInput inp__'
247
275
alexMonadScan
276
+ #ifndef ALEX_MONAD_BYTESTRING
248
277
AlexToken inp__' len action -> do
278
+ #else /* ALEX_MONAD_BYTESTRING */
279
+ AlexToken inp__'@ (_,_,_,n') _ action -> let len = n'- n in do
280
+ #endif /* ALEX_MONAD_BYTESTRING */
249
281
alexSetInput inp__'
250
282
action (ignorePendingBytes inp__) len
251
283
252
284
-- -----------------------------------------------------------------------------
253
285
-- Useful token actions
254
286
287
+ #ifndef ALEX_MONAD_BYTESTRING
255
288
type AlexAction result = AlexInput -> Int -> Alex result
256
-
257
- -- just ignore this token and scan another one
258
- -- skip :: AlexAction result
259
- skip _input _len = alexMonadScan
260
-
261
- -- ignore this token, but set the start code to a new value
262
- -- begin :: Int -> AlexAction result
263
- begin code _input _len = do alexSetStartCode code; alexMonadScan
264
-
265
- -- perform an action for this token, and set the start code to a new value
266
- andBegin :: AlexAction result -> Int -> AlexAction result
267
- (action `andBegin` code) input__ len = do
268
- alexSetStartCode code
269
- action input__ len
270
-
271
- token :: (AlexInput -> Int -> token ) -> AlexAction token
272
- token t input__ len = return (t input__ len)
273
- #endif /* ALEX_MONAD */
274
-
275
-
276
- -- -----------------------------------------------------------------------------
277
- -- Monad (with ByteString input)
278
-
279
- #ifdef ALEX_MONAD_BYTESTRING
280
- data AlexState = AlexState {
281
- alex_pos :: ! AlexPosn , -- position at current input location
282
- alex_bpos :: ! Int64 , -- bytes consumed so far
283
- alex_inp :: ByteString. ByteString , -- the current input
284
- alex_chr :: ! Char , -- the character before the input
285
- alex_scd :: ! Int -- the current startcode
286
- # ifdef ALEX_MONAD_USER_STATE
287
- , alex_ust :: AlexUserState -- AlexUserState will be defined in the user program
288
- # endif
289
- }
290
-
291
- -- Compile with -funbox-strict-fields for best results!
292
-
293
- runAlex :: ByteString. ByteString -> Alex a -> Either String a
294
- runAlex input__ (Alex f)
295
- = case f (AlexState {alex_pos = alexStartPos,
296
- alex_bpos = 0 ,
297
- alex_inp = input__,
298
- alex_chr = ' \n ' ,
299
- #ifdef ALEX_MONAD_USER_STATE
300
- alex_ust = alexInitUserState,
301
- #endif
302
- alex_scd = 0 }) of Left msg -> Left msg
303
- Right ( _, a ) -> Right a
304
-
305
- newtype Alex a = Alex { unAlex :: AlexState -> Either String (AlexState , a ) }
306
-
307
- instance Functor Alex where
308
- fmap f m = do x <- m; return (f x)
309
-
310
- instance Applicative Alex where
311
- pure a = Alex $ \ s -> Right (s,a)
312
- (<*>) = Control.Monad. ap
313
-
314
- instance Monad Alex where
315
- m >>= k = Alex $ \ s -> case unAlex m s of
316
- Left msg -> Left msg
317
- Right (s',a) -> unAlex (k a) s'
318
- return = App. pure
319
-
320
- alexGetInput :: Alex AlexInput
321
- alexGetInput
322
- = Alex $ \ s@ AlexState {alex_pos= pos,alex_bpos= bpos,alex_chr= c,alex_inp= inp__} ->
323
- Right (s, (pos,c,inp__,bpos))
324
-
325
- alexSetInput :: AlexInput -> Alex ()
326
- alexSetInput (pos,c,inp__,bpos)
327
- = Alex $ \ s -> case s{alex_pos= pos,
328
- alex_bpos= bpos,
329
- alex_chr= c,
330
- alex_inp= inp__} of
331
- state__@ (AlexState {}) -> Right (state__, () )
332
-
333
- alexError :: String -> Alex a
334
- alexError message = Alex $ const $ Left message
335
-
336
- alexGetStartCode :: Alex Int
337
- alexGetStartCode = Alex $ \ s@ AlexState {alex_scd= sc} -> Right (s, sc)
338
-
339
- alexSetStartCode :: Int -> Alex ()
340
- alexSetStartCode sc = Alex $ \ s -> Right (s{alex_scd= sc}, () )
341
-
342
- alexMonadScan = do
343
- inp__@ (_,_,_,n) <- alexGetInput
344
- sc <- alexGetStartCode
345
- case alexScan inp__ sc of
346
- AlexEOF -> alexEOF
347
- AlexError ((AlexPn _ line column),_,_,_) -> alexError $ " lexical error at line " ++ (show line) ++ " , column " ++ (show column)
348
- AlexSkip inp__' _len -> do
349
- alexSetInput inp__'
350
- alexMonadScan
351
- AlexToken inp__'@ (_,_,_,n') _ action -> do
352
- alexSetInput inp__'
353
- action (ignorePendingBytes inp__) len
354
- where
355
- len = n'- n
356
-
357
- -- -----------------------------------------------------------------------------
358
- -- Useful token actions
359
-
289
+ #else /* ALEX_MONAD_BYTESTRING */
360
290
type AlexAction result = AlexInput -> Int64 -> Alex result
291
+ #endif /* ALEX_MONAD_BYTESTRING */
361
292
362
293
-- just ignore this token and scan another one
363
294
-- skip :: AlexAction result
@@ -373,9 +304,13 @@ andBegin :: AlexAction result -> Int -> AlexAction result
373
304
alexSetStartCode code
374
305
action input__ len
375
306
307
+ #ifndef ALEX_MONAD_BYTESTRING
308
+ token :: (AlexInput -> Int -> token ) -> AlexAction token
309
+ #else /* ALEX_MONAD_BYTESTRING */
376
310
token :: (AlexInput -> Int64 -> token ) -> AlexAction token
377
- token t input__ len = return (t input__ len)
378
311
#endif /* ALEX_MONAD_BYTESTRING */
312
+ token t input__ len = return (t input__ len)
313
+ #endif /* defined(ALEX_MONAD) || defined(ALEX_MONAD_BYTESTRING) */
379
314
380
315
381
316
-- -----------------------------------------------------------------------------
0 commit comments