File: issue_119.x

package info (click to toggle)
alex 3.4.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 768 kB
  • sloc: haskell: 4,648; makefile: 138; yacc: 56; ansic: 4
file content (73 lines) | stat: -rw-r--r-- 1,704 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
-- -*- haskell -*-
{
-- Issue 119,
-- reported 2017-10-11 by Herbert Valerio Riedel,
-- fixed 2020-01-26 by Andreas Abel.
--
-- Problem was: the computed token length (in number of characters)
-- attached to AlexToken is tailored to UTF8 encoding and wrong
-- for LATIN1 encoding.

module Main where

import Control.Monad (unless)
import qualified Data.ByteString as B
import Data.Word
import System.Exit   (exitFailure)
}

%encoding "latin1"

:-

[\x01-\xff]+ { False }
[\x00]       { True  }

{
type AlexInput = B.ByteString

alexGetByte :: AlexInput -> Maybe (Word8,AlexInput)
alexGetByte = B.uncons

alexInputPrevChar :: AlexInput -> Char
alexInputPrevChar = undefined

-- generated by @alex@
alexScan :: AlexInput -> Int -> AlexReturn Bool

{-

GOOD cases:

("012\NUL3","012","\NUL3",3,3,False)
("\NUL0","\NUL","0",1,1,True)
("012","012","",3,3,False)

BAD case:

("0@P`p\128\144\160","0@P`p","",5,8,False)

expected:

("0@P`p\128\144\160","0@P`p\128\144\160","",8,8,False)

-}
main :: IO ()
main = do
    go (B.pack [0x30,0x31,0x32,0x00,0x33])                -- GOOD
    go (B.pack [0x00,0x30])                               -- GOOD
    go (B.pack [0x30,0x31,0x32])                          -- GOOD

    go (B.pack [0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xa0]) -- WAS: BAD
  where
    go inp = do
      case (alexScan inp 0) of
        -- expected invariant: len == B.length inp - B.length inp'
        AlexToken inp' len b -> do
          let diff = B.length inp - B.length inp'
          unless (len == diff) $ do
            putStrLn $ "ERROR: reported length and consumed length differ!"
            print (inp, B.take len inp, inp', len, diff, b)
            exitFailure
        _ -> undefined
}