@@ -41,12 +41,12 @@ parseRegex x = runParser (do pat <- p_regex
41
41
type P = CharParser (GroupIndex , Int )
42
42
43
43
p_regex :: P Pattern
44
- p_regex = liftM POr $ sepBy1 p_branch (char ' |' )
44
+ p_regex = POr <$> sepBy1 p_branch (char ' |' )
45
45
46
46
-- man re_format helps a lot, it says one-or-more pieces so this is
47
47
-- many1 not many. Use "()" to indicate an empty piece.
48
48
p_branch :: P Pattern
49
- p_branch = liftM PConcat $ many1 p_piece
49
+ p_branch = PConcat <$> many1 p_piece
50
50
51
51
p_piece :: P Pattern
52
52
p_piece = (p_anchor <|> p_atom) >>= p_post_atom -- correct specification
@@ -62,35 +62,36 @@ group_index = do
62
62
return (Just index)
63
63
64
64
p_group :: P Pattern
65
- p_group = lookAhead (char ' ( ' ) >> do
66
- index <- group_index
67
- liftM ( PGroup index) $ between (char ' (' ) (char ' )' ) p_regex
65
+ p_group = do
66
+ _ <- lookAhead (char ' ( ' )
67
+ PGroup <$> group_index <*> between (char ' (' ) (char ' )' ) p_regex
68
68
69
69
-- p_post_atom takes the previous atom as a parameter
70
70
p_post_atom :: Pattern -> P Pattern
71
- p_post_atom atom = (char ' ?' >> return ( PQuest atom) )
72
- <|> (char ' +' >> return ( PPlus atom) )
73
- <|> (char ' *' >> return ( PStar True atom) )
71
+ p_post_atom atom = (char ' ?' $> PQuest atom)
72
+ <|> (char ' +' $> PPlus atom)
73
+ <|> (char ' *' $> PStar True atom)
74
74
<|> p_bound atom
75
75
<|> return atom
76
76
77
77
p_bound :: Pattern -> P Pattern
78
78
p_bound atom = try $ between (char ' {' ) (char ' }' ) (p_bound_spec atom)
79
79
80
80
p_bound_spec :: Pattern -> P Pattern
81
- p_bound_spec atom = do lowS <- many1 digit
82
- let lowI = read lowS
83
- highMI <- option (Just lowI) $ try $ do
84
- _ <- char ' ,'
85
- -- parsec note: if 'many digits' fails below then the 'try' ensures
86
- -- that the ',' will not match the closing '}' in p_bound, same goes
87
- -- for any non '}' garbage after the 'many digits'.
88
- highS <- many digit
89
- if null highS then return Nothing -- no upper bound
90
- else do let highI = read highS
91
- guard (lowI <= highI)
92
- return (Just (read highS))
93
- return (PBound lowI highMI atom)
81
+ p_bound_spec atom = do
82
+ lowI <- read <$> many1 digit
83
+ highMI <- option (Just lowI) $ try $ do
84
+ _ <- char ' ,'
85
+ -- parsec note: if 'many digits' fails below then the 'try' ensures
86
+ -- that the ',' will not match the closing '}' in p_bound, same goes
87
+ -- for any non '}' garbage after the 'many digits'.
88
+ highS <- many digit
89
+ if null highS then return Nothing -- no upper bound
90
+ else do
91
+ let highI = read highS
92
+ guard (lowI <= highI)
93
+ return $ Just highI
94
+ return $ PBound lowI highMI atom
94
95
95
96
-- An anchor cannot be modified by a repetition specifier
96
97
p_anchor :: P Pattern
@@ -102,18 +103,29 @@ p_anchor = (char '^' >> liftM PCarat char_index)
102
103
<?> " empty () or anchor ^ or $"
103
104
104
105
char_index :: P DoPa
105
- char_index = do (gi,ci) <- getState
106
- let ci' = succ ci
107
- setState (gi,ci')
108
- return (DoPa ci')
106
+ char_index = do
107
+ (gi, ci) <- getState
108
+ let ci' = succ ci
109
+ setState (gi, ci')
110
+ return $ DoPa ci'
109
111
110
112
p_char :: P Pattern
111
- p_char = p_dot <|> p_left_brace <|> p_escaped <|> p_other_char where
112
- p_dot = char ' .' >> char_index >>= return . PDot
113
- p_left_brace = try $ (char ' {' >> notFollowedBy digit >> char_index >>= return . (`PChar ` ' {' ))
114
- p_escaped = char ' \\ ' >> anyChar >>= \ c -> char_index >>= return . (`PEscape ` c)
115
- p_other_char = noneOf specials >>= \ c -> char_index >>= return . (`PChar ` c)
116
- where specials = " ^.[$()|*+?{\\ "
113
+ p_char = p_dot <|> p_left_brace <|> p_escaped <|> p_other_char
114
+ where
115
+ p_dot = do
116
+ _ <- char ' .'
117
+ PDot <$> char_index
118
+
119
+ p_left_brace = try $ do
120
+ _ <- char ' {'
121
+ _ <- notFollowedBy digit
122
+ flip PChar ' {' <$> char_index
123
+
124
+ p_escaped = do
125
+ _ <- char ' \\ '
126
+ flip PEscape <$> anyChar <*> char_index
127
+
128
+ p_other_char = flip PChar <$> noneOf " ^.[$()|*+?{\\ " <*> char_index
117
129
118
130
-- parse [bar] and [^bar] sets of characters
119
131
p_bracket :: P Pattern
@@ -161,15 +173,13 @@ p_set_elem_coll = liftM BEColl $
161
173
162
174
p_set_elem_range :: P BracketElement
163
175
p_set_elem_range = try $ do
164
- start <- noneOf " ]"
165
- _ <- char ' -'
166
- end <- noneOf " ]"
176
+ start <- noneOf " ]- "
177
+ _ <- char ' -'
178
+ end <- noneOf " ]"
167
179
return $ BERange start end
168
180
169
181
p_set_elem_char :: P BracketElement
170
- p_set_elem_char = do
171
- c <- noneOf " ]"
172
- return (BEChar c)
182
+ p_set_elem_char = BEChar <$> noneOf " ]"
173
183
174
184
-- | Fail when 'BracketElement' is invalid, e.g. empty range @1-0@.
175
185
-- This failure should not be caught.
0 commit comments