Created
July 10, 2012 19:24
-
-
Save zsol/3085658 to your computer and use it in GitHub Desktop.
Parsers
This file contains 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 NoMonomorphismRestriction, FlexibleContexts #-} | |
module Parsers | |
(getCategoryAndEvent, getFields) | |
where | |
import Data.Attoparsec.Lazy | |
import qualified Data.ByteString.Lazy.Char8 as B | |
import qualified Data.ByteString.Char8 as SB | |
import Data.List (sort) | |
spaces :: Parser () | |
spaces = skipWhile $ inClass " \t" | |
field_ :: Parser () | |
field_ = (skipWhile $ notInClass " \t") >> spaces | |
field :: Parser SB.ByteString | |
field = do | |
ret <- takeWhile1 $ notInClass " \t" | |
spaces | |
return ret | |
fields :: [Int] -> Parser [SB.ByteString] | |
fields [] = return [] | |
fields (n:ns) = do | |
count (n-1) field_ | |
ret <- field | |
rest <- fields $ map ((-) n) ns | |
return (ret:rest) | |
getFields :: [Int] -> B.ByteString -> Either String [SB.ByteString] | |
getFields numbers = eitherResult . parse (fields $ sort numbers) | |
categoryAndEvent :: Parser (SB.ByteString, SB.ByteString) | |
categoryAndEvent = do | |
count 3 field_ | |
category <- field | |
count 2 field_ | |
event <- field | |
return (category, last $ SB.split ':' event) | |
getCategoryAndEvent :: B.ByteString -> Either String (SB.ByteString, SB.ByteString) | |
getCategoryAndEvent = eitherResult . parse categoryAndEvent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
flip (-) n
grrrr