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
 89struct TSLanguage {
 90  uint32_t version;
 91  uint32_t symbol_count;
 92  uint32_t alias_count;
 93  uint32_t token_count;
 94  uint32_t external_token_count;
 95  uint32_t state_count;
 96  uint32_t large_state_count;
 97  uint32_t production_id_count;
 98  uint32_t field_count;
 99  uint16_t max_alias_sequence_length;
100  const uint16_t *parse_table;
101  const uint16_t *small_parse_table;
102  const uint32_t *small_parse_table_map;
103  const TSParseActionEntry *parse_actions;
104  const char * const *symbol_names;
105  const char * const *field_names;
106  const TSFieldMapSlice *field_map_slices;
107  const TSFieldMapEntry *field_map_entries;
108  const TSSymbolMetadata *symbol_metadata;
109  const TSSymbol *public_symbol_map;
110  const uint16_t *alias_map;
111  const TSSymbol *alias_sequences;
112  const TSLexMode *lex_modes;
113  bool (*lex_fn)(TSLexer *, TSStateId);
114  bool (*keyword_lex_fn)(TSLexer *, TSStateId);
115  TSSymbol keyword_capture_token;
116  struct {
117    const bool *states;
118    const TSSymbol *symbol_map;
119    void *(*create)(void);
120    void (*destroy)(void *);
121    bool (*scan)(void *, TSLexer *, const bool *symbol_whitelist);
122    unsigned (*serialize)(void *, char *);
123    void (*deserialize)(void *, const char *, unsigned);
124  } external_scanner;
125  const TSStateId *primary_state_ids;
126};
127
128/*
129 *  Lexer Macros
130 */
131
132#ifdef _MSC_VER
133#define UNUSED __pragma(warning(suppress : 4101))
134#else
135#define UNUSED __attribute__((unused))
136#endif
137
138#define START_LEXER()           \
139  bool result = false;          \
140  bool skip = false;            \
141  UNUSED                        \
142  bool eof = false;             \
143  int32_t lookahead;            \
144  goto start;                   \
145  next_state:                   \
146  lexer->advance(lexer, skip);  \
147  start:                        \
148  skip = false;                 \
149  lookahead = lexer->lookahead;
150
151#define ADVANCE(state_value) \
152  {                          \
153    state = state_value;     \
154    goto next_state;         \
155  }
156
157#define SKIP(state_value) \
158  {                       \
159    skip = true;          \
160    state = state_value;  \
161    goto next_state;      \
162  }
163
164#define ACCEPT_TOKEN(symbol_value)     \
165  result = true;                       \
166  lexer->result_symbol = symbol_value; \
167  lexer->mark_end(lexer);
168
169#define END_STATE() return result;
170
171/*
172 *  Parse Table Macros
173 */
174
175#define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT)
176
177#define STATE(id) id
178
179#define ACTIONS(id) id
180
181#define SHIFT(state_value)            \
182  {{                                  \
183    .shift = {                        \
184      .type = TSParseActionTypeShift, \
185      .state = (state_value)          \
186    }                                 \
187  }}
188
189#define SHIFT_REPEAT(state_value)     \
190  {{                                  \
191    .shift = {                        \
192      .type = TSParseActionTypeShift, \
193      .state = (state_value),         \
194      .repetition = true              \
195    }                                 \
196  }}
197
198#define SHIFT_EXTRA()                 \
199  {{                                  \
200    .shift = {                        \
201      .type = TSParseActionTypeShift, \
202      .extra = true                   \
203    }                                 \
204  }}
205
206#define REDUCE(symbol_val, child_count_val, ...) \
207  {{                                             \
208    .reduce = {                                  \
209      .type = TSParseActionTypeReduce,           \
210      .symbol = symbol_val,                      \
211      .child_count = child_count_val,            \
212      __VA_ARGS__                                \
213    },                                           \
214  }}
215
216#define RECOVER()                    \
217  {{                                 \
218    .type = TSParseActionTypeRecover \
219  }}
220
221#define ACCEPT_INPUT()              \
222  {{                                \
223    .type = TSParseActionTypeAccept \
224  }}
225
226#ifdef __cplusplus
227}
228#endif
229
230#endif  // TREE_SITTER_PARSER_H_