|
5 | 5 | -- the parsed form of a Regular Expression.
|
6 | 6 |
|
7 | 7 | module Text.Regex.TDFA.Pattern
|
8 |
| - (Pattern(..) |
9 |
| - ,PatternSet(..) |
10 |
| - ,PatternSetCharacterClass(..) |
11 |
| - ,PatternSetCollatingElement(..) |
12 |
| - ,PatternSetEquivalenceClass(..) |
13 |
| - ,GroupIndex |
14 |
| - ,DoPa(..) |
15 |
| - ,showPattern |
16 |
| --- ** Internal use |
17 |
| - ,starTrans |
18 |
| --- ** Internal use, Operations to support debugging under ghci |
19 |
| - ,starTrans',simplify',dfsPattern |
| 8 | + ( Pattern(..) |
| 9 | + , PatternSet(..) |
| 10 | + , patternSetChars |
| 11 | + , patternSetCharacterClasses |
| 12 | + , patternSetCollatingElements |
| 13 | + , patternSetEquivalenceClasses |
| 14 | + , PatternSetCharacterClass(..) |
| 15 | + , PatternSetCollatingElement(..) |
| 16 | + , PatternSetEquivalenceClass(..) |
| 17 | + , GroupIndex |
| 18 | + , DoPa(..) |
| 19 | + , showPattern |
| 20 | + -- ** Internal use |
| 21 | + , starTrans |
| 22 | + -- ** Internal use, Operations to support debugging under ghci |
| 23 | + , starTrans', simplify', dfsPattern |
20 | 24 | ) where
|
21 | 25 |
|
22 | 26 | {- By Chris Kuklewicz, 2007. BSD License, see the LICENSE file. -}
|
23 | 27 |
|
24 | 28 | import Data.List(intersperse,partition)
|
25 | 29 | import qualified Data.Set as Set(toAscList,toList)
|
26 | 30 | import Data.Set(Set) -- XXX EnumSet
|
| 31 | + |
| 32 | +import Utils |
27 | 33 | import Text.Regex.TDFA.Common(DoPa(..),GroupIndex,common_error)
|
28 | 34 |
|
29 | 35 | err :: String -> a
|
@@ -92,19 +98,54 @@ showPattern pIn =
|
92 | 98 | -}
|
93 | 99 | paren s = ('(':s)++")"
|
94 | 100 |
|
95 |
| -data PatternSet = PatternSet (Maybe (Set Char)) |
96 |
| - (Maybe (Set PatternSetCharacterClass)) |
97 |
| - (Maybe (Set PatternSetCollatingElement)) |
98 |
| - (Maybe (Set PatternSetEquivalenceClass)) |
99 |
| - deriving (Eq) |
| 101 | +-- | Processed content of a bracket expression. |
| 102 | +data PatternSet = PatternSet |
| 103 | + { _patternSetChars :: Set Char |
| 104 | + -- ^ Characters included in the pattern. |
| 105 | + , _patternSetCharacterClasses :: Set PatternSetCharacterClass |
| 106 | + -- ^ POSIX character classes included in the pattern. |
| 107 | + , _patternSetCollatingElements :: Set PatternSetCollatingElement |
| 108 | + -- ^ Collating elements included in the pattern. |
| 109 | + , _patternSetEquivalenceClasses :: Set PatternSetEquivalenceClass |
| 110 | + -- ^ Equivalence classes included in the pattern. |
| 111 | + } |
| 112 | + deriving (Eq) |
| 113 | + |
| 114 | +instance Semigroup PatternSet where |
| 115 | + PatternSet a b c d <> PatternSet a' b' c' d' = |
| 116 | + PatternSet (a <> a') (b <> b') (c <> c') (d <> d') |
| 117 | + |
| 118 | +instance Monoid PatternSet where |
| 119 | + mempty = PatternSet mempty mempty mempty mempty |
| 120 | + mappend = (<>) |
| 121 | + |
| 122 | +-- | Lens for '_patternSetChars'. |
| 123 | +patternSetChars :: Lens' PatternSet (Set Char) |
| 124 | +patternSetChars f ps = |
| 125 | + f (_patternSetChars ps) <&> \ i -> ps{ _patternSetChars = i } |
| 126 | + |
| 127 | +-- | Lens for '_patternSetCharacterClasses'. |
| 128 | +patternSetCharacterClasses :: Lens' PatternSet (Set PatternSetCharacterClass) |
| 129 | +patternSetCharacterClasses f ps = |
| 130 | + f (_patternSetCharacterClasses ps) <&> \ i -> ps{ _patternSetCharacterClasses = i } |
| 131 | + |
| 132 | +-- | Lens for '_patternSetCollatingElements'. |
| 133 | +patternSetCollatingElements :: Lens' PatternSet (Set PatternSetCollatingElement) |
| 134 | +patternSetCollatingElements f ps = |
| 135 | + f (_patternSetCollatingElements ps) <&> \ i -> ps{ _patternSetCollatingElements = i } |
| 136 | + |
| 137 | +-- | Lens for '_patternSetEquivalenceClasses'. |
| 138 | +patternSetEquivalenceClasses :: Lens' PatternSet (Set PatternSetEquivalenceClass) |
| 139 | +patternSetEquivalenceClasses f ps = |
| 140 | + f (_patternSetEquivalenceClasses ps) <&> \ i -> ps{ _patternSetEquivalenceClasses = i } |
100 | 141 |
|
101 | 142 | instance Show PatternSet where
|
102 | 143 | showsPrec i (PatternSet s scc sce sec) =
|
103 |
| - let (special,normal) = maybe ("","") ((partition (`elem` "]-")) . Set.toAscList) s |
| 144 | + let (special,normal) = partition (`elem` "]-") $ Set.toAscList s |
104 | 145 | charSpec = (if ']' `elem` special then (']':) else id) (byRange normal)
|
105 |
| - scc' = maybe "" ((concatMap show) . Set.toList) scc |
106 |
| - sce' = maybe "" ((concatMap show) . Set.toList) sce |
107 |
| - sec' = maybe "" ((concatMap show) . Set.toList) sec |
| 146 | + scc' = concatMap show $ Set.toList scc |
| 147 | + sce' = concatMap show $ Set.toList sce |
| 148 | + sec' = concatMap show $ Set.toList sec |
108 | 149 | in shows charSpec
|
109 | 150 | . showsPrec i scc' . showsPrec i sce' . showsPrec i sec'
|
110 | 151 | . if '-' `elem` special then showChar '-' else id
|
|
0 commit comments