Text.ParserCombinators.Incremental

incremental-parser-0.2.4.1: Generic parser library capable of providing partial results from partial input.

Safe Haskell | Safe
Language | Haskell2010

Text.ParserCombinators.Incremental

Contents

- The Parser type
- Using a Parser
- Parser primitives
  - Character primitives
- Parser combinators
- Utilities

Description

This module defines parsing combinators for incremental parsers.

The exported `Parser` type can provide partial parsing results from partial input, as long as the output is a `Monoid`. Construct a parser using the primitives and combinators, supply it with input using functions `feed` and `feedEof`, and extract the parsed output using `results`.

If your parser only ever uses the symmetric choice `<||>`, import the Text.ParserCombinators.Incremental.Symmetric module instead. Vice versa, if you always use the shortcutting `<<|>` choice, import Text.ParserCombinators.Incremental.LeftBiasedLocal instead of this module.

Implementation is based on Brzozowski derivatives.

Synopsis

- data Parser t s r
- feed :: Monoid s => s -> Parser t s r -> Parser t s r
- feedEof :: Monoid s => Parser t s r -> Parser t s r
- inspect :: Parser t s r -> ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
- results :: Monoid r => Parser t s r -> ([(r, s)], Maybe (r, Parser t s r))
- completeResults :: Parser t s r -> [(r, s)]
- resultPrefix :: Monoid r => Parser t s r -> (r, Parser t s r)
- failure :: Parser t s r
- (<?>) :: Monoid s => Parser t s r -> String -> Parser t s r
- more :: (s -> Parser t s r) -> Parser t s r
- eof :: (MonoidNull s, Monoid r) => Parser t s r
- anyToken :: FactorialMonoid s => Parser t s s
- token :: (Eq s, FactorialMonoid s) => s -> Parser t s s
- satisfy :: FactorialMonoid s => (s -> Bool) -> Parser t s s
- acceptAll :: Monoid s => Parser t s s
- string :: (LeftReductiveMonoid s, MonoidNull s) => s -> Parser t s s
- takeWhile :: (FactorialMonoid s, MonoidNull s) => (s -> Bool) -> Parser t s s
- takeWhile1 :: (FactorialMonoid s, MonoidNull s) => (s -> Bool) -> Parser t s s
- satisfyChar :: TextualMonoid s => (Char -> Bool) -> Parser t s s
- takeCharsWhile :: (TextualMonoid s, MonoidNull s) => (Char -> Bool) -> Parser t s s
- takeCharsWhile1 :: (TextualMonoid s, MonoidNull s) => (Char -> Bool) -> Parser t s s
- count :: (Monoid s, Monoid r) => Int -> Parser t s r -> Parser t s r
- skip :: (Monoid s, Monoid r) => Parser t s r' -> Parser t s r
- moptional :: (MonoidAlternative f, Monoid a) => f a -> f a
- concatMany :: (MonoidAlternative f, Monoid a) => f a -> f a
- concatSome :: (MonoidAlternative f, Monoid a) => f a -> f a
- manyTill :: (Monoid s, Monoid r) => Parser t s r -> Parser t s r' -> Parser t s r
- mapType :: (Parser t s r -> Parser b s r) -> Parser t s r -> Parser b s r
- mapIncremental :: (Monoid s, Monoid a, Monoid b) => (a -> b) -> Parser p s a -> Parser p s b
- (+<*>) :: MonoidApplicative f => f (a -> a) -> f a -> f a
- (<||>) :: Parser t s r -> Parser t s r -> Parser t s r
- (<<|>) :: Monoid s => Parser t s r -> Parser t s r -> Parser t s r
- (><) :: (MonoidApplicative f, Monoid a) => f a -> f a -> f a
- lookAhead :: Monoid s => Parser t s r -> Parser t s r
- notFollowedBy :: (Monoid s, Monoid r) => Parser t s r' -> Parser t s r
- and :: (Monoid s, Monoid r1, Monoid r2) => Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
- andThen :: (Monoid s, Monoid r1, Monoid r2) => Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
- isInfallible :: Parser t s r -> Bool
- showWith :: (Monoid s, Monoid r, Show s) => ((s -> Parser t s r) -> String) -> (r -> String) -> Parser t s r -> String
- defaultMany :: (Monoid s, Alternative (Parser t s)) => Parser t s r -> Parser t s [r]
- defaultSome :: (Monoid s, Alternative (Parser t s)) => Parser t s r -> Parser t s [r]

# The Parser type

data Parser t s r #

The central parser type. Its first parameter is the subtype of the parser, the second is the input monoid type, the third the output type.

Instances

