summaryrefslogtreecommitdiffstats
path: root/src/Reaktor/Parser.hs
blob: 6fbcce9fefdab5147405607bf1ae2eb808d33a69 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{-# LANGUAGE OverloadedStrings #-}
module Reaktor.Parser where

import Control.Applicative
import Data.Attoparsec.Text
import qualified Data.Char
import Data.String.Conversions (convertString)
import qualified Data.Text.Extended as T
import Prelude.Extended
import Reaktor.IRC
import Reaktor.Internal


prefix :: Parser Text
prefix = T.pack <$> many (satisfy Data.Char.isAlphaNum <|>
                           satisfy (flip elem (":.-@/!~[]\\`_^{|}" :: String)))

command :: Parser Command
command =
    convertString . T.pack <$> many1 (satisfy Data.Char.isAlphaNum)

nospcrlfcl :: Parser Char
nospcrlfcl =
  satisfy (flip notElem ("\NUL\CR\LF :" :: String)) <?> "nospcrlfcl"

middle :: Parser Text
middle =
    T.pack <$> ((:) <$> nospcrlfcl <*> many (char ':' <|> nospcrlfcl))
    <?> "middle"

trailing :: Parser Text
trailing =
    T.pack <$> many (char ':' <|> char ' ' <|> nospcrlfcl)
    <?> "trailing"

params :: Parser [Text]
params = (do
    a <- many (char ' ' *> middle)
    b <- optional (char ' ' *> char ':' *> trailing)
    return $ a <> (maybe [] (:[]) b))
    <?> "params"

message :: Parser Message
message =
    Message
      <$> optional (char ':' *> prefix <* char ' ')
      <*> command
      <*> params
      <* string "\r\n"