Skip to content

Commit e7398f4

Browse files
committed
ReadRegex: cosmetic changes using Functor and Applicative notation
1 parent 01642a0 commit e7398f4

File tree

1 file changed

+47
-37
lines changed

1 file changed

+47
-37
lines changed

lib/Text/Regex/TDFA/ReadRegex.hs

+47-37
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ parseRegex x = runParser (do pat <- p_regex
4141
type P = CharParser (GroupIndex, Int)
4242

4343
p_regex :: P Pattern
44-
p_regex = liftM POr $ sepBy1 p_branch (char '|')
44+
p_regex = POr <$> sepBy1 p_branch (char '|')
4545

4646
-- man re_format helps a lot, it says one-or-more pieces so this is
4747
-- many1 not many. Use "()" to indicate an empty piece.
4848
p_branch :: P Pattern
49-
p_branch = liftM PConcat $ many1 p_piece
49+
p_branch = PConcat <$> many1 p_piece
5050

5151
p_piece :: P Pattern
5252
p_piece = (p_anchor <|> p_atom) >>= p_post_atom -- correct specification
@@ -62,35 +62,36 @@ group_index = do
6262
return (Just index)
6363

6464
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
6868

6969
-- p_post_atom takes the previous atom as a parameter
7070
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)
7474
<|> p_bound atom
7575
<|> return atom
7676

7777
p_bound :: Pattern -> P Pattern
7878
p_bound atom = try $ between (char '{') (char '}') (p_bound_spec atom)
7979

8080
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
9495

9596
-- An anchor cannot be modified by a repetition specifier
9697
p_anchor :: P Pattern
@@ -102,18 +103,29 @@ p_anchor = (char '^' >> liftM PCarat char_index)
102103
<?> "empty () or anchor ^ or $"
103104

104105
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'
109111

110112
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
117129

118130
-- parse [bar] and [^bar] sets of characters
119131
p_bracket :: P Pattern
@@ -161,15 +173,13 @@ p_set_elem_coll = liftM BEColl $
161173

162174
p_set_elem_range :: P BracketElement
163175
p_set_elem_range = try $ do
164-
start <- noneOf "]"
165-
_ <- char '-'
166-
end <- noneOf "]"
176+
start <- noneOf "]-"
177+
_ <- char '-'
178+
end <- noneOf "]"
167179
return $ BERange start end
168180

169181
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 "]"
173183

174184
-- | Fail when 'BracketElement' is invalid, e.g. empty range @1-0@.
175185
-- This failure should not be caught.

0 commit comments

Comments
 (0)