{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DataKinds #-}
module GHC.Parser.Types
( SumOrTuple(..)
, pprSumOrTuple
, PatBuilder(..)
, DataConBuilder(..)
)
where
import GHC.Prelude
import GHC.Types.Basic
import GHC.Types.SrcLoc
import GHC.Types.Name.Reader
import GHC.Hs.Extension
import GHC.Hs.Lit
import GHC.Hs.Pat
import GHC.Hs.Type
import GHC.Utils.Outputable as Outputable
import GHC.Data.OrdList
import Data.Foldable
import GHC.Parser.Annotation
import Language.Haskell.Syntax
data SumOrTuple b
= Sum ConTag Arity (LocatedA b) [EpaLocation] [EpaLocation]
| Tuple [Either (EpAnn EpaLocation) (LocatedA b)]
pprSumOrTuple :: Outputable b => Boxity -> SumOrTuple b -> SDoc
pprSumOrTuple :: forall b. Outputable b => Boxity -> SumOrTuple b -> SDoc
pprSumOrTuple Boxity
boxity = \case
Sum Int
alt Int
arity LocatedA b
e [EpaLocation]
_ [EpaLocation]
_ ->
SDoc
parOpen SDoc -> SDoc -> SDoc
<+> Int -> SDoc
ppr_bars (Int
alt forall a. Num a => a -> a -> a
- Int
1) SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr LocatedA b
e SDoc -> SDoc -> SDoc
<+> Int -> SDoc
ppr_bars (Int
arity forall a. Num a => a -> a -> a
- Int
alt)
SDoc -> SDoc -> SDoc
<+> SDoc
parClose
Tuple [Either (EpAnn EpaLocation) (LocatedA b)]
xs ->
SDoc
parOpen SDoc -> SDoc -> SDoc
<> ([SDoc] -> SDoc
fcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
comma forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall {a} {a}. Outputable a => Either a a -> SDoc
ppr_tup [Either (EpAnn EpaLocation) (LocatedA b)]
xs)
SDoc -> SDoc -> SDoc
<> SDoc
parClose
where
ppr_tup :: Either a a -> SDoc
ppr_tup (Left a
_) = SDoc
empty
ppr_tup (Right a
e) = forall a. Outputable a => a -> SDoc
ppr a
e
ppr_bars :: Int -> SDoc
ppr_bars Int
n = [SDoc] -> SDoc
hsep (forall a. Int -> a -> [a]
replicate Int
n (Char -> SDoc
Outputable.char Char
'|'))
(SDoc
parOpen, SDoc
parClose) =
case Boxity
boxity of
Boxity
Boxed -> (String -> SDoc
text String
"(", String -> SDoc
text String
")")
Boxity
Unboxed -> (String -> SDoc
text String
"(#", String -> SDoc
text String
"#)")
data PatBuilder p
= PatBuilderPat (Pat p)
| PatBuilderPar (LHsToken "(" p) (LocatedA (PatBuilder p)) (LHsToken ")" p)
| PatBuilderApp (LocatedA (PatBuilder p)) (LocatedA (PatBuilder p))
| PatBuilderAppType (LocatedA (PatBuilder p)) (LHsToken "@" p) (HsPatSigType GhcPs)
| PatBuilderOpApp (LocatedA (PatBuilder p)) (LocatedN RdrName)
(LocatedA (PatBuilder p)) (EpAnn [AddEpAnn])
| PatBuilderVar (LocatedN RdrName)
| PatBuilderOverLit (HsOverLit GhcPs)
instance Outputable (PatBuilder GhcPs) where
ppr :: PatBuilder GhcPs -> SDoc
ppr (PatBuilderPat Pat GhcPs
p) = forall a. Outputable a => a -> SDoc
ppr Pat GhcPs
p
ppr (PatBuilderPar LHsToken "(" GhcPs
_ (L SrcSpanAnnA
_ PatBuilder GhcPs
p) LHsToken ")" GhcPs
_) = SDoc -> SDoc
parens (forall a. Outputable a => a -> SDoc
ppr PatBuilder GhcPs
p)
ppr (PatBuilderApp (L SrcSpanAnnA
_ PatBuilder GhcPs
p1) (L SrcSpanAnnA
_ PatBuilder GhcPs
p2)) = forall a. Outputable a => a -> SDoc
ppr PatBuilder GhcPs
p1 SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr PatBuilder GhcPs
p2
ppr (PatBuilderAppType (L SrcSpanAnnA
_ PatBuilder GhcPs
p) LHsToken "@" GhcPs
_ HsPatSigType GhcPs
t) = forall a. Outputable a => a -> SDoc
ppr PatBuilder GhcPs
p SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"@" SDoc -> SDoc -> SDoc
<> forall a. Outputable a => a -> SDoc
ppr HsPatSigType GhcPs
t
ppr (PatBuilderOpApp (L SrcSpanAnnA
_ PatBuilder GhcPs
p1) LocatedN RdrName
op (L SrcSpanAnnA
_ PatBuilder GhcPs
p2) EpAnn [AddEpAnn]
_) = forall a. Outputable a => a -> SDoc
ppr PatBuilder GhcPs
p1 SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr LocatedN RdrName
op SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr PatBuilder GhcPs
p2
ppr (PatBuilderVar LocatedN RdrName
v) = forall a. Outputable a => a -> SDoc
ppr LocatedN RdrName
v
ppr (PatBuilderOverLit HsOverLit GhcPs
l) = forall a. Outputable a => a -> SDoc
ppr HsOverLit GhcPs
l
data DataConBuilder
= PrefixDataConBuilder
(OrdList (LHsType GhcPs))
(LocatedN RdrName)
| InfixDataConBuilder
(LHsType GhcPs)
(LocatedN RdrName)
(LHsType GhcPs)
instance Outputable DataConBuilder where
ppr :: DataConBuilder -> SDoc
ppr (PrefixDataConBuilder OrdList (LHsType GhcPs)
flds LocatedN RdrName
data_con) =
SDoc -> Int -> SDoc -> SDoc
hang (forall a. Outputable a => a -> SDoc
ppr LocatedN RdrName
data_con) Int
2 ([SDoc] -> SDoc
sep (forall a b. (a -> b) -> [a] -> [b]
map forall a. Outputable a => a -> SDoc
ppr (forall (t :: * -> *) a. Foldable t => t a -> [a]
toList OrdList (LHsType GhcPs)
flds)))
ppr (InfixDataConBuilder LHsType GhcPs
lhs LocatedN RdrName
data_con LHsType GhcPs
rhs) =
forall a. Outputable a => a -> SDoc
ppr LHsType GhcPs
lhs SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr LocatedN RdrName
data_con SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr LHsType GhcPs
rhs
type instance Anno [LocatedA (StmtLR GhcPs GhcPs (LocatedA (PatBuilder GhcPs)))] = SrcSpanAnnL