diff options
author | defanor <defanor@uberspace.net> | 2024-01-28 08:37:24 +0300 |
---|---|---|
committer | defanor <defanor@uberspace.net> | 2024-01-28 08:37:24 +0300 |
commit | bad85121628b574b25b6a1e163728069497eb63b (patch) | |
tree | c73dcedcd93d8519f7983ed1e48c0caaadf911f2 | |
parent | 788de39262809040ebf1096aff22190ad526dc1b (diff) |
-rw-r--r-- | Coalpit.hs | 11 |
1 files changed, 11 insertions, 0 deletions
@@ -31,6 +31,11 @@ module Coalpit ( fromDSV , SelNamePolicy(..) , Options(..) , defOpt + -- * Parsing and composition helpers + , escape + , pString + , pFieldSep + , pRecordSep ) where import GHC.Generics @@ -133,6 +138,8 @@ fromDSVList :: Coalpit a => Options -> String -> Either String [a] fromDSVList opt str = parseDSV (coalpitParser opt `sepEndBy` pRecordSep opt) str +-- | Enquote and escape a string, if it contains any characters that +-- need it. escape :: Options -> String -> String escape opt str | not (null str) && @@ -189,16 +196,20 @@ usageToString (UProduct u1 u2) = usageToString u1 ++ " " ++ usageToString u2 usageToString UUnit = "" usageToString (UType t) = t +-- | Parse a field separator. pFieldSep :: Options -> Parsec String m () pFieldSep opt = oneOf (NE.toList $ fieldSeparators opt) *> pure () +-- | Parse a record (line) separator. pRecordSep :: Options -> Parsec String m () pRecordSep opt = choice (eof : map (\c -> char c *> pure ()) (NE.toList $ recordSeparators opt)) +-- | Parse a token: either a quoted string or a string without +-- unescaped separators. The opposite of 'escape'. pString :: Options -> Parsec String m String pString opt = (try (quotedString <?> "quoted string")) |