Scanner

scanner-0.2: Fast non-backtracking incremental combinator parsing for bytestrings

Safe Haskell | None
Language | Haskell2010

Scanner

Description

Fast not-backtracking incremental scanner for bytestrings

Unlike attoparsec or most of other parser combinator libraries, scanner doesn't support backtracking. But you probably don't need it anyway, at least if you need fast parser.

Scanner processes input incrementally. When more input is needed, scanner returns `More` continuation. All the already processed input is discarded.

Synopsis

- data Scanner a
- data Result r
  - = Done ByteString r
  - | Fail ByteString String
  - | More (ByteString -> Result r)
- scan :: Scanner r -> ByteString -> Result r
- scanOnly :: Scanner a -> ByteString -> Either String a
- scanLazy :: Scanner a -> ByteString -> Either String a
- scanWith :: Monad m => m ByteString -> Scanner a -> ByteString -> m (Result a)
- anyWord8 :: Scanner Word8
- anyChar8 :: Scanner Char
- word8 :: Word8 -> Scanner ()
- char8 :: Char -> Scanner ()
- take :: Int -> Scanner ByteString
- takeWhile :: (Word8 -> Bool) -> Scanner ByteString
- takeWhileChar8 :: (Char -> Bool) -> Scanner ByteString
- string :: ByteString -> Scanner ()
- skipWhile :: (Word8 -> Bool) -> Scanner ()
- skipSpace :: Scanner ()
- lookAhead :: Scanner (Maybe Word8)
- lookAheadChar8 :: Scanner (Maybe Char)

# Documentation

data Scanner a #

CPS scanner without backtracking

Instances

Monad Scanner # |
Methods (>>=) :: Scanner a -> (a -> Scanner b) -> Scanner b # (>>) :: Scanner a -> Scanner b -> Scanner b # return :: a -> Scanner a # fail :: String -> Scanner a #
Functor Scanner # |
Methods fmap :: (a -> b) -> Scanner a -> Scanner b # (<$) :: a -> Scanner b -> Scanner a #
Applicative Scanner # |
Methods pure :: a -> Scanner a # (<*>) :: Scanner (a -> b) -> Scanner a -> Scanner b # liftA2 :: (a -> b -> c) -> Scanner a -> Scanner b -> Scanner c # (*>) :: Scanner a -> Scanner b -> Scanner b # (<*) :: Scanner a -> Scanner b -> Scanner a #

data Result r #

Scanner result

Constructors

Done ByteString r | Successful result with the rest of input
Fail ByteString String | Scanner failed with rest of input and error message
More (ByteString -> Result r) | Need more input

scan :: Scanner r -> ByteString -> Result r #

Run scanner with the input

scanOnly :: Scanner a -> ByteString -> Either String a #

Scan the complete input, without resupplying

scanLazy :: Scanner a -> ByteString -> Either String a #

Scan lazy bytestring by resupplying scanner with chunks

scanWith :: Monad m => m ByteString -> Scanner a -> ByteString -> m (Result a) #

Scan with the provided resupply action

anyWord8 :: Scanner Word8 #

Consume the next word

It fails if end of input

anyChar8 :: Scanner Char #

Consume the next 8-bit char

It fails if end of input

word8 :: Word8 -> Scanner () #

Consume the specified word or fail

char8 :: Char -> Scanner () #

Consume the specified 8-bit char or fail

take :: Int -> Scanner ByteString #

Take the specified number of bytes

takeWhile :: (Word8 -> Bool) -> Scanner ByteString #

Take input while the predicate is `True`

takeWhileChar8 :: (Char -> Bool) -> Scanner ByteString #

Take input while the predicate is `True`

string :: ByteString -> Scanner () #

Consume the specified string

Warning: it is not optimized yet, so for for small string it is better to consume it byte-by-byte using `word8`

skipWhile :: (Word8 -> Bool) -> Scanner () #

Skip any input while the preducate is `True`

skipSpace :: Scanner () #

Skip space

lookAhead :: Scanner (Maybe Word8) #

Return the next byte, if any, without consuming it

lookAheadChar8 :: Scanner (Maybe Char) #

Return the next byte, if any, without consuming it