Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module contains parsers for formatting strings. We have to deal with two kinds of strings:
- String formats. This is the whole construct like
"Hello, {}! Your account balance is {1:+8.4}."
. - Variable formats. This is only part after colon in braces, i.e. the
+8.4
thing in previous example.
The string format syntax is supposed to be very stable and simple. It is basically defined by phrase "any part in braces is variable substitution".
Variable formats syntax depends on type of data which we are going to format. These formats can be pretty complex, for example they can include alignment, rounding, and so on.
- parseFormat :: Text -> Either ParseError Format
- parseFormat' :: Text -> Format
- parseGenericFormat :: Text -> Either ParseError GenericFormat
- parseBoolFormat :: Text -> Either ParseError BoolFormat
- parseMaybeFormat :: Text -> Maybe (Text, Text)
- pFormat :: Parser Format
- pGenericFormat :: Parsec Text st GenericFormat
- pBoolFormat :: Parsec Text st BoolFormat
- type Parser a = Parsec Text ParserState a
- data ParserState = ParserState {
- psNextIndex :: Int
- initParserState :: ParserState
Parse functions
parseFormat :: Text -> Either ParseError Format Source #
Parse string format definition.
parseFormat' :: Text -> Format Source #
Version of parseFormat which throws error
in case of syntax error in the formatting string.
parseGenericFormat :: Text -> Either ParseError GenericFormat Source #
Parse generic variable format.
Syntax is:
[[fill]align][sign][#][width][.precision][radix]
where:
- fill - padding character (space by default)
- align - alignment indicator (
<
,>
, or^
) - sign - when to show number's sign (
+
,-
, or space) #
- if specified, then for hexadecimal numbers the leading0x
will be added- width - minimum length of the field
- precision - number of decimal places after point, for floatting-point numbers
- radix -
h
orx
for hexadecimal,d
for decimal (default).
parseBoolFormat :: Text -> Either ParseError BoolFormat Source #
Parse Bool format.
Syntax is:
TRUE:FALSE
Colon can be replaced with comma or semicolon.
For example, valid format specifications are true:false
(the default one),
True:False
, yes:no
, and so on.
parseMaybeFormat :: Text -> Maybe (Text, Text) Source #
Try to parse format for Maybe x
type.
The syntax is:
someformat|nothing
where someformat
is format for the x
type, and nothing
is the string
to be substituted for Nothing
value.
Returns Nothing, if format does not contain |
. Otherwise, returns
Just (someformat, nothing)
.
Parsec functions
pGenericFormat :: Parsec Text st GenericFormat Source #
Parsec parser for generic (Python-like) variable format.
pBoolFormat :: Parsec Text st BoolFormat Source #
Parsec parser for Bool format
Utility types
data ParserState Source #