{-# 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
import SrcLoc
import TysWiredIn
import RdrName
import OccName
import FastString
patToStr :: LPat GhcPs -> String
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
patToStr (L _ (ConPatIn (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "True" = "True"
patToStr (L _ (ConPatIn (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "False" = "False"
patToStr (L _ (ConPatIn (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "[]" = "[]"
patToStr _ = ""
#else
patToStr :: LPat GhcPs -> String
patToStr (LPat GhcPs -> Located (SrcSpanLess (LPat GhcPs))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL -> L _ (ConPatIn (L _ x) (PrefixCon []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
IdP GhcPs
x) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "True" = "True"
patToStr (LPat GhcPs -> Located (SrcSpanLess (LPat GhcPs))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL -> L _ (ConPatIn (L _ x) (PrefixCon []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
IdP GhcPs
x) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "False" = "False"
patToStr (LPat GhcPs -> Located (SrcSpanLess (LPat GhcPs))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL -> L _ (ConPatIn (L _ x) (PrefixCon []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
IdP GhcPs
x) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "[]" = "[]"
patToStr _ = ""
#endif
strToPat :: String -> Pat GhcPs
strToPat :: String -> LPat GhcPs
strToPat z :: String
z
| String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "True" = Located (IdP GhcPs) -> HsConPatDetails GhcPs -> LPat 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) ([LPat GhcPs] -> HsConPatDetails GhcPs
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon [])
| String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "False" = Located (IdP GhcPs) -> HsConPatDetails GhcPs -> LPat 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) ([LPat GhcPs] -> HsConPatDetails GhcPs
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon [])
| String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "[]" = Located (IdP GhcPs) -> HsConPatDetails GhcPs -> LPat 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) ([LPat GhcPs] -> HsConPatDetails GhcPs
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon [])
| Bool
otherwise =
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
VarPat noExtField (noLoc $ mkVarUnqual (fsLit z))
#else
XVarPat GhcPs -> Located (IdP GhcPs) -> LPat GhcPs
forall p. XVarPat p -> Located (IdP p) -> Pat p
VarPat NoExt
XVarPat GhcPs
noExt (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))
#endif
fromPChar :: LPat GhcPs -> Maybe Char
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
fromPChar (L _ (LitPat _ (HsChar _ x))) = Just x
#else
fromPChar :: LPat GhcPs -> Maybe Char
fromPChar (LPat GhcPs -> Located (SrcSpanLess (LPat GhcPs))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL -> L _ (LitPat _ (HsChar _ x))) = Char -> Maybe Char
forall a. a -> Maybe a
Just Char
x
#endif
fromPChar _ = Maybe Char
forall a. Maybe a
Nothing
hasPFieldsDotDot :: HsRecFields GhcPs (Pat GhcPs) -> Bool
hasPFieldsDotDot :: HsRecFields GhcPs (LPat GhcPs) -> Bool
hasPFieldsDotDot HsRecFields {rec_dotdot :: forall p arg. HsRecFields p arg -> Maybe Int
rec_dotdot=Just _} = Bool
True
hasPFieldsDotDot _ = Bool
False
isPFieldWildcard :: LHsRecField GhcPs (Pat GhcPs) -> Bool
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
isPFieldWildcard (L _ HsRecField {hsRecFieldArg=WildPat _}) = True
isPFieldWildcard (L _ HsRecField {hsRecPun=True}) = True
isPFieldWildcard (L _ HsRecField {}) = False
#else
isPFieldWildcard :: LHsRecField GhcPs (LPat GhcPs) -> Bool
isPFieldWildcard (LHsRecField GhcPs (LPat GhcPs)
-> Located (SrcSpanLess (LHsRecField GhcPs (LPat GhcPs)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL -> L _ HsRecField {hsRecFieldArg=WildPat _}) = Bool
True
isPFieldWildcard (LHsRecField GhcPs (LPat GhcPs)
-> Located (SrcSpanLess (LHsRecField GhcPs (LPat GhcPs)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL -> L _ HsRecField {hsRecPun=True}) = Bool
True
isPFieldWildcard (LHsRecField GhcPs (LPat GhcPs)
-> Located (SrcSpanLess (LHsRecField GhcPs (LPat GhcPs)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL -> L _ HsRecField {}) = Bool
False
#endif
isPWildcard :: LPat GhcPs -> Bool
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
isPWildcard (L _ (WildPat _)) = True
#else
isPWildcard :: LPat GhcPs -> Bool
isPWildcard (LPat GhcPs -> Located (SrcSpanLess (LPat GhcPs))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL -> L _ (WildPat _)) = Bool
True
#endif
isPWildcard _ = Bool
False
isPFieldPun :: LHsRecField GhcPs (Pat GhcPs) -> Bool
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
isPFieldPun (L _ HsRecField {hsRecPun=True}) = True
#else
isPFieldPun :: LHsRecField GhcPs (LPat GhcPs) -> Bool
isPFieldPun (LHsRecField GhcPs (LPat GhcPs)
-> Located (SrcSpanLess (LHsRecField GhcPs (LPat GhcPs)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL -> L _ HsRecField {hsRecPun=True}) = Bool
True
#endif
isPFieldPun _ = Bool
False
isPatTypeSig, isPBangPat, isPViewPat :: LPat GhcPs -> Bool
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
isPatTypeSig (L _ SigPat{}) = True; isPatTypeSig _ = False
isPBangPat (L _ BangPat{}) = True; isPBangPat _ = False
isPViewPat (L _ ViewPat{}) = True; isPViewPat _ = False
#else
isPatTypeSig :: LPat GhcPs -> Bool
isPatTypeSig (LPat GhcPs -> Located (SrcSpanLess (LPat GhcPs))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL -> L _ SigPat{}) = Bool
True; isPatTypeSig _ = Bool
False
isPBangPat :: LPat GhcPs -> Bool
isPBangPat (LPat GhcPs -> Located (SrcSpanLess (LPat GhcPs))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL -> L _ BangPat{}) = Bool
True; isPBangPat _ = Bool
False
isPViewPat :: LPat GhcPs -> Bool
isPViewPat (LPat GhcPs -> Located (SrcSpanLess (LPat GhcPs))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL -> L _ ViewPat{}) = Bool
True; isPViewPat _ = Bool
False
#endif