cassette-0.1.0: A combinator library for simultaneously defining parsers and pretty printers.

Safe HaskellSafe
LanguageHaskell2010

Text.Cassette.Prim

Contents

Synopsis

Datatypes

data K7 a b c d #

A cassette consists of two tracks, represented by functions. The functions on each track are not necessarily inverses of each other, and do not necessarily connect the same start and end types.

Constructors

K7 

Fields

newtype Sym a b #

Symmetric cassettes do have functions that are inverses of each other on each track. Symmetric cassettes form a category under splicing (see '(<>)').

Constructors

Sym 

Fields

Instances

Category * Sym # 

Methods

id :: cat a a #

(.) :: cat b c -> cat a b -> cat a c #

type C r = (String -> r) -> String -> r #

The type of string transformers in CPS, i.e. functions from strings to strings.

type PP a = forall r r'. K7 (C (a -> r)) (C r) (C (a -> r')) (C r') #

The type of cassettes with a string transformer on each side. The A-side produces a value in addition to transforming the string, i.e. it is a parser. The B-side consumes a value to transform the string, i.e. it is a printer.

type PP0 = forall r r'. K7 (C r) (C r) (C r') (C r') #

Composition

(<>) :: K7 b c b' c' -> K7 a b a' b' -> K7 a c a' c' infixr 9 #

Tape splicing operator. Functions on each track are composed pairwise.

(-->) :: K7 a b a' b' -> K7 b c b' c' -> K7 a c a' c' infixr 8 #

A synonym to '(<>)' with its arguments flipped and with lower precedence.

(<|>) :: PP a -> PP a -> PP a infixr 1 #

Choice operator. If the first cassette fails, then try the second parser. Note that this is an unrestricted backtracking operator: it never commits to any particular choice.

Extraction

play :: K7 a b c d -> a -> b #

Select the A-side.

flip :: K7 a b c d -> K7 d c b a #

Switch the A-side and B-side around.

parse :: PP a -> String -> Maybe a #

Extract the parser from a cassette.

pretty :: PP a -> a -> Maybe String #

Flip the cassette around to extract the pretty printer.

Primitive combinators

empty :: PP0 #

Always fail.

nothing :: PP0 #

Do nothing.

shift :: a -> PP0 -> PP a #

Turn the given pure transformer into a parsing/printing pair. That is, return a cassette that produces and output on the one side, and consumes an input on the other, in addition to the string transformations of the given pure transformer. shift x p produces x as the output of p on the parsing side, and on the printing side accepts an input that is ignored.

unshift :: a -> PP a -> PP0 #

Turn the given cassette into a pure string transformer. That is, return a cassette that does not produce an output or consume an input. unshift x p throws away the output of p on the parsing side, and on the printing side sets the input to x.

string :: String -> PP0 #

Stripadd the given string fromto the output string.

satisfy :: (Char -> Bool) -> PP Char #

Successful only if predicate holds.

lookAhead :: PP a -> PP a #

Parseprint without consumingproducing any input.

eof :: PP0 #

Succeeds if input string is empty.