Monoid s => Monad (Parser t s) # | Usage of `>>=` destroys the incrementality of its left argument's parsing results, but `>>` is safe to use.
Methods (>>=) :: Parser t s a -> (a -> Parser t s b) -> Parser t s b # (>>) :: Parser t s a -> Parser t s b -> Parser t s b # return :: a -> Parser t s a # fail :: String -> Parser t s a #
Monoid s => Functor (Parser t s) # | Usage of `fmap` destroys the incrementality of parsing results, if you need it use `mapIncremental` instead.
Methods fmap :: (a -> b) -> Parser t s a -> Parser t s b # (<$) :: a -> Parser t s b -> Parser t s a #
Monoid s => Applicative (Parser t s) # | The `<*>` combinator requires its both arguments to provide complete parsing results, whereas `*>` and `<*` preserve the incremental results.
Methods pure :: a -> Parser t s a # (<*>) :: Parser t s (a -> b) -> Parser t s a -> Parser t s b # (*>) :: Parser t s a -> Parser t s b -> Parser t s b # (<*) :: Parser t s a -> Parser t s b -> Parser t s a #
Monoid s => Alternative (Parser LeftBiasedLocal s) # | Left-biased choice. The right parser is used only if the left one utterly fails.
Methods empty :: Parser LeftBiasedLocal s a # (<|>) :: Parser LeftBiasedLocal s a -> Parser LeftBiasedLocal s a -> Parser LeftBiasedLocal s a # some :: Parser LeftBiasedLocal s a -> Parser LeftBiasedLocal s [a] # many :: Parser LeftBiasedLocal s a -> Parser LeftBiasedLocal s [a] #
Monoid s => Alternative (Parser Symmetric s) # | The symmetric version of the `<|>` choice combinator.
Methods empty :: Parser Symmetric s a # (<|>) :: Parser Symmetric s a -> Parser Symmetric s a -> Parser Symmetric s a # some :: Parser Symmetric s a -> Parser Symmetric s [a] # many :: Parser Symmetric s a -> Parser Symmetric s [a] #
Monoid s => MonadPlus (Parser LeftBiasedLocal s) # | The `MonadPlus` instances are the same as the `Alternative` instances.
Methods mzero :: Parser LeftBiasedLocal s a # mplus :: Parser LeftBiasedLocal s a -> Parser LeftBiasedLocal s a -> Parser LeftBiasedLocal s a #
Monoid s => MonadPlus (Parser Symmetric s) # | The `MonadPlus` instances are the same as the `Alternative` instances.
Methods mzero :: Parser Symmetric s a # mplus :: Parser Symmetric s a -> Parser Symmetric s a -> Parser Symmetric s a #
(Alternative (Parser t s), Monoid s) => MonoidAlternative (Parser t s) # |
Methods moptional :: Monoid a => Parser t s a -> Parser t s a # concatMany :: Monoid a => Parser t s a -> Parser t s a # concatSome :: Monoid a => Parser t s a -> Parser t s a #
Monoid s => MonoidApplicative (Parser t s) # | The `+<*>` operator is specialized to return incremental parsing results.
Methods (+<*>) :: Parser t s (a -> a) -> Parser t s a -> Parser t s a # (><) :: Monoid a => Parser t s a -> Parser t s a -> Parser t s a #
(Monoid s, Monoid r) => Monoid (Parser t s r) # | Two parsers can be sequentially joined.
Methods mempty :: Parser t s r # mappend :: Parser t s r -> Parser t s r -> Parser t s r # mconcat :: [Parser t s r] -> Parser t s r #

# Using a Parser

feed :: Monoid s => s -> Parser t s r -> Parser t s r #

Feeds a chunk of the input to the parser.

feedEof :: Monoid s => Parser t s r -> Parser t s r #

Signals the end of the input.

inspect :: Parser t s r -> ([(r, s)], Maybe (Maybe (r -> r), Parser t s r)) #

Like `results`, but more general: doesn't assume that the result type is a `Monoid`.

results :: Monoid r => Parser t s r -> ([(r, s)], Maybe (r, Parser t s r)) #

Extracts all available parsing results from a `Parser`. The first component of the result pair is a list of complete results together with the unconsumed remainder of the input. If the parsing can continue further, the second component of the pair provides the partial result prefix together with the parser for the rest of the input.

completeResults :: Parser t s r -> [(r, s)] #

Like `results`, but returns only the complete results with the corresponding unconsumed inputs.

resultPrefix :: Monoid r => Parser t s r -> (r, Parser t s r) #

Like `results`, but returns only the partial result prefix.

# Parser primitives

failure :: Parser t s r #

(<?>) :: Monoid s => Parser t s r -> String -> Parser t s r infix 0 #

Name a parser for error reporting in case it fails.

more :: (s -> Parser t s r) -> Parser t s r #

eof :: (MonoidNull s, Monoid r) => Parser t s r #

