Skip to content

Commit 804868f

Browse files
committed
[ fixed #71 ] do not produce tokens of length 0
Changed `alex_scan_tkn` to return `AlexNone` instead of `AlexLastAcc` or `AlexLastSkip` if length of newly processed input is still 0. This fixes the problem that Alex would produce an infinite sequence of tokens of length 0 when the DFA was stuck in a state accepting a nullable token. Now it will either produce EOF (if at end of input) or a lexer error (if not at the end of input). This is the intuitively expected behavior.
1 parent 6c4db72 commit 804868f

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

templates/GenericTemplate.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ alexScanUser user__ input__ IBOX(sc)
152152
alex_scan_tkn user__ orig_input len input__ s last_acc =
153153
input__ `seq` -- strict in the input
154154
let
155-
new_acc = (check_accs (alex_accept `quickIndex` IBOX(s)))
155+
new_acc = (if GTE(ILIT(0),len)
156+
then AlexNone -- issue #71, do not output tokens of length 0
157+
else check_accs (alex_accept `quickIndex` IBOX(s)))
156158
in
157159
new_acc `seq`
158160
case alexGetByte input__ of

tests/issue_71.x

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ expected_result = [A,A,Whitespaces,A,A,Whitespaces,A]
3333

3434
main :: IO ()
3535
main
36-
-- Since the whitespaces token is nullable, Alex
37-
-- will recognize an infinite number of those
38-
-- at the end of file. This behavior is problematic,
39-
-- but we don't fix it here.
40-
-- We just test here whether the expected result
41-
-- is a prefix of the produced result.
42-
| take (length expected_result) result == expected_result = do
36+
| result == expected_result = do
4337
exitWith ExitSuccess
4438
| otherwise = do
4539
print $ take 20 result

0 commit comments

Comments
 (0)