parser.h 6.8 KB

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