HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulebytesmith-0.3.11.1Haskell2010

Data.Bytes.Parser

Parse non-resumable sequence of bytes. To parse a byte sequence as text, use the Ascii, Latin, and Utf8 modules instead. Functions for parsing decimal-encoded numbers are found in those modules.

  • 3 types
  • 52 values

Types

3 declarations
newtypenewtype Parser a b (c :: TYPE r) where
#

A non-resumable parser.

Instances4Monad, Functor, Applicative, Alternative
  • Monad (Parser e s)Defined in bytesmith-0.3.11.1 · Data.Bytes.Parser.Internal
  • Functor (Parser e s)Defined in bytesmith-0.3.11.1 · Data.Bytes.Parser.Internal
  • Applicative (Parser e s)Defined in bytesmith-0.3.11.1 · Data.Bytes.Parser.Internal
  • Monoid e => Alternative (Parser e s)Defined in bytesmith-0.3.11.1 · Data.Bytes.Parser.Internal

    Combines the error messages using <> when both parsers fail.

datadata Result e a
#

The result of running a parser.

Constructors

  • Failure e

    An error message indicating what went wrong.

  • Success !(Slice a)

    The parsed value and the number of bytes remaining in parsed slice.

Instances4Functor, Foldable, Eq, Show
  • Functor (Result e)Defined in bytesmith-0.3.11.1 · Data.Bytes.Parser.Types
  • Foldable (Result e)Defined in bytesmith-0.3.11.1 · Data.Bytes.Parser.Types
  • (Eq e, Eq a) => Eq (Result e a)Defined in bytesmith-0.3.11.1 · Data.Bytes.Parser.Types
  • (Show e, Show a) => Show (Result e a)Defined in bytesmith-0.3.11.1 · Data.Bytes.Parser.Types
datadata Slice a
#

Slicing metadata (an offset and a length) accompanied by a value. This does not represent a slice into the value. This type is intended to be used as the result of an executed parser. In this context the slicing metadata describe a slice into to the array (or byte array) that from which the value was parsed.

It is often useful to check the length when a parser succeeds since a non-zero length indicates that there was additional unconsumed input. The offset is only ever needed to construct a new slice (via Bytes or SmallVector) from the remaining input.

Constructors

  • Slice
    • offset :: !Int

      Offset into the array.

    • length :: !Int

      Length of the slice.

    • value :: a

      The structured data that was successfully parsed.

Instances4Functor, Foldable, Eq, Show
  • Functor SliceDefined in bytesmith-0.3.11.1 · Data.Bytes.Parser.Types
  • Foldable SliceDefined in bytesmith-0.3.11.1 · Data.Bytes.Parser.Types
  • Eq a => Eq (Slice a)Defined in bytesmith-0.3.11.1 · Data.Bytes.Parser.Types
  • Show a => Show (Slice a)Defined in bytesmith-0.3.11.1 · Data.Bytes.Parser.Types

Run Parsers

0 declarations

Result

valueparseBytes :: (forall s. Parser e s a) -> Bytes -> Result e a
#

Parse a byte sequence. This can succeed even if the entire slice was not consumed by the parser.

One Byte

1 declaration
valueany :: e -> Parser e s Word8
#

Consumes and returns the next byte in the input. Fails if no characters are left.

Many Bytes

5 declarations
valuetake :: e -> Int -> Parser e s Bytes
#

Take the given number of bytes. Fails if there is not enough remaining input.

valuetakeN :: e -> Nat n -> Parser e s (BytesN n)
#

Variant of take that tracks the length of the result in the result type.

valuetakeUpTo :: Int -> Parser e s Bytes
#

Take at most the given number of bytes. This is greedy. It will consume as many bytes as there are available until it has consumed n bytes. This never fails.

valuetakeTrailedBy :: e -> Word8 -> Parser e s Bytes
#

Take bytes until the specified byte is encountered. Consumes the matched byte as well. Fails if the byte is not present. Visually, the cursor advancement and resulting Bytes for takeTrailedBy 0x19 look like this:

 0x10 0x13 0x08 0x15 0x19 0x23 0x17 | input
|---->---->---->---->----|          | cursor
{\----*----*----*----\}               | result bytes

Skip

5 declarations
valueskipTrailedBy :: e -> Word8 -> Parser e s ()
#

Skip all characters until the character from the is encountered and then consume the matching byte as well.

valueskipTrailedBy2
  1. :: e

    Error message

  2. -> Word8

    First trailer, False indicates that this was encountered

  3. -> Word8

    Second trailer, True indicates that this was encountered

  4. -> Parser e s Bool
#

Skip all bytes until either of the bytes in encountered. Then, consume the matched byte. True indicates that the first argument byte was encountered. False indicates that the second argument byte was encountered.

