simple-parser-0.6.0: Simple parser combinators

SimpleParser.Parser

Description

ParserT is the core monad transformer for parsing.

Synopsis

# Documentation

newtype ParserT l s e m a Source #

A ParserT is a stateerrorlist transformer useful for parsing. All MTL instances are for this transformer only. If, for example, your effect has its own MonadState instance, you'll have to use 'lift get' instead of get.

Constructors

 ParserT FieldsrunParserT :: s -> m (Maybe (ParseResult l s e a))

#### Instances

Instances details

type Parser l s e a = ParserT l s e Identity a Source #

Use Parser if you have no need for other monadic effects.

runParser :: Parser l s e a -> s -> Maybe (ParseResult l s e a) Source #

Runs a non-effectful parser from an inital state and collects all results.

pureParser :: Monad m => a -> ParserT l s e m a Source #

Applicative pure

bindParser :: Monad m => ParserT l s e m a -> (a -> ParserT l s e m b) -> ParserT l s e m b Source #

failParser :: Monad m => Text -> ParserT l s e m a Source #

A simple failing parser

liftParser :: Monad m => m a -> ParserT l s e m a Source #

hoistParser :: (forall x. m x -> n x) -> ParserT l s e m a -> ParserT l s e n a Source #

catchJustParser :: Monad m => (e -> Maybe b) -> ParserT l s e m a -> (b -> ParserT l s e m a) -> ParserT l s e m a Source #

Catch only a subset of custom errors. This preserves label information vs rethrowing.

throwParser :: Monad m => e -> ParserT l s e m a Source #

Throws a custom error

catchParser :: Monad m => ParserT l s e m a -> (e -> ParserT l s e m a) -> ParserT l s e m a Source #

Catches a custom error

emptyParser :: Monad m => ParserT l s e m a Source #

The empty parser

orParser :: Monad m => ParserT l s e m a -> ParserT l s e m a -> ParserT l s e m a Source #

Yields from the first parser of the two that returns a successfull result. Otherwise will merge and yield all errors.

greedyStarParser :: (Chunked seq elem, Monad m) => ParserT l s e m elem -> ParserT l s e m seq Source #

Yields the LONGEST string of 0 or more successes of the given parser. Failures will be silenced.

greedyStarParser_ :: Monad m => ParserT l s e m a -> ParserT l s e m () Source #

Same as greedyStarParser but discards the result.

greedyPlusParser :: (Chunked seq elem, Monad m) => ParserT l s e m elem -> ParserT l s e m seq Source #

Yields the LONGEST string of 1 or more successes of the given parser. Failures in the tail will be silenced, but those in the head will be returned.

greedyPlusParser_ :: Monad m => ParserT l s e m a -> ParserT l s e m () Source #

Same as greedyPlusParser but discards the result.

defaultParser :: Monad m => a -> ParserT l s e m a -> ParserT l s e m a Source #

If the parser does not succeed, yield the given value.

optionalParser :: Monad m => ParserT l s e m a -> ParserT l s e m (Maybe a) Source #

A parser that yields Nothing if the parser does not succeed, otherwise wraps success in Just.

reflectParser :: Monad m => ParserT l s e m a -> ParserT l s e m (Maybe (ParseResult l s e a)) Source #

Run the parser speculatively and return results. Does not advance state or throw errors.

silenceParser :: Monad m => ParserT l s e m a -> ParserT l s e m a Source #

Removes all failures from the parse results. Catches more errors than 'catchError (const empty)' because this includes stream errors, not just custom errors. If you want more fine-grained control, use reflectParser and map over the results.

lookAheadParser :: Monad m => Maybe l -> ParserT l s e m a -> ParserT l s e m a Source #

Yield the results of the given parser, but rewind back to the starting state in ALL cases (success and error). Note that these results may contain errors, so you may want to stifle them with silenceParser, for example.

markParser :: Monad m => Maybe l -> ParserT l s e m a -> ParserT l s e m a Source #

Yield the results of the given parser, but rewind back to the starting state on error ONLY.

markWithStateParser :: Monad m => Maybe l -> (s -> (b, s)) -> (b -> ParserT l s e m a) -> ParserT l s e m a Source #

Like markParser but allows you to mutate state. See withToken and withChunk.

markWithOptStateParser :: Monad m => Maybe l -> (s -> Maybe (b, s)) -> (Maybe b -> ParserT l s e m a) -> ParserT l s e m a Source #

Like markParser but allows you to mutate state. See withToken and withChunk.

unmarkParser :: Monad m => ParserT l s e m a -> ParserT l s e m a Source #

Clear marks from parse errors. You can mark immediately after to widen the narrowest marked span to the range you want to report.

commitParser :: Monad m => ParserT l s e m () -> ParserT l s e m a -> ParserT l s e m a Source #

If the first parser succeeds in the given state, yield results from the second parser in the given state. This is likely the look-ahead you want. Use the first parser to check a prefix of input, and use the second to consume that input.

onEmptyParser :: Parser l s e a -> Parser l s e a -> Parser l s e a Source #

If the first parser yields NO results (success or failure), yield from the second. Note that this is different from orParser in that it does not try the second if there are errors in the first. You might use this on the outside of a complex parser with a fallback to fail to indicate that there are no matches.