Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Text.Cassette.Prim
Description
The primitive parser combinators.
Synopsis
- data K7 (p :: Type -> Type -> Type) a b = K7 {}
- data Tr r r'
- type PP a = forall r. K7 Tr r (a -> r)
- type PP0 = forall r. K7 Tr r r
- (-->) :: forall (p :: Type -> Type -> Type) b c a. Category p => K7 p b c -> K7 p a b -> K7 p a c
- parse :: PP a -> String -> Maybe a
- pretty :: PP a -> a -> Maybe String
- sscanf :: HasCallStack => K7 Tr r r' -> String -> r' -> r
- sprintf :: HasCallStack => K7 Tr String r -> r
- nothing :: PP0
- set :: a -> PP0 -> PP a
- unset :: a -> PP a -> PP0
- string :: String -> PP0
- satisfy :: (Char -> Bool) -> PP Char
- lookAhead :: PP a -> PP a
- eof :: PP0
Data types
data K7 (p :: Type -> Type -> Type) a b Source #
A cassette consists of two tracks, represented by profunctors. The second track has its polarities flipped relative to the first.
is the type of string transformers with answer type
modification from Tr
r r'r
to r'
through control effects.
type PP a = forall r. K7 Tr r (a -> 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. K7 Tr r r Source #
The type of cassettes only useful for their effect on the input or output strings, but do not produce/consume any value.
Composition
(-->) :: forall (p :: Type -> Type -> Type) b c a. Category p => K7 p b c -> K7 p a b -> K7 p a c infixr 9 Source #
A synonym to (.)
Extraction
sscanf :: HasCallStack => K7 Tr r r' -> String -> r' -> r Source #
An equivalent to sscanf()
in C:
extracts data from
string sscanf
fmt k ss
according to format descriptor fmt
and hands the data to
continuation k
.
>>>
spec = satisfy (=='A') . satisfy (=='B') . satisfy (=='C')
>>>
sscanf spec "ABC" (,,)
('A','B','C')
sprintf :: HasCallStack => K7 Tr String r -> r Source #
An equivalent to sprintf()
in C:
returns a function that
returns a string and whose number of arguments depends on format descriptor
sprintf
fmtfmt
.
>>>
spec = satisfy (=='A') . satisfy (=='B') . satisfy (=='C')
>>>
sprintf spec 'A' 'B' 'C'
"ABC"
Primitive combinators
set :: a -> PP0 -> PP a Source #
Turn the given pure transformer into a parsing/printing pair. That is,
return a cassette that provides an output on the one side, and consumes an
input on the other, in addition to the string transformations of the given
pure transformer.
provides set
x px
as the output of p
on the
parsing side, and on the printing side accepts an input that is ignored.
unset :: a -> PP a -> PP0 Source #
Turn the given parsing/printing pair into a pure string transformer. That
is, return a cassette that does not produce an output or consume an input.
throws away the output of unset
x pp
on the parsing side, and on the
printing side sets the input to x
.