valueskipTrailedBy2#
  1. :: e

    Error message

  2. -> Word8

    First trailer, 0 indicates that this was encountered

  3. -> Word8

    Second trailer, 1 indicates that this was encountered

  4. -> Parser e s Int#
#
valueskipTrailedBy3#
  1. :: e

    Error message

  2. -> Word8

    First trailer, 0 indicates that this was encountered

  3. -> Word8

    Second trailer, 1 indicates that this was encountered

  4. -> Word8

    Third trailer, 2 indicates that this was encountered

  5. -> Parser e s Int#
#

Match

5 declarations
valuebytes :: e -> Bytes -> Parser e s ()
#

Consume input matching the byte sequence.

valuesatisfy :: e -> (Word8 -> Bool) -> Parser e s Word8
#

The parser satisfy p succeeds for any byte for which the predicate p returns True. Returns the byte that is actually parsed.

valuesatisfyWith :: e -> (Word8 -> a) -> (a -> Bool) -> Parser e s a
#

The parser satisfyWith f p transforms a byte, and succeeds if the predicate p returns True on the transformed value. The parser returns the transformed byte that was parsed.

End of Input

4 declarations
valueendOfInput :: e -> Parser e s ()
#

Fails if there is still more input remaining.

valueisEndOfInput :: Parser e s Bool
#

Returns true if there are no more bytes in the input. Returns false otherwise. Always succeeds.

Scanning

1 declaration
valuescan :: state -> (state -> Word8 -> Maybe state) -> Parser e s state
#

A stateful scanner. The predicate consumes and transforms a state argument, and each transformed state is passed to successive invocations of the predicate on each byte of the input until one returns Nothing or the input ends.

This parser does not fail. It will return the initial state if the predicate returns Nothing on the first byte of input.

Note: Because this parser does not fail, do not use it with combinators such a many, because such parsers loop until a failure occurs. Careless use will thus result in an infinite loop.

Lookahead

2 declarations
valuepeek :: Parser e s (Maybe Word8)
#

Match any byte, to perform lookahead. Returns Nothing if end of input has been reached. Does not consume any input.

Note: Because this parser does not fail, do not use it with combinators such as many, because such as many, because such parsers loop until a failure occurs. Careless use will thus result in an infinite loop.

valuepeek' :: e -> Parser e s Word8
#

Match any byte, to perform lookahead. Does not consume any input, but will fail if end of input has been reached.

Control Flow

5 declarations
valuefail
  1. :: e

    Error message

  2. -> Parser e s a
#

Fail with the provided error message.

valueorElse :: Parser x s a -> Parser e s a -> Parser e s a
#

There is a law-abiding instance of Alternative for Parser. However, it is not terribly useful since error messages seldom have a Monoid instance. This function is a variant of <|> that is right-biased in its treatment of error messages. Consequently, orElse lacks an identity. See attoparsec issue #122 for more discussion of this topic.

valueannotate :: Parser x s a -> e -> Parser e s a
#

Annotate a parser. If the parser fails, the error will be returned.

Repetition

1 declaration
valuereplicate
  1. :: (Contiguous arr, Element arr a)
  2. => Int

    Number of times to run the parser

  3. -> Parser e s a

    Parser

  4. -> Parser e s (arr a)
#

Replicate a parser n times, writing the results into an array of length n. For Array and SmallArray, this is lazy in the elements, so be sure the they result of the parser is evaluated appropriately to avoid unwanted thunks.

Subparsing

4 declarations
valuedelimit
  1. :: e

    Error message when not enough bytes are present

  2. -> e

    Error message when delimited parser does not consume all input

  3. -> Int

    Exact number of bytes delimited parser is expected to consume

  4. -> Parser e s a

    Parser to execute in delimited context

  5. -> Parser e s a
#

Run a parser in a delimited context, failing if the requested number of bytes are not available or if the delimited parser does not consume all input. This combinator can be understood as a composition of take, effect, parseBytesEffectfully, and endOfInput. It is provided as a single combinator because for convenience and because it is easy to make mistakes when manually assembling the aforementioned parsers. The pattern of prefixing an encoding with its length is common. This is discussed more in attoparsec issue #129.

delimit e1 e2 n remaining === take e1 n
valuemeasure :: Parser e s a -> Parser e s (Int, a)
#

Augment a parser with the number of bytes that were consume while it executed.

valuemeasure_ :: Parser e s a -> Parser e s Int
#

Run a parser and discard the result, returning instead the number of bytes that the parser consumed.

Lift Effects

1 declaration
valueeffect :: ST s a -> Parser e s a
#

Lift an effectful computation into a parser.

Box Result

2 declarations

Unbox Result

2 declarations

Specialized Bind

7 declarations

Sometimes, GHC ends up building join points in a way that boxes arguments unnecessarily. In this situation, special variants of monadic >>= can be helpful. If C#, I#, etc. never get used in your original source code, GHC will not introduce them.

Specialized Pure

1 declaration

Specialized Fail

1 declaration