| 1 |
#ifndef TREE_SITTER_PARSER_H_ |
| 2 |
#define TREE_SITTER_PARSER_H_ |
| 3 |
|
| 4 |
#ifdef __cplusplus |
| 5 |
extern "C" { |
| 6 |
#endif |
| 7 |
|
| 8 |
#include <stdbool.h> |
| 9 |
#include <stdint.h> |
| 10 |
#include <stdlib.h> |
| 11 |
|
| 12 |
#define ts_builtin_sym_error ((TSSymbol)-1) |
| 13 |
#define ts_builtin_sym_end 0 |
| 14 |
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 |
| 15 |
|
| 16 |
#ifndef TREE_SITTER_API_H_ |
| 17 |
typedef uint16_t TSStateId; |
| 18 |
typedef uint16_t TSSymbol; |
| 19 |
typedef uint16_t TSFieldId; |
| 20 |
typedef struct TSLanguage TSLanguage; |
| 21 |
typedef struct TSLanguageMetadata { |
| 22 |
uint8_t major_version; |
| 23 |
uint8_t minor_version; |
| 24 |
uint8_t patch_version; |
| 25 |
} TSLanguageMetadata; |
| 26 |
#endif |
| 27 |
|
| 28 |
typedef struct { |
| 29 |
TSFieldId field_id; |
| 30 |
uint8_t child_index; |
| 31 |
bool inherited; |
| 32 |
} TSFieldMapEntry; |
| 33 |
|
| 34 |
// Used to index the field and supertype maps. |
| 35 |
typedef struct { |
| 36 |
uint16_t index; |
| 37 |
uint16_t length; |
| 38 |
} TSMapSlice; |
| 39 |
|
| 40 |
typedef struct { |
| 41 |
bool visible; |
| 42 |
bool named; |
| 43 |
bool supertype; |
| 44 |
} TSSymbolMetadata; |
| 45 |
|
| 46 |
typedef struct TSLexer TSLexer; |
| 47 |
|
| 48 |
struct TSLexer { |
| 49 |
int32_t lookahead; |
| 50 |
TSSymbol result_symbol; |
| 51 |
void (*advance)(TSLexer *, bool); |
| 52 |
void (*mark_end)(TSLexer *); |
| 53 |
uint32_t (*get_column)(TSLexer *); |
| 54 |
bool (*is_at_included_range_start)(const TSLexer *); |
| 55 |
bool (*eof)(const TSLexer *); |
| 56 |
void (*log)(const TSLexer *, const char *, ...); |
| 57 |
}; |
| 58 |
|
| 59 |
typedef enum { |
| 60 |
TSParseActionTypeShift, |
| 61 |
TSParseActionTypeReduce, |
| 62 |
TSParseActionTypeAccept, |
| 63 |
TSParseActionTypeRecover, |
| 64 |
} TSParseActionType; |
| 65 |
|
| 66 |
typedef union { |
| 67 |
struct { |
| 68 |
uint8_t type; |
| 69 |
TSStateId state; |
| 70 |
bool extra; |
| 71 |
bool repetition; |
| 72 |
} shift; |
| 73 |
struct { |
| 74 |
uint8_t type; |
| 75 |
uint8_t child_count; |
| 76 |
TSSymbol symbol; |
| 77 |
int16_t dynamic_precedence; |
| 78 |
uint16_t production_id; |
| 79 |
} reduce; |
| 80 |
uint8_t type; |
| 81 |
} TSParseAction; |
| 82 |
|
| 83 |
typedef struct { |
| 84 |
uint16_t lex_state; |
| 85 |
uint16_t external_lex_state; |
| 86 |
} TSLexMode; |
| 87 |
|
| 88 |
typedef struct { |
| 89 |
uint16_t lex_state; |
| 90 |
uint16_t external_lex_state; |
| 91 |
uint16_t reserved_word_set_id; |
| 92 |
} TSLexerMode; |
| 93 |
|
| 94 |
typedef union { |
| 95 |
TSParseAction action; |
| 96 |
struct { |
| 97 |
uint8_t count; |
| 98 |
bool reusable; |
| 99 |
} entry; |
| 100 |
} TSParseActionEntry; |
| 101 |
|
| 102 |
typedef struct { |
| 103 |
int32_t start; |
| 104 |
int32_t end; |
| 105 |
} TSCharacterRange; |
| 106 |
|
| 107 |
struct TSLanguage { |
| 108 |
uint32_t abi_version; |
| 109 |
uint32_t symbol_count; |
| 110 |
uint32_t alias_count; |
| 111 |
uint32_t token_count; |
| 112 |
uint32_t external_token_count; |
| 113 |
uint32_t state_count; |
| 114 |
uint32_t large_state_count; |
| 115 |
uint32_t production_id_count; |
| 116 |
uint32_t field_count; |
| 117 |
uint16_t max_alias_sequence_length; |
| 118 |
const uint16_t *parse_table; |
| 119 |
const uint16_t *small_parse_table; |
| 120 |
const uint32_t *small_parse_table_map; |
| 121 |
const TSParseActionEntry *parse_actions; |
| 122 |
const char * const *symbol_names; |
| 123 |
const char * const *field_names; |
| 124 |
const TSMapSlice *field_map_slices; |
| 125 |
const TSFieldMapEntry *field_map_entries; |
| 126 |
const TSSymbolMetadata *symbol_metadata; |
| 127 |
const TSSymbol *public_symbol_map; |
| 128 |
const uint16_t *alias_map; |
| 129 |
const TSSymbol *alias_sequences; |
| 130 |
const TSLexerMode *lex_modes; |
| 131 |
bool (*lex_fn)(TSLexer *, TSStateId); |
| 132 |
bool (*keyword_lex_fn)(TSLexer *, TSStateId); |
| 133 |
TSSymbol keyword_capture_token; |
| 134 |
struct { |
| 135 |
const bool *states; |
| 136 |
const TSSymbol *symbol_map; |
| 137 |
void *(*create)(void); |
| 138 |
void (*destroy)(void *); |
| 139 |
bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist); |
| 140 |
unsigned (*serialize)(void *, char *); |
| 141 |
void (*deserialize)(void *, const char *, unsigned); |
| 142 |
} external_scanner; |
| 143 |
const TSStateId *primary_state_ids; |
| 144 |
const char *name; |
| 145 |
const TSSymbol *reserved_words; |
| 146 |
uint16_t max_reserved_word_set_size; |
| 147 |
uint32_t supertype_count; |
| 148 |
const TSSymbol *supertype_symbols; |
| 149 |
const TSMapSlice *supertype_map_slices; |
| 150 |
const TSSymbol *supertype_map_entries; |
| 151 |
TSLanguageMetadata metadata; |
| 152 |
}; |
| 153 |
|
| 154 |
static inline bool set_contains(const TSCharacterRange *ranges, uint32_t len, int32_t lookahead) { |
| 155 |
uint32_t index = 0; |
| 156 |
uint32_t size = len - index; |
| 157 |
while (size > 1) { |
| 158 |
uint32_t half_size = size / 2; |
| 159 |
uint32_t mid_index = index + half_size; |
| 160 |
const TSCharacterRange *range = &ranges[mid_index]; |
| 161 |
if (lookahead >= range->start && lookahead <= range->end) { |
| 162 |
return true; |
| 163 |
} else if (lookahead > range->end) { |
| 164 |
index = mid_index; |
| 165 |
} |
| 166 |
size -= half_size; |
| 167 |
} |
| 168 |
const TSCharacterRange *range = &ranges[index]; |
| 169 |
return (lookahead >= range->start && lookahead <= range->end); |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* Lexer Macros |
| 174 |
*/ |
| 175 |
|
| 176 |
#ifdef _MSC_VER |
| 177 |
#define UNUSED __pragma(warning(suppress : 4101)) |
| 178 |
#else |
| 179 |
#define UNUSED __attribute__((unused)) |
| 180 |
#endif |
| 181 |
|
| 182 |
#define START_LEXER() \ |
| 183 |
bool result = false; \ |
| 184 |
bool skip = false; \ |
| 185 |
UNUSED \ |
| 186 |
bool eof = false; \ |
| 187 |
int32_t lookahead; \ |
| 188 |
goto start; \ |
| 189 |
next_state: \ |
| 190 |
lexer->advance(lexer, skip); \ |
| 191 |
start: \ |
| 192 |
skip = false; \ |
| 193 |
lookahead = lexer->lookahead; |
| 194 |
|
| 195 |
#define ADVANCE(state_value) \ |
| 196 |
{ \ |
| 197 |
state = state_value; \ |
| 198 |
goto next_state; \ |
| 199 |
} |
| 200 |
|
| 201 |
#define ADVANCE_MAP(...) \ |
| 202 |
{ \ |
| 203 |
static const uint16_t map[] = { __VA_ARGS__ }; \ |
| 204 |
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \ |
| 205 |
if (map[i] == lookahead) { \ |
| 206 |
state = map[i + 1]; \ |
| 207 |
goto next_state; \ |
| 208 |
} \ |
| 209 |
} \ |
| 210 |
} |
| 211 |
|
| 212 |
#define SKIP(state_value) \ |
| 213 |
{ \ |
| 214 |
skip = true; \ |
| 215 |
state = state_value; \ |
| 216 |
goto next_state; \ |
| 217 |
} |
| 218 |
|
| 219 |
#define ACCEPT_TOKEN(symbol_value) \ |
| 220 |
result = true; \ |
| 221 |
lexer->result_symbol = symbol_value; \ |
| 222 |
lexer->mark_end(lexer); |
| 223 |
|
| 224 |
#define END_STATE() return result; |
| 225 |
|
| 226 |
/* |
| 227 |
* Parse Table Macros |
| 228 |
*/ |
| 229 |
|
| 230 |
#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT) |
| 231 |
|
| 232 |
#define STATE(id) id |
| 233 |
|
| 234 |
#define ACTIONS(id) id |
| 235 |
|
| 236 |
#define SHIFT(state_value) \ |
| 237 |
{{ \ |
| 238 |
.shift = { \ |
| 239 |
.type = TSParseActionTypeShift, \ |
| 240 |
.state = (state_value) \ |
| 241 |
} \ |
| 242 |
}} |
| 243 |
|
| 244 |
#define SHIFT_REPEAT(state_value) \ |
| 245 |
{{ \ |
| 246 |
.shift = { \ |
| 247 |
.type = TSParseActionTypeShift, \ |
| 248 |
.state = (state_value), \ |
| 249 |
.repetition = true \ |
| 250 |
} \ |
| 251 |
}} |
| 252 |
|
| 253 |
#define SHIFT_EXTRA() \ |
| 254 |
{{ \ |
| 255 |
.shift = { \ |
| 256 |
.type = TSParseActionTypeShift, \ |
| 257 |
.extra = true \ |
| 258 |
} \ |
| 259 |
}} |
| 260 |
|
| 261 |
#define REDUCE(symbol_name, children, precedence, prod_id) \ |
| 262 |
{{ \ |
| 263 |
.reduce = { \ |
| 264 |
.type = TSParseActionTypeReduce, \ |
| 265 |
.symbol = symbol_name, \ |
| 266 |
.child_count = children, \ |
| 267 |
.dynamic_precedence = precedence, \ |
| 268 |
.production_id = prod_id \ |
| 269 |
}, \ |
| 270 |
}} |
| 271 |
|
| 272 |
#define RECOVER() \ |
| 273 |
{{ \ |
| 274 |
.type = TSParseActionTypeRecover \ |
| 275 |
}} |
| 276 |
|
| 277 |
#define ACCEPT_INPUT() \ |
| 278 |
{{ \ |
| 279 |
.type = TSParseActionTypeAccept \ |
| 280 |
}} |
| 281 |
|
| 282 |
#ifdef __cplusplus |
| 283 |
} |
| 284 |
#endif |
| 285 |
|
| 286 |
#endif // TREE_SITTER_PARSER_H_ |