aboutsummaryrefslogtreecommitdiffstats
path: root/app/TreeSitter.hs
blob: 230fefcf91a636af57878ca12a48c6a8daadf1d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module TreeSitter
  ( Language,
    Node (..),
    Point (..),
    extract_comments,
    tree_sitter_haskell,
  )
where

import Foreign.C.String (CString)
import Foreign.C.Types (CInt (..))
import Foreign.Ptr (Ptr)
import Foreign.Storable (Storable (..))

data Language

data Node = Node
  { startPoint :: Point,
    endPoint :: Point,
    startByte :: CInt,
    endByte :: CInt
  }
  deriving (Show, Eq)

instance Storable Node where
  alignment _ = 8
  sizeOf _ = 24
  peek ptr =
    Node
      <$> peekByteOff ptr 0
      <*> peekByteOff ptr 8
      <*> peekByteOff ptr 16
      <*> peekByteOff ptr 20
  poke ptr (Node {..}) = do
    pokeByteOff ptr 0 startPoint
    pokeByteOff ptr 8 endPoint
    pokeByteOff ptr 16 startByte
    pokeByteOff ptr 20 endByte

data Point = Point
  { row :: CInt,
    column :: CInt
  }
  deriving (Show, Eq)

instance Storable Point where
  alignment _ = 4
  sizeOf _ = 8
  peek ptr = Point <$> peekByteOff ptr 0 <*> peekByteOff ptr 4
  poke ptr (Point {..}) = pokeByteOff ptr 0 row >> pokeByteOff ptr 4 column

foreign import ccall unsafe "extract_comments"
  extract_comments ::
    Ptr Language ->
    CString ->
    Ptr (Ptr Node) ->
    Ptr CInt ->
    IO ()

foreign import ccall unsafe "tree_sitter_haskell" tree_sitter_haskell :: Ptr Language