Module Data.MicroParsec

Small parser, inspired by Parsec, but much less versatile. A bit faster and uses less memory than the transformer in Data.NanoParsec.

Imports

Table of Content

Definitions

data Parser s t r

Parser Monad

 Parser s t r

is the type of parsers that reads tokens of type t from a stream s t and returns either an error message string or a result r paired with the yet unprocessed input.

Works in principle like a combination of State and Either, where the state is the current input to get parsed, and the bind operations will stop on the first Either.Left result, which signals a syntax error.

Parser is also an instance of MonadAlt. The operations MonadAlt.<+> and Alt.<|> can be used to implement choice, i.e. they allow to continue on failure.

The input for a Parser must be something that is an instance of ListView. Ordinary lists, Strings, StringIterator and ArrayIterator are such types. The parser combinators use only the ListView.uncons and ListEmpty.null operations.

Constructors

private P {run ∷ s t → ((String | r), s t)}

Member Functions

runParser α β γ → α β → ((String | γ), α β)

access field run

getPosParser α β (α β)

Remember the current state of the input stream.

getPos always succeeds.

putPos ∷ α β → Parser α β ()

Resume parsing with the given input stream.

putPos always succeeds.

modifyPos ∷ (α β→α β) → Parser α β ()
failureStringParser α β γ

fail with given error message

runidParser α β γ → α β → ((String | γ), α β)

run a parser

parseParser α β γ → α β → (String | γ)

run a Parser, return just the result/error

parseTest ∷ (Show r, Show t, ListView s) ⇒ Parser s t r → s t → IO ()

run a Parser on some input and report

reporterror ∷ (ListSource β, Show α) ⇒ β α → StringString
labelStringParser γ β αParser γ β α

Causes the error message to be msg when p fails.

The error will be reported at the position where p was attempted, not necessarily at the position where p failed:

 Parser.run (letter >> digit) "a?"
 unexpected token, found ?
 Parser.run (label "id expected" $ letter >> digit) "a?"
 id expected, found a?
<?>Parser γ β αStringParser γ β α
infix  14
 p <?> msg

Causes the error message to be msg when p fails.

The error will be reported at the position where p failed.

expect ∷ (Show β, Eq β, ListView α) ⇒ β → Parser α β β
 expect t

This parser succeeds if the input is not empty and the head of the input equals t.

eosListView α ⇒ Parser α β ()

The eos parser succeeds if the input is empty.

satisfyListView s ⇒ (t → Bool) → Parser s t t
 satisfy property

Succeeds if there is a next token t and property t is true.

exceptWhenListView α ⇒ (β→Bool) → Parser α β ()
 exceptWhen prop

Fails if and only if there is a next token t and prop t is true.

Succeeds on end of stream or when the token does not satisfy the property.

In other words, exceptWhen p succeeds if and only if satisfy p fails.

panyListView α ⇒ Parser α β β
 pany

Fails if and only if eos succeeds, otherwise returns the next token.

symbolListView β ⇒ Parser β Char αParser β Char α
 symbol p

Is the same parser as p, but skips spaces afterwards

spaceListView α ⇒ Parser α Char Char

parse a single space character

digitListView α ⇒ Parser α Char Char

parse a single digit

letterListView α ⇒ Parser α Char Char

parse a single letter

uppercaseListView α ⇒ Parser α Char Char

parse a single uppercase letter

spacesListView α ⇒ Parser α Char ()

skip any spaces

stringStringParser StringIterator Char String
matchRegexParser StringIterator Char MatchResult

This parser succeeds if the pattern matches the beginning of the string.

For efficiency reasons, the pattern should start with ^

optionalParser s t rParser s t (Maybe r)

The optional parser always succeeds and returns its result wrapped in Maybe

manyParser γ β αParser γ β [α]
 many p

Collects as many p as possible and returns the results in a list.

Succeeds also when p fails the first time, in that case the result is an empty list.

