-- This file is part of irc.-- irc is free software; you can redistribute it and/or modify-- it under the terms of the GNU Lesser General Public License as published by-- the Free Software Foundation; either version 3 of the License, or-- (at your option) any later version.-- irc is distributed in the hope that it will be useful,-- but WITHOUT ANY WARRANTY; without even the implied warranty of-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-- GNU Lesser General Public License for more details.-- You should have received a copy of the GNU Lesser General Public License-- along with this program. If not, see <http://www.gnu.org/licenses/>.-- | Parsec parsers and a general parsing interface for IRC messagesmoduleNetwork.IRC.Parser(-- * Parsing and Formatting FunctionsparseMessage-- :: String -> Maybe Message-- * Parsec Combinators for Parsing IRC messages,prefix-- :: CharParser st Prefix,serverPrefix-- :: CharParser st Prefix,nicknamePrefix-- :: CharParser st Prefix,command-- :: CharParser st Command,parameter-- :: CharParser st Parameter,message-- :: CharParser st Message,crlf-- :: CharParser st (),spaces-- :: CharParser st ()-- * Other Parser Combinators,maybeP-- :: GenParser tok st a -> GenParser tok st (Maybe a),tokenize-- :: CharParser st a -> CharParser st a,takeUntil-- :: String -> CharParser st String)whereimportControl.MonadimportData.MaybeimportText.ParserCombinators.Parsechiding(spaces)importNetwork.IRC.Types-- | Parse a String into a Message.parseMessage::String-- ^ Message string->MaybeMessage-- ^ Parsed messageparseMessage=(either(constNothing)Just).(parsemessage"")-- | Take all tokens until one character from a given string is foundtakeUntil::String->CharParserstStringtakeUntil=many1.noneOf-- | Convert a parser into an optional one that returns a MaybemaybeP::GenParsertoksta->GenParsertokst(Maybea)maybePp=optionNothing(liftMJustp)-- | Convert a parser that consumes all space after ittokenize::CharParsersta->CharParserstatokenizep=p>>=\x->spaces>>returnx-- | Consume only spaces tabs or the bell characterspaces::CharParserst()spaces=skipMany1(oneOf" \t\b")-- | Parse a Prefixprefix::CharParserstPrefixprefix=char':'>>(trynicknamePrefix<|>serverPrefix)-- | Parse a Server prefixserverPrefix::CharParserstPrefixserverPrefix=takeUntil" ">>=return.Server-- | Parse a NickName prefixnicknamePrefix::CharParserstPrefixnicknamePrefix=don<-takeUntil" .!@\r\n"p<-optionFalse(char'.'>>returnTrue)whenp(fail"")u<-maybeP$char'!'>>takeUntil" @\r\n"s<-maybeP$char'@'>>takeUntil" \r\n"return$NickNamenus-- | Parse a command. Either a string of capital letters, or 3 digits.command::CharParserstCommandcommand=many1upper<|>dox<-digity<-digitz<-digitreturn[x,y,z]-- | Parse a command parameter.parameter::CharParserstParameterparameter=(char':'>>takeUntil"\r\n")<|>(takeUntil" \r\n")-- | Parse a cr lfcrlf::CharParserst()crlf=optional(char'\r')>>char'\n'>>return()-- | Parse a Messagemessage::CharParserstMessagemessage=dop<-maybeP$tokenizeprefixc<-commandps<-many(spaces>>parameter)crlf>>eofreturn$Messagepcps