{-# LANGUAGE CPP #-}
{-# LANGUAGE ViewPatterns #-}
#include "ghclib_api.h"
module Language.Haskell.GhclibParserEx.GHC.Hs.Pat(
patToStr, strToPat
, fromPChar
, hasPFieldsDotDot
, isPFieldWildcard, isPWildcard, isPFieldPun, isPatTypeSig, isPBangPat, isPViewPat
) where
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
import GHC.Hs
#else
import HsSyn
#endif
#if defined (GHCLIB_API_811)
import GHC.Types.SrcLoc
import GHC.Builtin.Types
import GHC.Types.Name.Reader
import GHC.Types.Name
import GHC.Data.FastString
#else
import SrcLoc
import TysWiredIn
import RdrName
import OccName
import FastString
#endif
patToStr :: LPat GhcPs -> String
#if defined (GHCLIB_API_811)
patToStr (L _ (ConPat _ (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "True" = "True"
patToStr (L _ (ConPat _ (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "False" = "False"
patToStr (L _ (ConPat _ (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "[]" = "[]"
patToStr _ = ""
#elif defined (GHCLIB_API_810)
patToStr :: LPat GhcPs -> String
patToStr (L _ (ConPatIn (L _ x) (PrefixCon []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc IdP GhcPs
RdrName
x) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "True" = "True"
patToStr (L _ (ConPatIn (L _ x) (PrefixCon []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc IdP GhcPs
RdrName
x) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "False" = "False"
patToStr (L _ (ConPatIn (L _ x) (PrefixCon []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc IdP GhcPs
RdrName
x) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "[]" = "[]"
patToStr _ = ""
#else
patToStr (dL -> L _ (ConPatIn (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "True" = "True"
patToStr (dL -> L _ (ConPatIn (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "False" = "False"
patToStr (dL -> L _ (ConPatIn (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "[]" = "[]"
patToStr _ = ""
#endif
strToPat :: String -> LPat GhcPs
strToPat :: String -> LPat GhcPs
strToPat z :: String
z
| String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "True" =
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs)
-> SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs
forall a b. (a -> b) -> a -> b
$
#endif
#if defined (GHCLIB_API_811)
ConPat noExtField (noLoc true_RDR) (PrefixCon [])
#else
Located (IdP GhcPs) -> HsConPatDetails GhcPs -> Pat GhcPs
forall p. Located (IdP p) -> HsConPatDetails p -> Pat p
ConPatIn (SrcSpanLess (Located RdrName) -> Located RdrName
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc RdrName
SrcSpanLess (Located RdrName)
true_RDR) ([Located (Pat GhcPs)]
-> HsConDetails
(Located (Pat GhcPs)) (HsRecFields GhcPs (Located (Pat GhcPs)))
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon [])
#endif
| String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "False" =
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs)
-> SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs
forall a b. (a -> b) -> a -> b
$
#endif
#if defined (GHCLIB_API_811)
ConPat noExtField (noLoc false_RDR) (PrefixCon [])
#else
Located (IdP GhcPs) -> HsConPatDetails GhcPs -> Pat GhcPs
forall p. Located (IdP p) -> HsConPatDetails p -> Pat p
ConPatIn (SrcSpanLess (Located RdrName) -> Located RdrName
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc RdrName
SrcSpanLess (Located RdrName)
false_RDR) ([Located (Pat GhcPs)]
-> HsConDetails
(Located (Pat GhcPs)) (HsRecFields GhcPs (Located (Pat GhcPs)))
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon [])
#endif
| String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "[]" =
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs)
-> SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs
forall a b. (a -> b) -> a -> b
$
#endif
#if defined (GHCLIB_API_811)
ConPat noExtField (noLoc $ nameRdrName nilDataConName) (PrefixCon [])
#else
Located (IdP GhcPs) -> HsConPatDetails GhcPs -> Pat GhcPs
forall p. Located (IdP p) -> HsConPatDetails p -> Pat p
ConPatIn (SrcSpanLess (Located RdrName) -> Located (IdP GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located RdrName) -> Located (IdP GhcPs))
-> SrcSpanLess (Located RdrName) -> Located (IdP GhcPs)
forall a b. (a -> b) -> a -> b
$ Name -> RdrName
nameRdrName Name
nilDataConName) ([Located (Pat GhcPs)]
-> HsConDetails
(Located (Pat GhcPs)) (HsRecFields GhcPs (Located (Pat GhcPs)))
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon [])
#endif
| Bool
otherwise =
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs)
-> SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs
forall a b. (a -> b) -> a -> b
$ XVarPat GhcPs -> Located (IdP GhcPs) -> Pat GhcPs
forall p. XVarPat p -> Located (IdP p) -> Pat p
VarPat NoExtField
XVarPat GhcPs
noExtField (SrcSpanLess (Located RdrName) -> Located (IdP GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located RdrName) -> Located (IdP GhcPs))
-> SrcSpanLess (Located RdrName) -> Located (IdP GhcPs)
forall a b. (a -> b) -> a -> b
$ FastString -> RdrName
mkVarUnqual (String -> FastString
fsLit String
z))
#else
VarPat noExt (noLoc $ mkVarUnqual (fsLit z))
#endif
fromPChar :: LPat GhcPs -> Maybe Char
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
fromPChar :: LPat GhcPs -> Maybe Char
fromPChar (L _ (LitPat _ (HsChar _ x))) = Char -> Maybe Char
forall a. a -> Maybe a
Just Char
x
#else
fromPChar (dL -> L _ (LitPat _ (HsChar _ x))) = Just x
#endif
fromPChar _ = Maybe Char
forall a. Maybe a
Nothing
hasPFieldsDotDot :: HsRecFields GhcPs (LPat GhcPs) -> Bool
hasPFieldsDotDot :: HsRecFields GhcPs (LPat GhcPs) -> Bool
hasPFieldsDotDot HsRecFields {rec_dotdot :: forall p arg. HsRecFields p arg -> Maybe (Located Int)
rec_dotdot=Just _} = Bool
True
hasPFieldsDotDot _ = Bool
False
isPFieldWildcard :: LHsRecField GhcPs (LPat GhcPs) -> Bool
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
isPFieldWildcard :: LHsRecField GhcPs (LPat GhcPs) -> Bool
isPFieldWildcard (L _ HsRecField {hsRecFieldArg :: forall id arg. HsRecField' id arg -> arg
hsRecFieldArg=L _ WildPat {}}) = Bool
True
isPFieldWildcard (L _ HsRecField {hsRecPun :: forall id arg. HsRecField' id arg -> Bool
hsRecPun=Bool
True}) = Bool
True
isPFieldWildcard (L _ HsRecField {}) = Bool
False
#else
isPFieldWildcard (dL -> L _ HsRecField {hsRecFieldArg=LL _ WildPat {}}) = True
isPFieldWildcard (dL -> L _ HsRecField {hsRecPun=True}) = True
isPFieldWildcard (dL -> L _ HsRecField {}) = False
#endif
isPWildcard :: LPat GhcPs -> Bool
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
isPWildcard :: LPat GhcPs -> Bool
isPWildcard (L _ (WildPat _)) = Bool
True
#else
isPWildcard (dL -> L _ (WildPat _)) = True
#endif
isPWildcard _ = Bool
False
isPFieldPun :: LHsRecField GhcPs (LPat GhcPs) -> Bool
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
isPFieldPun :: LHsRecField GhcPs (LPat GhcPs) -> Bool
isPFieldPun (L _ HsRecField {hsRecPun :: forall id arg. HsRecField' id arg -> Bool
hsRecPun=Bool
True}) = Bool
True
#else
isPFieldPun (dL -> L _ HsRecField {hsRecPun=True}) = True
#endif
isPFieldPun _ = Bool
False
isPatTypeSig, isPBangPat, isPViewPat :: LPat GhcPs -> Bool
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
isPatTypeSig :: LPat GhcPs -> Bool
isPatTypeSig (L _ SigPat{}) = Bool
True; isPatTypeSig _ = Bool
False
isPBangPat :: LPat GhcPs -> Bool
isPBangPat (L _ BangPat{}) = Bool
True; isPBangPat _ = Bool
False
isPViewPat :: LPat GhcPs -> Bool
isPViewPat (L _ ViewPat{}) = Bool
True; isPViewPat _ = Bool
False
#else
isPatTypeSig (dL -> L _ SigPat{}) = True; isPatTypeSig _ = False
isPBangPat (dL -> L _ BangPat{}) = True; isPBangPat _ = False
isPViewPat (dL -> L _ ViewPat{}) = True; isPViewPat _ = False
#endif