Skip to content

Commit 13fd23e

Browse files
committed
Merge the ALEX_MONAD and ALEX_MONAD_BYTESTRING sections via diff -D
1 parent a4f43f8 commit 13fd23e

File tree

1 file changed

+60
-105
lines changed

1 file changed

+60
-105
lines changed

templates/wrappers.hs

Lines changed: 60 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,21 @@ alexMove (AlexPn a l c) _ = AlexPn (a+1) l (c+1)
160160
#endif
161161

162162
-- -----------------------------------------------------------------------------
163-
-- Default monad
163+
-- Monad (default and with ByteString input)
164164

165-
#ifdef ALEX_MONAD
165+
#if defined(ALEX_MONAD) || defined(ALEX_MONAD_BYTESTRING)
166166
data AlexState = AlexState {
167167
alex_pos :: !AlexPosn, -- position at current input location
168+
#ifndef ALEX_MONAD_BYTESTRING
168169
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 */
169174
alex_chr :: !Char, -- the character before the input
175+
#ifndef ALEX_MONAD_BYTESTRING
170176
alex_bytes :: [Byte],
177+
#endif /* ! ALEX_MONAD_BYTESTRING */
171178
alex_scd :: !Int -- the current startcode
172179
#ifdef ALEX_MONAD_USER_STATE
173180
, alex_ust :: AlexUserState -- AlexUserState will be defined in the user program
@@ -176,12 +183,21 @@ data AlexState = AlexState {
176183

177184
-- Compile with -funbox-strict-fields for best results!
178185

186+
#ifndef ALEX_MONAD_BYTESTRING
179187
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 */
180191
runAlex input__ (Alex f)
181192
= case f (AlexState {alex_pos = alexStartPos,
193+
#ifdef ALEX_MONAD_BYTESTRING
194+
alex_bpos = 0,
195+
#endif /* ALEX_MONAD_BYTESTRING */
182196
alex_inp = input__,
183197
alex_chr = '\n',
198+
#ifndef ALEX_MONAD_BYTESTRING
184199
alex_bytes = [],
200+
#endif /* ! ALEX_MONAD_BYTESTRING */
185201
#ifdef ALEX_MONAD_USER_STATE
186202
alex_ust = alexInitUserState,
187203
#endif
@@ -191,17 +207,26 @@ runAlex input__ (Alex f)
191207
newtype Alex a = Alex { unAlex :: AlexState -> Either String (AlexState, a) }
192208

193209
instance Functor Alex where
210+
#ifndef ALEX_MONAD_BYTESTRING
194211
fmap f a = Alex $ \s -> case unAlex a s of
195212
Left msg -> Left msg
196213
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 */
197217

198218
instance Applicative Alex where
219+
#ifndef ALEX_MONAD_BYTESTRING
199220
pure a = Alex $ \s -> Right (s, a)
200221
fa <*> a = Alex $ \s -> case unAlex fa s of
201222
Left msg -> Left msg
202223
Right (s', f) -> case unAlex a s' of
203224
Left msg -> Left msg
204225
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 */
205230

206231
instance Monad Alex where
207232
m >>= k = Alex $ \s -> case unAlex m s of
@@ -211,12 +236,25 @@ instance Monad Alex where
211236

212237
alexGetInput :: Alex AlexInput
213238
alexGetInput
239+
#ifndef ALEX_MONAD_BYTESTRING
214240
= Alex $ \s@AlexState{alex_pos=pos,alex_chr=c,alex_bytes=bs,alex_inp=inp__} ->
215241
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 */
216246

217247
alexSetInput :: AlexInput -> Alex ()
248+
#ifndef ALEX_MONAD_BYTESTRING
218249
alexSetInput (pos,c,bs,inp__)
219250
= 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 */
220258
state__@(AlexState{}) -> Right (state__, ())
221259

222260
alexError :: String -> Alex a
@@ -228,6 +266,7 @@ alexGetStartCode = Alex $ \s@AlexState{alex_scd=sc} -> Right (s, sc)
228266
alexSetStartCode :: Int -> Alex ()
229267
alexSetStartCode sc = Alex $ \s -> Right (s{alex_scd=sc}, ())
230268

269+
#ifndef ALEX_MONAD_BYTESTRING
231270
#ifdef ALEX_MONAD_USER_STATE
232271
alexGetUserState :: Alex AlexUserState
233272
alexGetUserState = Alex $ \s@AlexState{alex_ust=ust} -> Right (s,ust)
@@ -236,128 +275,40 @@ alexSetUserState :: AlexUserState -> Alex ()
236275
alexSetUserState ss = Alex $ \s -> Right (s{alex_ust=ss}, ())
237276
#endif
238277

278+
#endif /* ! ALEX_MONAD_BYTESTRING */
239279
alexMonadScan = do
280+
#ifndef ALEX_MONAD_BYTESTRING
240281
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 */
343283
inp__@(_,_,_,n) <- alexGetInput
284+
#endif /* ALEX_MONAD_BYTESTRING */
344285
sc <- alexGetStartCode
345286
case alexScan inp__ sc of
346287
AlexEOF -> alexEOF
347288
AlexError ((AlexPn _ line column),_,_,_) -> alexError $ "lexical error at line " ++ (show line) ++ ", column " ++ (show column)
348289
AlexSkip inp__' _len -> do
349290
alexSetInput inp__'
350291
alexMonadScan
292+
#ifndef ALEX_MONAD_BYTESTRING
293+
AlexToken inp__' len action -> do
294+
#else /* ALEX_MONAD_BYTESTRING */
351295
AlexToken inp__'@(_,_,_,n') _ action -> do
296+
#endif /* ALEX_MONAD_BYTESTRING */
352297
alexSetInput inp__'
353298
action (ignorePendingBytes inp__) len
299+
#ifdef ALEX_MONAD_BYTESTRING
354300
where
355301
len = n'-n
302+
#endif /* ALEX_MONAD_BYTESTRING */
356303

357304
-- -----------------------------------------------------------------------------
358305
-- Useful token actions
359306

307+
#ifndef ALEX_MONAD_BYTESTRING
308+
type AlexAction result = AlexInput -> Int -> Alex result
309+
#else /* ALEX_MONAD_BYTESTRING */
360310
type AlexAction result = AlexInput -> Int64 -> Alex result
311+
#endif /* ALEX_MONAD_BYTESTRING */
361312

362313
-- just ignore this token and scan another one
363314
-- skip :: AlexAction result
@@ -373,9 +324,13 @@ andBegin :: AlexAction result -> Int -> AlexAction result
373324
alexSetStartCode code
374325
action input__ len
375326

327+
#ifndef ALEX_MONAD_BYTESTRING
328+
token :: (AlexInput -> Int -> token) -> AlexAction token
329+
#else /* ALEX_MONAD_BYTESTRING */
376330
token :: (AlexInput -> Int64 -> token) -> AlexAction token
377-
token t input__ len = return (t input__ len)
378331
#endif /* ALEX_MONAD_BYTESTRING */
332+
token t input__ len = return (t input__ len)
333+
#endif /* defined(ALEX_MONAD) || defined(ALEX_MONAD_BYTESTRING) */
379334

380335

381336
-- -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)