format- Rendering from and scanning to format strings

Safe HaskellSafe-Infered




parseFormat :: String -> Either String FormatSource

Parse a format string into a Format object ready to be used in renderFormat and scanFormat.

The format string consists of raw tokens (ordinary characters), and variables, marked '$varname' or '${varname}'. The dollar sign may be used as a raw token by escaping it with another dollar sign, like so: $$. (If you want a variable named $, use '${$}'.)

Not all syntactically valid parse strings are semantically valid. In particular, two variables must not occur consecutively without interleaving raw tokens. (If this were permitted, the resulting grammar would be ambiguous.)

Variable names may be used twice; however, this will make the result of scanFormat somewhat difficult to deal with.

The functions renderFormatString and scanFormatString are provided as conveniences to make doing this explicitly unnecessary.

renderFormatString :: String -> (String -> Maybe String) -> Either String StringSource

A more convenient alternative to using parseFormat and renderFormat.

scanFormat :: Format -> String -> Either String [(String, String)]Source

Parses a string using the given format as a guide, generating a list of pairs of variable names and values.

To determine where a variable ends, the entire subsequent string of raw tokens (until the next variable or the end of the string) is used as a terminator. It must occur verbatim in the scanned string or the parse will fail. The smallest match is used: if the format string is '${a}:' and the input string is '1:2:', the parse will exit with an error, as only the first character will be considered part of the variable a.

scanFormatString :: String -> String -> Either String [(String, String)]Source

A more convenient alternative to using parseFormat and scanFormat.