1
0

parser.h 7.4 KB

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