Skip to content

Instantly share code, notes, and snippets.

@JacobLeach
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacobLeach/9817107 to your computer and use it in GitHub Desktop.
Save JacobLeach/9817107 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
import Data.Attoparsec.Char8
import Data.Attoparsec.ByteString.Char8
import Data.Word
import Control.Applicative
data IP = IP Int Int Int Int deriving (Show)
parseIP :: Parser IP
parseIP =
num8 >>= \d1 ->
char '.' >>
num8 >>= \d2 ->
char '.' >>
num8 >>= \d3 ->
char '.' >>
num8 >>= \d4 ->
endOfInput >> (return (IP d1 d2 d3 d4))
num8 :: Integral a => Parser a
num8 = decimal >>= check
check :: (Num a, Ord a) => a -> Parser a
check n
| n < 256 = return (n)
| otherwise = fail "what"
main :: IO ()
main = print $ parseOnly parseIP "0.123.123.67"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment