{-# LANGUAGE CPP #-}
module Seminal.Enumerator.Patterns (enumerateChangesInPattern) where
import Seminal.Enumerator.Enumerator (Enumerator)
import Seminal.Compiler.API
import Seminal.Enumerator.Literals (enumerateChangeInLiteral)
import Seminal.Change (Change(..), ChangeType (Wildcard), (<&&>), node)

-- | Enumerate possible changes for patterns,
-- See [API doc](https://hackage.haskell.org/package/ghc-9.6.1/docs/Language-Haskell-Syntax-Pat.html#t:Pat)
enumerateChangesInPattern :: Enumerator (Pat GhcPs)
enumerateChangesInPattern :: Enumerator (Pat GhcPs)
enumerateChangesInPattern (WildPat XWildPat GhcPs
_) SrcSpan
_ = []
enumerateChangesInPattern Pat GhcPs
pat SrcSpan
loc = Change (Pat GhcPs)
wildpatChange Change (Pat GhcPs) -> [Change (Pat GhcPs)] -> [Change (Pat GhcPs)]
forall a. a -> [a] -> [a]
: case Pat GhcPs
pat of
    (LitPat XLitPat GhcPs
xlit HsLit GhcPs
litExpr) -> Enumerator (HsLit GhcPs)
enumerateChangeInLiteral HsLit GhcPs
litExpr SrcSpan
loc [Change (HsLit GhcPs)]
-> (HsLit GhcPs -> Pat GhcPs) -> [Change (Pat GhcPs)]
forall a b. [Change a] -> (a -> b) -> [Change b]
<&&> (XLitPat GhcPs -> HsLit GhcPs -> Pat GhcPs
forall p. XLitPat p -> HsLit p -> Pat p
LitPat XLitPat GhcPs
xlit)
#if MIN_VERSION_ghc(9,4,1)
    (ParPat XParPat GhcPs
xpar LHsToken "(" GhcPs
openParTok (L SrcSpanAnn' (EpAnn AnnListItem)
lpat Pat GhcPs
subpat) LHsToken ")" GhcPs
closeParTok) ->
#else
    (ParPat xpar (L lpat subpat)) ->
#endif
        Enumerator (Pat GhcPs)
enumerateChangesInPattern Pat GhcPs
subpat (SrcSpanAnn' (EpAnn AnnListItem) -> SrcSpan
forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnn' (EpAnn AnnListItem)
lpat)
            [Change (Pat GhcPs)]
-> (Pat GhcPs
    -> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (Pat GhcPs))
-> [Change
      (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (Pat GhcPs))]
forall a b. [Change a] -> (a -> b) -> [Change b]
<&&> (SrcSpanAnn' (EpAnn AnnListItem)
-> Pat GhcPs
-> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (Pat GhcPs)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnn' (EpAnn AnnListItem)
lpat)
#if MIN_VERSION_ghc(9,4,1)
            [Change (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (Pat GhcPs))]
-> (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (Pat GhcPs)
    -> Pat GhcPs)
-> [Change (Pat GhcPs)]
forall a b. [Change a] -> (a -> b) -> [Change b]
<&&> (\GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (Pat GhcPs)
newPat -> XParPat GhcPs
-> LHsToken "(" GhcPs
-> LPat GhcPs
-> LHsToken ")" GhcPs
-> Pat GhcPs
forall p.
XParPat p -> LHsToken "(" p -> LPat p -> LHsToken ")" p -> Pat p
ParPat XParPat GhcPs
xpar LHsToken "(" GhcPs
openParTok LPat GhcPs
GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (Pat GhcPs)
newPat LHsToken ")" GhcPs
closeParTok)
#else
            <&&> (ParPat xpar)
#endif
    Pat GhcPs
_ -> []
    where
        wildpatChange :: Change (Pat GhcPs)
wildpatChange = ChangeNode (Pat GhcPs)
-> [ChangeNode (Pat GhcPs)]
-> SrcSpan
-> [Change (Pat GhcPs)]
-> String
-> ChangeType
-> Change (Pat GhcPs)
forall node.
ChangeNode node
-> [ChangeNode node]
-> SrcSpan
-> [Change node]
-> String
-> ChangeType
-> Change node
Change (Pat GhcPs -> ChangeNode (Pat GhcPs)
forall n. Outputable n => n -> ChangeNode n
node Pat GhcPs
pat) [Pat GhcPs -> ChangeNode (Pat GhcPs)
forall n. Outputable n => n -> ChangeNode n
node (Pat GhcPs -> ChangeNode (Pat GhcPs))
-> Pat GhcPs -> ChangeNode (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$ XWildPat GhcPs -> Pat GhcPs
forall p. XWildPat p -> Pat p
WildPat XWildPat GhcPs
NoExtField
NoExtField] SrcSpan
loc [] String
"The pattern is invalid." ChangeType
Wildcard