Copyright | (c) 2020 Composewell Technologies |
---|---|
License | BSD-3-Clause |
Maintainer | streamly@composewell.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | None |
Language | Haskell2010 |
Streamly.Internal.Data.ParserK
Contents
Description
Synopsis
- chainr :: ParserK b IO a -> ParserK b IO (a -> a -> a) -> a -> ParserK b IO a
- chainl :: ParserK b IO a -> ParserK b IO (a -> a -> a) -> a -> ParserK b IO a
- chainr1 :: ParserK b IO a -> ParserK b IO (a -> a -> a) -> ParserK b IO a
- chainl1 :: ParserK b IO a -> ParserK b IO (a -> a -> a) -> ParserK b IO a
- die :: forall a (m :: Type -> Type) b. String -> ParserK a m b
- data Input a
- data Step a (m :: Type -> Type) r
- fromPure :: forall b a (m :: Type -> Type). b -> ParserK a m b
- toParserK :: forall (m :: Type -> Type) a b. Monad m => Parser a m b -> ParserK a m b
- newtype ParserK a (m :: Type -> Type) b = MkParser {
- runParser :: forall r. (ParseResult b -> Int -> StepParser a m r) -> Int -> Int -> StepParser a m r
- data ParseResult b
- adapt :: forall (m :: Type -> Type) a b. Monad m => Parser a m b -> ParserK a m b
- fromEffect :: Monad m => m b -> ParserK a m b
- parserDone :: Applicative m => ParseResult b -> Int -> Input a -> m (Step a m b)
- toParser :: forall (m :: Type -> Type) a b. Monad m => ParserK a m b -> Parser a m b
- adaptC :: forall (m :: Type -> Type) a b. (Monad m, Unbox a) => Parser a m b -> ParserK (Array a) m b
- adaptCG :: forall (m :: Type -> Type) a b. Monad m => Parser a m b -> ParserK (Array a) m b
Documentation
chainr :: ParserK b IO a -> ParserK b IO (a -> a -> a) -> a -> ParserK b IO a Source #
chainr p op x
is like chainr1
but allows zero or more occurrences of
p
, separated by op
. If there are zero occurrences of p
, the value x
is returned.
chainl :: ParserK b IO a -> ParserK b IO (a -> a -> a) -> a -> ParserK b IO a Source #
chainl p op x
is like chainl1
but allows zero or more occurrences of
p
, separated by op
. If there are zero occurrences of p
, the value x
is returned.
chainr1 :: ParserK b IO a -> ParserK b IO (a -> a -> a) -> ParserK b IO a Source #
Like chainl1 but parses right associative application of the operator instead of left associative.
>>>
num = Parser.decimal
>>>
pow = Parser.char '^' *> pure (^)
>>>
expr = ParserK.chainr1 (StreamK.toParserK num) (StreamK.toParserK pow)
>>>
StreamK.parse expr $ StreamK.fromStream $ Stream.fromList "2^3^2"
Right 512
chainl1 :: ParserK b IO a -> ParserK b IO (a -> a -> a) -> ParserK b IO a Source #
chainl1 p op x
parses one or more occurrences of p
, separated by
op
. Returns a value obtained by a left associative application of all
functions returned by op
to the values returned by p
.
>>>
num = Parser.decimal
>>>
plus = Parser.char '+' *> pure (+)
>>>
expr = ParserK.chainl1 (StreamK.toParserK num) (StreamK.toParserK plus)
>>>
StreamK.parse expr $ StreamK.fromStream $ Stream.fromList "1+2+3"
Right 6
If you're building full expression parsers with operator precedence and
associativity, consider using makeExprParser
from the parser-combinators
package.
See also deintercalate
.
die :: forall a (m :: Type -> Type) b. String -> ParserK a m b Source #
A parser that always fails with an error message without consuming any input.
Pre-release
data Step a (m :: Type -> Type) r Source #
The intermediate result of running a parser step. The parser driver may
(1) stop with a final result (Done
) with no more inputs to be accepted,
(2) generate an intermediate result (Partial
) and accept more inputs, (3)
generate no result but wait for more input (Continue
), (4) or fail with an
error (Error
).
The Int is a count by which the current stream position should be adjusted before calling the next parsing step.
See the documentation of Step
for more details, this
has the same semantics.
Pre-release
fromPure :: forall b a (m :: Type -> Type). b -> ParserK a m b Source #
A parser that always yields a pure value without consuming any input.
Pre-release
toParserK :: forall (m :: Type -> Type) a b. Monad m => Parser a m b -> ParserK a m b Source #
Convert a Parser
to ParserK
.
Pre-release
newtype ParserK a (m :: Type -> Type) b Source #
A continuation passing style parser representation.
Constructors
MkParser | |
Fields
|
Instances
Monad m => MonadFail (ParserK a m) Source # | |
Defined in Streamly.Internal.Data.ParserK.Type | |
MonadIO m => MonadIO (ParserK a m) Source # | |
Defined in Streamly.Internal.Data.ParserK.Type | |
Monad m => Alternative (ParserK a m) Source # |
|
Monad m => Applicative (ParserK a m) Source # |
|
Defined in Streamly.Internal.Data.ParserK.Type | |
Functor m => Functor (ParserK a m) Source # | Map a function on the result i.e. on |
Monad m => Monad (ParserK a m) Source # | Monad composition can be used for lookbehind parsers, we can dynamically compose new parsers based on the results of the previously parsed values. |
Monad m => MonadPlus (ParserK a m) Source # |
|
data ParseResult b Source #
The parser's result.
Int is the position index in the stream relative to the position on entry i.e. when the parser started running. When the parser enters the position index is zero. If the parser consumed n elements then the new position index would be n. If the parser is backtracking then the position index would be negative.
Pre-release
Instances
Functor ParseResult Source # | Map a function over |
Defined in Streamly.Internal.Data.ParserK.Type Methods fmap :: (a -> b) -> ParseResult a -> ParseResult b # (<$) :: a -> ParseResult b -> ParseResult a # |
fromEffect :: Monad m => m b -> ParserK a m b Source #
See fromEffect
.
Pre-release
parserDone :: Applicative m => ParseResult b -> Int -> Input a -> m (Step a m b) Source #
A continuation to extract the result when a CPS parser is done.
toParser :: forall (m :: Type -> Type) a b. Monad m => ParserK a m b -> Parser a m b Source #
Convert a CPS style ParserK
to a direct style Parser
.
Pre-release