Guest User

Untitled

a guest
Jul 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. module Data.Attoparsec.NBT where
  2.  
  3. import Prelude hiding (take)
  4. import Control.Applicative hiding (empty)
  5. import Control.Monad.Error
  6. import Data.Attoparsec
  7. import Data.Attoparsec.Combinator
  8. import Data.Serialize.Get (runGet, getWord8, getWord16be, getWord32be, getWord64be)
  9. import Data.NBT
  10.  
  11. data ParseError = ParseError String
  12.  
  13. type Fallible = Either ParseError
  14.  
  15. parseTag :: Parser (Fallible Tag)
  16. parseTag = parseEnd
  17. <|> parseByte
  18. <|> parseShort
  19.  
  20. parseEnd :: Parser (Fallible Tag)
  21. parseEnd = do
  22. word8 0x00
  23. return $ Right TAG_End
  24.  
  25. parseByte :: Parser (Fallible Tag)
  26. parseByte = do
  27. word8 0x01
  28. b <- anyWord8
  29. return $ Right (TAG_Byte $ fromIntegral b)
  30.  
  31. parseShort :: Parser (Fallible Tag)
  32. parseShort = do
  33. word8 0x02
  34. s <- take 2
  35. case runGet getWord16be s of
  36. Left e -> return $ Left (ParseError "Failed to parse short.")
  37. Right a -> return $ Right (TAG_Short $ fromIntegral a)
Add Comment
Please, Sign In to add comment