Skip to content

Commit 261aa08

Browse files
Replace undefined with more descriptive error (#262)
* Replace undefined with more descriptive error * Make sure alexScanUser is inlined * Add missing issue_262.x * Add LANGUAGE ScopedTypeVariables to issue_262.x --------- Co-authored-by: Andreas Abel <[email protected]>
1 parent fbbf632 commit 261aa08

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

alex.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ extra-source-files:
9090
tests/issue_119.x
9191
tests/issue_141.x
9292
tests/issue_197.x
93+
tests/issue_262.x
9394
tests/strict_text_typeclass.x
9495
tests/posn_typeclass_strict_text.x
9596
tests/tokens_monadUserState_strict_text.x

data/AlexTemplate.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,12 @@ data AlexReturn a
9494

9595
-- alexScan :: AlexInput -> StartCode -> AlexReturn a
9696
alexScan input__ IBOX(sc)
97-
= alexScanUser undefined input__ IBOX(sc)
97+
= alexScanUser (error "alex rule requiring context was invoked by alexScan; use alexScanUser instead?") input__ IBOX(sc)
98+
99+
-- If the generated alexScan/alexScanUser functions are called multiple times
100+
-- in the same file, alexScanUser gets broken out into a separate function and
101+
-- increases memory usage. Make sure GHC inlines this function and optimizes it.
102+
{-# INLINE alexScanUser #-}
98103

99104
alexScanUser user__ input__ IBOX(sc)
100105
= case alex_scan_tkn user__ input__ ILIT(0) input__ sc AlexNone of

tests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ TESTS = \
5858
issue_119.x \
5959
issue_141.x \
6060
issue_197.x \
61+
issue_262.x \
6162
monad_typeclass.x \
6263
monad_typeclass_bytestring.x \
6364
monadUserState_typeclass.x \

tests/issue_262.x

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
-- https://github.com/haskell/alex/pull/262
3+
-- https://gitlab.haskell.org/ghc/ghc/-/issues/25609
4+
--
5+
-- Error happens when using alexScan with a lexer that
6+
-- inspects the context.
7+
8+
{-# LANGUAGE ScopedTypeVariables #-}
9+
10+
import Control.Exception
11+
import Data.List (isInfixOf)
12+
}
13+
14+
%wrapper "basic"
15+
16+
:-
17+
.* / { \state _ _ _ -> state == 'x' } { id }
18+
19+
{
20+
main :: IO ()
21+
main = do
22+
result <- try $ evaluate $ alexScan ('\n', [], "") 0 `seq` ()
23+
case result of
24+
Left (e :: SomeException)
25+
| "use alexScanUser instead" `isInfixOf` show e
26+
-> pure ()
27+
_ -> error $ "Got unexpected result: " ++ show result
28+
}

0 commit comments

Comments
 (0)