Skip to content

Commit

Permalink
Make alex{G,S}etUserState available with `monadUserState-bytestring…
Browse files Browse the repository at this point in the history
…` wrapper (#254)

Corrects some CPP in the template.

Closes #220.

---------

Co-authored-by: Emeka Nkurumeh <[email protected]>
  • Loading branch information
andreasabel and emekoi authored Dec 30, 2023
1 parent 48f6cfb commit b0acbff
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Add option `--numeric-version`.
* Remove deprecated `-v` as alias for `--version`.
* Add `-v` as placeholder for a future `--verbose` option.
* Make `alex{G,S}etUserState` available with the `monadUserState-bytestring` wrapper
[Issue #220](https://github.com/haskell/alex/issues/220).

## Changes in 3.4.0.1

Expand Down
4 changes: 2 additions & 2 deletions data/AlexWrappers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,13 @@ alexGetStartCode = Alex $ \s@AlexState{alex_scd=sc} -> Right (s, sc)
alexSetStartCode :: Int -> Alex ()
alexSetStartCode sc = Alex $ \s -> Right (s{alex_scd=sc}, ())

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

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

#ifdef ALEX_MONAD
alexMonadScan = do
Expand Down
5 changes: 5 additions & 0 deletions tests/monadUserState_typeclass_bytestring.x
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ lex inp =
let
lexAll =
do
-- Andreas Abel, 2023-12-30, issue #220:
-- Test that alex{G,S}etUserState are in scope.
u <- alexGetUserState
alexSetUserState (u + 1)

res <- alexMonadScan
case res of
EOF -> return []
Expand Down
39 changes: 24 additions & 15 deletions tests/tokens_monadUserState_bytestring.x
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,39 @@ tokens :-
tok f (p,_,input,_) len = return (f p (B.take (fromIntegral len) input))
-- The token type:
data Token =
Let AlexPosn |
In AlexPosn |
Sym AlexPosn Char |
Var AlexPosn String |
Int AlexPosn Int |
Err AlexPosn |
EOF
deriving (Eq,Show)
data Token
= Let AlexPosn
| In AlexPosn
| Sym AlexPosn Char
| Var AlexPosn String
| Int AlexPosn Int
| Err AlexPosn
| EOF
deriving (Eq,Show)
alexEOF = return EOF
main = if test1 /= result1 then do print test1; exitFailure
else exitWith ExitSuccess
else exitWith ExitSuccess
type AlexUserState = ()
alexInitUserState = ()
scanner str = runAlex str $ do
let loop = do tk <- alexMonadScan
if tk == EOF
then return [tk]
else do toks <- loop
return (tk:toks)
let
loop = do
-- Andreas Abel, 2023-12-30, issue #220:
-- Test that alex{G,S}etUserState are in scope.
() <- alexGetUserState
alexSetUserState ()
tk <- alexMonadScan
if tk == EOF
then return [tk]
else do
toks <- loop
return (tk:toks)
loop
test1 = case scanner " let in 012334\n=+*foo bar__'" of
Expand Down

0 comments on commit b0acbff

Please sign in to comment.