/
Flags.purs
112 lines (98 loc) · 2.45 KB
/
Flags.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
module Data.String.Regex.Flags where
import Prelude
import Control.MonadPlus (guard)
import Data.String (joinWith)
type RegexFlagsRec =
{ global :: Boolean
, ignoreCase :: Boolean
, multiline :: Boolean
, sticky :: Boolean
, unicode :: Boolean
}
-- | Flags that control matching.
data RegexFlags = RegexFlags RegexFlagsRec
-- | All flags set to false.
noFlags :: RegexFlags
noFlags = RegexFlags
{ global: false
, ignoreCase: false
, multiline: false
, sticky: false
, unicode: false
}
-- | Only global flag set to true
global :: RegexFlags
global = RegexFlags
{ global: true
, ignoreCase: false
, multiline: false
, sticky: false
, unicode: false
}
-- | Only ignoreCase flag set to true
ignoreCase :: RegexFlags
ignoreCase = RegexFlags
{ global: false
, ignoreCase: true
, multiline: false
, sticky: false
, unicode: false
}
-- | Only multiline flag set to true
multiline :: RegexFlags
multiline = RegexFlags
{ global: false
, ignoreCase: false
, multiline: true
, sticky: false
, unicode: false
}
-- | Only sticky flag set to true
sticky :: RegexFlags
sticky = RegexFlags
{ global: false
, ignoreCase: false
, multiline: false
, sticky: true
, unicode: false
}
-- | Only unicode flag set to true
unicode :: RegexFlags
unicode = RegexFlags
{ global: false
, ignoreCase: false
, multiline: false
, sticky: false
, unicode: true
}
instance semigroupRegexFlags :: Semigroup RegexFlags where
append (RegexFlags x) (RegexFlags y) = RegexFlags
{ global: x.global || y.global
, ignoreCase: x.ignoreCase || y.ignoreCase
, multiline: x.multiline || y.multiline
, sticky: x.sticky || y.sticky
, unicode: x.unicode || y.unicode
}
instance monoidRegexFlags :: Monoid RegexFlags where
mempty = noFlags
instance eqRegexFlags :: Eq RegexFlags where
eq (RegexFlags x) (RegexFlags y)
= x.global == y.global
&& x.ignoreCase == y.ignoreCase
&& x.multiline == y.multiline
&& x.sticky == y.sticky
&& x.unicode == y.unicode
instance showRegexFlags :: Show RegexFlags where
show (RegexFlags flags) =
let
usedFlags =
[]
<> (guard flags.global $> "global")
<> (guard flags.ignoreCase $> "ignoreCase")
<> (guard flags.multiline $> "multiline")
<> (guard flags.sticky $> "sticky")
<> (guard flags.unicode $> "unicode")
in
if usedFlags == []
then "noFlags"
else "(" <> joinWith " <> " usedFlags <> ")"