Skip to content

Commit 670955a

Browse files
authored
Merge pull request #12193 from cabalism/refactor/applicative-parsing
Use applicative parsing
2 parents ae97637 + 86b81e3 commit 670955a

9 files changed

Lines changed: 23 additions & 115 deletions

File tree

Cabal-syntax/src/Distribution/Backpack.hs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,8 @@ instance Pretty OpenModule where
180180
instance Parsec OpenModule where
181181
parsec = parsecModuleVar <|> parsecOpenModule
182182
where
183-
parsecOpenModule = do
184-
uid <- parsec
185-
_ <- P.char ':'
186-
mod_name <- parsec
187-
return (OpenModule uid mod_name)
188-
189-
parsecModuleVar = do
190-
_ <- P.char '<'
191-
mod_name <- parsec
192-
_ <- P.char '>'
193-
return (OpenModuleVar mod_name)
183+
parsecOpenModule = OpenModule <$> parsec <* P.char ':' <*> parsec
184+
parsecModuleVar = OpenModuleVar <$ P.char '<' <*> parsec <* P.char '>'
194185

195186
-- | Get the set of holes ('ModuleVar') embedded in a 'Module'.
196187
openModuleFreeHoles :: OpenModule -> Set ModuleName

Cabal-syntax/src/Distribution/SPDX/LicenseExpression.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,7 @@ instance Parsec LicenseExpression where
112112
where
113113
expr = compoundOr
114114

115-
simple = do
116-
s <- parsec
117-
exc <- exception
118-
return $ ELicense s exc
115+
simple = ELicense <$> parsec <*> exception
119116

120117
exception = P.optional $ P.try (spaces1 *> P.string "WITH" *> spaces1) *> parsec
121118

Cabal-syntax/src/Distribution/System.hs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,7 @@ instance Parsec Platform where
294294
-- We could support that preferring variants 'OtherOS' or 'OtherArch'
295295
--
296296
-- For now we split into arch and os parts on the first dash.
297-
parsec = do
298-
arch <- parsecDashlessArch
299-
_ <- P.char '-'
300-
os <- parsec
301-
return (Platform arch os)
297+
parsec = Platform <$> parsecDashlessArch <* P.char '-' <*> parsec
302298
where
303299
parsecDashlessArch = classifyArch Strict <$> dashlessIdent
304300

Cabal-syntax/src/Distribution/Types/AbiDependency.hs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ instance Pretty AbiDependency where
3131
pretty uid <<>> Disp.char '=' <<>> pretty abi
3232

3333
instance Parsec AbiDependency where
34-
parsec = do
35-
uid <- parsec
36-
_ <- P.char '='
37-
abi <- parsec
38-
return (AbiDependency uid abi)
34+
parsec = AbiDependency <$> parsec <* P.char '=' <*> parsec
3935

4036
instance Binary AbiDependency
4137
instance Structured AbiDependency

Cabal-syntax/src/Distribution/Types/Module.hs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ instance Pretty Module where
3232
pretty uid <<>> Disp.text ":" <<>> pretty mod_name
3333

3434
instance Parsec Module where
35-
parsec = do
36-
uid <- parsec
37-
_ <- P.char ':'
38-
mod_name <- parsec
39-
return (Module uid mod_name)
35+
parsec = Module <$> parsec <* P.char ':' <*> parsec
4036

4137
instance NFData Module where
4238
rnf (Module uid mod_name) = rnf uid `seq` rnf mod_name

Cabal-syntax/src/Distribution/Types/VersionRange/Internal.hs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -356,27 +356,14 @@ instance Parsec VersionRange where
356356
versionRangeParser :: forall m. CabalParsing m => m Int -> CabalSpecVersion -> m VersionRange
357357
versionRangeParser digitParser csv = expr
358358
where
359-
expr = do
360-
P.spaces
361-
t <- term
362-
P.spaces
363-
do
364-
_ <- P.string "||"
365-
checkOp
366-
P.spaces
367-
e <- expr
368-
return (unionVersionRanges t e)
369-
<|> return t
370-
term = do
371-
f <- factor
372-
P.spaces
373-
do
374-
_ <- P.string "&&"
375-
checkOp
376-
P.spaces
377-
t <- term
378-
return (intersectVersionRanges f t)
379-
<|> return f
359+
expr =
360+
(maybe <*> unionVersionRanges)
361+
<$> (P.spaces *> term <* P.spaces)
362+
<*> P.optional (P.string "||" *> checkOp *> P.spaces *> expr)
363+
term =
364+
(maybe <*> intersectVersionRanges)
365+
<$> (factor <* P.spaces)
366+
<*> P.optional (P.string "&&" *> checkOp *> P.spaces *> term)
380367
factor = parens expr <|> prim
381368

