Created
June 13, 2014 16:45
-
-
Save queertypes/7c6a28ad904a3b96f1c3 to your computer and use it in GitHub Desktop.
Parse an IP address with attoparsec
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 Control.Applicative | |
import Data.Attoparsec.Text | |
import Data.Word | |
data IP = IP Word8 Word8 Word8 Word8 deriving (Eq, Show) | |
parseIP :: Parser IP | |
parseIP = IP <$> dec <*> dec <*> dec <*> decimal <?> "parse IP Address" | |
where dot = char '.' | |
dec = decimal <* dot | |
main :: IO () | |
main = print $ parseOnly parseIP "1.2.3.4" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment