+
+instance Pretty IPv4Octet where
+ pretty_show (IPv4Octet x) = show x
+
+
+-- | Parse an IPv4 octet, which should contain a string of digits.
+-- Should fail if the parsed integer does not lie between @0@ and
+-- @255@ inclusive.
+--
+-- ==== _Examples_
+--
+-- >>> import Text.Parsec ( parseTest )
+--
+-- Standard octets are parsed correctly:
+--
+-- >>> parseTest v4octet "0"
+-- IPv4Octet 0
+--
+-- >>> parseTest v4octet "127"
+-- IPv4Octet 127
+--
+-- >>> parseTest v4octet "255"
+-- IPv4Octet 255
+--
+-- Non-digit input throws an error:
+--
+-- >>> parseTest v4octet "Hello, World!"
+-- parse error at (line 1, column 1):
+-- unexpected "H"
+-- expecting digit
+--
+-- If we're given an integer outside the range @0..255@ (i.e. not a
+-- valid octet), we fail:
+--
+-- >>> parseTest v4octet "9000"
+-- parse error at (line 1, column 5):
+-- unexpected end of input
+-- expecting digit
+-- Octet "9000" must be between 0 and 255.
+--
+v4octet :: Parser IPv4Octet
+v4octet = do
+ s <- many1 digit
+ case ( readMaybe s :: Maybe Int ) of
+ -- If "many1 digit" gives us a list of digits, we should be able
+ -- to convert that to an Int! It will overflow rather than fail
+ -- if the input is too big/small, so it should really always
+ -- succeed.
+ Nothing -> unexpected "v4octet: readMaybe failed on a sequence of digits!"
+
+ -- If we got an Int, make sure it's actually a representation of
+ -- an octet.
+ Just k -> if 0 <= k && k <= 255
+ then return (IPv4Octet k)
+ else fail ("Octet \"" ++ (show k)
+ ++ "\" must be between 0 and 255.")
+
+
+
+
+-- * Sequence members
+
+
+-- | An ipv4 \"sequence member\". A sequence member is either an
+-- integer (an octet) or a range of integers (contained in an
+-- octet). This data type corresponds to \"v4seq_member\" in the
+-- postscreen parser.
+--