Text.ParserCombinators.Poly.StateParser

polyparse-1.12: A variety of alternative parser combinator libraries.

Safe Haskell | Safe
Language | Haskell98

Text.ParserCombinators.Poly.StateParser

Contents

- The Parser datatype
  - basic parsers
  - State-handling
  - re-parsing

Description

This module contains the definitions for a generic parser, with running state. These are the parts that are shared between the State and StateLazy variations. Do not import this module directly, but only via T.P.Poly.State or T.P.Poly.StateLazy.

Synopsis

- newtype Parser s t a = P (s -> [t] -> Result ([t], s) a)
- data Result z a
  - = Success z a
  - | Failure z String
  - | Committed (Result z a)
- next :: Parser s t t
- eof :: Parser s t ()
- satisfy :: (t -> Bool) -> Parser s t t
- onFail :: Parser s t a -> Parser s t a -> Parser s t a
- stUpdate :: (s -> s) -> Parser s t ()
- stQuery :: (s -> a) -> Parser s t a
- stGet :: Parser s t s
- reparse :: [t] -> Parser s t ()

# The Parser datatype

newtype Parser s t a

This `Parser` datatype is a fairly generic parsing monad with error reporting, and running state. It can be used for arbitrary token types, not just String input. (If you do not require a running state, use module Poly.Plain instead)

Constructors

P (s -> [t] -> Result ([t], s) a) |

Instances

Monad (Parser s t) |
Functor (Parser s t) |
Applicative (Parser s t) |
Alternative (Parser s t) |
PolyParse (Parser s t) |
Commitment (Parser s t) |

data Result z a

A return type like Either, that distinguishes not only between right and wrong answers, but also has commitment, so that a failure cannot be undone. This should only be used for writing very primitive parsers - really it is an internal detail of the library. The z type is the remaining unconsumed input.

Constructors

Success z a |
Failure z String |
Committed (Result z a) |

Instances

Functor (Result z) |

## basic parsers

next :: Parser s t t

Simply return the next token in the input tokenstream.

eof :: Parser s t ()

Succeed if the end of file/input has been reached, fail otherwise.

satisfy :: (t -> Bool) -> Parser s t t

Return the next token if it satisfies the given predicate.

onFail :: Parser s t a -> Parser s t a -> Parser s t a infixl 6

`p `onFail` q` means parse p, unless p fails, in which case parse q instead. Can be chained together to give multiple attempts to parse something. (Note that q could itself be a failing parser, e.g. to change the error message from that defined in p to something different.) However, a severe failure in p cannot be ignored.

## State-handling

stUpdate :: (s -> s) -> Parser s t ()

Update the internal state.

stQuery :: (s -> a) -> Parser s t a

Query the internal state.

stGet :: Parser s t s

Deliver the entire internal state.

## re-parsing

reparse :: [t] -> Parser s t ()

Push some tokens back onto the front of the input stream and reparse. This is useful e.g. for recursively expanding macros. When the user-parser recognises a macro use, it can lookup the macro expansion from the parse state, lex it, and then stuff the lexed expansion back down into the parser.