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