Skip to content

Instantly share code, notes, and snippets.

@funrep
Created October 14, 2013 14:56
Show Gist options
  • Save funrep/6977017 to your computer and use it in GitHub Desktop.
Save funrep/6977017 to your computer and use it in GitHub Desktop.
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
stripNick :: String -> String
stripNick s = if head s `elem` "@~!&" then tail s else s
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
optional $ 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!\r\n"
*** Exception: fak
>

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