Portability | non-portable |
---|---|
Stability | experimental |
Maintainer | leather@cs.uu.nl |
Safe Haskell | Safe-Infered |
This module defines an extensible, type-indexed function for reading
well-typed values from a string with a format descriptor. This may be
considered a Haskell variant of the C scanf
function.
If you are primarily interested in using this library, you will want to see
readsf
and readf
, the more user-friendly functions.
If you are also interested in extending this library with your own format
descriptors, you should read about the Format
class.
- class Format d a | d -> a where
- readsf :: Format d a => d -> ReadS a
- readf :: Format d a => d -> String -> Maybe a
- data CharF = Char
- data IntF = Int
- data IntegerF = Integer
- data FloatF = Float
- data DoubleF = Double
- data StringF = String
- data ReadF a = Read
- data NumF a = Num
- data a :%: b = a :%: b
- (%) :: a -> b -> a :%: b
- data WrapF inner outer = Wrap outer inner outer
- data MaybeF a = Maybe a
- data ChoiceF a = Choice [a]
- data EitherF a b = Either a b
- data EitherLF a b = EitherL a b
- data SpaceF = Space
The Class
class Format d a | d -> a whereSource
This class provides the signature for an extensible, type-indexed function
that uses a format descriptor to parse a string input and return a well-typed
value. The type variable d
is the format descriptor, and the variable a
is the type of the value to be read from the input.
An instance of Format
adds a (type) case to the function. Before defining
an instance, you must first define a format descriptor for your specific type
and expected input. The descriptor is often very simple. See the descriptors
in this module for examples.
Here is the instance for types that are instances of Read
.
dataReadF
a =Read
-- Format descriptor
instance (Read
a) => Format (ReadF
a) a wherereadpf
Read
=readS_to_P
reads
Note that you will need some of the combinators (such as readS_to_P
) in
Text.ParserCombinators.ReadP.
Format Char Char | Parse a |
Format String String | Parse a |
Format SpaceF String | |
Format DoubleF Double | |
Format FloatF Float | |
Format IntegerF Integer | |
Format IntF Int | |
Format StringF String | |
Format CharF Char | |
Format (ReadP a) a | Parse a |
Format d a => Format (ChoiceF d) a | |
(Read a, Num a) => Format (NumF a) a | |
Read a => Format (ReadF a) a | |
Format d a => Format (MaybeF d) (Maybe a) | |
(Format d1 a1, Format d2 a2) => Format (d1, d2) (a1, a2) | |
(Format d1 a1, Format d2 a2) => Format (EitherLF d1 d2) (Either a1 a2) | |
(Format d1 a1, Format d2 a2) => Format (EitherF d1 d2) (Either a1 a2) | |
(Format din ain, Format dout aout) => Format (WrapF din dout) (:%: aout (:%: ain aout)) | |
(Format d1 a1, Format d2 a2) => Format (:%: d1 d2) (:%: a1 a2) | |
(Format d1 a1, Format d2 a2, Format d3 a3) => Format (d1, d2, d3) (a1, a2, a3) | |
(Format d1 a1, Format d2 a2, Format d3 a3, Format d4 a4) => Format (d1, d2, d3, d4) (a1, a2, a3, a4) | |
(Format d1 a1, Format d2 a2, Format d3 a3, Format d4 a4, Format d5 a5) => Format (d1, d2, d3, d4, d5) (a1, a2, a3, a4, a5) | |
(Format d1 a1, Format d2 a2, Format d3 a3, Format d4 a4, Format d5 a5, Format d6 a6) => Format (d1, d2, d3, d4, d5, d6) (a1, a2, a3, a4, a5, a6) | |
(Format d1 a1, Format d2 a2, Format d3 a3, Format d4 a4, Format d5 a5, Format d6 a6, Format d7 a7) => Format (d1, d2, d3, d4, d5, d6, d7) (a1, a2, a3, a4, a5, a6, a7) | |
(Format d1 a1, Format d2 a2, Format d3 a3, Format d4 a4, Format d5 a5, Format d6 a6, Format d7 a7, Format d8 a8) => Format (d1, d2, d3, d4, d5, d6, d7, d8) (a1, a2, a3, a4, a5, a6, a7, a8) | |
(Format d1 a1, Format d2 a2, Format d3 a3, Format d4 a4, Format d5 a5, Format d6 a6, Format d7 a7, Format d8 a8, Format d9 a9) => Format (d1, d2, d3, d4, d5, d6, d7, d8, d9) (a1, a2, a3, a4, a5, a6, a7, a8, a9) | |
(Format d1 a1, Format d2 a2, Format d3 a3, Format d4 a4, Format d5 a5, Format d6 a6, Format d7 a7, Format d8 a8, Format d9 a9, Format d10 a10) => Format (d1, d2, d3, d4, d5, d6, d7, d8, d9, d10) (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) | |
(Format d1 a1, Format d2 a2, Format d3 a3, Format d4 a4, Format d5 a5, Format d6 a6, Format d7 a7, Format d8 a8, Format d9 a9, Format d10 a10, Format d11 a11) => Format (d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11) (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) | |
(Format d1 a1, Format d2 a2, Format d3 a3, Format d4 a4, Format d5 a5, Format d6 a6, Format d7 a7, Format d8 a8, Format d9 a9, Format d10 a10, Format d11 a11, Format d12 a12) => Format (d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12) (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) | |
(Format d1 a1, Format d2 a2, Format d3 a3, Format d4 a4, Format d5 a5, Format d6 a6, Format d7 a7, Format d8 a8, Format d9 a9, Format d10 a10, Format d11 a11, Format d12 a12, Format d13 a13) => Format (d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13) (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) | |
(Format d1 a1, Format d2 a2, Format d3 a3, Format d4 a4, Format d5 a5, Format d6 a6, Format d7 a7, Format d8 a8, Format d9 a9, Format d10 a10, Format d11 a11, Format d12 a12, Format d13 a13, Format d14 a14) => Format (d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14) (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) | |
(Format d1 a1, Format d2 a2, Format d3 a3, Format d4 a4, Format d5 a5, Format d6 a6, Format d7 a7, Format d8 a8, Format d9 a9, Format d10 a10, Format d11 a11, Format d12 a12, Format d13 a13, Format d14 a14, Format d15 a15) => Format (d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15) (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) |
The Functions
Format Descriptors
These are used to indicate which values and types to read.
Basic Format Descriptors
Class-based Format Descriptors
Parse a value whose type is an instance of the class Read
.
Parse a value whose type is an instance of the class Num
.
Recursive Format Descriptors
Right-associative pair. First parse a a
-type format and then a b
-type
format.
a :%: b |
(%) :: a -> b -> a :%: bSource
Right-associative pair. This is a shorter, functional equivalent to the
type (:%:)
.
Parse a format of one type wrapped by two other formats of a different type.
Wrap outer inner outer |
Parse one of the optional formats in a list.
Choice [a] |
Parse one of two formats in a fully symmetric choice.
Either a b |
Parse one of two formats, trying the left one first.
EitherL a b |