NMDanny

Untitled

Apr 26th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module LogParser where
  2. import Numeric ()
  3. data MessageType = Info
  4.                  | Warning
  5.                  | Error Int
  6.                  deriving (Show,Eq)
  7.  
  8. type TimeStamp = Int
  9.  
  10. data LogMessage = LogMessage MessageType TimeStamp String | Unknown String
  11.                 deriving (Show,Eq)
  12.  
  13.  
  14. parseMessage :: String -> LogMessage
  15. parseMessage st = case words st of
  16.   (errIdent:errCode:timeStamp:msgWords) | Just timeStampInt <- readMaybe timeStamp , Just errCodeInt <- readMaybe errCode, errIdent == "E" ->
  17.     LogMessage (Error errCodeInt) timeStampInt (concat msgWords)
  18.   (msgIdent:timeStamp:msgWords) | Just timeStampInt <- readMaybe timeStamp -> case msgIdent of
  19.     "I" -> LogMessage Info timeStampInt (concat msgWords)
  20.     "W" -> LogMessage Warning timeStampInt (concat msgWords)
  21.     _   -> Unknown st
  22.   _ -> Unknown st
  23.  
  24.  
  25. readMaybe :: (Read a) => String -> Maybe a
  26. readMaybe s = case reads s of
  27.   [(x,"")] -> Just x
  28.   _ -> Nothing
Advertisement
Add Comment
Please, Sign In to add comment