Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- escape :: Parser String -- remove escapes before '/', as jq does
- escape = do
- string "\\\\" <|> string "\\n" <|> string "\\t" <|> string "\\r" <|> string "\\f" <|> string "\\b" <|> (string "\\/" *> pure "/") <|> string "\\\""
- parseHex :: Parser String
- parseHex = do
- _ <- string "\\u" -- starts w/ "\" => needs to be escaped
- hex <- parseHexChar -- <|> parseOtherEscapeChar
- return [hex]
- parseHexChar :: Parser Char
- parseHexChar = do
- b3 <- parseHexDigit
- b2 <- parseHexDigit
- b1 <- parseHexDigit
- b0 <- parseHexDigit
- let
- decimalNumber = read ("0x" ++ [b3, b2, b1, b0]) :: Int
- decimalStr = show decimalNumber
- actualChar = fst $ head $ readLitChar ("\\" ++ decimalStr)
- in
- return actualChar
- -- HELPER
- parseHexDigit :: Parser Char -- [0..9] or [A..F]
- parseHexDigit = do
- digit <|> sat (`elem` "abcdefABCDEF")
- parseBasicChar :: Parser String
- parseBasicChar = do
- ch <- sat isBasicChar
- return [ch]
- isControlChar :: Char -> Bool
- isControlChar ch = let codePoint = ord ch in codePoint >= 0x0000 && codePoint <= 0x001F
- isBasicChar :: Char -> Bool
- isBasicChar ch = ch `notElem` ['"', '\\'] && (not (isControlChar ch))
- parseString :: Parser String
- parseString = do
- char '\"'
- str <- many (parseBasicChar <|> parseHex <|> escape)
- char '\"'
- return (concat str)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement