ghc-exactprint-1.7.0.1: ExactPrint for GHC

Safe Haskell | Safe-Inferred
Language | Haskell2010

Language.Haskell.GHC.ExactPrint.Parsers

Contents

- Utility
- Module Parsers
- Basic Parsers
- Internal

Description

This module rexposes wrapped parsers from the GHC API. Along with returning the parse result, the corresponding annotations are also returned such that it is then easy to modify the annotations and print the result.

Synopsis

- type Parser a = DynFlags -> FilePath -> String -> ParseResult a
- type ParseResult a = Either ErrorMessages a
- withDynFlags :: LibDir -> (DynFlags -> a) -> IO a
- data CppOptions = CppOptions {
  - cppDefine :: [String]
  - cppInclude :: [FilePath]
  - cppFile :: [FilePath]
  }
- defaultCppOptions :: CppOptions
- type LibDir = FilePath
- parseModule :: LibDir -> FilePath -> IO (ParseResult ParsedSource)
- parseModuleFromString :: LibDir -> FilePath -> String -> IO (ParseResult ParsedSource)
- parseModuleWithOptions :: LibDir -> FilePath -> IO (ParseResult ParsedSource)
- parseModuleWithCpp :: LibDir -> CppOptions -> FilePath -> IO (ParseResult ParsedSource)
- parseExpr :: Parser (LHsExpr GhcPs)
- parseImport :: Parser (LImportDecl GhcPs)
- parseType :: Parser (LHsType GhcPs)
- parseDecl :: Parser (LHsDecl GhcPs)
- parsePattern :: Parser (LPat GhcPs)
- parseStmt :: Parser (ExprLStmt GhcPs)
- parseWith :: DynFlags -> FilePath -> P w -> String -> ParseResult w
- ghcWrapper :: LibDir -> Ghc a -> IO a
- initDynFlags :: GhcMonad m => FilePath -> m DynFlags
- initDynFlagsPure :: GhcMonad m => FilePath -> String -> m DynFlags
- parseModuleFromStringInternal :: Parser ParsedSource
- parseModuleEpAnnsWithCpp :: LibDir -> CppOptions -> FilePath -> IO (Either ErrorMessages ([LEpaComment], DynFlags, ParsedSource))
- parseModuleEpAnnsWithCppInternal :: GhcMonad m => CppOptions -> DynFlags -> FilePath -> m (Either ErrorMessages ([LEpaComment], DynFlags, ParsedSource))
- postParseTransform :: Either a ([LEpaComment], DynFlags, ParsedSource) -> Either a ParsedSource

# Utility

type Parser a = DynFlags -> FilePath -> String -> ParseResult a #

type ParseResult a = Either ErrorMessages a #

withDynFlags :: LibDir -> (DynFlags -> a) -> IO a #

Provides a safe way to consume a properly initialised set of `DynFlags`.

    myParser fname expr = withDynFlags (\d -> parseExpr d fname expr)

data CppOptions #

Constructors

CppOptions |

Fields

- cppDefine :: [String]

  CPP #define macros

- cppInclude :: [FilePath]

  CPP Includes directory

- cppFile :: [FilePath]

  CPP pre-include file

defaultCppOptions :: CppOptions #

type LibDir = FilePath #

# Module Parsers

parseModule :: LibDir -> FilePath -> IO (ParseResult ParsedSource) #

This entry point will also work out which language extensions are required and perform CPP processing if necessary.

    parseModule = parseModuleWithCpp defaultCppOptions

Note: `ParsedSource` is a synonym for `Located` (`HsModule` `GhcPs`)

parseModuleFromString :: LibDir -> FilePath -> String -> IO (ParseResult ParsedSource) #

This entry point will work out which language extensions are required but will _not_ perform CPP processing. In contrast to `parseModoule` the input source is read from the provided string; the `FilePath` parameter solely exists to provide a name in source location annotations.

parseModuleWithOptions #

Arguments

:: LibDir | GHC libdir
-> FilePath |
-> IO (ParseResult ParsedSource) |

parseModuleWithCpp #

Arguments

:: LibDir | GHC libdir
-> CppOptions |
-> FilePath | File to be parsed
-> IO (ParseResult ParsedSource) |

Parse a module with specific instructions for the C pre-processor.

# Basic Parsers

parseExpr :: Parser (LHsExpr GhcPs) #

parseImport :: Parser (LImportDecl GhcPs) #

parseType :: Parser (LHsType GhcPs) #

parseDecl :: Parser (LHsDecl GhcPs) #

parsePattern :: Parser (LPat GhcPs) #

parseStmt :: Parser (ExprLStmt GhcPs) #

parseWith :: DynFlags -> FilePath -> P w -> String -> ParseResult w #

Wrapper function which returns Annotations along with the parsed element.

# Internal

ghcWrapper :: LibDir -> Ghc a -> IO a #

Internal function. Default runner of GHC.Ghc action in IO.

initDynFlags :: GhcMonad m => FilePath -> m DynFlags #

Internal function. Initializes DynFlags value for parsing.

Passes "-hide-all-packages" to the GHC API to prevent parsing of package environment files. However this only works if there is no invocation of `setSessionDynFlags` before calling `initDynFlags`. See ghc tickets #15513, #15541.

initDynFlagsPure :: GhcMonad m => FilePath -> String -> m DynFlags #

Requires GhcMonad constraint because there is no pure variant of `parseDynamicFilePragma`. Yet, in constrast to `initDynFlags`, it does not (try to) read the file at filepath, but solely depends on the module source in the input string.

Passes "-hide-all-packages" to the GHC API to prevent parsing of package environment files. However this only works if there is no invocation of `setSessionDynFlags` before calling `initDynFlagsPure`. See ghc tickets #15513, #15541.

parseModuleFromStringInternal :: Parser ParsedSource #

Internal part of `parseModuleFromString`.

parseModuleEpAnnsWithCpp #

Arguments

:: LibDir | GHC libdir
-> CppOptions |
-> FilePath | File to be parsed
-> IO (Either ErrorMessages ([LEpaComment], DynFlags, ParsedSource)) |

Low level function which is used in the internal tests. It is advised to use `parseModule` or `parseModuleWithCpp` instead of this function.

parseModuleEpAnnsWithCppInternal :: GhcMonad m => CppOptions -> DynFlags -> FilePath -> m (Either ErrorMessages ([LEpaComment], DynFlags, ParsedSource)) #

Internal function. Exposed if you want to muck with DynFlags before parsing.

postParseTransform :: Either a ([LEpaComment], DynFlags, ParsedSource) -> Either a ParsedSource #

Internal function. Exposed if you want to muck with DynFlags before parsing. Or after parsing.