{-# LANGUAGE CPP #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-missing-fields -Wno-name-shadowing -Wno-unused-imports #-}

module PyF.Internal.ParserEx (fakeSettings, fakeLlvmConfig, parseExpression) where

{- ORMOLU_DISABLE -}

#if MIN_VERSION_ghc(9,6,0)
import GHC.Settings.Config
import GHC.CmmToLlvm.Config (LlvmConfig(..))
import GHC.Utils.Fingerprint
import GHC.Platform
import GHC.Settings
#elif MIN_VERSION_ghc(9,0,0)
import GHC.Settings.Config
import GHC.Driver.Session
import GHC.Utils.Fingerprint
import GHC.Platform
import GHC.Settings
#elif MIN_VERSION_ghc(8, 10, 0)
import Config
import DynFlags
import Fingerprint
import GHC.Platform
import ToolSettings
#else
import Config
import DynFlags
import Fingerprint
import Platform
#endif

#if MIN_VERSION_ghc(8,10,0)
import GHC.Hs
#else
import HsSyn
#endif

#if MIN_VERSION_ghc(9, 2, 0)
import GHC.Driver.Config
#endif

#if MIN_VERSION_ghc(9,0,0)
import GHC.Parser.PostProcess
import GHC.Driver.Session
import GHC.Data.StringBuffer
import GHC.Parser.Lexer
import qualified GHC.Parser.Lexer as Lexer
import qualified GHC.Parser as Parser
import GHC.Data.FastString
import GHC.Types.SrcLoc
import GHC.Driver.Backpack.Syntax
import GHC.Unit.Info
import GHC.Types.Name.Reader
#else
import StringBuffer
import Lexer
import qualified Parser
import FastString
import SrcLoc
import RdrName
#endif

#if MIN_VERSION_ghc(9, 0, 0)
#else
import RdrHsSyn
#endif

import Data.Data hiding (Fixity)

#if MIN_VERSION_ghc(9,0,0)
import GHC.Hs

#if MIN_VERSION_ghc(9, 2, 0)
import GHC.Types.Fixity
import GHC.Types.SourceText
#else
import GHC.Types.Basic
#endif

import GHC.Types.Name.Reader
import GHC.Types.Name
import GHC.Types.SrcLoc
#elif MIN_VERSION_ghc(8, 10, 0)
import BasicTypes
import OccName
#else
import BasicTypes
import OccName
#endif

#if MIN_VERSION_ghc(9,3,0)
import GHC.Driver.Config.Parser (initParserOpts)
#endif

import Data.Maybe

fakeSettings :: Settings
fakeSettings :: Settings
fakeSettings = Settings
#if MIN_VERSION_ghc(9, 2, 0)
  { sGhcNameVersion :: GhcNameVersion
sGhcNameVersion=GhcNameVersion
ghcNameVersion
  , sFileSettings :: FileSettings
sFileSettings=FileSettings
fileSettings
  , sTargetPlatform :: Platform
sTargetPlatform=Platform
platform
  , sPlatformMisc :: PlatformMisc
sPlatformMisc=PlatformMisc
platformMisc
  , sToolSettings :: ToolSettings
sToolSettings=ToolSettings
toolSettings
  }
#elif MIN_VERSION_ghc(8, 10, 0)
  { sGhcNameVersion=ghcNameVersion
  , sFileSettings=fileSettings
  , sTargetPlatform=platform
  , sPlatformMisc=platformMisc
  , sPlatformConstants=platformConstants
  , sToolSettings=toolSettings
  }
#else
  { sTargetPlatform=platform
  , sPlatformConstants=platformConstants
  , sProjectVersion=cProjectVersion
  , sProgramName="ghc"
  , sOpt_P_fingerprint=fingerprint0
  }
#endif
  where
#if MIN_VERSION_ghc(8, 10, 0)
    toolSettings :: ToolSettings
toolSettings = ToolSettings {
      toolSettings_opt_P_fingerprint :: Fingerprint
toolSettings_opt_P_fingerprint=Fingerprint
fingerprint0
      }
    fileSettings :: FileSettings
fileSettings = FileSettings {}
    platformMisc :: PlatformMisc
platformMisc = PlatformMisc {}
    ghcNameVersion :: GhcNameVersion
ghcNameVersion =
      GhcNameVersion{ghcNameVersion_programName :: String
ghcNameVersion_programName=String
"ghc"
                    ,ghcNameVersion_projectVersion :: String
ghcNameVersion_projectVersion=String
cProjectVersion
                    }
#endif
    platform :: Platform
platform =
      Platform{
#if MIN_VERSION_ghc(9, 0, 0)
    -- It doesn't matter what values we write here as these fields are
    -- not referenced for our purposes. However the fields are strict
    -- so we must say something.
        platformByteOrder :: ByteOrder
platformByteOrder=ByteOrder
LittleEndian
      , platformHasGnuNonexecStack :: Bool
platformHasGnuNonexecStack=Bool
True
      , platformHasIdentDirective :: Bool
platformHasIdentDirective=Bool
False
      , platformHasSubsectionsViaSymbols :: Bool
platformHasSubsectionsViaSymbols=Bool
False
      , platformIsCrossCompiling :: Bool
platformIsCrossCompiling=Bool
False
      , platformLeadingUnderscore :: Bool
platformLeadingUnderscore=Bool
False
      , platformTablesNextToCode :: Bool
platformTablesNextToCode=Bool
False
#if MIN_VERSION_ghc(9, 2, 0)
      , platform_constants :: Maybe PlatformConstants
platform_constants=Maybe PlatformConstants
forall {a}. Maybe a
platformConstants
#endif
#if MIN_VERSION_ghc(9, 3, 0)
      , platformHasLibm :: Bool
platformHasLibm=Bool
True
#endif

      ,
#endif

#if MIN_VERSION_ghc(9, 2, 0)
        platformWordSize :: PlatformWordSize
platformWordSize=PlatformWordSize
PW8
      , platformArchOS :: ArchOS
platformArchOS=ArchOS {archOS_arch :: Arch
archOS_arch=Arch
ArchUnknown, archOS_OS :: OS
archOS_OS=OS
OSUnknown}
#elif MIN_VERSION_ghc(8, 10, 0)
        platformWordSize=PW8
      , platformMini=PlatformMini {platformMini_arch=ArchUnknown, platformMini_os=OSUnknown}
#else
        platformWordSize=8
      , platformOS=OSUnknown
#endif
      , platformUnregisterised :: Bool
platformUnregisterised=Bool
True
      }
#if MIN_VERSION_ghc(9, 2, 0)
    platformConstants :: Maybe a
platformConstants = Maybe a
forall {a}. Maybe a
Nothing
#else
    platformConstants =
      PlatformConstants{pc_DYNAMIC_BY_DEFAULT=False,pc_WORD_SIZE=8}
#endif

#if MIN_VERSION_ghc(8, 10, 0)
fakeLlvmConfig :: LlvmConfig
fakeLlvmConfig :: LlvmConfig
fakeLlvmConfig = [(String, LlvmTarget)] -> [(Int, String)] -> LlvmConfig
LlvmConfig [] []
#else
fakeLlvmConfig :: (LlvmTargets, LlvmPasses)
fakeLlvmConfig = ([], [])
#endif

-- From Language.Haskell.GhclibParserEx.GHC.Parser

parse :: RealSrcLoc -> P a -> String -> DynFlags -> ParseResult a
parse :: forall a. RealSrcLoc -> P a -> String -> DynFlags -> ParseResult a
parse RealSrcLoc
initLoc P a
p String
str DynFlags
flags =
  P a -> PState -> ParseResult a
forall a. P a -> PState -> ParseResult a
Lexer.unP P a
p PState
parseState
  where
    buffer :: StringBuffer
buffer = String -> StringBuffer
stringToStringBuffer String
str
    parseState :: PState
parseState =
#if MIN_VERSION_ghc(9, 2, 0)
      ParserOpts -> StringBuffer -> RealSrcLoc -> PState
initParserState (DynFlags -> ParserOpts
initParserOpts DynFlags
flags) StringBuffer
buffer RealSrcLoc
initLoc
#else
      mkPState flags buffer initLoc
#endif

#if MIN_VERSION_ghc(9, 2, 0)
parseExpression :: RealSrcLoc -> String -> DynFlags -> ParseResult (LocatedA (HsExpr GhcPs))
parseExpression :: RealSrcLoc
-> String -> DynFlags -> ParseResult (LocatedA (HsExpr GhcPs))
parseExpression RealSrcLoc
initLoc String
s DynFlags
flags =
  case RealSrcLoc -> P ECP -> String -> DynFlags -> ParseResult ECP
forall a. RealSrcLoc -> P a -> String -> DynFlags -> ParseResult a
parse RealSrcLoc
initLoc P ECP
Parser.parseExpression String
s DynFlags
flags of
    POk PState
s ECP
e -> P (LocatedA (HsExpr GhcPs))
-> PState -> ParseResult (LocatedA (HsExpr GhcPs))
forall a. P a -> PState -> ParseResult a
unP (PV (LocatedA (HsExpr GhcPs)) -> P (LocatedA (HsExpr GhcPs))
forall a. PV a -> P a
runPV (ECP -> forall b. DisambECP b => PV (LocatedA b)
unECP ECP
e)) PState
s
    PFailed PState
ps -> PState -> ParseResult (LocatedA (HsExpr GhcPs))
forall a. PState -> ParseResult a
PFailed PState
ps
#elif MIN_VERSION_ghc(8, 10, 0)
parseExpression :: RealSrcLoc -> String -> DynFlags -> ParseResult (Located (HsExpr GhcPs))
parseExpression initLoc s flags =
  case parse initLoc Parser.parseExpression s flags of
    POk s e -> unP (runECP_P e) s
    PFailed ps -> PFailed ps
#else
parseExpression :: RealSrcLoc -> String -> DynFlags -> ParseResult (LHsExpr GhcPs)
parseExpression initLoc = parse initLoc Parser.parseExpression
#endif