-- Copyright (c) 2020, Shayne Fletcher. All rights reserved.
-- SPDX-License-Identifier: BSD-3-Clause.

{-# OPTIONS_GHC -Wno-missing-fields #-}
{-# LANGUAGE CPP #-}
#include "ghclib_api.h"

module Language.Haskell.GhclibParserEx.Parse(
    parseFile
  , parseModule
  , parseSignature
  , parseImport
  , parseStatement
  , parseBackpack
  , parseDeclaration
  , parseExpression
  , parsePattern
  , parseTypeSignature
  , parseStmt
  , parseIdentifier
  , parseType
  , parseHeader
  , parse
  )
  where

#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
import GHC.Hs
import RdrHsSyn
#else
import HsSyn
#endif
#if defined (GHCLIB_API_811)
import GHC.Driver.Session
#else
import DynFlags
#endif
import StringBuffer
import Lexer
import qualified Parser
import FastString
import SrcLoc
#if defined (GHCLIB_API_811)
import GHC.Driver.Backpack.Syntax
#else
import BkpSyn
#endif
#if defined (GHCLIB_API_811)
import UnitInfo
#else
import PackageConfig
#endif
import RdrName

parse :: P a -> String -> DynFlags -> ParseResult a
parse :: P a -> String -> DynFlags -> ParseResult a
parse p :: P a
p str :: String
str flags :: DynFlags
flags =
  P a -> PState -> ParseResult a
forall a. P a -> PState -> ParseResult a
Lexer.unP P a
p PState
parseState
  where
    location :: RealSrcLoc
location = FastString -> Int -> Int -> RealSrcLoc
mkRealSrcLoc (String -> FastString
mkFastString "<string>") 1 1
    buffer :: StringBuffer
buffer = String -> StringBuffer
stringToStringBuffer String
str
    parseState :: PState
parseState = DynFlags -> StringBuffer -> RealSrcLoc -> PState
mkPState DynFlags
flags StringBuffer
buffer RealSrcLoc
location

#if defined (GHCLIB_API_811)
parseModule :: String -> DynFlags -> ParseResult (Located HsModule)
#else
parseModule :: String -> DynFlags -> ParseResult (Located (HsModule GhcPs))
#endif
parseModule :: String -> DynFlags -> ParseResult (Located (HsModule GhcPs))
parseModule = P (Located (HsModule GhcPs))
-> String -> DynFlags -> ParseResult (Located (HsModule GhcPs))
forall a. P a -> String -> DynFlags -> ParseResult a
parse P (Located (HsModule GhcPs))
Parser.parseModule

#if defined (GHCLIB_API_811)
parseSignature :: String -> DynFlags -> ParseResult (Located HsModule)
#else
parseSignature :: String -> DynFlags -> ParseResult (Located (HsModule GhcPs))
#endif
parseSignature :: String -> DynFlags -> ParseResult (Located (HsModule GhcPs))
parseSignature = P (Located (HsModule GhcPs))
-> String -> DynFlags -> ParseResult (Located (HsModule GhcPs))
forall a. P a -> String -> DynFlags -> ParseResult a
parse P (Located (HsModule GhcPs))
Parser.parseSignature

parseImport :: String -> DynFlags -> ParseResult (LImportDecl GhcPs)
parseImport :: String -> DynFlags -> ParseResult (LImportDecl GhcPs)
parseImport = P (LImportDecl GhcPs)
-> String -> DynFlags -> ParseResult (LImportDecl GhcPs)
forall a. P a -> String -> DynFlags -> ParseResult a
parse P (LImportDecl GhcPs)
Parser.parseImport

parseStatement :: String -> DynFlags -> ParseResult (LStmt GhcPs (LHsExpr GhcPs))
parseStatement :: String -> DynFlags -> ParseResult (LStmt GhcPs (LHsExpr GhcPs))
parseStatement = P (LStmt GhcPs (LHsExpr GhcPs))
-> String -> DynFlags -> ParseResult (LStmt GhcPs (LHsExpr GhcPs))
forall a. P a -> String -> DynFlags -> ParseResult a
parse P (LStmt GhcPs (LHsExpr GhcPs))
Parser.parseStatement

parseBackpack :: String -> DynFlags -> ParseResult [LHsUnit PackageName]
parseBackpack :: String -> DynFlags -> ParseResult [LHsUnit PackageName]
parseBackpack = P [LHsUnit PackageName]
-> String -> DynFlags -> ParseResult [LHsUnit PackageName]
forall a. P a -> String -> DynFlags -> ParseResult a
parse P [LHsUnit PackageName]
Parser.parseBackpack

parseDeclaration :: String -> DynFlags -> ParseResult (LHsDecl GhcPs)
parseDeclaration :: String -> DynFlags -> ParseResult (LHsDecl GhcPs)
parseDeclaration = P (LHsDecl GhcPs)
-> String -> DynFlags -> ParseResult (LHsDecl GhcPs)
forall a. P a -> String -> DynFlags -> ParseResult a
parse P (LHsDecl GhcPs)
Parser.parseDeclaration

#if defined (GHCLIB_API_811)
parseExpression :: String -> DynFlags -> ParseResult RdrHsSyn.ECP
#else
parseExpression :: String -> DynFlags -> ParseResult (LHsExpr GhcPs)
#endif
parseExpression :: String -> DynFlags -> ParseResult (LHsExpr GhcPs)
parseExpression = P (LHsExpr GhcPs)
-> String -> DynFlags -> ParseResult (LHsExpr GhcPs)
forall a. P a -> String -> DynFlags -> ParseResult a
parse P (LHsExpr GhcPs)
Parser.parseExpression

parsePattern :: String -> DynFlags -> ParseResult (LPat GhcPs)
parsePattern :: String -> DynFlags -> ParseResult (LPat GhcPs)
parsePattern = P (LPat GhcPs) -> String -> DynFlags -> ParseResult (LPat GhcPs)
forall a. P a -> String -> DynFlags -> ParseResult a
parse P (LPat GhcPs)
Parser.parsePattern

parseTypeSignature :: String -> DynFlags -> ParseResult (LHsDecl GhcPs)
parseTypeSignature :: String -> DynFlags -> ParseResult (LHsDecl GhcPs)
parseTypeSignature = P (LHsDecl GhcPs)
-> String -> DynFlags -> ParseResult (LHsDecl GhcPs)
forall a. P a -> String -> DynFlags -> ParseResult a
parse P (LHsDecl GhcPs)
Parser.parseTypeSignature

parseStmt :: String -> DynFlags -> ParseResult (Maybe (LStmt GhcPs (LHsExpr GhcPs)))
parseStmt :: String
-> DynFlags -> ParseResult (Maybe (LStmt GhcPs (LHsExpr GhcPs)))
parseStmt = P (Maybe (LStmt GhcPs (LHsExpr GhcPs)))
-> String
-> DynFlags
-> ParseResult (Maybe (LStmt GhcPs (LHsExpr GhcPs)))
forall a. P a -> String -> DynFlags -> ParseResult a
parse P (Maybe (LStmt GhcPs (LHsExpr GhcPs)))
Parser.parseStmt

parseIdentifier :: String -> DynFlags -> ParseResult (Located RdrName)
parseIdentifier :: String -> DynFlags -> ParseResult (Located RdrName)
parseIdentifier = P (Located RdrName)
-> String -> DynFlags -> ParseResult (Located RdrName)
forall a. P a -> String -> DynFlags -> ParseResult a
parse P (Located RdrName)
Parser.parseIdentifier

parseType :: String -> DynFlags -> ParseResult (LHsType GhcPs)
parseType :: String -> DynFlags -> ParseResult (LHsType GhcPs)
parseType = P (LHsType GhcPs)
-> String -> DynFlags -> ParseResult (LHsType GhcPs)
forall a. P a -> String -> DynFlags -> ParseResult a
parse P (LHsType GhcPs)
Parser.parseType

#if defined(GHCLIB_API_811)
parseHeader :: String -> DynFlags -> ParseResult (Located HsModule)
#else
parseHeader :: String -> DynFlags -> ParseResult (Located (HsModule GhcPs))
#endif
parseHeader :: String -> DynFlags -> ParseResult (Located (HsModule GhcPs))
parseHeader = P (Located (HsModule GhcPs))
-> String -> DynFlags -> ParseResult (Located (HsModule GhcPs))
forall a. P a -> String -> DynFlags -> ParseResult a
parse P (Located (HsModule GhcPs))
Parser.parseHeader

#if defined (GHCLIB_API_811)
parseFile :: String
          -> DynFlags
          -> String
          -> ParseResult (Located HsModule)
#else
parseFile :: String
          -> DynFlags
          -> String
          -> ParseResult (Located (HsModule GhcPs))
#endif
parseFile :: String
-> DynFlags -> String -> ParseResult (Located (HsModule GhcPs))
parseFile filename :: String
filename flags :: DynFlags
flags str :: String
str =
  P (Located (HsModule GhcPs))
-> PState -> ParseResult (Located (HsModule GhcPs))
forall a. P a -> PState -> ParseResult a
unP P (Located (HsModule GhcPs))
Parser.parseModule PState
parseState
  where
    location :: RealSrcLoc
location = FastString -> Int -> Int -> RealSrcLoc
mkRealSrcLoc (String -> FastString
mkFastString String
filename) 1 1
    buffer :: StringBuffer
buffer = String -> StringBuffer
stringToStringBuffer String
str
    parseState :: PState
parseState = DynFlags -> StringBuffer -> RealSrcLoc -> PState
mkPState DynFlags
flags StringBuffer
buffer RealSrcLoc
location