Skip to content

Commit b0acbff

Browse files
andreasabelemekoi
andauthored
Make alex{G,S}etUserState available with monadUserState-bytestring wrapper (#254)
Corrects some CPP in the template. Closes #220. --------- Co-authored-by: Emeka Nkurumeh <[email protected]>
1 parent 48f6cfb commit b0acbff

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Add option `--numeric-version`.
44
* Remove deprecated `-v` as alias for `--version`.
55
* Add `-v` as placeholder for a future `--verbose` option.
6+
* Make `alex{G,S}etUserState` available with the `monadUserState-bytestring` wrapper
7+
[Issue #220](https://github.com/haskell/alex/issues/220).
68

79
## Changes in 3.4.0.1
810

data/AlexWrappers.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,13 @@ alexGetStartCode = Alex $ \s@AlexState{alex_scd=sc} -> Right (s, sc)
360360
alexSetStartCode :: Int -> Alex ()
361361
alexSetStartCode sc = Alex $ \s -> Right (s{alex_scd=sc}, ())
362362

363-
#if !defined(ALEX_MONAD_BYTESTRING) && defined(ALEX_MONAD_USER_STATE)
363+
#if defined(ALEX_MONAD_USER_STATE)
364364
alexGetUserState :: Alex AlexUserState
365365
alexGetUserState = Alex $ \s@AlexState{alex_ust=ust} -> Right (s,ust)
366366

367367
alexSetUserState :: AlexUserState -> Alex ()
368368
alexSetUserState ss = Alex $ \s -> Right (s{alex_ust=ss}, ())
369-
#endif /* !defined(ALEX_MONAD_BYTESTRING) && defined(ALEX_MONAD_USER_STATE) */
369+
#endif /* defined(ALEX_MONAD_USER_STATE) */
370370

371371
#ifdef ALEX_MONAD
372372
alexMonadScan = do

tests/monadUserState_typeclass_bytestring.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ lex inp =
7070
let
7171
lexAll =
7272
do
73+
-- Andreas Abel, 2023-12-30, issue #220:
74+
-- Test that alex{G,S}etUserState are in scope.
75+
u <- alexGetUserState
76+
alexSetUserState (u + 1)
77+
7378
res <- alexMonadScan
7479
case res of
7580
EOF -> return []

tests/tokens_monadUserState_bytestring.x

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,39 @@ tokens :-
2828
tok f (p,_,input,_) len = return (f p (B.take (fromIntegral len) input))
2929
3030
-- The token type:
31-
data Token =
32-
Let AlexPosn |
33-
In AlexPosn |
34-
Sym AlexPosn Char |
35-
Var AlexPosn String |
36-
Int AlexPosn Int |
37-
Err AlexPosn |
38-
EOF
39-
deriving (Eq,Show)
31+
data Token
32+
= Let AlexPosn
33+
| In AlexPosn
34+
| Sym AlexPosn Char
35+
| Var AlexPosn String
36+
| Int AlexPosn Int
37+
| Err AlexPosn
38+
| EOF
39+
deriving (Eq,Show)
4040
4141
alexEOF = return EOF
4242
4343
main = if test1 /= result1 then do print test1; exitFailure
44-
else exitWith ExitSuccess
44+
else exitWith ExitSuccess
4545
4646
type AlexUserState = ()
4747
alexInitUserState = ()
4848
4949
scanner str = runAlex str $ do
50-
let loop = do tk <- alexMonadScan
51-
if tk == EOF
52-
then return [tk]
53-
else do toks <- loop
54-
return (tk:toks)
50+
let
51+
loop = do
52+
53+
-- Andreas Abel, 2023-12-30, issue #220:
54+
-- Test that alex{G,S}etUserState are in scope.
55+
() <- alexGetUserState
56+
alexSetUserState ()
57+
58+
tk <- alexMonadScan
59+
if tk == EOF
60+
then return [tk]
61+
else do
62+
toks <- loop
63+
return (tk:toks)
5564
loop
5665
5766
test1 = case scanner " let in 012334\n=+*foo bar__'" of

0 commit comments

Comments
 (0)