-- 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 SrcSpanAnnA
_ (ConPat XConPat GhcPs
_ (L SrcSpanAnnN
_ RdrName
x) (PrefixCon [] []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
x) forall a. Eq a => a -> a -> Bool
== String
"True" = String
"True"
patToStr (L SrcSpanAnnA
_ (ConPat XConPat GhcPs
_ (L SrcSpanAnnN
_ RdrName
x) (PrefixCon [] []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
x) forall a. Eq a => a -> a -> Bool
== String
"False" = String
"False"
patToStr (L SrcSpanAnnA
_ (ConPat XConPat GhcPs
_ (L SrcSpanAnnN
_ RdrName
x) (PrefixCon [] []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
x) 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 forall a. Eq a => a -> a -> Bool
== String
"True"  =
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902)
  forall a an. a -> LocatedAn an a
noLocA 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)
    forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat forall a. EpAnn a
noAnn (forall a an. a -> LocatedAn an a
noLocA RdrName
true_RDR) (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 forall a. Eq a => a -> a -> Bool
== String
"False" =
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902)
  forall a an. a -> LocatedAn an a
noLocA 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)
    forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat forall a. EpAnn a
noAnn (forall a an. a -> LocatedAn an a
noLocA RdrName
false_RDR) (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 forall a. Eq a => a -> a -> Bool
== String
"[]"    =
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_904) || defined (GHCLIB_API_902)
  forall a an. a -> LocatedAn an a
noLocA 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)
    forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat forall a. EpAnn a
noAnn (forall a an. a -> LocatedAn an a
noLocA forall a b. (a -> b) -> a -> b
$ Name -> RdrName
nameRdrName Name
nilDataConName) (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)
      forall a an. a -> LocatedAn an a
noLocA forall a b. (a -> b) -> a -> b
$ forall p. XVarPat p -> LIdP p -> Pat p
VarPat NoExtField
noExtField (forall a an. a -> LocatedAn an a
noLocA 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 SrcSpanAnnA
_ (LitPat XLitPat GhcPs
_ (HsChar XHsChar GhcPs
_ Char
x))) = forall a. a -> Maybe a
Just Char
x
#else
fromPChar (dL -> L _ (LitPat _ (HsChar _ x))) = Just x
#endif
fromPChar LPat GhcPs
_ = 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 (XRec p RecFieldsDotDot)
rec_dotdot=Just XRec GhcPs RecFieldsDotDot
_} = 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 :: LHsFieldBind GhcPs (LFieldOcc GhcPs) (LPat GhcPs) -> Bool
isPFieldWildcard (L SrcSpanAnnA
_ HsFieldBind {hfbRHS :: forall lhs rhs. HsFieldBind lhs rhs -> rhs
hfbRHS=L SrcSpanAnnA
_ WildPat {}}) = Bool
True
isPFieldWildcard (L SrcSpanAnnA
_ HsFieldBind {hfbPun :: forall lhs rhs. HsFieldBind lhs rhs -> Bool
hfbPun=Bool
True}) = Bool
True
isPFieldWildcard (L SrcSpanAnnA
_ HsFieldBind {}) = Bool
False
#elif defined (GHCLIB_API_902) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
isPFieldWildcard (L _ HsRecField {hsRecFieldArg=L _ WildPat {}}) = True
isPFieldWildcard (L _ HsRecField {hsRecPun=True}) = True
isPFieldWildcard (L _ HsRecField {}) = 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 SrcSpanAnnA
_ (WildPat XWildPat GhcPs
_)) = 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 :: LHsFieldBind GhcPs (LFieldOcc GhcPs) (LPat GhcPs) -> Bool
isPFieldPun (L SrcSpanAnnA
_ HsFieldBind {hfbPun :: forall lhs rhs. HsFieldBind lhs rhs -> Bool
hfbPun=Bool
True}) = Bool
True
#elif defined (GHCLIB_API_902) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
isPFieldPun (L _ HsRecField {hsRecPun=True}) = True
#else
isPFieldPun (dL -> L _ HsRecField {hsRecPun=True}) = True
#endif
isPFieldPun LHsFieldBind GhcPs (LFieldOcc 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 SrcSpanAnnA
_ SigPat{}) = Bool
True; isPatTypeSig LPat GhcPs
_ = Bool
False
isPBangPat :: LPat GhcPs -> Bool
isPBangPat (L SrcSpanAnnA
_ BangPat{}) = Bool
True; isPBangPat LPat GhcPs
_ = Bool
False
isPViewPat :: LPat GhcPs -> Bool
isPViewPat (L SrcSpanAnnA
_ 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 SrcSpanAnnA
_ SplicePat{}) = Bool
True; isSplicePat LPat GhcPs
_ = Bool
False
#else
isSplicePat (dL -> L _ SplicePat{}) = True; isSplicePat _ = False
#endif