Must not be applied to a parser that always succeeds!

someParser γ β αParser γ β [α]
 some p

Like many, except there must be at least one p

Must not be applied to a parser that always succeeds!

many1Parser γ β αParser γ β [α]

Alias for some

skipParser β δ γParser β δ ()
 skip p

equivalent to

 many p >> return ()

but faster, because it does not build up lists.

Must not be applied to a parser that always succeeds!

manyWhileListView β ⇒ (α→Bool) → Parser β α [α]
 manyWhile p

Collect tokens as long as they satisfy p

skipWhileListView α ⇒ (β→Bool) → Parser α β ()
 skipWhile p

Skip tokens as long as they satisfy predicate p.

Beware of negative predicates like

 skipWhile (!= 'a')

This will loop forever if there is no 'a' in the input stream. Instead use

 skipUntil (== 'a')
skipUntilListView β ⇒ (α→Bool) → Parser β α ()
 skipUntil p

Skip tokens as long as they do not satisfy p

When this succeeds, the next token will satisfy p or the stream is exhausted.

condParser β γ αParser β γ δ → Parser β γ δ → Parser β γ δ
 cond pcond pthen pelse

if pcond succeeds, then pthen is run, else pelse

To both pthen and pelse it will appear as if pcond didn't consume any tokens.

select[(Parser a d b, Parser a d c)]Parser a d c → Parser a d c
 select [(if1, t1), (if2, t2), ..., (ifn, tn)] e

unfolds as

 cond if1 t1 (cond if2 t2 (... (cond ifn tn e)))    
choicePlus β ⇒ [β α] → β α
 choice ps

Tries the parsers in the list from left to right, until success.

countMonad β ⇒ Int → β α → β [α]
 count n p

Applies p n times and returns a list of the results

betweenMonad α ⇒ α β → α δ → α γ → α γ
 between left right p

Parses left, then p and finally right and returns the result of p

option ∷ α → Parser γ β αParser γ β α
 option v p

Applies p, and returns v when it fails.

Always succeeds.

sepBy1Parser δ γ αParser δ γ βParser δ γ [α]
 p `sepBy1` q

Parses p and many q followed by p

sepByParser δ γ αParser δ γ βParser δ γ [α]
 p `sepBy` q

Like sepBy1, but allows zero p elements

endByParser δ γ αParser δ γ βParser δ γ [α]
 p `endBy` q

Parses zero or more occurrences of p separated and ended by q

endBy1Parser δ γ αParser δ γ βParser δ γ [α]
 p `endBy1` q

Parses one or more occurrences of p separated and ended by q

sepEndByParser δ γ αParser δ γ βParser δ γ [α]
 p `sepEndBy` q

Parses zero or more occurrences of p, separated and optionally ended by q

sepEndBy1Parser δ γ αParser δ γ βParser δ γ [α]
 p `sepEndBy1` q

Parses one or more occurrences of p, separated and optionally ended by q

Instances

instance MonadAlt (Parser s t)

Member Functions

*>Parser β α γParser β α δ → Parser β α δ
infixl  4
<*Parser β α γParser β α δParser β α γ
infixl  4
<*>Parser β α (γ→δ) → Parser β α γ → Parser β α δ
infixl  4

inherited from Monad.<*>

<+>Parser β α γParser β α γ → Parser β α γ
infixr  13
 p <+> q

The result is that of the first parser, if it succeeds, otherwise that of the second one, which is applied to the input left over by p

<|>Parser β α γParser β α γ → Parser β α γ
infixl  3
 p <|> q

The result is that of the first parser, if it succeeds, otherwise that of the second one. Note that q is run on the same input as p, even if p already consumed something.

 expect 'c' <|> expect 'd'

would succeed if the input starts with 'c' or 'd'.

>>Parser β α γParser β α δ → Parser β α δ
infixl  3

inherited from Monad.>>

