Begin moving the name parsers to the Network.DNS.RBL.Domain namespace.
[dead/harbl.git] / harbl / src / Network / DNS / RBL / Domain / Hyphen.hs
1 -- | OK, I lied about "Network.DNS.RBL.Domain.Digit" and
2 -- "Network.DNS.RBL.Domain.Letter" being the simplest modules you'd
3 -- ever see. Because this is. It contains the 'Hyphen' type and a
4 -- Parsec parser to parse one. We don't export its constructor because
5 -- then you could do something dumb like stick a letter inside one.
6 --
7 module Network.DNS.RBL.Domain.Hyphen (
8 Hyphen,
9 hyphen )
10 where
11
12 import Text.Parsec ( char )
13 import Text.Parsec.String ( Parser )
14
15 import Network.DNS.RBL.Pretty ( Pretty(..) )
16
17
18
19 -- | A wrapper around a single hyphen character.
20 --
21 -- ==== _Examples_
22 --
23 -- >>> Hyphen '-'
24 -- Hyphen '-'
25 --
26 -- >>> let h1 = Hyphen '-'
27 -- >>> let h2 = Hyphen '-'
28 -- >>> h1 == h2
29 -- True
30 --
31 newtype Hyphen = Hyphen Char
32
33
34 -- | Equality is defined semantically (all hyphens are equal).
35 --
36 -- ==== _Examples_
37 --
38 -- >>> let h1 = Hyphen '-'
39 -- >>> let h2 = Hyphen '-'
40 -- >>> h1 == h2
41 -- True
42 --
43 -- If you do something stupid, that's your fault:
44 --
45 -- >>> let h1 = Hyphen '-'
46 -- >>> let h2 = Hyphen 'x'
47 -- >>> h1 == h2
48 -- True
49 --
50 instance Eq Hyphen where _ == _ = True
51
52
53 -- | 'Show' is defined semantically; all hyphens display as \'-\'.
54 -- The implementation is based on what GHC derives, discovered via
55 -- @ghci -ddump-deriv@.
56 --
57 -- ==== _Examples_
58 --
59 -- >>> let h = Hyphen '-'
60 -- >>> h
61 -- Hyphen '-'
62 --
63 -- If you do something stupid, that's your fault:
64 --
65 -- >>> let h = Hyphen 'x'
66 -- >>> h
67 -- Hyphen '-'
68 --
69 instance Show Hyphen where
70 showsPrec d _ =
71 showParen (d > application_precedence) (showString "Hyphen '-'")
72 where
73 application_precedence = 10
74
75
76 -- | 'Pretty' is defined semantically; all hyphens display as \'-\'.
77 --
78 -- ==== _Examples_
79 --
80 -- >>> let h = Hyphen '-'
81 -- >>> pretty_print h
82 -- -
83 --
84 -- If you do something stupid, that's your fault:
85 --
86 -- >>> let h = Hyphen 'x'
87 -- >>> pretty_print h
88 -- -
89 --
90 instance Pretty Hyphen where pretty_show _ = "-"
91
92
93 -- | Parse a single hyphen and wrap it in our 'Hyphen' type.
94 --
95 -- ==== _Examples_
96 --
97 -- >>> import Text.Parsec ( parseTest )
98 --
99 -- Hyphens are parsed:
100 --
101 -- >>> parseTest hyphen "-"
102 -- Hyphen '-'
103 --
104 -- But not letters or digits:
105 --
106 -- >>> parseTest hyphen "1"
107 -- parse error at (line 1, column 1):
108 -- unexpected "1"
109 -- expecting "-"
110 --
111 -- >>> parseTest hyphen "x"
112 -- parse error at (line 1, column 1):
113 -- unexpected "x"
114 -- expecting "-"
115 --
116 hyphen :: Parser Hyphen
117 hyphen = fmap Hyphen (char '-')