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