Safe Haskell | None |
---|---|
Language | Haskell2010 |
Parse token sequences.
Synopsis
- data Parser :: Type -> Type -> Type -> Type -> Type
- data Result e a
- data Slice a = Slice !Int !Int a
- parseSmallArray :: forall a e b. (forall s. Parser a e s b) -> SmallArray a -> Result e b
- parseSmallArrayEffectfully :: Parser a e s b -> SmallArray a -> ST s (Result e b)
- any :: e -> Parser a e s a
- opt :: Parser a e s (Maybe a)
- peek :: e -> Parser a e s a
- token :: Eq a => e -> a -> Parser a e s ()
- effect :: ST s b -> Parser a e s b
- fail :: e -> Parser a e s b
- trySatisfy :: (a -> Bool) -> Parser a e s Bool
- foldSepBy1 :: Parser a e s Bool -> (b -> Parser a e s b) -> b -> Parser a e s b
- sepBy1 :: Parser a e s Bool -> Parser a e s b -> Parser a e s ()
- skipWhile :: (a -> Bool) -> Parser a e s ()
Documentation
data Parser :: Type -> Type -> Type -> Type -> Type Source #
A non-resumable toke parser.
Instances
Monad (Parser a e s) Source # | |
Functor (Parser a e s) Source # | |
Applicative (Parser a e s) Source # | |
Defined in Data.Parser.Unsafe pure :: a0 -> Parser a e s a0 # (<*>) :: Parser a e s (a0 -> b) -> Parser a e s a0 -> Parser a e s b # liftA2 :: (a0 -> b -> c) -> Parser a e s a0 -> Parser a e s b -> Parser a e s c # (*>) :: Parser a e s a0 -> Parser a e s b -> Parser a e s b # (<*) :: Parser a e s a0 -> Parser a e s b -> Parser a e s a0 # |
The result of running a parser.
Failure e | An error message indicating what went wrong. |
Success !(Slice a) | The parsed value and the number of bytes remaining in parsed slice. |
Instances
Functor (Result e) | |
Foldable (Result e) | |
Defined in Data.Bytes.Parser.Types fold :: Monoid m => Result e m -> m # foldMap :: Monoid m => (a -> m) -> Result e a -> m # foldr :: (a -> b -> b) -> b -> Result e a -> b # foldr' :: (a -> b -> b) -> b -> Result e a -> b # foldl :: (b -> a -> b) -> b -> Result e a -> b # foldl' :: (b -> a -> b) -> b -> Result e a -> b # foldr1 :: (a -> a -> a) -> Result e a -> a # foldl1 :: (a -> a -> a) -> Result e a -> a # elem :: Eq a => a -> Result e a -> Bool # maximum :: Ord a => Result e a -> a # minimum :: Ord a => Result e a -> a # | |
(Eq e, Eq a) => Eq (Result e a) | |
(Show e, Show a) => Show (Result e 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.
Instances
Functor Slice | |
Foldable Slice | |
Defined in Data.Bytes.Parser.Types fold :: Monoid m => Slice m -> m # foldMap :: Monoid m => (a -> m) -> Slice a -> m # foldr :: (a -> b -> b) -> b -> Slice a -> b # foldr' :: (a -> b -> b) -> b -> Slice a -> b # foldl :: (b -> a -> b) -> b -> Slice a -> b # foldl' :: (b -> a -> b) -> b -> Slice a -> b # foldr1 :: (a -> a -> a) -> Slice a -> a # foldl1 :: (a -> a -> a) -> Slice a -> a # elem :: Eq a => a -> Slice a -> Bool # maximum :: Ord a => Slice a -> a # minimum :: Ord a => Slice a -> a # | |
Eq a => Eq (Slice a) | |
Show a => Show (Slice a) | |
parseSmallArray :: forall a e b. (forall s. Parser a e s b) -> SmallArray a -> Result e b Source #
parseSmallArrayEffectfully :: Parser a e s b -> SmallArray a -> ST s (Result e b) Source #
Primitives
any :: e -> Parser a e s a Source #
Consumes and returns the next token from the input. Fails if no tokens are left.
opt :: Parser a e s (Maybe a) Source #
Consume a token from the input or return Nothing
if
end of the stream has been reached. This parser never fails.
peek :: e -> Parser a e s a Source #
Returns the next token from the input without consuming it. Fails if no tokens are left.
Consumes the next token from the input. Fails if it is not equal to the expected value.
fail :: e -> Parser a e s b Source #
Consumes and returns the next token from the input. Fails if no tokens are left.
trySatisfy :: (a -> Bool) -> Parser a e s Bool Source #
Looks at the next token from the input. If the token matches
the predicate, consume the token and return True
. Otherwise,
do not consume the token and return False
. If no tokens
remain in the input, return False
. This parser never fails.
Control Flow
Fold over the tokens, repeatedly running step
followed
by separator
until separator
returns False
. This is
strict in the accumulator and always runs step
at least
once. There is no backtracking; any failure causes the whole
combinator to fail.
Fold over the tokens, repeatedly running step
followed
by separator
until separator
returns False
. The results
of step
are discarded, but in conjunction with effect
,
this can be used to populate an array or a builder. This
always runs step
at least once.
sepBy1 sep step === step *> (sep >>= bool (pure ()) (step *> (sep >>= bool (pure ()) (...))))