1 implementation module format
12 import Control.Applicative
14 import Text => qualified join
15 import Text.Parsers.Simple.ParserCombinators
17 :: FormatString :== [Format]
20 | FMTApply FormatFun [Format]
25 derive gEq Format, FormatFun
26 instance == FormatString where == x y = x === y
28 format :: [(String, String)] FormatString -> MaybeErrorString String
29 format tags f = concat <$> mapM fmt f
31 fmt :: Format -> MaybeErrorString String
32 fmt (FMTLiteral s) = pure s
33 fmt (FMTApply fun args) = case (fun, args) of
35 = flip fromMaybe o flip lookup tags <$> fmt tag <*> fmt alt
36 (FFTag, _) = Error "`tag` must have exactly two arguments"
37 _ = Error "Unimplemented function"
39 parseFormat :: [Char] -> MaybeErrorString FormatString
40 parseFormat s = either (Error o concat) Ok (parse (some pFormat) s)
42 pFormat :: Parser Char Format
44 = FMTApply <$ pToken '$' <*> pFormatFun <* pToken '{' <*> pSepBy pFormat pComma <* pToken '}'
45 <|> FMTLiteral <$> toString <$> some (
46 pToken '\\' *> pSatisfy (\x->isMember x ['${},'])
47 <|> pSatisfy (\x->not (isMember x ['$[],'])))
50 pFormatFun :: Parser Char FormatFun
51 pFormatFun = FFTag <$ pList (map pToken ['tag'])