From: Michael Orlitzky Date: Tue, 7 Jul 2015 05:01:58 +0000 (-0400) Subject: Finish fleshing out the Domain module; update the TODO. X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=15e7cd1fba0dc7ebbdd517154d64e4d7fe3750e1;p=dead%2Fharbl.git Finish fleshing out the Domain module; update the TODO. --- diff --git a/doc/TODO b/doc/TODO index 20b8bea..0e56c5f 100644 --- a/doc/TODO +++ b/doc/TODO @@ -1,3 +1 @@ -1. Move the Pretty class into its own module. -2. Implement pretty printing of Domains to clean up the doctests. -3. Add exactly-63 test to labels. +1. Add no-equal-brothers restriction for subdomains (RFC1034). diff --git a/src/Domain.hs b/src/Domain.hs index ce20aae..8d1e088 100644 --- a/src/Domain.hs +++ b/src/Domain.hs @@ -25,62 +25,96 @@ import Text.Parsec ( import qualified Text.Parsec as Parsec ( digit, letter) import Text.Parsec.String ( Parser ) -newtype Domain = Domain String deriving Show +import Pretty +-- * Digits --- | The derived instance of 'Eq' for domain names is incorrect. All --- comparisons are currently made case-insensitively. The following is --- an excerpt from RFC1035: --- --- 2.3.3. Character Case --- --- For all parts of the DNS that are part of the official --- protocol, all comparisons between character strings (e.g., --- labels, domain names, etc.) are done in a case-insensitive --- manner... +-- | A wrapper around a digit character. -- --- So to compare two DNS names, we compare their lower-case --- counterparts. --- -instance Eq Domain where - (Domain d1) == (Domain d2) = - (map toLower d1) == (map toLower d2) - - --- * Digits newtype Digit = Digit Char deriving (Eq, Show) +instance Pretty Digit where pretty_show (Digit d) = [d] +-- | Parse a single digit, but wrap it in our 'Digit' type. +-- digit :: Parser Digit digit = fmap Digit Parsec.digit -- * Letters -newtype Letter = Letter Char deriving (Eq, Show) +-- | A wrapper around a letter character. +-- +newtype Letter = Letter Char deriving (Show) +instance Pretty Letter where pretty_show (Letter l) = [l] + + +-- | Parse a single letter, but wrap it in our 'Letter' type. +-- letter :: Parser Letter letter = fmap Letter Parsec.letter +-- | The derived instance of 'Eq' for letters is incorrect. All +-- comparisons should be made case-insensitively. The following +-- is an excerpt from RFC1035: +-- +-- 2.3.3. Character Case +-- +-- For all parts of the DNS that are part of the official +-- protocol, all comparisons between character strings (e.g., +-- labels, domain names, etc.) are done in a case-insensitive +-- manner... +-- +-- Since each part of DNS name is composed of our custom types, it +-- suffices to munge the equality for 'Letter'. +-- +instance Eq Letter where + (Letter l1) == (Letter l2) = (toLower l1) == (toLower l2) -- * Letters/Digits -data LetDig = LetDigLetter Letter | LetDigDigit Digit deriving (Eq, Show) +-- | A sum type representing either a letter or a digit. +-- +data LetDig = + LetDigLetter Letter | + LetDigDigit Digit + deriving (Eq, Show) + +instance Pretty LetDig where + pretty_show (LetDigLetter l) = pretty_show l + pretty_show (LetDigDigit d) = pretty_show d + +-- | Parse a letter or a digit and wrap it in our 'LetDig' type. +-- let_dig :: Parser LetDig let_dig = (fmap LetDigLetter letter) <|> (fmap LetDigDigit digit) -- * Hyphens + +-- | A wrapper around a single hyphen character. +-- newtype Hyphen = Hyphen Char deriving (Eq, Show) +instance Pretty Hyphen where pretty_show (Hyphen h) = [h] +-- | Parse a single hyphen and wrap it in our 'Hyphen' type. +-- hyphen :: Parser Hyphen hyphen = fmap Hyphen (char '-') -- * Letter, Digit, or Hyphen. -data LetDigHyp = LetDigHypLetDig LetDig - | LetDigHypHyphen Hyphen +-- | A sum type representing a letter, digit, or hyphen. +-- +data LetDigHyp = + LetDigHypLetDig LetDig | + LetDigHypHyphen Hyphen deriving (Eq, Show) +instance Pretty LetDigHyp where + pretty_show (LetDigHypLetDig ld) = pretty_show ld + pretty_show (LetDigHypHyphen h) = pretty_show h + -- | The following is the simplest type in the domain grammar that -- isn't already implemented for us. @@ -129,6 +163,17 @@ let_dig_hyp = -- These are represented as either a single instance of a -- 'LetDigHyp', or a string of them (recursive). -- +data LdhStr = + LdhStrSingleLdh LetDigHyp | + LdhStrMultipleLdh LetDigHyp LdhStr + deriving (Eq, Show) + +instance Pretty LdhStr where + pretty_show (LdhStrSingleLdh ldh) = pretty_show ldh + pretty_show (LdhStrMultipleLdh ldh s) = (pretty_show ldh) ++ (pretty_show s) + +-- | Parse a string of letters, digits, and hyphens (an 'LdhStr'). +-- -- ==== _Examples_ -- -- >>> import Text.Parsec ( parseTest ) @@ -146,12 +191,9 @@ let_dig_hyp = -- -- As well as strings of them: -- --- LdhStr (LetDigHypLetter (Letter 'a')) (LdhStr (LetDigHypDigit (Digit '0')) (LdhStr (LetDigHypHyphen (Hyphen '-')) (LdhStrSingleLdh (LetDigHypLetter (Letter 'b'))))) +-- >>> pretty_print $ parse ldh_str "" "a0-b" +-- a0-b -- -data LdhStr = - LdhStrSingleLdh LetDigHyp | LdhStrMultipleLdh LetDigHyp LdhStr - deriving (Eq, Show) - ldh_str :: Parser LdhStr ldh_str = try both <|> just_one where @@ -252,6 +294,10 @@ length_ldh_str (LdhStrMultipleLdh _ t) = 1 + (length_ldh_str t) data LdhStrLetDig = LdhStrLetDig (Maybe LdhStr) LetDig deriving (Eq, Show) +instance Pretty LdhStrLetDig where + pretty_show (LdhStrLetDig Nothing ld) = pretty_show ld + pretty_show (LdhStrLetDig (Just s) ld) = (pretty_show s) ++ (pretty_show ld) + -- | Parse an 'LdhStrLetDig'. This isn't in the grammar, but we might -- as well define the parser for it independently since we gave it -- its own data type. @@ -267,11 +313,11 @@ data LdhStrLetDig = LdhStrLetDig (Maybe LdhStr) LetDig -- -- And longer strings: -- --- >>> parseTest ldh_str_let_dig "ab" --- LdhStrLetDig (Just (LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'a'))))) (LetDigLetter (Letter 'b')) +-- >>> pretty_print $ parse ldh_str_let_dig "" "ab" +-- ab -- --- >>> parseTest ldh_str_let_dig "-b" --- LdhStrLetDig (Just (LdhStrSingleLdh (LetDigHypHyphen (Hyphen '-')))) (LetDigLetter (Letter 'b')) +-- >>> pretty_print $ parse ldh_str_let_dig "" "-b" +-- -b -- -- >>> parseTest ldh_str_let_dig "b-" -- parse error at (line 1, column 3): @@ -332,6 +378,9 @@ length_ldh_str_let_dig (LdhStrLetDig (Just ldhstring) _) = data Label = Label Letter (Maybe LdhStrLetDig) deriving (Eq, Show) +instance Pretty Label where + pretty_show (Label l Nothing) = pretty_show l + pretty_show (Label l (Just s)) = (pretty_show l) ++ (pretty_show s) -- | Parse a 'Label'. -- @@ -358,8 +407,8 @@ data Label = Label Letter (Maybe LdhStrLetDig) -- -- And longer strings: -- --- >>> parseTest label "abc-def" --- Label (Letter 'a') (Just (LdhStrLetDig (Just (LdhStrMultipleLdh (LetDigHypLetDig (LetDigLetter (Letter 'b'))) (LdhStrMultipleLdh (LetDigHypLetDig (LetDigLetter (Letter 'c'))) (LdhStrMultipleLdh (LetDigHypHyphen (Hyphen '-')) (LdhStrMultipleLdh (LetDigHypLetDig (LetDigLetter (Letter 'd'))) (LdhStrSingleLdh (LetDigHypLetDig (LetDigLetter (Letter 'e'))))))))) (LetDigLetter (Letter 'f')))) +-- >>> pretty_print $ parse label "" "abc-def" +-- abc-def -- -- But not anything ending in a hyphen: -- @@ -375,7 +424,8 @@ data Label = Label Letter (Maybe LdhStrLetDig) -- -- However, /exactly/ 63 characters is acceptable: -- --- TODO +-- >>> pretty_print $ parse label "" (replicate 63 'x') +-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -- label :: Parser Label label = do @@ -393,8 +443,203 @@ label = do else fail "labels must be 63 or fewer characters" + +-- * Subdomains + + +-- | The data type representing a \"subdomain\" from RFC1035: +-- +-- ::=