Skip to content

Instantly share code, notes, and snippets.

@funrep
Created January 5, 2014 13:40
Show Gist options
  • Save funrep/8268390 to your computer and use it in GitHub Desktop.
Save funrep/8268390 to your computer and use it in GitHub Desktop.
{-# 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
@funrep
Copy link
Author

funrep commented Jan 5, 2014

src/Tob/NewParser.hs:54:36:
    Couldn't match type `Text' with `Maybe (Maybe a0)'
    Expected type: attoparsec-0.10.4.0:Data.Attoparsec.Internal.Types.Parser
                     Text (Maybe (Maybe a0))
      Actual type: Parser Text
    In the return type of a call of `takeWhile'
    In the second argument of `($)', namely `takeWhile (/= '\r')'
    In a stmt of a 'do' block:
      m <- option (return Nothing) $ takeWhile (/= '\r')

src/Tob/NewParser.hs:56:29:
    Couldn't match type `Maybe a0' with `Text'
    Expected type: Maybe Text
      Actual type: Maybe (Maybe a0)
    In the fourth argument of `Message', namely `m'
    In the second argument of `($)', namely `Message p c ps m'
    In a stmt of a 'do' block: return $ Message p c ps m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment