Debian Codegraph @

Xtree-house/bindings/vendor/src/language.h

raw
1 #ifndef TREE_SITTER_LANGUAGE_H_
2 #define TREE_SITTER_LANGUAGE_H_
3
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7
8 #include "./subtree.h"
9 #include "./parser.h"
10
11 #define ts_builtin_sym_error_repeat (ts_builtin_sym_error - 1)
12
13 #define LANGUAGE_VERSION_WITH_RESERVED_WORDS 15
14 #define LANGUAGE_VERSION_WITH_PRIMARY_STATES 14
15
16 typedef struct {
17 const TSParseAction *actions;
18 uint32_t action_count;
19 bool is_reusable;
20 } TableEntry;
21
22 typedef struct {
23 const TSLanguage *language;
24 const uint16_t *data;
25 const uint16_t *group_end;
26 TSStateId state;
27 uint16_t table_value;
28 uint16_t section_index;
29 uint16_t group_count;
30 bool is_small_state;
31
32 const TSParseAction *actions;
33 TSSymbol symbol;
34 TSStateId next_state;
35 uint16_t action_count;
36 } LookaheadIterator;
37
38 void ts_language_table_entry(const TSLanguage *self, TSStateId state, TSSymbol symbol, TableEntry *result);
39 TSLexerMode ts_language_lex_mode_for_state(const TSLanguage *self, TSStateId state);
40 bool ts_language_is_reserved_word(const TSLanguage *self, TSStateId state, TSSymbol symbol);
41 TSSymbolMetadata ts_language_symbol_metadata(const TSLanguage *self, TSSymbol symbol);
42 TSSymbol ts_language_public_symbol(const TSLanguage *self, TSSymbol symbol);
43
44 static inline const TSParseAction *ts_language_actions(
45 const TSLanguage *self,
46 TSStateId state,
47 TSSymbol symbol,
48 uint32_t *count
49 ) {
50 TableEntry entry;
51 ts_language_table_entry(self, state, symbol, &entry);
52 *count = entry.action_count;
53 return entry.actions;
54 }
55
56 static inline bool ts_language_has_reduce_action(
57 const TSLanguage *self,
58 TSStateId state,
59 TSSymbol symbol
60 ) {
61 TableEntry entry;
62 ts_language_table_entry(self, state, symbol, &entry);
63 return entry.action_count > 0 && entry.actions[0].type == TSParseActionTypeReduce;
64 }
65
66 // Lookup the table value for a given symbol and state.
67 //
68 // For non-terminal symbols, the table value represents a successor state.
69 // For terminal symbols, it represents an index in the actions table.
70 // For 'large' parse states, this is a direct lookup. For 'small' parse
71 // states, this requires searching through the symbol groups to find
72 // the given symbol.
73 static inline uint16_t ts_language_lookup(
74 const TSLanguage *self,
75 TSStateId state,
76 TSSymbol symbol
77 ) {
78 if (state >= self->large_state_count) {
79 uint32_t index = self->small_parse_table_map[state - self->large_state_count];
80 const uint16_t *data = &self->small_parse_table[index];
81 uint16_t group_count = *(data++);
82 for (unsigned i = 0; i < group_count; i++) {
83 uint16_t section_value = *(data++);
84 uint16_t symbol_count = *(data++);
85 for (unsigned j = 0; j < symbol_count; j++) {
86 if (*(data++) == symbol) return section_value;
87 }
88 }
89 return 0;
90 } else {
91 return self->parse_table[state * self->symbol_count + symbol];
92 }
93 }
94
95 static inline bool ts_language_has_actions(
96 const TSLanguage *self,
97 TSStateId state,
98 TSSymbol symbol
99 ) {
100 return ts_language_lookup(self, state, symbol) != 0;
101 }
102
103 // Iterate over all of the symbols that are valid in the given state.
104 //
105 // For 'large' parse states, this just requires iterating through
106 // all possible symbols and checking the parse table for each one.
107 // For 'small' parse states, this exploits the structure of the
108 // table to only visit the valid symbols.
109 static inline LookaheadIterator ts_language_lookaheads(
110 const TSLanguage *self,
111 TSStateId state
112 ) {
113 bool is_small_state = state >= self->large_state_count;
114 const uint16_t *data;
115 const uint16_t *group_end = NULL;
116 uint16_t group_count = 0;
117 if (is_small_state) {
118 uint32_t index = self->small_parse_table_map[state - self->large_state_count];
119 data = &self->small_parse_table[index];
120 group_end = data + 1;
121 group_count = *data;
122 } else {
123 data = &self->parse_table[state * self->symbol_count] - 1;
124 }
125 return (LookaheadIterator) {
126 .language = self,
127 .data = data,
128 .group_end = group_end,
129 .group_count = group_count,
130 .is_small_state = is_small_state,
131 .symbol = UINT16_MAX,
132 .next_state = 0,
133 };
134 }
135
136 static inline bool ts_lookahead_iterator__next(LookaheadIterator *self) {
137 // For small parse states, valid symbols are listed explicitly,
138 // grouped by their value. There's no need to look up the actions
139 // again until moving to the next group.
140 if (self->is_small_state) {
141 self->data++;
142 if (self->data == self->group_end) {
143 if (self->group_count == 0) return false;
144 self->group_count--;
145 self->table_value = *(self->data++);
146 unsigned symbol_count = *(self->data++);
147 self->group_end = self->data + symbol_count;
148 self->symbol = *self->data;
149 } else {
150 self->symbol = *self->data;
151 return true;
152 }
153 }
154
155 // For large parse states, iterate through every symbol until one
156 // is found that has valid actions.
157 else {
158 do {
159 self->data++;
160 self->symbol++;
161 if (self->symbol >= self->language->symbol_count) return false;
162 self->table_value = *self->data;
163 } while (!self->table_value);
164 }
165
166 // Depending on if the symbols is terminal or non-terminal, the table value either
167 // represents a list of actions or a successor state.
168 if (self->symbol < self->language->token_count) {
169 const TSParseActionEntry *entry = &self->language->parse_actions[self->table_value];
170 self->action_count = entry->entry.count;
171 self->actions = (const TSParseAction *)(entry + 1);
172 self->next_state = 0;
173 } else {
174 self->action_count = 0;
175 self->next_state = self->table_value;
176 }
177 return true;
178 }
179
180 // Whether the state is a "primary state". If this returns false, it indicates that there exists
181 // another state that behaves identically to this one with respect to query analysis.
182 static inline bool ts_language_state_is_primary(
183 const TSLanguage *self,
184 TSStateId state
185 ) {
186 if (self->abi_version >= LANGUAGE_VERSION_WITH_PRIMARY_STATES) {
187 return state == self->primary_state_ids[state];
188 } else {
189 return true;
190 }
191 }
192
193 static inline const bool *ts_language_enabled_external_tokens(
194 const TSLanguage *self,
195 unsigned external_scanner_state
196 ) {
197 if (external_scanner_state == 0) {
198 return NULL;
199 } else {
200 return self->external_scanner.states + self->external_token_count * external_scanner_state;
201 }
202 }
203
204 static inline const TSSymbol *ts_language_alias_sequence(
205 const TSLanguage *self,
206 uint32_t production_id
207 ) {
208 return production_id ?
209 &self->alias_sequences[production_id * self->max_alias_sequence_length] :
210 NULL;
211 }
212
213 static inline TSSymbol ts_language_alias_at(
214 const TSLanguage *self,
215 uint32_t production_id,
216 uint32_t child_index
217 ) {
218 return production_id ?
219 self->alias_sequences[production_id * self->max_alias_sequence_length + child_index] :
220 0;
221 }
222
223 static inline void ts_language_field_map(
224 const TSLanguage *self,
225 uint32_t production_id,
226 const TSFieldMapEntry **start,
227 const TSFieldMapEntry **end
228 ) {
229 if (self->field_count == 0) {
230 *start = NULL;
231 *end = NULL;
232 return;
233 }
234
235 TSMapSlice slice = self->field_map_slices[production_id];
236 *start = &self->field_map_entries[slice.index];
237 *end = &self->field_map_entries[slice.index] + slice.length;
238 }
239
240 static inline void ts_language_aliases_for_symbol(
241 const TSLanguage *self,
242 TSSymbol original_symbol,
243 const TSSymbol **start,
244 const TSSymbol **end
245 ) {
246 *start = &self->public_symbol_map[original_symbol];
247 *end = *start + 1;
248
249 unsigned idx = 0;
250 for (;;) {
251 TSSymbol symbol = self->alias_map[idx++];
252 if (symbol == 0 || symbol > original_symbol) break;
253 uint16_t count = self->alias_map[idx++];
254 if (symbol == original_symbol) {
255 *start = &self->alias_map[idx];
256 *end = &self->alias_map[idx + count];
257 break;
258 }
259 idx += count;
260 }
261 }
262
263 static inline void ts_language_write_symbol_as_dot_string(
264 const TSLanguage *self,
265 FILE *f,
266 TSSymbol symbol
267 ) {
268 const char *name = ts_language_symbol_name(self, symbol);
269 for (const char *chr = name; *chr; chr++) {
270 switch (*chr) {
271 case '"':
272 case '\\':
273 fputc('\\', f);
274 fputc(*chr, f);
275 break;
276 case '\n':
277 fputs("\\n", f);
278 break;
279 case '\t':
280 fputs("\\t", f);
281 break;
282 default:
283 fputc(*chr, f);
284 break;
285 }
286 }
287 }
288
289 #ifdef __cplusplus
290 }
291 #endif
292
293 #endif // TREE_SITTER_LANGUAGE_H_