| 1 |
#include "./language.h" |
| 2 |
#include "./wasm_store.h" |
| 3 |
#include "tree_sitter/api.h" |
| 4 |
#include <string.h> |
| 5 |
|
| 6 |
const TSLanguage *ts_language_copy(const TSLanguage *self) { |
| 7 |
if (self && ts_language_is_wasm(self)) { |
| 8 |
ts_wasm_language_retain(self); |
| 9 |
} |
| 10 |
return self; |
| 11 |
} |
| 12 |
|
| 13 |
void ts_language_delete(const TSLanguage *self) { |
| 14 |
if (self && ts_language_is_wasm(self)) { |
| 15 |
ts_wasm_language_release(self); |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
uint32_t ts_language_symbol_count(const TSLanguage *self) { |
| 20 |
return self->symbol_count + self->alias_count; |
| 21 |
} |
| 22 |
|
| 23 |
uint32_t ts_language_state_count(const TSLanguage *self) { |
| 24 |
return self->state_count; |
| 25 |
} |
| 26 |
|
| 27 |
const TSSymbol *ts_language_supertypes(const TSLanguage *self, uint32_t *length) { |
| 28 |
if (self->abi_version >= LANGUAGE_VERSION_WITH_RESERVED_WORDS) { |
| 29 |
*length = self->supertype_count; |
| 30 |
return self->supertype_symbols; |
| 31 |
} else { |
| 32 |
*length = 0; |
| 33 |
return NULL; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
const TSSymbol *ts_language_subtypes( |
| 38 |
const TSLanguage *self, |
| 39 |
TSSymbol supertype, |
| 40 |
uint32_t *length |
| 41 |
) { |
| 42 |
if (self->abi_version < LANGUAGE_VERSION_WITH_RESERVED_WORDS || !ts_language_symbol_metadata(self, supertype).supertype) { |
| 43 |
*length = 0; |
| 44 |
return NULL; |
| 45 |
} |
| 46 |
|
| 47 |
TSMapSlice slice = self->supertype_map_slices[supertype]; |
| 48 |
*length = slice.length; |
| 49 |
return &self->supertype_map_entries[slice.index]; |
| 50 |
} |
| 51 |
|
| 52 |
uint32_t ts_language_abi_version(const TSLanguage *self) { |
| 53 |
return self->abi_version; |
| 54 |
} |
| 55 |
|
| 56 |
const TSLanguageMetadata *ts_language_metadata(const TSLanguage *self) { |
| 57 |
return self->abi_version >= LANGUAGE_VERSION_WITH_RESERVED_WORDS ? &self->metadata : NULL; |
| 58 |
} |
| 59 |
|
| 60 |
const char *ts_language_name(const TSLanguage *self) { |
| 61 |
return self->abi_version >= LANGUAGE_VERSION_WITH_RESERVED_WORDS ? self->name : NULL; |
| 62 |
} |
| 63 |
|
| 64 |
uint32_t ts_language_field_count(const TSLanguage *self) { |
| 65 |
return self->field_count; |
| 66 |
} |
| 67 |
|
| 68 |
void ts_language_table_entry( |
| 69 |
const TSLanguage *self, |
| 70 |
TSStateId state, |
| 71 |
TSSymbol symbol, |
| 72 |
TableEntry *result |
| 73 |
) { |
| 74 |
if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) { |
| 75 |
result->action_count = 0; |
| 76 |
result->is_reusable = false; |
| 77 |
result->actions = NULL; |
| 78 |
} else { |
| 79 |
ts_assert(symbol < self->token_count); |
| 80 |
uint32_t action_index = ts_language_lookup(self, state, symbol); |
| 81 |
const TSParseActionEntry *entry = &self->parse_actions[action_index]; |
| 82 |
result->action_count = entry->entry.count; |
| 83 |
result->is_reusable = entry->entry.reusable; |
| 84 |
result->actions = (const TSParseAction *)(entry + 1); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
TSLexerMode ts_language_lex_mode_for_state( |
| 89 |
const TSLanguage *self, |
| 90 |
TSStateId state |
| 91 |
) { |
| 92 |
if (self->abi_version < 15) { |
| 93 |
TSLexMode mode = ((const TSLexMode *)self->lex_modes)[state]; |
| 94 |
return (TSLexerMode) { |
| 95 |
.lex_state = mode.lex_state, |
| 96 |
.external_lex_state = mode.external_lex_state, |
| 97 |
.reserved_word_set_id = 0, |
| 98 |
}; |
| 99 |
} else { |
| 100 |
return self->lex_modes[state]; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
bool ts_language_is_reserved_word( |
| 105 |
const TSLanguage *self, |
| 106 |
TSStateId state, |
| 107 |
TSSymbol symbol |
| 108 |
) { |
| 109 |
TSLexerMode lex_mode = ts_language_lex_mode_for_state(self, state); |
| 110 |
if (lex_mode.reserved_word_set_id > 0) { |
| 111 |
unsigned start = lex_mode.reserved_word_set_id * self->max_reserved_word_set_size; |
| 112 |
unsigned end = start + self->max_reserved_word_set_size; |
| 113 |
for (unsigned i = start; i < end; i++) { |
| 114 |
if (self->reserved_words[i] == symbol) return true; |
| 115 |
if (self->reserved_words[i] == 0) break; |
| 116 |
} |
| 117 |
} |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
TSSymbolMetadata ts_language_symbol_metadata( |
| 122 |
const TSLanguage *self, |
| 123 |
TSSymbol symbol |
| 124 |
) { |
| 125 |
if (symbol == ts_builtin_sym_error) { |
| 126 |
return (TSSymbolMetadata) {.visible = true, .named = true}; |
| 127 |
} else if (symbol == ts_builtin_sym_error_repeat) { |
| 128 |
return (TSSymbolMetadata) {.visible = false, .named = false}; |
| 129 |
} else { |
| 130 |
return self->symbol_metadata[symbol]; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
TSSymbol ts_language_public_symbol( |
| 135 |
const TSLanguage *self, |
| 136 |
TSSymbol symbol |
| 137 |
) { |
| 138 |
if (symbol == ts_builtin_sym_error) return symbol; |
| 139 |
return self->public_symbol_map[symbol]; |
| 140 |
} |
| 141 |
|
| 142 |
TSStateId ts_language_next_state( |
| 143 |
const TSLanguage *self, |
| 144 |
TSStateId state, |
| 145 |
TSSymbol symbol |
| 146 |
) { |
| 147 |
if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) { |
| 148 |
return 0; |
| 149 |
} else if (symbol < self->token_count) { |
| 150 |
uint32_t count; |
| 151 |
const TSParseAction *actions = ts_language_actions(self, state, symbol, &count); |
| 152 |
if (count > 0) { |
| 153 |
TSParseAction action = actions[count - 1]; |
| 154 |
if (action.type == TSParseActionTypeShift) { |
| 155 |
return action.shift.extra ? state : action.shift.state; |
| 156 |
} |
| 157 |
} |
| 158 |
return 0; |
| 159 |
} else { |
| 160 |
return ts_language_lookup(self, state, symbol); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
const char *ts_language_symbol_name( |
| 165 |
const TSLanguage *self, |
| 166 |
TSSymbol symbol |
| 167 |
) { |
| 168 |
if (symbol == ts_builtin_sym_error) { |
| 169 |
return "ERROR"; |
| 170 |
} else if (symbol == ts_builtin_sym_error_repeat) { |
| 171 |
return "_ERROR"; |
| 172 |
} else if (symbol < ts_language_symbol_count(self)) { |
| 173 |
return self->symbol_names[symbol]; |
| 174 |
} else { |
| 175 |
return NULL; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
TSSymbol ts_language_symbol_for_name( |
| 180 |
const TSLanguage *self, |
| 181 |
const char *string, |
| 182 |
uint32_t length, |
| 183 |
bool is_named |
| 184 |
) { |
| 185 |
if (is_named && !strncmp(string, "ERROR", length)) return ts_builtin_sym_error; |
| 186 |
uint16_t count = (uint16_t)ts_language_symbol_count(self); |
| 187 |
for (TSSymbol i = 0; i < count; i++) { |
| 188 |
TSSymbolMetadata metadata = ts_language_symbol_metadata(self, i); |
| 189 |
if ((!metadata.visible && !metadata.supertype) || metadata.named != is_named) continue; |
| 190 |
const char *symbol_name = self->symbol_names[i]; |
| 191 |
if (!strncmp(symbol_name, string, length) && !symbol_name[length]) { |
| 192 |
return self->public_symbol_map[i]; |
| 193 |
} |
| 194 |
} |
| 195 |
return 0; |
| 196 |
} |
| 197 |
|
| 198 |
TSSymbolType ts_language_symbol_type( |
| 199 |
const TSLanguage *self, |
| 200 |
TSSymbol symbol |
| 201 |
) { |
| 202 |
TSSymbolMetadata metadata = ts_language_symbol_metadata(self, symbol); |
| 203 |
if (metadata.named && metadata.visible) { |
| 204 |
return TSSymbolTypeRegular; |
| 205 |
} else if (metadata.visible) { |
| 206 |
return TSSymbolTypeAnonymous; |
| 207 |
} else if (metadata.supertype) { |
| 208 |
return TSSymbolTypeSupertype; |
| 209 |
} else { |
| 210 |
return TSSymbolTypeAuxiliary; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
const char *ts_language_field_name_for_id( |
| 215 |
const TSLanguage *self, |
| 216 |
TSFieldId id |
| 217 |
) { |
| 218 |
uint32_t count = ts_language_field_count(self); |
| 219 |
if (count && id <= count) { |
| 220 |
return self->field_names[id]; |
| 221 |
} else { |
| 222 |
return NULL; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
TSFieldId ts_language_field_id_for_name( |
| 227 |
const TSLanguage *self, |
| 228 |
const char *name, |
| 229 |
uint32_t name_length |
| 230 |
) { |
| 231 |
uint16_t count = (uint16_t)ts_language_field_count(self); |
| 232 |
for (TSSymbol i = 1; i < count + 1; i++) { |
| 233 |
switch (strncmp(name, self->field_names[i], name_length)) { |
| 234 |
case 0: |
| 235 |
if (self->field_names[i][name_length] == 0) return i; |
| 236 |
break; |
| 237 |
case -1: |
| 238 |
return 0; |
| 239 |
default: |
| 240 |
break; |
| 241 |
} |
| 242 |
} |
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
TSLookaheadIterator *ts_lookahead_iterator_new(const TSLanguage *self, TSStateId state) { |
| 247 |
if (state >= self->state_count) return NULL; |
| 248 |
LookaheadIterator *iterator = ts_malloc(sizeof(LookaheadIterator)); |
| 249 |
*iterator = ts_language_lookaheads(self, state); |
| 250 |
return (TSLookaheadIterator *)iterator; |
| 251 |
} |
| 252 |
|
| 253 |
void ts_lookahead_iterator_delete(TSLookaheadIterator *self) { |
| 254 |
ts_free(self); |
| 255 |
} |
| 256 |
|
| 257 |
bool ts_lookahead_iterator_reset_state(TSLookaheadIterator * self, TSStateId state) { |
| 258 |
LookaheadIterator *iterator = (LookaheadIterator *)self; |
| 259 |
if (state >= iterator->language->state_count) return false; |
| 260 |
*iterator = ts_language_lookaheads(iterator->language, state); |
| 261 |
return true; |
| 262 |
} |
| 263 |
|
| 264 |
const TSLanguage *ts_lookahead_iterator_language(const TSLookaheadIterator *self) { |
| 265 |
const LookaheadIterator *iterator = (const LookaheadIterator *)self; |
| 266 |
return iterator->language; |
| 267 |
} |
| 268 |
|
| 269 |
bool ts_lookahead_iterator_reset(TSLookaheadIterator *self, const TSLanguage *language, TSStateId state) { |
| 270 |
if (state >= language->state_count) return false; |
| 271 |
LookaheadIterator *iterator = (LookaheadIterator *)self; |
| 272 |
*iterator = ts_language_lookaheads(language, state); |
| 273 |
return true; |
| 274 |
} |
| 275 |
|
| 276 |
bool ts_lookahead_iterator_next(TSLookaheadIterator *self) { |
| 277 |
LookaheadIterator *iterator = (LookaheadIterator *)self; |
| 278 |
return ts_lookahead_iterator__next(iterator); |
| 279 |
} |
| 280 |
|
| 281 |
TSSymbol ts_lookahead_iterator_current_symbol(const TSLookaheadIterator *self) { |
| 282 |
const LookaheadIterator *iterator = (const LookaheadIterator *)self; |
| 283 |
return iterator->symbol; |
| 284 |
} |
| 285 |
|
| 286 |
const char *ts_lookahead_iterator_current_symbol_name(const TSLookaheadIterator *self) { |
| 287 |
const LookaheadIterator *iterator = (const LookaheadIterator *)self; |
| 288 |
return ts_language_symbol_name(iterator->language, iterator->symbol); |
| 289 |
} |