Advertisement
VladNitu

StringParserVladFP

Mar 17th, 2024
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. escape :: Parser String -- remove escapes before '/', as jq does
  2. escape = do
  3. string "\\\\" <|> string "\\n" <|> string "\\t" <|> string "\\r" <|> string "\\f" <|> string "\\b" <|> (string "\\/" *> pure "/") <|> string "\\\""
  4.  
  5. parseHex :: Parser String
  6. parseHex = do
  7. _ <- string "\\u" -- starts w/ "\" => needs to be escaped
  8. hex <- parseHexChar -- <|> parseOtherEscapeChar
  9. return [hex]
  10.  
  11.  
  12. parseHexChar :: Parser Char
  13. parseHexChar = do
  14. b3 <- parseHexDigit
  15. b2 <- parseHexDigit
  16. b1 <- parseHexDigit
  17. b0 <- parseHexDigit
  18. let
  19. decimalNumber = read ("0x" ++ [b3, b2, b1, b0]) :: Int
  20. decimalStr = show decimalNumber
  21. actualChar = fst $ head $ readLitChar ("\\" ++ decimalStr)
  22. in
  23. return actualChar
  24.  
  25. -- HELPER
  26. parseHexDigit :: Parser Char -- [0..9] or [A..F]
  27. parseHexDigit = do
  28. digit <|> sat (`elem` "abcdefABCDEF")
  29.  
  30. parseBasicChar :: Parser String
  31. parseBasicChar = do
  32. ch <- sat isBasicChar
  33. return [ch]
  34.  
  35. isControlChar :: Char -> Bool
  36. isControlChar ch = let codePoint = ord ch in codePoint >= 0x0000 && codePoint <= 0x001F
  37.  
  38. isBasicChar :: Char -> Bool
  39. isBasicChar ch = ch `notElem` ['"', '\\'] && (not (isControlChar ch))
  40.  
  41. parseString :: Parser String
  42. parseString = do
  43. char '\"'
  44. str <- many (parseBasicChar <|> parseHex <|> escape)
  45. char '\"'
  46. return (concat str)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement