-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ fixed #119 ] latin1 encoding: each byte counts as 1 char
The computation of the length component of AlexToken was tailored to the utf8 encoding, and didn't work correctly for latin1. This is fixed by having a new flag ALEX_LATIN1 in templates/GenericTemplate.hs that turns on code that increases the length by 1 for each byte, while for utf8 something more sophisticated is done. The fix requires more template instances to be generated. To streamline the instance generation, now all 2^4 = 16 template instances are generated for the 4 flags - ghc - latin1 - nopred - debug To ensure consistent reference to the template instance, a function templateFileName residing both in src/Main and gen-alex-sdist/Main needs to be kept consistent, should more dimensions be added to the template. (Putting this function into a separate file that is included by both modules could be an option, but seemed not enough in the spirit of cabal-organized projects.)
- Loading branch information
1 parent
0198f73
commit ae525e3
Showing
7 changed files
with
154 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |