Created
October 14, 2013 13:55
-
-
Save funrep/6976028 to your computer and use it in GitHub Desktop.
IRC parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Text.ParserCombinators.Parsec | |
| data Prefix = Prefix String String String deriving Show | |
| data Command = Action String | Code Int deriving Show | |
| data Params = Params [String] deriving Show | |
| data Message = Message (Maybe Prefix) Command Params String deriving Show | |
| prefix :: Parser Prefix | |
| prefix = do | |
| n <- many1 (noneOf "!") | |
| char '!' | |
| u <- many1 (noneOf "@") | |
| char '@' | |
| h <- many1 (noneOf " ") | |
| return $ Prefix n u h | |
| command :: Parser Command | |
| command = action <|> code | |
| action :: Parser Command | |
| action = many1 upper >>= return . Action | |
| code :: Parser Command | |
| code = do | |
| x <- digit | |
| y <- digit | |
| z <- digit | |
| return $ Code $ read [x,y,z] | |
| params :: Parser Params | |
| params = fmap Params $ many1 (noneOf ":") `sepBy` spaces | |
| message :: Parser Message | |
| message = do | |
| optional $ char ':' | |
| p <- optionMaybe prefix | |
| c <- command | |
| ps <- params | |
| char ':' | |
| m <- many (noneOf "\r\n") | |
| string "\r\n" | |
| return $ Message p c ps m |
Author
funrep
commented
Oct 14, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment