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

{-# 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
  , isSplicePat
 ) where

#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
import GHC.Hs
#else
import HsSyn
#endif
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902) || defined (GHCLIB_API_900)
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_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902)
patToStr :: LPat GhcPs -> String
patToStr (L _ (ConPat _ (L _ x) (PrefixCon [] []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
x) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"True" = String
"True"
patToStr (L _ (ConPat _ (L _ x) (PrefixCon [] []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
x) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"False" = String
"False"
patToStr (L _ (ConPat _ (L _ x) (PrefixCon [] []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
x) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"[]" = String
"[]"
patToStr LPat GhcPs
_ = String
""
#elif defined (GHCLIB_API_900)
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 (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 (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 String
z
  | String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"True"  =
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902)
  Pat GhcPs -> LocatedAn AnnListItem (Pat GhcPs)
forall a an. a -> LocatedAn an a
noLocA (Pat GhcPs -> LocatedAn AnnListItem (Pat GhcPs))
-> Pat GhcPs -> LocatedAn AnnListItem (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$
#elif defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
  noLoc $
#endif
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902)
    XConPat GhcPs
-> XRec GhcPs (ConLikeP GhcPs)
-> HsConPatDetails GhcPs
-> Pat GhcPs
forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat XConPat GhcPs
forall a. EpAnn a
noAnn (RdrName -> LocatedAn NameAnn RdrName
forall a an. a -> LocatedAn an a
noLocA RdrName
true_RDR) ([HsPatSigType GhcPs]
-> [LocatedAn AnnListItem (Pat GhcPs)]
-> HsConDetails
     (HsPatSigType GhcPs)
     (LocatedAn AnnListItem (Pat GhcPs))
     (HsRecFields GhcPs (LocatedAn AnnListItem (Pat GhcPs)))
forall tyarg arg rec.
[tyarg] -> [arg] -> HsConDetails tyarg arg rec
PrefixCon [] [])
#elif defined (GHCLIB_API_900)
    ConPat noExtField (noLoc true_RDR) (PrefixCon [])
#else
    ConPatIn (noLoc true_RDR) (PrefixCon [])
#endif
  | String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"False" =
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902)
  Pat GhcPs -> LocatedAn AnnListItem (Pat GhcPs)
forall a an. a -> LocatedAn an a
noLocA (Pat GhcPs -> LocatedAn AnnListItem (Pat GhcPs))
-> Pat GhcPs -> LocatedAn AnnListItem (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$
#elif defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
  noLoc $
#endif
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902)
    XConPat GhcPs
-> XRec GhcPs (ConLikeP GhcPs)
-> HsConPatDetails GhcPs
-> Pat GhcPs
forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat XConPat GhcPs
forall a. EpAnn a
noAnn (RdrName -> LocatedAn NameAnn RdrName
forall a an. a -> LocatedAn an a
noLocA RdrName
false_RDR) ([HsPatSigType GhcPs]
-> [LocatedAn AnnListItem (Pat GhcPs)]
-> HsConDetails
     (HsPatSigType GhcPs)
     (LocatedAn AnnListItem (Pat GhcPs))
     (HsRecFields GhcPs (LocatedAn AnnListItem (Pat GhcPs)))
forall tyarg arg rec.
[tyarg] -> [arg] -> HsConDetails tyarg arg rec
PrefixCon [] [])
#elif defined (GHCLIB_API_900)
    ConPat noExtField (noLoc false_RDR) (PrefixCon [])
#else
    ConPatIn (noLoc false_RDR) (PrefixCon [])
#endif
  | String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"[]"    =
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902)
  Pat GhcPs -> LocatedAn AnnListItem (Pat GhcPs)
forall a an. a -> LocatedAn an a
noLocA (Pat GhcPs -> LocatedAn AnnListItem (Pat GhcPs))
-> Pat GhcPs -> LocatedAn AnnListItem (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$
#elif defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
  noLoc $
#endif
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902)
    XConPat GhcPs
-> XRec GhcPs (ConLikeP GhcPs)
-> HsConPatDetails GhcPs
-> Pat GhcPs
forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat XConPat GhcPs
forall a. EpAnn a
noAnn (RdrName -> LocatedAn NameAnn RdrName
forall a an. a -> LocatedAn an a
noLocA (RdrName -> LocatedAn NameAnn RdrName)
-> RdrName -> LocatedAn NameAnn RdrName
forall a b. (a -> b) -> a -> b
$ Name -> RdrName
nameRdrName Name
nilDataConName) ([HsPatSigType GhcPs]
-> [LocatedAn AnnListItem (Pat GhcPs)]
-> HsConDetails
     (HsPatSigType GhcPs)
     (LocatedAn AnnListItem (Pat GhcPs))
     (HsRecFields GhcPs (LocatedAn AnnListItem (Pat GhcPs)))
forall tyarg arg rec.
[tyarg] -> [arg] -> HsConDetails tyarg arg rec
PrefixCon [] [])
#elif defined (GHCLIB_API_900)
    ConPat noExtField (noLoc $ nameRdrName nilDataConName) (PrefixCon [])
#else
    ConPatIn (noLoc $ nameRdrName nilDataConName) (PrefixCon [])
#endif
  | Bool
otherwise =
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902)
      Pat GhcPs -> LocatedAn AnnListItem (Pat GhcPs)
forall a an. a -> LocatedAn an a
noLocA (Pat GhcPs -> LocatedAn AnnListItem (Pat GhcPs))
-> Pat GhcPs -> LocatedAn AnnListItem (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$ XVarPat GhcPs -> LIdP GhcPs -> Pat GhcPs
forall p. XVarPat p -> LIdP p -> Pat p
VarPat NoExtField
XVarPat GhcPs
noExtField (RdrName -> LocatedAn NameAnn RdrName
forall a an. a -> LocatedAn an a
noLocA (RdrName -> LocatedAn NameAnn RdrName)
-> RdrName -> LocatedAn NameAnn RdrName
forall a b. (a -> b) -> a -> b
$ FastString -> RdrName
mkVarUnqual (String -> FastString
fsLit String
z))
#elif defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
      noLoc $ VarPat noExtField (noLoc $ mkVarUnqual (fsLit z))
#else
      VarPat noExt (noLoc $ mkVarUnqual (fsLit z))
#endif

fromPChar :: LPat GhcPs -> Maybe Char
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902) || defined (GHCLIB_API_900) || 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 LPat GhcPs
_ = Maybe Char
forall a. Maybe a
Nothing

-- Contains a '..' as in 'Foo{..}'
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 Located Int
_} = Bool
True
hasPFieldsDotDot HsRecFields GhcPs (LPat GhcPs)
_ = Bool
False

-- Field has a '_' as in '{foo=_} or is punned e.g. '{foo}'.
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904)
isPFieldWildcard :: LHsFieldBind GhcPs (LFieldOcc GhcPs) (LPat GhcPs) -> Bool
#else
isPFieldWildcard :: LHsRecField GhcPs (LPat GhcPs) -> Bool
#endif
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904)
isPFieldWildcard (L _ HsFieldBind {hfbRHS=L _ WildPat {}}) = True
isPFieldWildcard (L _ HsFieldBind {hfbPun=True}) = True
isPFieldWildcard (L _ HsFieldBind {}) = False
#elif defined (GHCLIB_API_902) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
isPFieldWildcard :: LHsRecField GhcPs (LPat GhcPs) -> Bool
isPFieldWildcard (L _ HsRecField {hsRecFieldArg=L _ WildPat {}}) = Bool
True
isPFieldWildcard (L _ HsRecField {hsRecPun=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_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
isPWildcard :: LPat GhcPs -> Bool
isPWildcard (L _ (WildPat _)) = Bool
True
#else
isPWildcard (dL -> L _ (WildPat _)) = True
#endif
isPWildcard LPat GhcPs
_ = Bool
False

#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904)
isPFieldPun :: LHsFieldBind GhcPs (LFieldOcc GhcPs) (LPat GhcPs) -> Bool
#else
isPFieldPun :: LHsRecField GhcPs (LPat GhcPs) -> Bool
#endif
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904)
isPFieldPun (L _ HsFieldBind {hfbPun=True}) = True
#elif defined (GHCLIB_API_902) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
isPFieldPun :: LHsRecField GhcPs (LPat GhcPs) -> Bool
isPFieldPun (L _ HsRecField {hsRecPun=True}) = Bool
True
#else
isPFieldPun (dL -> L _ HsRecField {hsRecPun=True}) = True
#endif
isPFieldPun LHsRecField GhcPs (LPat GhcPs)
_ = Bool
False

isPatTypeSig, isPBangPat, isPViewPat :: LPat GhcPs -> Bool
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
isPatTypeSig :: LPat GhcPs -> Bool
isPatTypeSig (L _ SigPat{}) = Bool
True; isPatTypeSig LPat GhcPs
_ = Bool
False
isPBangPat :: LPat GhcPs -> Bool
isPBangPat (L _ BangPat{}) = Bool
True; isPBangPat LPat GhcPs
_ = Bool
False
isPViewPat :: LPat GhcPs -> Bool
isPViewPat (L _ ViewPat{}) = Bool
True; isPViewPat LPat GhcPs
_ = 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

isSplicePat :: LPat GhcPs -> Bool
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
isSplicePat :: LPat GhcPs -> Bool
isSplicePat (L _ SplicePat{}) = Bool
True; isSplicePat LPat GhcPs
_ = Bool
False
#else
isSplicePat (dL -> L _ SplicePat{}) = True; isSplicePat _ = False
#endif