From 80375cb5c33d2a86e2b5e2bd5b7d8025e8a74f1f Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Wed, 12 Jul 2017 13:50:35 +0200 Subject: [PATCH] fix message parsing without prefix --- IRC.icl | 21 ++++++++++++--------- test.icl | 12 ++++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 test.icl diff --git a/IRC.icl b/IRC.icl index 5d3ba4f..4a9fa95 100644 --- a/IRC.icl +++ b/IRC.icl @@ -40,25 +40,28 @@ where , parseIRCMessage ":cherryh.freenode.net JOIN #cha,#ch-b #twilight\r\n" , parseIRCMessage ":cherryh.freenode.net ISON a b c d e f :g h\r\n" , parseIRCMessage ":wilhelm.freenode.net 001 clooglebot :Welcome to the freenode Internet Relay Chat Network clooglebot\r\n" + , parseIRCMessage "PING :orwell.freenode.net\r\n" ] parseIRCMessage :: String -> Either [Error] IRCMessage parseIRCMessage s = case runParser parsePrefix (fromString s) of + // Prefix is parsed ([(prefix, rest):_], _) - = case parse parseReply rest of - Left e = case parseCmd rest of - Left e2 = Left [e2:e] - Right cmd - = Right {IRCMessage | irc_prefix=prefix, irc_command=Right cmd} - Right repl - = Right {IRCMessage | irc_prefix=prefix, irc_command=Left repl} - (_, es) = Left ["couldn't parse prefix":es] + //Try parsing a numeric reply + = case parse parseReply rest of + //Try a normal command + Left e = case parseCmd rest of + Left e2 = Left [e2:e] + Right cmd = Right {IRCMessage | irc_prefix=prefix, irc_command=Right cmd} + Right repl = Right {IRCMessage | irc_prefix=prefix, irc_command=Left repl} + // Error parsing prefix + (_, es) = Left ["Error parsing prefix"] //Prefix parsePrefix :: Parser Char (Maybe (Either IRCUser String)) parsePrefix - = optional (pToken ':' >>| parseEither parseUser parseHost) <* pToken ' ' + = optional (pToken ':' >>| parseEither parseUser parseHost <* pToken ' ') where parseEither :: (Parser a b) (Parser a c) -> Parser a (Either b c) parseEither p q = Left <$> p <|> Right <$> q diff --git a/test.icl b/test.icl new file mode 100644 index 0000000..f402f99 --- /dev/null +++ b/test.icl @@ -0,0 +1,12 @@ +module test + + +import gast +import IRC + +derive ggen IRCMessage + +Start = Test [] pParsePrint + +pParsePrint :: IRCMessage -> Bool +pParsePrint a = toString (parseIRCMessage (toString a)) == toString a -- 2.20.1