1 /* Header file for the tree-sitter integration.
2
3 Copyright (C) 2021-2023 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
19
20 #ifndef EMACS_TREESIT_H
21 #define EMACS_TREESIT_H
22
23 #include <config.h>
24
25 #ifdef HAVE_TREE_SITTER
26
27 #include <tree_sitter/api.h>
28 #include "lisp.h"
29
30 INLINE_HEADER_BEGIN
31
32 /* A wrapper for a tree-sitter parser, but also contains a parse tree
33 and other goodies for convenience. */
34 struct Lisp_TS_Parser
35 {
36 union vectorlike_header header;
37 /* A symbol representing the language this parser uses. See the
38 manual for more explanation. */
39 Lisp_Object language_symbol;
40 /* A list of functions to call after re-parse. Every function is
41 called with the changed ranges and the parser. The changed
42 ranges is a list of (BEG . END). */
43 Lisp_Object after_change_functions;
44 /* The buffer associated with this parser. */
45 Lisp_Object buffer;
46 /* The pointer to the tree-sitter parser. Never NULL. */
47 TSParser *parser;
48 /* Pointer to the syntax tree. Initially is NULL, so check for NULL
49 before use. */
50 TSTree *tree;
51 /* Teaches tree-sitter how to read an Emacs buffer. */
52 TSInput input;
53 /* Re-parsing an unchanged buffer is not free for tree-sitter, so we
54 only make it re-parse when need_reparse == true. That usually
55 means some change is made in the buffer. But others could set
56 this field to true to force tree-sitter to re-parse. */
57 bool need_reparse;
58 /* These two positions record the buffer byte position (1-based) of
59 the "visible region" that tree-sitter sees. Before re-parse, we
60 move these positions to match BUF_BEGV_BYTE and BUF_ZV_BYTE.
61 Note that we don't need to synchronize these positions when
62 retrieving them in a function that involves a node: if the node
63 is not outdated, these positions are synchronized. See comment
64 (ref:visible-beg-null) in treesit.c for more explanation. */
65 ptrdiff_t visible_beg;
66 ptrdiff_t visible_end;
67 /* This counter is incremented every time a change is made to the
68 buffer in treesit_record_change. The node retrieved from this parser
69 inherits this timestamp. This way we can make sure the node is
70 not outdated when we access its information. */
71 ptrdiff_t timestamp;
72 /* If this field is true, parser functions raises
73 treesit-parser-deleted signal. */
74 bool deleted;
75 /* If this field is true, the parser has ranges set. See
76 Ftreesit_parser_included_ranges for why we need this. */
77 bool has_range;
78 };
79
80 /* A wrapper around a tree-sitter node. */
81 struct Lisp_TS_Node
82 {
83 union vectorlike_header header;
84 /* This prevents gc from collecting the tree before the node is done
85 with it. TSNode contains a pointer to the tree it belongs to,
86 and the parser object, when collected by gc, will free that
87 tree. */
88 Lisp_Object parser;
89 TSNode node;
90 /* A node inherits its parser's timestamp at creation time. The
91 parser's timestamp increments as the buffer changes. This way we
92 can make sure the node is not outdated when we access its
93 information. */
94 ptrdiff_t timestamp;
95 };
96
97 /* A compiled tree-sitter query.
98
99 When we create a query object by treesit-compile-query, it is not
100 immediately compiled, because that would require the language
101 definition to be loaded. For example, python.el contains
102
103 (defvar xxx (treesit-compile-query ...))
104
105 and (require 'python.el) requires python's language definition to
106 be available. In the case of python.el, Emacs requires it when
107 building, so that breaks the build. */
108 struct Lisp_TS_Query
109 {
110 union vectorlike_header header;
111 /* Language symbol for the query. */
112 Lisp_Object language;
113 /* Source lisp (sexp or string) query. */
114 Lisp_Object source;
115 /* Pointer to the query object. This can be NULL, meaning this
116 query is not initialized/compiled. We compile the query when
117 it is used the first time (in treesit-query-capture). */
118 TSQuery *query;
119 /* Pointer to a cursor. If we are storing the query object, we
120 might as well store a cursor, too. */
121 TSQueryCursor *cursor;
122 };
123
124 INLINE bool
125 TS_PARSERP (Lisp_Object x)
126 {
127 return PSEUDOVECTORP (x, PVEC_TS_PARSER);
128 }
129
130 INLINE struct Lisp_TS_Parser *
131 XTS_PARSER (Lisp_Object a)
132 {
133 eassert (TS_PARSERP (a));
134 return XUNTAG (a, Lisp_Vectorlike, struct Lisp_TS_Parser);
135 }
136
137 INLINE bool
138 TS_NODEP (Lisp_Object x)
139 {
140 return PSEUDOVECTORP (x, PVEC_TS_NODE);
141 }
142
143 INLINE struct Lisp_TS_Node *
144 XTS_NODE (Lisp_Object a)
145 {
146 eassert (TS_NODEP (a));
147 return XUNTAG (a, Lisp_Vectorlike, struct Lisp_TS_Node);
148 }
149
150 INLINE bool
151 TS_COMPILED_QUERY_P (Lisp_Object x)
152 {
153 return PSEUDOVECTORP (x, PVEC_TS_COMPILED_QUERY);
154 }
155
156 INLINE struct Lisp_TS_Query *
157 XTS_COMPILED_QUERY (Lisp_Object a)
158 {
159 eassert (TS_COMPILED_QUERY_P (a));
160 return XUNTAG (a, Lisp_Vectorlike, struct Lisp_TS_Query);
161 }
162
163 INLINE void
164 CHECK_TS_PARSER (Lisp_Object parser)
165 {
166 CHECK_TYPE (TS_PARSERP (parser), Qtreesit_parser_p, parser);
167 }
168
169 INLINE void
170 CHECK_TS_NODE (Lisp_Object node)
171 {
172 CHECK_TYPE (TS_NODEP (node), Qtreesit_node_p, node);
173 }
174
175 INLINE void
176 CHECK_TS_COMPILED_QUERY (Lisp_Object query)
177 {
178 CHECK_TYPE (TS_COMPILED_QUERY_P (query),
179 Qtreesit_compiled_query_p, query);
180 }
181
182 INLINE_HEADER_END
183
184 extern void treesit_record_change (ptrdiff_t, ptrdiff_t, ptrdiff_t);
185 extern Lisp_Object make_treesit_parser (Lisp_Object, TSParser *, TSTree *,
186 Lisp_Object);
187 extern Lisp_Object make_treesit_node (Lisp_Object, TSNode);
188
189 extern bool treesit_node_uptodate_p (Lisp_Object);
190
191 extern void treesit_delete_parser (struct Lisp_TS_Parser *);
192 extern void treesit_delete_query (struct Lisp_TS_Query *);
193 extern bool treesit_named_node_p (TSNode);
194 extern bool treesit_node_eq (Lisp_Object, Lisp_Object);
195
196 #endif /* HAVE_TREE_SITTER */
197
198 extern void syms_of_treesit (void);
199
200 #endif /* EMACS_TREESIT_H */