Rivest.hs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. module Data.SCargot.Rivest where
  2. import Data.ByteString (ByteString)
  3. import qualified Data.ByteString as BS
  4. import qualified Data.ByteString.Base64 as B64
  5. import Data.Text (Text)
  6. import qualified Data.Text as T
  7. pToken :: Parser ByteString
  8. pToken = do
  9. x <- char (isAlpha || isTokenPunct)
  10. xs <- takeWhile1 (isAlpha || isDigit || isTokenPunct)
  11. isTokenPunct :: Char -> Bool
  12. isTokenPunct c = c `elem` "-./_:*+="
  13. pWithLength :: Parser ByteString
  14. pWithLength = do
  15. n <- takeWhile1 isDigit
  16. pFindType (Just (read (T.unpack n)))
  17. pFindType :: Maybe Int -> Parser ByteString
  18. pFindType len = do
  19. c <- peekChar
  20. case c of
  21. ':' -> case len of
  22. Just l -> pVerbatim l
  23. Nothing -> fail "Verbatim encoding without length given"
  24. '"' -> pQuoted len
  25. '#' -> pHex len
  26. '{' -> pBase64Verbatim len
  27. '|' -> pBase64 len
  28. _ -> case len of
  29. Just _ -> fail "Unexpected length field"
  30. Nothing -> pToken
  31. pQuoted :: Maybe Int -> Parser ByteString
  32. pQuoted = do
  33. char '"'
  34. ss <- many1 quoteChar
  35. char '"'
  36. return ss
  37. pHex :: Parser ByteString
  38. pHex = do
  39. pVerbatim :: Int -> Parser ByteString
  40. pVerbatim = do
  41. char ':'
  42. take n
  43. pBase64Verbatim :: Maybe Int -> Parser ByteString
  44. pBase64Verbatim = undefined
  45. pBase64 :: Maybe Int -> Parser ByteString
  46. pBase64 = undefined