aboutsummaryrefslogtreecommitdiffstats
path: root/app/TreeSitter.hs
blob: e911d1b8368a0b35d11fe8136565278c4aa44a03 (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
61
62
63
64
65
module TreeSitter where

-- | References: [tree-sitter/api.h](https://github.com/tree-sitter/tree-sitter/blob/master/lib/include/tree_sitter/api.h)

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

data Parser

data Language

data Tree = Tree

data Node = Node

instance Storable Node where
  sizeOf _ = 32

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

instance Storable Point where
  sizeOf _ = 8
  alignment _ = 8
  peek p = Point <$> peekByteOff p 0 <*> peekByteOff p 4

withParser :: Ptr Language -> (Ptr Parser -> IO a) -> IO a
withParser l f = do
  p <- ts_parser_new
  ts_parser_set_language p l
  x <- f p
  ts_parser_delete p
  pure x

foreign import ccall unsafe "ts_node_start_point_p" ts_node_start_point :: Ptr Node -> Ptr Point -> IO ()

foreign import ccall unsafe "ts_node_end_point_p" ts_node_end_point :: Ptr Node -> Ptr Point -> IO ()

foreign import ccall unsafe "ts_node_start_byte_p" ts_node_start_byte :: Ptr Node -> IO CInt

foreign import ccall unsafe "ts_node_end_byte_p" ts_node_end_byte :: Ptr Node -> IO CInt

foreign import ccall unsafe "ts_node_type_p" ts_node_type :: Ptr Node -> IO CString

foreign import ccall unsafe "ts_node_named_child_p" ts_node_named_child :: Ptr Node -> CInt -> Ptr Node -> IO ()

foreign import ccall unsafe "ts_node_named_child_count_p" ts_node_named_child_count :: Ptr Node -> IO CInt

foreign import ccall unsafe "ts_tree_root_node_p" ts_tree_root_node :: Ptr Tree -> Ptr Node -> IO ()

foreign import ccall unsafe "ts_tree_delete" ts_tree_delete :: Ptr Tree -> IO ()

foreign import ccall unsafe "ts_parser_parse_string" ts_parser_parse_string :: Ptr Parser -> Ptr Tree -> CString -> CInt -> IO (Ptr Tree)

foreign import ccall unsafe "ts_parser_new" ts_parser_new :: IO (Ptr Parser)

foreign import ccall unsafe "ts_parser_delete" ts_parser_delete :: Ptr Parser -> IO ()

foreign import ccall unsafe "ts_parser_set_language" ts_parser_set_language :: Ptr Parser -> Ptr Language -> IO ()

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