Last active
August 29, 2015 13:57
-
-
Save JacobLeach/9816831 to your computer and use it in GitHub Desktop.
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 OverloadedStrings #-} | |
import Data.Attoparsec.Char8 | |
import Data.Word | |
import Control.Applicative | |
data IP = IP Int Int Int Int deriving (Show) | |
parseIP :: Parser IP | |
parseIP = do | |
d1 <- (check decimal) | |
char '.' | |
d2 <- (check decimal) | |
char '.' | |
d3 <- (check decimal) | |
char '.' | |
d4 <- (check decimal) | |
endOfInput | |
return (IP d1 d2 d3 d4) | |
check :: (Num a, Ord a) => Parser a -> Parser a | |
check p = p >>= check1 | |
check1 :: (Num a, Ord a) => a -> Parser a | |
check1 n | |
| n < 256 = return (n) | |
| otherwise = fail "what" | |
main :: IO () | |
main = print $ parseOnly parseIP "256.123.123.123" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment