Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue 220 #254

Merged
merged 2 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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