2014-03-09 22:05:17 -07:00
|
|
|
#ifndef TREE_SITTER_PARSER_H_
|
|
|
|
|
#define TREE_SITTER_PARSER_H_
|
2014-02-15 17:00:33 -08:00
|
|
|
|
2014-01-11 18:14:24 -08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
2014-03-08 15:04:23 -08:00
|
|
|
|
2014-02-15 15:43:32 -08:00
|
|
|
//#define TS_DEBUG_PARSE
|
2014-06-26 08:52:42 -07:00
|
|
|
// #define TS_DEBUG_LEX
|
2014-05-09 15:37:30 -07:00
|
|
|
|
2014-05-09 14:43:43 -07:00
|
|
|
#include "tree_sitter/runtime.h"
|
|
|
|
|
#include "tree_sitter/parser/lexer.h"
|
2014-05-09 15:37:30 -07:00
|
|
|
#include "tree_sitter/parser/stack.h"
|
2014-06-28 19:22:16 -07:00
|
|
|
#include "tree_sitter/parser/state_machine.h"
|
2014-05-09 15:37:30 -07:00
|
|
|
|
2014-03-25 19:51:34 -07:00
|
|
|
#define SYMBOL_NAMES \
|
2014-04-14 20:30:03 -07:00
|
|
|
static const char *ts_symbol_names[]
|
2014-03-28 13:51:32 -07:00
|
|
|
|
2014-03-26 12:52:31 -07:00
|
|
|
#define HIDDEN_SYMBOLS \
|
2014-04-08 21:53:13 -07:00
|
|
|
static const int hidden_symbol_flags[SYMBOL_COUNT]
|
2014-03-28 13:51:32 -07:00
|
|
|
|
2014-03-26 12:52:31 -07:00
|
|
|
#define LEX_STATES \
|
2014-06-28 18:51:06 -07:00
|
|
|
static TSStateId ts_lex_states[STATE_COUNT]
|
2014-03-25 19:34:17 -07:00
|
|
|
|
2014-06-04 13:34:37 -07:00
|
|
|
#define PARSE_TABLE \
|
2014-06-28 19:06:37 -07:00
|
|
|
static const TSParseAction ts_parse_actions[STATE_COUNT][SYMBOL_COUNT]
|
2014-06-04 13:34:37 -07:00
|
|
|
|
2014-03-13 00:43:23 -07:00
|
|
|
#define LEX_FN() \
|
2014-06-28 19:01:46 -07:00
|
|
|
static TSTree * ts_lex(TSLexer *lexer, TSStateId lex_state)
|
2014-01-11 18:14:24 -08:00
|
|
|
|
2014-06-04 13:34:37 -07:00
|
|
|
#ifdef TS_DEBUG_LEX
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#define DEBUG_LEX(...) fprintf(stderr, "\n" __VA_ARGS__)
|
|
|
|
|
#else
|
|
|
|
|
#define DEBUG_LEX(...)
|
|
|
|
|
#endif
|
2014-06-09 21:14:38 -07:00
|
|
|
|
2014-03-25 19:51:34 -07:00
|
|
|
#define START_LEXER() \
|
2014-05-30 13:29:54 -07:00
|
|
|
DEBUG_LEX("LEX %d", lex_state); \
|
2014-03-25 19:51:34 -07:00
|
|
|
char lookahead; \
|
|
|
|
|
next_state: \
|
2014-04-14 20:30:03 -07:00
|
|
|
lookahead = ts_lexer_lookahead_char(lexer); \
|
|
|
|
|
DEBUG_LEX("CHAR '%c'", lookahead);
|
2014-04-04 13:10:55 -07:00
|
|
|
|
2014-04-03 19:10:09 -07:00
|
|
|
#define START_TOKEN() \
|
|
|
|
|
ts_lexer_start_token(lexer);
|
2014-03-28 13:51:32 -07:00
|
|
|
|
2014-03-25 19:51:34 -07:00
|
|
|
#define ADVANCE(state_index) \
|
2014-06-11 11:29:13 -07:00
|
|
|
{ \
|
|
|
|
|
DEBUG_LEX("ADVANCE %d", state_index); \
|
|
|
|
|
if (!ts_lexer_advance(lexer)) ACCEPT_TOKEN(ts_builtin_sym_end); \
|
|
|
|
|
lex_state = state_index; goto next_state; \
|
|
|
|
|
}
|
2014-03-28 13:51:32 -07:00
|
|
|
|
2014-03-25 19:51:34 -07:00
|
|
|
#define ACCEPT_TOKEN(symbol) \
|
2014-06-11 11:29:13 -07:00
|
|
|
{ \
|
|
|
|
|
DEBUG_LEX("TOKEN %s", ts_symbol_names[symbol]); \
|
|
|
|
|
return ts_lexer_build_node(lexer, symbol); \
|
|
|
|
|
}
|
2014-03-28 13:51:32 -07:00
|
|
|
|
2014-03-25 19:51:34 -07:00
|
|
|
#define LEX_ERROR() \
|
2014-06-11 11:29:13 -07:00
|
|
|
{ \
|
|
|
|
|
DEBUG_LEX("ERROR"); \
|
|
|
|
|
return ts_lexer_build_node(lexer, ts_builtin_sym_error); \
|
|
|
|
|
}
|
2014-03-28 13:51:32 -07:00
|
|
|
|
2014-03-25 19:51:34 -07:00
|
|
|
#define LEX_PANIC() \
|
2014-06-11 11:29:13 -07:00
|
|
|
{ \
|
|
|
|
|
DEBUG_LEX("LEX ERROR: unexpected state %d", lex_state); \
|
|
|
|
|
return NULL; \
|
|
|
|
|
}
|
2014-03-13 00:43:23 -07:00
|
|
|
|
2014-06-04 13:34:37 -07:00
|
|
|
SYMBOL_NAMES;
|
2014-05-09 15:37:30 -07:00
|
|
|
|
2014-06-28 18:56:47 -07:00
|
|
|
static const TSTree * ts_parse(void *data, TSInput input, TSInputEdit *edit) {
|
2014-06-28 19:22:16 -07:00
|
|
|
TSStateMachine *parser = (TSStateMachine *)data;
|
|
|
|
|
ts_state_machine_initialize(parser, input, edit);
|
2014-06-04 13:34:37 -07:00
|
|
|
for (;;) {
|
2014-06-28 19:22:16 -07:00
|
|
|
const TSTree *tree = ts_state_machine_parse(parser, ts_symbol_names);
|
2014-06-04 13:34:37 -07:00
|
|
|
if (tree) return tree;
|
2014-03-17 13:32:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 14:43:43 -07:00
|
|
|
#define EXPORT_PARSER(constructor_name) \
|
2014-06-28 19:01:46 -07:00
|
|
|
TSParser constructor_name() { \
|
|
|
|
|
return (TSParser) { \
|
2014-05-09 14:43:43 -07:00
|
|
|
.parse_fn = ts_parse, \
|
2014-06-28 19:22:16 -07:00
|
|
|
.free_fn = ts_state_machine_free, \
|
2014-05-09 14:43:43 -07:00
|
|
|
.symbol_names = ts_symbol_names, \
|
2014-06-28 19:22:16 -07:00
|
|
|
.data = ts_state_machine_make( \
|
2014-05-09 14:43:43 -07:00
|
|
|
SYMBOL_COUNT, \
|
2014-06-28 19:06:37 -07:00
|
|
|
(const TSParseAction *)ts_parse_actions, \
|
2014-05-09 14:43:43 -07:00
|
|
|
ts_lex_states, \
|
|
|
|
|
ts_lex, \
|
2014-06-26 08:52:42 -07:00
|
|
|
hidden_symbol_flags \
|
2014-05-09 14:43:43 -07:00
|
|
|
), \
|
|
|
|
|
}; \
|
|
|
|
|
}
|
2014-05-09 15:37:30 -07:00
|
|
|
|
2014-01-05 15:43:00 -08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2014-02-15 17:00:33 -08:00
|
|
|
|
2014-03-09 22:05:17 -07:00
|
|
|
#endif // TREE_SITTER_PARSER_H_
|