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
|
|
|
|
|
//#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-05-09 14:43:43 -07:00
|
|
|
#include "tree_sitter/parser/lr_parser.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-05-06 12:54:04 -07:00
|
|
|
#define UBIQUITOUS_SYMBOLS \
|
|
|
|
|
static const int ubiquitous_symbol_flags[SYMBOL_COUNT]
|
|
|
|
|
|
2014-03-26 12:52:31 -07:00
|
|
|
#define LEX_STATES \
|
2014-05-08 13:20:05 -07:00
|
|
|
static ts_state_id ts_lex_states[STATE_COUNT]
|
2014-03-25 19:34:17 -07:00
|
|
|
|
2014-06-04 13:34:37 -07:00
|
|
|
#define PARSE_TABLE \
|
|
|
|
|
static const ts_parse_action ts_parse_actions[STATE_COUNT][SYMBOL_COUNT]
|
|
|
|
|
|
2014-03-13 00:43:23 -07:00
|
|
|
#define LEX_FN() \
|
2014-05-08 13:20:05 -07:00
|
|
|
static ts_tree * ts_lex(ts_lexer *lexer, ts_state_id 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-04-14 20:30:03 -07:00
|
|
|
{ DEBUG_LEX("ADVANCE %d", state_index); ts_lexer_advance(lexer); 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-04-14 20:30:03 -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-05-30 13:29:54 -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-04-14 23:11:10 -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-04 13:34:37 -07:00
|
|
|
static const ts_tree * ts_parse(void *data, ts_input input, ts_input_edit *edit) {
|
2014-03-17 13:32:14 -07:00
|
|
|
ts_lr_parser *parser = (ts_lr_parser *)data;
|
|
|
|
|
ts_lr_parser_initialize(parser, input, edit);
|
2014-06-04 13:34:37 -07:00
|
|
|
for (;;) {
|
|
|
|
|
ts_tree *tree = ts_lr_parser_parse(parser, ts_symbol_names);
|
|
|
|
|
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) \
|
|
|
|
|
ts_parser constructor_name() { \
|
|
|
|
|
return (ts_parser) { \
|
|
|
|
|
.parse_fn = ts_parse, \
|
2014-06-09 13:02:39 -07:00
|
|
|
.free_fn = ts_lr_parser_free, \
|
2014-05-09 14:43:43 -07:00
|
|
|
.symbol_names = ts_symbol_names, \
|
|
|
|
|
.data = ts_lr_parser_make( \
|
|
|
|
|
SYMBOL_COUNT, \
|
|
|
|
|
(const ts_parse_action *)ts_parse_actions, \
|
|
|
|
|
ts_lex_states, \
|
|
|
|
|
ts_lex, \
|
|
|
|
|
hidden_symbol_flags, \
|
|
|
|
|
ubiquitous_symbol_flags \
|
|
|
|
|
), \
|
|
|
|
|
}; \
|
|
|
|
|
}
|
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_
|