diff --git a/Setup.lhs b/Setup.lhs index 3ae97f3..b2a8610 100644 --- a/Setup.lhs +++ b/Setup.lhs @@ -65,6 +65,7 @@ wrappers :: [(FilePath,[String])] wrappers = [ ("AlexWrapper-basic", ["-DALEX_BASIC"]), ("AlexWrapper-basic-bytestring", ["-DALEX_BASIC_BYTESTRING"]), + ("AlexWrapper-strict-bytestring", ["-DALEX_STRICT_BYTESTRING"]), ("AlexWrapper-posn", ["-DALEX_POSN"]), ("AlexWrapper-posn-bytestring", ["-DALEX_POSN_BYTESTRING"]), ("AlexWrapper-monad", ["-DALEX_MONAD"]), diff --git a/templates/wrappers.hs b/templates/wrappers.hs index 052d26a..785ba5c 100644 --- a/templates/wrappers.hs +++ b/templates/wrappers.hs @@ -8,6 +8,12 @@ import qualified Data.ByteString.Lazy.Char8 as ByteString +#elif defined(ALEX_STRICT_BYTESTRING) + +import qualified Data.ByteString.Char8 as ByteString +import qualified Data.ByteString.Internal as ByteString +import qualified Data.ByteString.Unsafe as ByteString + #endif -- ----------------------------------------------------------------------------- @@ -268,11 +274,38 @@ alexInputPrevChar (c,_) = c -- alexScanTokens :: String -> [token] alexScanTokens str = go ('\n',str) where go inp@(_,str) = - case alexScan inp 0 of - AlexEOF -> [] - AlexError _ -> error "lexical error" - AlexSkip inp' len -> go inp' - AlexToken inp' len act -> act (ByteString.take (fromIntegral len) str) : go inp' + case alexScan inp 0 of + AlexEOF -> [] + AlexError _ -> error "lexical error" + AlexSkip inp' len -> go inp' + AlexToken inp' len act -> act (ByteString.take (fromIntegral len) str) : go inp' + + +#endif + +#ifdef ALEX_STRICT_BYTESTRING + +data AlexInput = AlexInput { alexChar :: {-# UNPACK #-}!Char + , alexStr :: {-# UNPACK #-}!ByteString.ByteString } + +alexGetChar (AlexInput _ cs) + | ByteString.null cs = Nothing + | otherwise = Just $! (ByteString.head cs, AlexInput c cs') + where + (c,cs') = (ByteString.w2c (ByteString.unsafeHead cs) + , ByteString.unsafeTail cs) + +alexInputPrevChar = alexChar + +-- alexScanTokens :: String -> [token] +alexScanTokens str = go (AlexInput '\n' str) + where go inp@(AlexInput _ str) = + case alexScan inp 0 of + AlexEOF -> [] + AlexError _ -> error "lexical error" + AlexSkip inp' len -> go inp' + AlexToken inp' len act -> act (ByteString.unsafeTake len str) : go inp' + #endif