diff options
author | tv <tv@krebsco.de> | 2019-01-13 23:52:05 +0100 |
---|---|---|
committer | tv <tv@krebsco.de> | 2019-01-21 02:12:00 +0100 |
commit | ce276eee82ec0b8c4106beb4c51d6f9eb77335c4 (patch) | |
tree | e41019c40471a45659fefba1671fa68395f062d6 /src/Reaktor/Parser.hs | |
parent | dffc580ca255cd118a0dfcdae7a5bb67f4824dcc (diff) |
src: initv0.0.0
Diffstat (limited to 'src/Reaktor/Parser.hs')
-rw-r--r-- | src/Reaktor/Parser.hs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/Reaktor/Parser.hs b/src/Reaktor/Parser.hs new file mode 100644 index 0000000..bdd2f98 --- /dev/null +++ b/src/Reaktor/Parser.hs @@ -0,0 +1,45 @@ +{-# LANGUAGE OverloadedStrings #-} +module Reaktor.Parser where + +import Control.Applicative +import Data.Attoparsec.ByteString.Char8 +import qualified Data.ByteString.Char8 as BS +import qualified Data.Char +import Reaktor.Types + + +prefix :: Parser Prefix +prefix = BS.pack <$> many (satisfy Data.Char.isAlphaNum <|> + satisfy (flip elem (":.-@/!~[]\\`_^{|}" :: String))) + +command :: Parser Command +command = BS.pack <$> many1 (satisfy Data.Char.isAlphaNum) + +nospcrlfcl :: Parser Char +nospcrlfcl = + satisfy (flip notElem ("\NUL\CR\LF :" :: String)) <?> "nospcrlfcl" + +middle :: Parser Param +middle = + BS.pack <$> ((:) <$> nospcrlfcl <*> many (char ':' <|> nospcrlfcl)) + <?> "middle" + +trailing :: Parser Param +trailing = + BS.pack <$> many (char ':' <|> char ' ' <|> nospcrlfcl) + <?> "trailing" + +params :: Parser [Param] +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" |