module Attoparsec.Data.Parsers where
import Attoparsec.Data.Prelude hiding (bool)
import Data.Attoparsec.Text
import qualified GHC.Show
import qualified Data.Text as Text
show :: Parser Text
show =
char '"' *> body <* char '"'
where
body =
mconcat <$> many chunk
where
chunk =
escaped <|> nonEscaped
nonEscaped =
takeWhile1 (\ a -> a /= '\\' && a /= '"')
escaped =
Text.singleton <$> escapedChar
escapedChar :: Parser Char
escapedChar =
char '\\' *> escapedCharBody
escapedCharBody :: Parser Char
escapedCharBody =
char '\\' <|>
char 'a' $> '\a' <|>
char 'b' $> '\b' <|>
char 'f' $> '\f' <|>
char 'n' $> '\n' <|>
char 'r' $> '\r' <|>
char 't' $> '\t' <|>
char 'v' $> '\v' <|>
string "DEL" $> '\DEL' <|>
ordEscapedCharBody <|>
asciiTabEscapedCharBody
asciiTabEscapedCharBody :: Parser Char
asciiTabEscapedCharBody =
zipWith asciiTabChar [0..] GHC.Show.asciiTab & asum
where
asciiTabChar index chars =
string (fromString chars) $> chr index
ordEscapedCharBody :: Parser Char
ordEscapedCharBody =
chr <$> decimal