Safe Haskell | None |
---|
This module defines 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
.
Implementation is based on Brzozowski derivatives.
- data Parser a s r
- feed :: Monoid s => s -> Parser a s r -> Parser a s r
- feedEof :: Monoid s => Parser a s r -> Parser a s r
- inspect :: Parser a s r -> ([(r, s)], Maybe (Maybe (r -> r), Parser a s r))
- results :: Monoid r => Parser a s r -> ([(r, s)], Maybe (r, Parser a s r))
- completeResults :: Parser a s r -> [(r, s)]
- resultPrefix :: Monoid r => Parser a s r -> (r, Parser a s r)
- failure :: Parser a s r
- more :: (s -> Parser a s r) -> Parser a s r
- eof :: (MonoidNull s, Monoid r) => Parser a s r
- anyToken :: FactorialMonoid s => Parser a s s
- token :: (Eq s, FactorialMonoid s) => s -> Parser a s s
- satisfy :: FactorialMonoid s => (s -> Bool) -> Parser a s s
- acceptAll :: Monoid s => Parser a s s
- string :: (LeftReductiveMonoid s, MonoidNull s) => s -> Parser a s s
- takeWhile :: (FactorialMonoid s, MonoidNull s) => (s -> Bool) -> Parser a s s
- takeWhile1 :: (FactorialMonoid s, MonoidNull s) => (s -> Bool) -> Parser a s s
- satisfyChar :: TextualMonoid s => (Char -> Bool) -> Parser a s s
- takeCharsWhile :: (TextualMonoid s, MonoidNull s) => (Char -> Bool) -> Parser a s s
- takeCharsWhile1 :: (TextualMonoid s, MonoidNull s) => (Char -> Bool) -> Parser a s s
- count :: (Monoid s, Monoid r) => Int -> Parser a s r -> Parser a s r
- skip :: (Monoid s, Monoid r) => Parser a s r' -> Parser a 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 :: (Alternative (Parser a s), Monoid s, Monoid r) => Parser a s r -> Parser a s r' -> Parser a s r
- mapType :: (Parser a s r -> Parser b s r) -> Parser a s r -> Parser b s r
- mapIncremental :: (Monoid s, Monoid a, Monoid b) => (a -> b) -> Parser p s a -> Parser p s b
- (<||>) :: Parser a s r -> Parser a s r -> Parser a s r
- (<<|>) :: Monoid s => Parser a s r -> Parser a s r -> Parser a s r
- (><) :: (MonoidApplicative f, Monoid a) => f a -> f a -> f a
- lookAhead :: Monoid s => Parser a s r -> Parser a s r
- notFollowedBy :: (Monoid s, Monoid r) => Parser a s r' -> Parser a s r
- and :: (Monoid s, Monoid r1, Monoid r2) => Parser a s r1 -> Parser a s r2 -> Parser a s (r1, r2)
- andThen :: (Monoid s, Monoid r1, Monoid r2) => Parser a s r1 -> Parser a s r2 -> Parser a s (r1, r2)
- isInfallible :: Parser a s r -> Bool
- showWith :: (Monoid s, Monoid r, Show s) => ((s -> Parser a s r) -> String) -> (r -> String) -> Parser a s r -> String
The Parser type
The central parser type. Its first parameter is the input monoid, the second the output.
Monoid s => Monad (Parser a s) | Usage of |
Monoid s => Functor (Parser a s) | Usage of |
Monoid s => MonadPlus (Parser LeftBiasedLocal s) | The |
Monoid s => MonadPlus (Parser Symmetric s) | The |
Monoid s => Applicative (Parser a s) | The |
Monoid s => Alternative (Parser LeftBiasedLocal s) | Left-biased choice. The right parser is used only if the left one utterly fails. |
Monoid s => Alternative (Parser Symmetric s) | The symmetric version of the |
(Alternative (Parser a s), Monoid s) => MonoidAlternative (Parser a s) | |
Monoid s => MonoidApplicative (Parser a s) | |
(Monoid s, Monoid r) => Monoid (Parser a s r) | Two parsers can be sequentially joined. |
Using a Parser
feed :: Monoid s => s -> Parser a s r -> Parser a s rSource
Feeds a chunk of the input to the parser.
results :: Monoid r => Parser a s r -> ([(r, s)], Maybe (r, Parser a s r))Source
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 a s r -> [(r, s)]Source
Like results
, but returns only the complete results with the corresponding unconsumed inputs.
resultPrefix :: Monoid r => Parser a s r -> (r, Parser a s r)Source
Like results
, but returns only the partial result prefix.
Parser primitives
eof :: (MonoidNull s, Monoid r) => Parser a s rSource
A parser that fails on any input and succeeds at its end.
anyToken :: FactorialMonoid s => Parser a s sSource
A parser that accepts any single input atom.
token :: (Eq s, FactorialMonoid s) => s -> Parser a s sSource
A parser that accepts a specific input atom.
satisfy :: FactorialMonoid s => (s -> Bool) -> Parser a s sSource
A parser that accepts an input atom only if it satisfies the given predicate.
string :: (LeftReductiveMonoid s, MonoidNull s) => s -> Parser a s sSource
A parser that consumes and returns the given prefix of the input.
takeWhile :: (FactorialMonoid s, MonoidNull s) => (s -> Bool) -> Parser a s sSource
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 a s sSource
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 a s sSource
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 a s sSource
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 a s sSource
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 a s r -> Parser a s rSource
Accepts the given number of occurrences of the argument parser.
skip :: (Monoid s, Monoid r) => Parser a s r' -> Parser a s rSource
Discards the results of the argument parser.
moptional :: (MonoidAlternative f, Monoid a) => f a -> f aSource
Like optional
, but restricted to Monoid
results.
concatMany :: (MonoidAlternative f, Monoid a) => f a -> f aSource
Zero or more argument occurrences like many
, but concatenated.
concatSome :: (MonoidAlternative f, Monoid a) => f a -> f aSource
One or more argument occurrences like some
, but concatenated.
manyTill :: (Alternative (Parser a s), Monoid s, Monoid r) => Parser a s r -> Parser a s r' -> Parser a s rSource
Repeats matching the first argument until the second one succeeds.
(><) :: (MonoidApplicative f, Monoid a) => f a -> f a -> f aSource
Lifted and potentially optimized monoid mappend
operation from the parameter type.
lookAhead :: Monoid s => Parser a s r -> Parser a s rSource
Behaves like the argument parser, but without consuming any input.
notFollowedBy :: (Monoid s, Monoid r) => Parser a s r' -> Parser a s rSource
Does not consume any input; succeeds (with mempty
result) iff the argument parser fails.
and :: (Monoid s, Monoid r1, Monoid r2) => Parser a s r1 -> Parser a s r2 -> Parser a s (r1, r2)Source
Parallel parser conjunction: the combined parser keeps accepting input as long as both arguments do.
andThen :: (Monoid s, Monoid r1, Monoid r2) => Parser a s r1 -> Parser a s r2 -> Parser a s (r1, r2)Source
A sequence parser that preserves incremental results, otherwise equivalent to liftA2
(,)
Utilities
isInfallible :: Parser a s r -> BoolSource