public
Created

  • Download Gist
parser.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
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
> :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
>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.