A parser that fails on any non-empty input and succeeds at its end.

anyToken :: FactorialMonoid s => Parser t s s #

A parser that accepts any single input atom.

token :: (Eq s, FactorialMonoid s) => s -> Parser t s s #

A parser that accepts a specific input atom.

satisfy :: FactorialMonoid s => (s -> Bool) -> Parser t s s #

A parser that accepts an input atom only if it satisfies the given predicate.

acceptAll :: Monoid s => Parser t s s #

A parser that accepts and consumes all input.

string :: (LeftReductiveMonoid s, MonoidNull s) => s -> Parser t s s #

A parser that consumes and returns the given prefix of the input.

takeWhile :: (FactorialMonoid s, MonoidNull s) => (s -> Bool) -> Parser t s s #

A parser accepting the longest sequence of input atoms that match the given predicate; an optimized version of 'concatMany . satisfy'.

takeWhile1 :: (FactorialMonoid s, MonoidNull s) => (s -> Bool) -> Parser t s s #

A parser accepting the longest non-empty sequence of input atoms that match the given predicate; an optimized version of 'concatSome . satisfy'.

## Character primitives

satisfyChar :: TextualMonoid s => (Char -> Bool) -> Parser t s s #

Specialization of `satisfy` on `TextualMonoid` inputs, accepting an input character only if it satisfies the given predicate.

takeCharsWhile :: (TextualMonoid s, MonoidNull s) => (Char -> Bool) -> Parser t s s #

Specialization of `takeWhile` on `TextualMonoid` inputs, accepting the longest sequence of input characters that match the given predicate; an optimized version of 'concatMany . satisfyChar'.

takeCharsWhile1 :: (TextualMonoid s, MonoidNull s) => (Char -> Bool) -> Parser t s s #

Specialization of `takeWhile1` on `TextualMonoid` inputs, accepting the longest non-empty sequence of input atoms that match the given predicate; an optimized version of 'concatSome . satisfyChar'.

# Parser combinators

count :: (Monoid s, Monoid r) => Int -> Parser t s r -> Parser t s r #

Accepts the given number of occurrences of the argument parser.

skip :: (Monoid s, Monoid r) => Parser t s r' -> Parser t s r #

Discards the results of the argument parser.

moptional :: (MonoidAlternative f, Monoid a) => f a -> f a #

Like `optional`, but restricted to `Monoid` results.

concatMany :: (MonoidAlternative f, Monoid a) => f a -> f a #

Zero or more argument occurrences like `many`, but concatenated.

concatSome :: (MonoidAlternative f, Monoid a) => f a -> f a #

One or more argument occurrences like `some`, but concatenated.

manyTill :: (Monoid s, Monoid r) => Parser t s r -> Parser t s r' -> Parser t s r #

Repeats matching the first argument until the second one succeeds.

mapType :: (Parser t s r -> Parser b s r) -> Parser t s r -> Parser b s r #

mapIncremental :: (Monoid s, Monoid a, Monoid b) => (a -> b) -> Parser p s a -> Parser p s b #

Like `fmap`, but capable of mapping partial results, being restricted to `Monoid` types only.

(+<*>) :: MonoidApplicative f => f (a -> a) -> f a -> f a #

A variant of the Applicative's `<*>` operator specialized for endomorphic functions.

(<||>) :: Parser t s r -> Parser t s r -> Parser t s r infixl 3 #

(<<|>) :: Monoid s => Parser t s r -> Parser t s r -> Parser t s r infixl 3 #

(><) :: (MonoidApplicative f, Monoid a) => f a -> f a -> f a #

Lifted and potentially optimized monoid `mappend` operation from the parameter type.

lookAhead :: Monoid s => Parser t s r -> Parser t s r #

Behaves like the argument parser, but without consuming any input.

notFollowedBy :: (Monoid s, Monoid r) => Parser t s r' -> Parser t s r #

Does not consume any input; succeeds (with `mempty` result) iff the argument parser fails.

and :: (Monoid s, Monoid r1, Monoid r2) => Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2) #

Parallel parser conjunction: the combined parser keeps accepting input as long as both arguments do.

andThen :: (Monoid s, Monoid r1, Monoid r2) => Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2) #

A sequence parser that preserves incremental results, otherwise equivalent to `liftA2` (,)

# Utilities

isInfallible :: Parser t s r -> Bool #

showWith :: (Monoid s, Monoid r, Show s) => ((s -> Parser t s r) -> String) -> (r -> String) -> Parser t s r -> String #

defaultMany :: (Monoid s, Alternative (Parser t s)) => Parser t s r -> Parser t s [r] #

defaultSome :: (Monoid s, Alternative (Parser t s)) => Parser t s r -> Parser t s [r] #