Created
January 5, 2014 13:40
-
-
Save funrep/8268390 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Tob.Parser where | |
-- (maybe ':') (maybe adress) (command) (params) (maybe ':') (maybe stuff) | |
import Prelude hiding (takeWhile) | |
import Control.Applicative ((<|>)) | |
import Data.Text (Text) | |
import Data.Char (isUpper) | |
import Data.Attoparsec.Text | |
data Prefix = Prefix Text Text Text | |
data Command = Action Text | Code Int | |
type Params = [Text] | |
data Message = Message (Maybe Prefix) Command Params (Maybe Text) | |
prefix :: Parser Prefix | |
prefix = do | |
n <- takeWhile (/= '!') | |
char '!' | |
u <- takeWhile (/= '@') | |
char '@' | |
h <- takeWhile (/= ' ') | |
return $ Prefix n u h | |
command :: Parser Command | |
command = action <|> code | |
action :: Parser Command | |
action = fmap Action $ takeWhile isUpper | |
code :: Parser Command | |
code = do | |
x <- digit | |
y <- digit | |
z <- digit | |
return $ Code $ read [x,y,z] | |
params :: Parser Params | |
params = takeWhile (notInClass ":\r") `sepBy` space | |
message :: Parser Message | |
message = do | |
try $ char ':' | |
p <- option Nothing $ fmap Just $ try prefix | |
space | |
c <- command | |
space | |
ps <- params | |
try $ char ':' | |
m <- option (return Nothing) $ takeWhile (/= '\r') | |
string "\r\n" | |
return $ Message p c ps m |
Author
funrep
commented
Jan 5, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment