-- 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
 ) 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 :: 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
== String
"True" = String
"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
== String
"False" = String
"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
== String
"[]" = String
"[]"
patToStr LPat GhcPs
_ = String
""
#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 -> Pat GhcPs
strToPat :: String -> Pat GhcPs
strToPat String
z
  | String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"True"  = 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 [])
  | String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"False" = 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 [])
  | String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"[]"    = 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 (SrcSpanLess (Located RdrName) -> Located RdrName)
-> SrcSpanLess (Located RdrName) -> Located RdrName
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 [])
  | Bool
otherwise =
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
      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 RdrName
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located RdrName) -> Located RdrName)
-> SrcSpanLess (Located RdrName) -> Located RdrName
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 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}'.
isPFieldWildcard :: LHsRecField GhcPs (LPat GhcPs) -> Bool
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
isPFieldWildcard :: LHsRecField GhcPs (LPat GhcPs) -> Bool
isPFieldWildcard (L SrcSpan
_ HsRecField {hsRecFieldArg :: forall id arg. HsRecField' id arg -> arg
hsRecFieldArg=L _ WildPat {}}) = Bool
True
isPFieldWildcard (L SrcSpan
_ HsRecField {hsRecPun :: forall id arg. HsRecField' id arg -> Bool
hsRecPun=Bool
True}) = Bool
True
isPFieldWildcard (L SrcSpan
_ 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 LPat GhcPs
_ = Bool
False

isPFieldPun :: LHsRecField GhcPs (Pat GhcPs) -> Bool
#if defined (GHCLIB_API_811) || defined (GHCLIB_API_810)
isPFieldPun :: LHsRecField GhcPs (Pat GhcPs) -> Bool
isPFieldPun (L SrcSpan
_ HsRecField {hsRecPun :: forall id arg. HsRecField' id arg -> Bool
hsRecPun=Bool
True}) = Bool
True
#else
isPFieldPun (dL -> L _ HsRecField {hsRecPun=True}) = True
#endif
isPFieldPun LHsRecField GhcPs (Pat GhcPs)
_ = 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 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