>>=Parser β α γ → (γ → Parser β α δ) → Parser β α δ
infixl  3
 p >>= (r -> q)
 p >> q

If p succeeds, the overall result is that of q Otherwise, the overall result is failure.

Could be read as p followed by q.

fmap ∷ (γ → δ) → Parser β α γParser β α δ
infixl  4

inherited from Monad.fmap

joinParser β α (Parser β α γ)Parser β α γ

inherited from Monad.join

pure ∷ γ → Parser β α γ

generic success

pzeroParser β α γ

generic failure

Functions and Values by Type

StringParser StringIterator Char String

string

RegexParser StringIterator Char MatchResult

match

α → Bool

Parser.has$run

ListView α ⇒ Parser α Char ()

spaces

ListView α ⇒ Parser α Char Char

digit, letter, space, uppercase

(α β→α β) → Parser α β ()

modifyPos

α β → Parser α β ()

putPos

(ListSource β, Show α) ⇒ β α → StringString

reporterror

ListView s ⇒ (t → Bool) → Parser s t t

satisfy

ListView α ⇒ (β→Bool) → Parser α β ()

exceptWhen, skipWhile

ListView β ⇒ Parser β Char α → Parser β Char α

symbol

ListView β ⇒ (α→Bool) → Parser β α [α]

manyWhile

ListView β ⇒ (α→Bool) → Parser β α ()

skipUntil

Monad β ⇒ Int → β α → β [α]

count

Plus β ⇒ [β α] → β α

choice

(Show β, Eq β, ListView α) ⇒ β → Parser α β β

expect

Parser α β (α β)

getPos

ListView α ⇒ Parser α β ()

eos

ListView α ⇒ Parser α β β

pany

(s t → ((String | r), s t)) → Parser s t r

Parser.P

Parser s t r → Parser s t (Maybe r)

optional

Parser α β γ → α β → ((String | γ), α β)

runid, Parser.run

Parser α β γ → α β → (String | γ)

parse

Parser β α (Parser β α γ) → Parser β α γ

MonadAlt_Parser.join

Parser β α γ → Parser β α γ → Parser β α γ

MonadAlt_Parser.<|>, MonadAlt_Parser.<+>

Parser β δ γ → Parser β δ ()

skip

Parser γ β α → StringParser γ β α

<?>

Parser γ β α → Parser γ β [α]

many, some

StringParser γ β α → Parser γ β α

label

StringParser α β γ

failure

α → Parser γ β α → Parser γ β α

option

γ → Parser β α γ

MonadAlt_Parser.pure

(Show r, Show t, ListView s) ⇒ Parser s t r → s t → IO ()

parseTest

Parser β α γ

MonadAlt_Parser.pzero

(γ → δ) → Parser β α γ → Parser β α δ

MonadAlt_Parser.fmap

Parser β α (γ→δ) → Parser β α γ → Parser β α δ

MonadAlt_Parser.<*>

Parser β α γ → (γ → Parser β α δ) → Parser β α δ

MonadAlt_Parser.>>=

Parser β α γ → Parser β α δ → Parser β α γ

MonadAlt_Parser.<*

Parser β α γ → Parser β α δ → Parser β α δ

MonadAlt_Parser.>>, MonadAlt_Parser.*>

Parser β γ α → Parser β γ δ → Parser β γ δ → Parser β γ δ

cond

Parser δ γ α → Parser δ γ β → Parser δ γ [α]

endBy, endBy1, sepBy, sepBy1, sepEndBy, sepEndBy1

[(Parser a d b, Parser a d c)] → Parser a d c → Parser a d c

select

Monad α ⇒ α β → α δ → α γ → α γ

between

Parser α β γ → (-> (α β) ((String | γ), α β)→δ ε→((String | ζ), δ ε)) → Parser δ ε ζ

Parser.chg$run

Parser α β γ → (δ ε→((String | ζ), δ ε)) → Parser δ ε ζ

Parser.upd$run

Valid HTML 4.01 Strict