382369
prim = do

Cabal-syntax/src/Distribution/Utils/MD5.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ binaryPutMD5 (Fingerprint a b) = do
5454

5555
-- | @since 3.2.0.0
5656
binaryGetMD5 :: Get MD5
57-
binaryGetMD5 = do
58-
a <- getWord64le
59-
b <- getWord64le
60-
return (Fingerprint a b)
57+
binaryGetMD5 = Fingerprint <$> getWord64le <*> getWord64le
6158

6259
-- |
6360
--

cabal-install/src/Distribution/Client/IndexUtils/ActiveRepos.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,7 @@ instance Parsec ActiveRepoEntry where
105105
"repo" -> P.char ':' *> leadRepo
106106
_ -> P.unexpected $ "Unknown active repository entry type: " ++ token
107107

108-
leadRepo = do
109-
r <- parsec
110-
s <- strategyP
111-
return (ActiveRepo r s)
108+
leadRepo = ActiveRepo <$> parsec <*> strategyP
112109

113110
strategyP = P.option CombineStrategyMerge (P.char ':' *> parsec)
114111

cabal-install/src/Distribution/Client/TargetSelector.hs

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -319,64 +319,15 @@ parseTargetString :: String -> Maybe TargetString
319319
parseTargetString =
320320
readPToMaybe parseTargetApprox
321321
where
322+
colon = Parse.char ':'
322323
parseTargetApprox :: Parse.ReadP TargetString
323324
parseTargetApprox =
324-
( do
325-
a <- tokenQEnd
326-
return (TargetString1 a)
327-
)
328-
+++ ( do
329-
a <- tokenQ0
330-
_ <- Parse.char ':'
331-
b <- tokenQEnd
332-
return (TargetString2 a b)
333-
)
334-
+++ ( do
335-
a <- tokenQ0
336-
_ <- Parse.char ':'
337-
b <- tokenQ
338-
_ <- Parse.char ':'
339-
c <- tokenQEnd
340-
return (TargetString3 a b c)
341-
)
342-
+++ ( do
343-
a <- tokenQ0
344-
_ <- Parse.char ':'
345-
b <- token
346-
_ <- Parse.char ':'
347-
c <- tokenQ
348-
_ <- Parse.char ':'
349-
d <- tokenQEnd
350-
return (TargetString4 a b c d)
351-
)
352-
+++ ( do
353-
a <- tokenQ0
354-
_ <- Parse.char ':'
355-
b <- token
356-
_ <- Parse.char ':'
357-
c <- tokenQ
358-
_ <- Parse.char ':'
359-
d <- tokenQ
360-
_ <- Parse.char ':'
361-
e <- tokenQEnd
362-
return (TargetString5 a b c d e)
363-
)
364-
+++ ( do
365-
a <- tokenQ0
366-
_ <- Parse.char ':'
367-
b <- token
368-
_ <- Parse.char ':'
369-
c <- tokenQ
370-
_ <- Parse.char ':'
371-
d <- tokenQ
372-
_ <- Parse.char ':'
373-
e <- tokenQ
374-
_ <- Parse.char ':'
375-
f <- tokenQ
376-
_ <- Parse.char ':'
377-
g <- tokenQEnd
378-
return (TargetString7 a b c d e f g)
379-
)
325+
(TargetString1 <$> tokenQEnd)
326+
+++ (TargetString2 <$> tokenQ0 <* colon <*> tokenQEnd)
327+
+++ (TargetString3 <$> tokenQ0 <* colon <*> tokenQ <* colon <*> tokenQEnd)
328+
+++ (TargetString4 <$> tokenQ0 <* colon <*> token <* colon <*> tokenQ <* colon <*> tokenQEnd)
329+
+++ (TargetString5 <$> tokenQ0 <* colon <*> token <* colon <*> tokenQ <* colon <*> tokenQ <* colon <*> tokenQEnd)
330+
+++ (TargetString7 <$> tokenQ0 <* colon <*> token <* colon <*> tokenQ <* colon <*> tokenQ <* colon <*> tokenQ <* colon <*> tokenQ <* colon <*> tokenQEnd)
380331

381332
token = Parse.munch1 (\x -> not (isSpace x) && x /= ':')
382333
tokenQ = parseHaskellString <++ token

0 commit comments

Comments
 (0)