|
| 1 | +-- -*- haskell -*- |
| 2 | +{ |
| 3 | +-- Issue 119, |
| 4 | +-- reported 2017-10-11 by Herbert Valerio Riedel, |
| 5 | +-- fixed 2020-01-26 by Andreas Abel. |
| 6 | +-- |
| 7 | +-- Problem was: the computed token length (in number of characters) |
| 8 | +-- attached to AlexToken is tailored to UTF8 encoding and wrong |
| 9 | +-- for LATIN1 encoding. |
| 10 | + |
| 11 | +module Main where |
| 12 | + |
| 13 | +import Control.Monad (unless) |
| 14 | +import qualified Data.ByteString as B |
| 15 | +import Data.Word |
| 16 | +import System.Exit (exitFailure) |
| 17 | +} |
| 18 | + |
| 19 | +%encoding "latin1" |
| 20 | + |
| 21 | +:- |
| 22 | + |
| 23 | +[\x01-\xff]+ { False } |
| 24 | +[\x00] { True } |
| 25 | + |
| 26 | +{ |
| 27 | +type AlexInput = B.ByteString |
| 28 | + |
| 29 | +alexGetByte :: AlexInput -> Maybe (Word8,AlexInput) |
| 30 | +alexGetByte = B.uncons |
| 31 | + |
| 32 | +alexInputPrevChar :: AlexInput -> Char |
| 33 | +alexInputPrevChar = undefined |
| 34 | + |
| 35 | +-- generated by @alex@ |
| 36 | +alexScan :: AlexInput -> Int -> AlexReturn Bool |
| 37 | + |
| 38 | +{- |
| 39 | +
|
| 40 | +GOOD cases: |
| 41 | +
|
| 42 | +("012\NUL3","012","\NUL3",3,3,False) |
| 43 | +("\NUL0","\NUL","0",1,1,True) |
| 44 | +("012","012","",3,3,False) |
| 45 | +
|
| 46 | +BAD case: |
| 47 | +
|
| 48 | +("0@P`p\128\144\160","0@P`p","",5,8,False) |
| 49 | +
|
| 50 | +expected: |
| 51 | +
|
| 52 | +("0@P`p\128\144\160","0@P`p\128\144\160","",8,8,False) |
| 53 | +
|
| 54 | +-} |
| 55 | +main :: IO () |
| 56 | +main = do |
| 57 | + go (B.pack [0x30,0x31,0x32,0x00,0x33]) -- GOOD |
| 58 | + go (B.pack [0x00,0x30]) -- GOOD |
| 59 | + go (B.pack [0x30,0x31,0x32]) -- GOOD |
| 60 | + |
| 61 | + go (B.pack [0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xa0]) -- WAS: BAD |
| 62 | + where |
| 63 | + go inp = do |
| 64 | + case (alexScan inp 0) of |
| 65 | + -- expected invariant: len == B.length inp - B.length inp' |
| 66 | + AlexToken inp' len b -> do |
| 67 | + let diff = B.length inp - B.length inp' |
| 68 | + unless (len == diff) $ do |
| 69 | + putStrLn $ "ERROR: reported length and consumed length differ!" |
| 70 | + print (inp, B.take len inp, inp', len, diff, b) |
| 71 | + exitFailure |
| 72 | + _ -> undefined |
| 73 | +} |
0 commit comments