summaryrefslogtreecommitdiffstats
path: root/Hirc/Parser.hs
blob: 701417166025ad0bf6aff546bc2d1da30daaf5af (plain)
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
{-# LANGUAGE LambdaCase #-}
module Hirc.Parser where

import Data.Char
import Hirc.Types
import Text.Parsec
import Text.Parsec.String

message :: Parser Message
message =
    Message <$> optionMaybe (char ':' *> prefix <* spaces1) <*> command <*> params
  where
    spaces1 = skipMany1 space
    prefix = Prefix <$> nick
                    <*> optionMaybe (char '!' *> user)
                    <*> optionMaybe (char '@' *> host)
    nick = many1 (noneOf " !@")
    user = many1 (noneOf " !@")
    host = many1 (noneOf " !@")
    command = many1 nonspace
    params = many1 (spaces1 *> (trailing <|> middle))
    trailing = char ':' *> many anyChar
    middle = many1 nonspace
    nonspace = satisfy (not . isSpace)



nickNum :: Parser (String, Int)
nickNum =
  (,) <$> (many1 (satisfy (not . isDigit)))
      <*> ((digitToInt <$> digit) <|> pure 0)