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

Safe HaskellSafe-Infered

Text.Cassette.Prim

Contents

Synopsis

Datatypes

data K7 a b c d Source

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

sideA :: a -> b
 
sideB :: d -> c
 

newtype Sym a b Source

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

unSym :: K7 a b a b
 

Instances

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

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')Source

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')Source

Composition

(<>) :: K7 b c b' c' -> K7 a b a' b' -> K7 a c a' c'Source

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'Source

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

(<|>) :: PP a -> PP a -> PP aSource

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 -> bSource

Select the A-side.

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

Switch the A-side and B-side around.

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

Extract the parser from a cassette.

pretty :: PP a -> a -> Maybe StringSource

Flip the cassette around to extract the pretty printer.

Primitive combinators

empty :: PP0Source

Always fail.

nothing :: PP0Source

Do nothing.

shift :: a -> PP0 -> PP aSource

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 -> PP0Source

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 -> PP0Source

Stripadd the given string fromto the output string.

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

Successful only if predicate holds.

lookAhead :: PP a -> PP aSource

Parseprint without consumingproducing any input.

eof :: PP0Source

Succeeds if input string is empty.