{-# LANGUAGE CPP #-}{-# LANGUAGE OverloadedStrings #-}{-|Module : GHCup.Prelude.MegaParsecDescription : MegaParsec utilitiesCopyright : (c) Julian Ospald, 2020License : LGPL-3.0Maintainer : hasufell@hasufell.deStability : experimentalPortability : portable-}module GHCup.Prelude.MegaParsec whereimport GHCup.Typesimport Control.Applicative ( Alternative (many, (<|>)) )#if !MIN_VERSION_base(4,13,0)import Control.Monad.Fail ( MonadFail )#endifimport Data.Foldable ( asum )import Data.Functorimport Data.Maybeimport Data.Text ( Text )import Data.Versionsimport Data.Voidimport System.FilePathimport Text.PrettyPrint.HughesPJClass ( prettyShow )import Data.List.NonEmpty ( NonEmpty ((:|)) )import qualified Data.List.NonEmpty as NEimport qualified Data.Text as Timport qualified Text.Megaparsec as MPimport qualified Text.Megaparsec.Char as MPCimport qualified Text.Megaparsec.Char.Lexer as Lchoice' :: (MonadFail f, MP.MonadParsec e s f) => [f a] -> f achoice' [] = fail "Empty list"choice' [x ] = xchoice' (x : xs) = MP.try x <|> choice' xsparseUntil :: MP.Parsec Void Text a -> MP.Parsec Void Text TextparseUntil p = do (MP.try (MP.lookAhead p) $> mempty) <|> (do c <- T.singleton <$> MP.anySingle c2 <- parseUntil p pure (c `mappend` c2) )parseUntil1 :: MP.Parsec Void Text a -> MP.Parsec Void Text TextparseUntil1 p = do i1 <- MP.getOffset t <- parseUntil p i2 <- MP.getOffset if i1 == i2 then fail "empty parse" else pure t-- | Parses e.g.-- * armv7-unknown-linux-gnueabihf-ghc-- * armv7-unknown-linux-gnueabihf-ghcighcTargetBinP :: Text -> MP.Parsec Void Text (Maybe Text, Text)ghcTargetBinP t = (,) <$> ( MP.try (Just <$> parseUntil1 (MP.chunk "-" *> MP.chunk t) <* MP.chunk "-" ) <|> ((\ _ x -> x) Nothing <$> mempty) ) <*> (MP.chunk t <* MP.eof)-- | Extracts the version from @ProjectVersion="8.10.5"@.ghcProjectVersion :: MP.Parsec Void Text VersionghcProjectVersion = do _ <- MP.chunk "ProjectVersion=\"" ver <- parseUntil1 $ MP.chunk "\"" MP.setInput ver version'-- | Extracts target triple and version from e.g.-- * armv7-unknown-linux-gnueabihf-8.8.3-- * armv7-unknown-linux-gnueabihf-8.8.3ghcTargetVerP :: MP.Parsec Void Text TargetVersionghcTargetVerP = (\x y -> TargetVersion x y) <$> (MP.try (Just <$> parseUntil1 (MP.chunk "-" *> verP') <* MP.chunk "-") <|> ((\ _ x -> x) Nothing <$> mempty) ) <*> (version' <* MP.eof) where verP' :: MP.Parsec Void Text Text verP' = do v <- version' let startsWithDigits = and . take 3 . map (\case Numeric _ -> True Alphanum _ -> False) . NE.toList . (\(Chunks nec) -> nec) $ _vChunks v if startsWithDigits && isNothing (_vEpoch v) then pure $ prettyVer v else fail "Oh"ghcLinkVersion :: MP.Parsec Void Text TargetVersionghcLinkVersion = (\x y -> TargetVersion x y) <$> (MP.try (Just <$> parseUntil1 (MP.chunk "-ghc-" *> verP') <* MP.chunk "-") <|> ((\ _ x -> x) Nothing <$> mempty) ) <*> (MP.chunk "ghc-" *> version' <* MP.eof) where verP' :: MP.Parsec Void Text Text verP' = do v <- version' let startsWithDigits = and . take 3 . map (\case Numeric _ -> True Alphanum _ -> False) . NE.toList . (\(Chunks nec) -> nec) $ _vChunks v if startsWithDigits && isNothing (_vEpoch v) then pure $ prettyVer v else fail "Oh"verP :: MP.Parsec Void Text Text -> MP.Parsec Void Text VersioningverP suffix = do ver <- parseUntil suffix if T.null ver then fail "empty version" else do rest <- MP.getInput MP.setInput ver v <- versioning' MP.setInput rest pure vpathSep :: MP.Parsec Void Text CharpathSep = MP.oneOf pathSeparatorsskipWhile :: (Char -> Bool) -> MP.Parsec Void Text ()skipWhile f = void $ MP.takeWhileP Nothing fskip :: (Char -> Bool) -> MP.Parsec Void Text ()skip f = void $ MP.satisfy fskipSpace :: MP.Parsec Void Text ()skipSpace = void $ MP.satisfy isSpaceskipSpaces :: MP.Parsec Void Text ()skipSpaces = void $ many skipSpaceisSpace :: Char -> BoolisSpace c = (c == ' ') || ('\t' <= c && c <= '\r'){-# INLINE isSpace #-}-- Obtain the version from the link or shim path-- ../ghc/<ver>/bin/ghc-- ../ghc/<ver>/bin/ghc-<ver>ghcVersionFromPath :: MP.Parsec Void Text TargetVersionghcVersionFromPath = toolVersionFromPath ghctoolVersionFromPath :: Tool -> MP.Parsec Void Text TargetVersiontoolVersionFromPath tool = MP.try legacyParse <|> newParse where legacyParse = do beforeBin <- parseUntil1 binDir <* MP.some pathSep MP.setInput beforeBin _ <- parseTillLastPathSep ghcTargetVerP where binDir = MP.some pathSep <* MP.chunk "bin" *> MP.some pathSep <* MP.takeWhile1P Nothing (not . isPathSeparator) <* MP.eof parseTillLastPathSep = (MP.try (parseUntil1 pathSep *> MP.some pathSep) *> parseTillLastPathSep) <|> pure () newParse = asum $ pathSeparators <&> MP.try . newParse' newParse' sep = do let toolPath = T.pack $ [sep] <> prettyShow tool <> [sep] ver <- parseUntilEmpty toolPath *> MP.chunk toolPath *> parseUntil1 (MP.chunk $ T.singleton sep) MP.setInput ver ghcTargetVerP{-- -- this doesn'twork because parseUntilEmpty can't take a parser as input--}ghcTargetVerRevP :: MP.Parsec Void Text TargetVersionReqghcTargetVerRevP = MP.try withRev <|> ((`TargetVersionReq` Nothing) <$> ghcTargetVerP) where withRev = do verText <- parseUntilEmpty "-r" rev <- MP.chunk "-r" *> L.decimal MP.setInput verText tver <- ghcTargetVerP pure $ TargetVersionReq tver (Just rev)verRevP :: MP.Parsec Void Text VersionReqverRevP = MP.try withRev <|> ((`VersionReq` Nothing) <$> version') where withRev = do verText <- parseUntilEmpty "-r" rev <- MP.chunk "-r" *> L.decimal MP.setInput verText ver <- version' pure $ VersionReq ver (Just rev)-- find a parse in a greedy mannerparseUntilEmpty :: Text -> MP.Parsec Void Text TextparseUntilEmpty needle = go where go = do prefix <- parseUntil (MP.chunk needle) s2 <- MP.try ((<>) <$> MP.chunk needle <*> go) <|> mempty pure $ prefix <> s2versionCmpP :: MP.Parsec Void T.Text VersionCmpversionCmpP = either (fail . T.unpack) pure =<< (translate <$> (MPC.space *> MP.try (MP.takeWhileP Nothing (`elem` ['>', '<', '=']))) <*> (MPC.space *> versioningEnd)) where translate ">" v = Right $ VR_gt v translate ">=" v = Right $ VR_gteq v translate "<" v = Right $ VR_lt v translate "<=" v = Right $ VR_lteq v translate "==" v = Right $ VR_eq v translate "" v = Right $ VR_eq v translate c _ = Left $ "unexpected comparator: " <> cversionRangeP :: MP.Parsec Void T.Text VersionRangeversionRangeP = go <* MP.eof where go = MP.try orParse <|> MP.try (fmap SimpleRange andParse) <|> fmap (SimpleRange . pure) versionCmpP orParse :: MP.Parsec Void T.Text VersionRange orParse = (\a o -> OrRange a o) <$> (MP.try andParse <|> fmap pure versionCmpP) <*> (MPC.space *> MP.chunk "||" *> MPC.space *> go) andParse :: MP.Parsec Void T.Text (NonEmpty VersionCmp) andParse = fmap (\h t -> h :| t) (MPC.space *> MP.chunk "(" *> MPC.space *> versionCmpP) <*> MP.try (MP.many (MPC.space *> MP.chunk "&&" *> MPC.space *> versionCmpP)) <* MPC.space <* MP.chunk ")" <* MPC.spaceversioningEnd :: MP.Parsec Void T.Text VersioningversioningEnd = MP.try (verP (MP.chunk " " <|> MP.chunk ")" <|> MP.chunk "&&") <* MPC.space) <|> versioning'