changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/ffi/tree-sitter/ffi.lisp

changeset 523: 04d0a4f857f6
parent: 48e671eac752
author: Richard Westhaver <ellis@rwest.io>
date: Sun, 07 Jul 2024 20:10:49 -0400
permissions: -rw-r--r--
description: tree-sitter ffi fixes
1 ;;; ffi/tree-sitter/ffi.lisp --- Low-level FFI bindings for Tree-sitter
2 
3 ;;
4 
5 ;; see https://github.com/death/cl-tree-sitter for an alternative
6 ;; implementation - has functions for working on pointers instead of
7 ;; raw objects like below:
8 
9 ;;(define-alien-routine ts-node-start-point-pointer ts-point (self (* ts-node)))
10 ;;(define-alien-routine ts-node-end-point-pointer ts-point (self (* ts-node)))
11 
12 ;;; Code:
13 (in-package :tree-sitter)
14 
15 ;;; Alien Types
16 (define-alien-type ts-state-id unsigned-int)
17 (define-alien-type ts-symbol unsigned-int)
18 (define-alien-type ts-field-id unsigned-int)
19 (define-alien-type ts-language (struct ts-language))
20 (define-alien-type ts-parser (struct ts-parser))
21 (define-alien-type ts-tree (struct ts-tree))
22 ;; not thread-safe
23 (define-alien-type ts-query (struct ts-query))
24 (define-alien-type ts-query-error unsigned-int)
25 (define-alien-type ts-query-cursor (struct ts-query-cursor))
26 (define-alien-type ts-lookahead-iterator (struct ts-lookahead-iterator))
27 (define-alien-type ts-point
28  (struct ts-point
29  (row unsigned-int)
30  (column unsigned-int)))
31 (define-alien-type ts-range
32  (struct ts-range
33  (start-point ts-point)
34  (end-point ts-point)
35  (start-byte unsigned-int)
36  (end-byte unsigned-int)))
37 
38 (define-alien-enum (ts-log-type int)
39  :parse 0
40  :lex 1)
41 
42 (define-alien-type ts-logger
43  (struct nil
44  (payload (* t))
45  (log (* (function void (* t) ts-log-type c-string)))))
46 
47 (define-alien-type ts-input-edit
48  (struct ts-input-edit
49  (start-byte unsigned-int)
50  (old-end-byte unsigned-int)
51  (new-end-byte unsigned-int)
52  (start-point ts-point)
53  (old-end-point ts-point)
54  (new-end-point ts-point)))
55 
56 (define-alien-type ts-node
57  (struct ts-node
58  (context (array unsigned-int 4))
59  (id (* t))
60  (tree (* ts-tree))))
61 
62 (define-alien-type ts-tree-cursor
63  (struct nil
64  (tree (* ts-tree))
65  (id (* t))
66  (context (array unsigned-int 2))))
67 
68 (define-alien-enum (ts-input-encoding int)
69  :utf-8 0
70  :utf-16 2)
71 
72 (define-alien-enum (ts-symbol-type int)
73  :regular 0
74  :anonymous 1
75  :auxiliary 2)
76 
77 (define-alien-type ts-input (struct ts-input
78  (payload (* t))
79  (read (* (function c-string (* t)
80  unsigned-int
81  ts-point
82  (* unsigned-int))))
83  (encoding ts-input-encoding)))
84 
85 ;;; Parser
86 (define-alien-routine ts-parser-new (* ts-parser))
87 (define-alien-routine ts-parser-delete void (self (* ts-parser)))
88 (define-alien-routine ts-parser-reset void (self (* ts-parser)))
89 (define-alien-routine ts-parser-set-language boolean (self (* ts-parser)) (language (* ts-language)))
90 (define-alien-routine ts-parser-language (* ts-language) (self (* ts-parser)))
91 ;; (define-alien-routine ts-parser-parse (* ts-tree) (self (* ts-parser)) (old-tree (* ts-tree)) (input ts-input))
92 (define-alien-routine ts-parser-parse-string (* ts-tree) (self (* ts-parser)) (old-tree (* ts-tree)) (string c-string) (length unsigned-int))
93 ;; Set the file descriptor to which the parser should write debugging graphs
94 ;; during parsing. The graphs are formatted in the DOT language. You may want
95 ;; to pipe these graphs directly to a `dot(1)` process in order to generate
96 ;; SVG output. You can turn off this logging by passing a negative number.
97 (define-alien-routine ts-parser-print-dot-graphs void (self (* ts-parser)) (fd int))
98 ;;; Tree
99 (define-alien-routine ts-tree-copy (* ts-tree) (self (* ts-tree)))
100 (define-alien-routine ts-tree-delete void (self (* ts-tree)))
101 (define-alien-routine ts-tree-language (* ts-language) (self (* ts-tree)))
102 (define-alien-routine ts-tree-edit void (self (* ts-tree)) (edit (* unsigned-int)))
103 (define-alien-routine ts-tree-print-dot-graph void (self (* ts-tree)) (file-descriptor int))
104 
105 ;;; Tree Cursor
106 (define-alien-routine ts-tree-cursor-current-field-name c-string (cursor (* ts-tree-cursor)))
107 
108 (define-alien-routine ts-tree-cursor-goto-next-sibling boolean (self (* ts-tree-cursor)))
109 
110 (define-alien-routine ts-tree-cursor-goto-parent boolean (self (* ts-tree-cursor)))
111 
112 (define-alien-routine ts-tree-cursor-goto-first-child boolean (self (* ts-tree-cursor)))
113 
114 (define-alien-routine ts-tree-cursor-delete void (cursor (* ts-tree-cursor)))
115 
116 (define-alien-routine ts-language-version unsigned-int (v (* ts-language)))
117 (define-alien-routine ts-language-symbol-count unsigned-int (v (* ts-language)))
118 (define-alien-routine ts-language-symbol-name c-string (v (* ts-language)) (s (* ts-symbol)))
119 (define-alien-routine ts-language-field-count unsigned-int (v (* ts-language)))
120 
121 ;;; Query
122 (define-alien-routine ts-query-new (* ts-query)
123  (lang (* ts-language))
124  (source (* char))
125  (source-len unsigned-int)
126  (error-offset (* unsigned-int))
127  (error-type (* ts-query-error)))
128 
129 (define-alien-routine ts-query-delete void (query (* ts-query)))
130 
131 ;;; ALIEN.C
132 (define-alien-routine ts-tree-root-node-pointer (* ts-node)
133  (tree (* ts-tree)))
134 
135 (define-alien-routine ts-tree-cursor-new-pointer (* ts-tree-cursor)
136  (node (* ts-node)))
137 
138 (define-alien-routine ts-node-is-named-pointer boolean
139  (node (* ts-node)))
140 
141 (define-alien-routine ts-tree-cursor-current-node-pointer (* ts-node)
142  (cursor (* ts-tree-cursor)))
143 
144 (define-alien-routine ts-node-start-point-pointer (* ts-point)
145  (node (* ts-node)))
146 
147 (define-alien-routine ts-node-end-point-pointer (* ts-point)
148  (node (* ts-node)))
149 
150 (define-alien-routine ts-node-type-pointer c-string
151  (node (* ts-node)))