Skip to content

Commit 9c43533

Browse files
committed
Fix patNodeBinders again with CPP
1 parent fc066c6 commit 9c43533

5 files changed

Lines changed: 58 additions & 16 deletions

File tree

sabela.cabal

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ library
2020
import: warnings
2121
autogen-modules: Paths_sabela
2222
other-modules: Paths_sabela
23+
Sabela.Parse.Ast.PatNodeBinders
24+
Sabela.Parse.Ast.Names
2325
exposed-modules: Sabela.Anthropic,
2426
Sabela.Anthropic.Types,
2527
Sabela.Anthropic.Types.Request,
@@ -114,6 +116,10 @@ library
114116
time >= 1.9 && < 2,
115117
wai >= 3.2 && < 3.3
116118
hs-source-dirs: src
119+
if impl(ghc >= 9.6)
120+
hs-source-dirs: src-ghc96
121+
else
122+
hs-source-dirs: src-ghc94
117123
default-language: Haskell2010
118124

119125
executable sabela
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{-# LANGUAGE LambdaCase #-}
2+
module Sabela.Parse.Ast.PatNodeBinders (patNodeBinders) where
3+
4+
import Sabela.Parse.Ast.Names
5+
import qualified Language.Haskell.Syntax as Hs
6+
import qualified Data.Set as S
7+
import GHC.Types.SrcLoc (unLoc)
8+
import qualified GHC.Hs as Hs
9+
import Data.Set (Set)
10+
import Data.Text (Text)
11+
12+
patNodeBinders :: Hs.Pat Hs.GhcPs -> Set Text
13+
patNodeBinders = \case
14+
Hs.VarPat _ ln -> S.singleton (rdrText (unLoc ln))
15+
Hs.AsPat _ ln _ _ -> S.singleton (rdrText (unLoc ln))
16+
Hs.NPlusKPat _ ln _ _ _ _ -> S.singleton (rdrText (unLoc ln))
17+
_ -> S.empty
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{-# LANGUAGE LambdaCase #-}
2+
module Sabela.Parse.Ast.PatNodeBinders (patNodeBinders) where
3+
4+
import Sabela.Parse.Ast.Names
5+
import qualified Language.Haskell.Syntax as Hs
6+
import qualified Data.Set as S
7+
import GHC.Types.SrcLoc (unLoc)
8+
import qualified GHC.Hs as Hs
9+
import Data.Set (Set)
10+
import Data.Text (Text)
11+
12+
patNodeBinders :: Hs.Pat Hs.GhcPs -> Set Text
13+
patNodeBinders = \case
14+
Hs.VarPat _ ln -> S.singleton (rdrText (unLoc ln))
15+
Hs.AsPat _ ln _ -> S.singleton (rdrText (unLoc ln))
16+
Hs.NPlusKPat _ ln _ _ _ _ -> S.singleton (rdrText (unLoc ln))
17+
_ -> S.empty

src/Sabela/Parse/Ast.hs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ import qualified Data.List.NonEmpty as NE
3030
import Data.Set (Set)
3131
import qualified Data.Set as S
3232
import Data.Text (Text)
33-
import qualified Data.Text as T
3433

3534
import Data.Generics.Uniplate.Data (universeBi)
3635

3736
import qualified GHC.Hs as Hs
38-
import GHC.Types.Name.Occurrence (occNameString)
39-
import GHC.Types.Name.Reader (RdrName, rdrNameOcc)
4037
import GHC.Types.SrcLoc (unLoc)
38+
import Sabela.Parse.Ast.Names (rdrText)
39+
import qualified Sabela.Parse.Ast.PatNodeBinders as PatNodeBinders
4140

4241
-- ---------------------------------------------------------------------------
4342
-- Module-level extraction
@@ -147,11 +146,7 @@ bumps — sub-patterns are reached generically rather than by hand-coded
147146
constructor matching.
148147
-}
149148
patNodeBinders :: Hs.Pat Hs.GhcPs -> Set Text
150-
patNodeBinders = \case
151-
Hs.VarPat _ ln -> S.singleton (rdrText (unLoc ln))
152-
Hs.AsPat _ ln _ _ -> S.singleton (rdrText (unLoc ln))
153-
Hs.NPlusKPat _ ln _ _ _ _ -> S.singleton (rdrText (unLoc ln))
154-
_ -> S.empty
149+
patNodeBinders = PatNodeBinders.patNodeBinders
155150

156151
-- | Recursive pattern-binder extraction (every level of nesting).
157152
patBinders :: Hs.Pat Hs.GhcPs -> Set Text
@@ -195,11 +190,3 @@ collectBinders x = S.unions [bindersFromBind, bindersFromPat, bindersFromTyCl]
195190
[ tyClBinders t
196191
| t <- universeBi x :: [Hs.TyClDecl Hs.GhcPs]
197192
]
198-
199-
-- ---------------------------------------------------------------------------
200-
-- Names
201-
-- ---------------------------------------------------------------------------
202-
203-
-- | Convert an 'RdrName' to its bare @OccName@ as 'Text'.
204-
rdrText :: RdrName -> Text
205-
rdrText = T.pack . occNameString . rdrNameOcc

src/Sabela/Parse/Ast/Names.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Sabela.Parse.Ast.Names ( rdrText) where
2+
3+
4+
import Data.Text (Text)
5+
import qualified Data.Text as T
6+
import GHC.Types.Name.Occurrence (occNameString)
7+
import GHC.Types.Name.Reader (RdrName, rdrNameOcc)
8+
9+
-- ---------------------------------------------------------------------------
10+
-- Names
11+
-- ---------------------------------------------------------------------------
12+
13+
-- | Convert an 'RdrName' to its bare @OccName@ as 'Text'.
14+
rdrText :: RdrName -> Text
15+
rdrText = T.pack . occNameString . rdrNameOcc

0 commit comments

Comments
 (0)