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-05-09 14:43:43 -07:00
|
|
|
SYMBOL_NAMES;
|
|
|
|
|
|
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-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-03-25 19:51:34 -07:00
|
|
|
#define START_LEXER() \
|
|
|
|
|
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() \
|
|
|
|
|
{ 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-03-26 12:52:31 -07:00
|
|
|
#define PARSE_TABLE \
|
2014-04-08 21:53:13 -07:00
|
|
|
static const ts_parse_action ts_parse_actions[STATE_COUNT][SYMBOL_COUNT]
|
2014-03-25 09:05:55 -07:00
|
|
|
|
2014-03-25 19:34:17 -07:00
|
|
|
#define SHIFT(to_state_value) \
|
2014-04-08 21:53:13 -07:00
|
|
|
{ .type = ts_parse_action_type_shift, .data = { .to_state = to_state_value } }
|
2014-03-13 00:43:23 -07:00
|
|
|
|
2014-03-25 19:34:17 -07:00
|
|
|
#define REDUCE(symbol_val, child_count_val) \
|
2014-04-08 21:53:13 -07:00
|
|
|
{ .type = ts_parse_action_type_reduce, .data = { .symbol = symbol_val, .child_count = child_count_val } }
|
2014-03-13 00:43:23 -07:00
|
|
|
|
2014-03-25 19:34:17 -07:00
|
|
|
#define ACCEPT_INPUT() \
|
2014-04-08 21:53:13 -07:00
|
|
|
{ .type = ts_parse_action_type_accept }
|
2014-05-09 15:37:30 -07:00
|
|
|
|
2014-05-09 14:43:43 -07:00
|
|
|
#ifdef TS_DEBUG_LEX
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#define DEBUG_LEX(...) fprintf(stderr, "\n" __VA_ARGS__)
|
|
|
|
|
#else
|
|
|
|
|
#define DEBUG_LEX(...)
|
|
|
|
|
#endif
|
2014-05-09 15:37:30 -07:00
|
|
|
|
2014-05-09 14:43:43 -07:00
|
|
|
#ifdef TS_DEBUG_PARSE
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#define DEBUG_PARSE(...) fprintf(stderr, "\n" __VA_ARGS__)
|
|
|
|
|
#else
|
|
|
|
|
#define DEBUG_PARSE(...)
|
|
|
|
|
#endif
|
2014-05-09 15:37:30 -07:00
|
|
|
|
|
|
|
|
static const ts_tree *
|
2014-05-09 14:43:43 -07:00
|
|
|
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-05-09 15:37:30 -07:00
|
|
|
|
2014-05-08 13:27:48 -07:00
|
|
|
int done = 0;
|
2014-03-17 13:32:14 -07:00
|
|
|
while (!done) {
|
2014-05-09 14:43:43 -07:00
|
|
|
ts_parse_action action = ts_lr_parser_next_action(parser);
|
2014-04-14 20:30:03 -07:00
|
|
|
DEBUG_PARSE("LOOKAHEAD %s", ts_symbol_names[ts_tree_symbol(parser->lookahead)]);
|
2014-03-17 13:32:14 -07:00
|
|
|
switch (action.type) {
|
|
|
|
|
case ts_parse_action_type_shift:
|
2014-04-14 20:30:03 -07:00
|
|
|
DEBUG_PARSE("SHIFT %d", action.data.to_state);
|
2014-03-17 13:32:14 -07:00
|
|
|
ts_lr_parser_shift(parser, action.data.to_state);
|
|
|
|
|
break;
|
|
|
|
|
case ts_parse_action_type_reduce:
|
2014-04-14 20:30:03 -07:00
|
|
|
DEBUG_PARSE("REDUCE %s %d", ts_symbol_names[action.data.symbol], action.data.child_count);
|
2014-03-25 09:05:55 -07:00
|
|
|
ts_lr_parser_reduce(parser, action.data.symbol, action.data.child_count);
|
2014-03-17 13:32:14 -07:00
|
|
|
break;
|
|
|
|
|
case ts_parse_action_type_accept:
|
2014-04-14 20:30:03 -07:00
|
|
|
DEBUG_PARSE("ACCEPT");
|
2014-03-17 13:32:14 -07:00
|
|
|
done = 1;
|
|
|
|
|
break;
|
|
|
|
|
case ts_parse_action_type_error:
|
2014-04-14 20:30:03 -07:00
|
|
|
DEBUG_PARSE("ERROR");
|
2014-03-17 13:32:14 -07:00
|
|
|
done = !ts_lr_parser_handle_error(parser);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-09 15:37:30 -07:00
|
|
|
|
2014-05-09 12:46:36 -07:00
|
|
|
return ts_lr_parser_tree_root(parser);
|
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, \
|
|
|
|
|
.free_fn = NULL, \
|
|
|
|
|
.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_
|