Skip to content

Instantly share code, notes, and snippets.

@funrep
Created October 14, 2013 13:55
Show Gist options
  • Select an option

  • Save funrep/6976028 to your computer and use it in GitHub Desktop.

Select an option

Save funrep/6976028 to your computer and use it in GitHub Desktop.
IRC parser
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
@funrep
Copy link
Author

funrep commented Oct 14, 2013

> :l parser.hs
[1 of 1] Compiling Main             ( parser.hs, interpreted )
Ok, modules loaded: Main.
> let parseIRC = \s -> either (\_ -> error "fak") (\b -> b) (parse message "irc" s)
> parseIRC ":CalebDelnay!calebd@localhost PRIVMSG #mychannel :Hello everyone!"
*** Exception: fak

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment