Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Text.Cassette.Prim
- data K7 a b c d = K7 {}
- newtype Sym a b = Sym {}
- type C r = (String -> r) -> String -> r
- type PP a = forall r r'. K7 (C (a -> r)) (C r) (C (a -> r')) (C r')
- type PP0 = forall r r'. K7 (C r) (C r) (C r') (C r')
- (<>) :: K7 b c b' c' -> K7 a b a' b' -> K7 a c a' c'
- (-->) :: K7 a b a' b' -> K7 b c b' c' -> K7 a c a' c'
- (<|>) :: PP a -> PP a -> PP a
- play :: K7 a b c d -> a -> b
- flip :: K7 a b c d -> K7 d c b a
- parse :: PP a -> String -> Maybe a
- pretty :: PP a -> a -> Maybe String
- empty :: PP0
- nothing :: PP0
- shift :: a -> PP0 -> PP a
- unshift :: a -> PP a -> PP0
- string :: String -> PP0
- satisfy :: (Char -> Bool) -> PP Char
- lookAhead :: PP a -> PP a
- eof :: PP0
Datatypes
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.
Symmetric cassettes do have functions that are inverses of each other on each track. Symmetric cassettes form a category under splicing (see '(<>)').
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.
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
Primitive combinators
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.
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
.