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-19 12:48:38 -08:00
|
|
|
#include <ctype.h>
|
2014-03-13 00:43:23 -07:00
|
|
|
#include <stdio.h>
|
2014-03-09 22:05:17 -07:00
|
|
|
#include "tree_sitter/runtime.h"
|
2014-01-05 15:43:00 -08:00
|
|
|
|
2014-02-15 15:43:32 -08:00
|
|
|
//#define TS_DEBUG_PARSE
|
|
|
|
|
//#define TS_DEBUG_LEX
|
2014-02-24 18:42:54 -08:00
|
|
|
|
2014-01-11 18:14:24 -08:00
|
|
|
#ifdef TS_DEBUG_LEX
|
2014-04-14 20:30:03 -07:00
|
|
|
#define DEBUG_LEX(...) fprintf(stderr, "\n" __VA_ARGS__)
|
2014-01-11 18:14:24 -08:00
|
|
|
#else
|
|
|
|
|
#define DEBUG_LEX(...)
|
2014-01-05 15:43:00 -08:00
|
|
|
#endif
|
2014-03-08 15:04:23 -08:00
|
|
|
|
2014-01-11 18:14:24 -08:00
|
|
|
#ifdef TS_DEBUG_PARSE
|
2014-04-14 20:30:03 -07:00
|
|
|
#define DEBUG_PARSE(...) fprintf(stderr, "\n" __VA_ARGS__)
|
2014-01-11 18:14:24 -08:00
|
|
|
#else
|
|
|
|
|
#define DEBUG_PARSE(...)
|
|
|
|
|
#endif
|
2014-02-15 17:00:33 -08: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-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-13 00:43:23 -07:00
|
|
|
#define EXPORT_PARSER(constructor_name) \
|
|
|
|
|
ts_parser constructor_name() { \
|
2014-04-03 19:10:09 -07:00
|
|
|
return (ts_parser) { \
|
2014-03-13 00:43:23 -07:00
|
|
|
.parse_fn = ts_parse, \
|
2014-05-06 12:54:04 -07:00
|
|
|
.free_fn = NULL, \
|
2014-03-13 00:43:23 -07:00
|
|
|
.symbol_names = ts_symbol_names, \
|
2014-05-06 12:54:04 -07:00
|
|
|
.data = ts_lr_parser_make( \
|
|
|
|
|
SYMBOL_COUNT, \
|
|
|
|
|
(const ts_parse_action *)ts_parse_actions, \
|
|
|
|
|
ts_lex_states, \
|
|
|
|
|
hidden_symbol_flags, \
|
|
|
|
|
ubiquitous_symbol_flags \
|
|
|
|
|
) \
|
2014-03-13 00:43:23 -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-03-13 00:43:23 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Stack
|
|
|
|
|
*/
|
2014-05-08 13:20:05 -07:00
|
|
|
typedef unsigned short ts_state_id;
|
2014-03-13 00:43:23 -07:00
|
|
|
typedef struct {
|
|
|
|
|
size_t size;
|
|
|
|
|
struct {
|
|
|
|
|
ts_tree *node;
|
2014-05-08 13:20:05 -07:00
|
|
|
ts_state_id state;
|
2014-03-13 00:43:23 -07:00
|
|
|
} *entries;
|
|
|
|
|
} ts_stack;
|
|
|
|
|
|
|
|
|
|
ts_stack ts_stack_make();
|
2014-05-09 12:46:36 -07:00
|
|
|
ts_tree * ts_stack_reduce(ts_stack *stack, ts_symbol symbol, size_t immediate_child_count, const int *hidden_symbol_flags, const int *ubiquitous_symbol_flags);
|
2014-03-13 00:43:23 -07:00
|
|
|
void ts_stack_shrink(ts_stack *stack, size_t new_size);
|
2014-05-08 13:20:05 -07:00
|
|
|
void ts_stack_push(ts_stack *stack, ts_state_id state, ts_tree *node);
|
|
|
|
|
ts_state_id ts_stack_top_state(const ts_stack *stack);
|
2014-03-18 13:23:21 -07:00
|
|
|
ts_tree * ts_stack_top_node(const ts_stack *stack);
|
2014-03-21 13:02:25 -07:00
|
|
|
size_t ts_stack_right_position(const ts_stack *stack);
|
2014-03-13 00:43:23 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Lexer
|
|
|
|
|
*/
|
2014-01-11 18:14:24 -08:00
|
|
|
typedef struct {
|
2014-03-01 22:43:25 -08:00
|
|
|
ts_input input;
|
2014-03-10 13:25:31 -07:00
|
|
|
const char *chunk;
|
|
|
|
|
size_t chunk_start;
|
|
|
|
|
size_t chunk_size;
|
|
|
|
|
size_t position_in_chunk;
|
2014-03-01 15:44:25 -08:00
|
|
|
size_t token_end_position;
|
|
|
|
|
size_t token_start_position;
|
2014-03-13 00:43:23 -07:00
|
|
|
} ts_lexer;
|
2014-03-10 13:25:31 -07:00
|
|
|
|
2014-03-13 00:43:23 -07:00
|
|
|
static ts_lexer ts_lexer_make() {
|
|
|
|
|
ts_lexer result = {
|
2014-03-11 08:30:19 -07:00
|
|
|
.chunk = NULL,
|
2014-03-10 13:25:31 -07:00
|
|
|
.chunk_start = 0,
|
2014-03-11 08:30:19 -07:00
|
|
|
.chunk_size = 0,
|
2014-03-10 13:25:31 -07:00
|
|
|
.position_in_chunk = 0,
|
2014-03-11 08:30:19 -07:00
|
|
|
.token_start_position = 0,
|
|
|
|
|
.token_end_position = 0,
|
2014-01-11 18:14:24 -08:00
|
|
|
};
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2014-03-11 08:30:19 -07:00
|
|
|
|
2014-03-13 00:43:23 -07:00
|
|
|
static size_t ts_lexer_position(const ts_lexer *lexer) {
|
|
|
|
|
return lexer->chunk_start + lexer->position_in_chunk;
|
2014-01-11 18:14:24 -08:00
|
|
|
}
|
|
|
|
|
|
2014-03-13 00:43:23 -07:00
|
|
|
static char ts_lexer_lookahead_char(const ts_lexer *lexer) {
|
|
|
|
|
return lexer->chunk[lexer->position_in_chunk];
|
2014-01-11 18:14:24 -08:00
|
|
|
}
|
|
|
|
|
|
2014-03-13 00:43:23 -07:00
|
|
|
static void ts_lexer_advance(ts_lexer *lexer) {
|
2014-03-17 13:32:14 -07:00
|
|
|
static const char empty_chunk[1] = "";
|
2014-03-13 00:43:23 -07:00
|
|
|
if (lexer->position_in_chunk + 1 < lexer->chunk_size) {
|
|
|
|
|
lexer->position_in_chunk++;
|
|
|
|
|
} else {
|
|
|
|
|
lexer->chunk_start += lexer->chunk_size;
|
|
|
|
|
lexer->chunk = lexer->input.read_fn(lexer->input.data, &lexer->chunk_size);
|
|
|
|
|
if (lexer->chunk_size == 0) {
|
|
|
|
|
lexer->chunk = empty_chunk;
|
|
|
|
|
lexer->chunk_size = 1;
|
|
|
|
|
}
|
|
|
|
|
lexer->position_in_chunk = 0;
|
|
|
|
|
}
|
2014-02-26 19:03:43 -08:00
|
|
|
}
|
2014-01-11 18:14:24 -08:00
|
|
|
|
2014-04-03 19:10:09 -07:00
|
|
|
static void ts_lexer_start_token(ts_lexer *lexer) {
|
|
|
|
|
lexer->token_start_position = ts_lexer_position(lexer);
|
2014-04-04 13:10:55 -07:00
|
|
|
}
|
2014-04-03 19:10:09 -07:00
|
|
|
|
2014-03-13 00:43:23 -07:00
|
|
|
static ts_tree * ts_lexer_build_node(ts_lexer *lexer, ts_symbol symbol) {
|
|
|
|
|
size_t current_position = ts_lexer_position(lexer);
|
|
|
|
|
size_t size = current_position - lexer->token_start_position;
|
|
|
|
|
size_t offset = lexer->token_start_position - lexer->token_end_position;
|
|
|
|
|
lexer->token_end_position = current_position;
|
|
|
|
|
return ts_tree_make_leaf(symbol, size, offset);
|
2014-01-11 18:14:24 -08:00
|
|
|
}
|
2014-03-08 15:04:23 -08:00
|
|
|
|
2014-05-08 13:20:05 -07:00
|
|
|
#define ts_lex_state_error 0
|
2014-03-08 15:04:23 -08:00
|
|
|
|
|
|
|
|
|
2014-03-17 13:32:14 -07:00
|
|
|
/*
|
|
|
|
|
* Parse Table components
|
|
|
|
|
*/
|
|
|
|
|
typedef enum {
|
|
|
|
|
ts_parse_action_type_error,
|
|
|
|
|
ts_parse_action_type_shift,
|
|
|
|
|
ts_parse_action_type_reduce,
|
|
|
|
|
ts_parse_action_type_accept,
|
|
|
|
|
} ts_parse_action_type;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
ts_parse_action_type type;
|
|
|
|
|
union {
|
2014-05-08 13:20:05 -07:00
|
|
|
ts_state_id to_state;
|
2014-03-17 13:32:14 -07:00
|
|
|
struct {
|
|
|
|
|
ts_symbol symbol;
|
2014-05-08 08:45:41 -07:00
|
|
|
unsigned short child_count;
|
2014-03-17 13:32:14 -07:00
|
|
|
};
|
|
|
|
|
} data;
|
|
|
|
|
} ts_parse_action;
|
2014-03-28 13:51:32 -07:00
|
|
|
|
2014-03-17 13:32:14 -07:00
|
|
|
|
2014-03-13 00:43:23 -07:00
|
|
|
/*
|
|
|
|
|
* Forward declarations
|
2014-03-17 13:32:14 -07:00
|
|
|
* The file including this header should use these macros to provide definitions.
|
2014-03-13 00:43:23 -07:00
|
|
|
*/
|
|
|
|
|
LEX_FN();
|
2014-04-14 20:30:03 -07:00
|
|
|
SYMBOL_NAMES;
|
2014-03-08 15:04:23 -08:00
|
|
|
|
2014-03-10 13:25:31 -07:00
|
|
|
|
2014-03-13 00:43:23 -07:00
|
|
|
/*
|
|
|
|
|
* Parser
|
|
|
|
|
*/
|
|
|
|
|
typedef struct {
|
|
|
|
|
ts_lexer lexer;
|
|
|
|
|
ts_stack stack;
|
2014-03-25 09:05:55 -07:00
|
|
|
const int *hidden_symbol_flags;
|
2014-05-06 12:54:04 -07:00
|
|
|
const int *ubiquitous_symbol_flags;
|
2014-03-13 00:43:23 -07:00
|
|
|
ts_tree *lookahead;
|
2014-03-15 15:15:59 -07:00
|
|
|
ts_tree *next_lookahead;
|
2014-03-25 19:34:17 -07:00
|
|
|
const ts_parse_action *parse_table;
|
2014-05-08 13:20:05 -07:00
|
|
|
const ts_state_id *lex_states;
|
2014-03-25 19:34:17 -07:00
|
|
|
size_t symbol_count;
|
2014-03-13 00:43:23 -07:00
|
|
|
} ts_lr_parser;
|
|
|
|
|
|
2014-05-06 12:54:04 -07:00
|
|
|
static ts_lr_parser *
|
|
|
|
|
ts_lr_parser_make(size_t symbol_count,
|
|
|
|
|
const ts_parse_action *parse_table,
|
2014-05-08 13:20:05 -07:00
|
|
|
const ts_state_id *lex_states,
|
2014-05-06 12:54:04 -07:00
|
|
|
const int *hidden_symbol_flags,
|
|
|
|
|
const int *ubiquitous_symbol_flags) {
|
2014-03-13 00:43:23 -07:00
|
|
|
ts_lr_parser *result = malloc(sizeof(ts_lr_parser));
|
|
|
|
|
result->lexer = ts_lexer_make();
|
|
|
|
|
result->stack = ts_stack_make();
|
2014-03-25 19:34:17 -07:00
|
|
|
result->symbol_count = symbol_count;
|
|
|
|
|
result->parse_table = parse_table;
|
|
|
|
|
result->lex_states = lex_states;
|
2014-03-25 09:05:55 -07:00
|
|
|
result->hidden_symbol_flags = hidden_symbol_flags;
|
2014-05-06 12:54:04 -07:00
|
|
|
result->ubiquitous_symbol_flags = ubiquitous_symbol_flags;
|
2014-03-13 00:43:23 -07:00
|
|
|
return result;
|
2014-03-01 22:43:25 -08:00
|
|
|
}
|
2014-03-28 13:51:32 -07:00
|
|
|
|
2014-05-08 13:20:05 -07:00
|
|
|
static const ts_parse_action * ts_lr_parser_table_actions(ts_lr_parser *parser, ts_state_id state) {
|
2014-03-25 19:34:17 -07:00
|
|
|
return parser->parse_table + (state * parser->symbol_count);
|
|
|
|
|
}
|
2014-03-21 12:46:23 -07:00
|
|
|
|
2014-03-18 13:23:21 -07:00
|
|
|
static size_t ts_lr_parser_breakdown_stack(ts_lr_parser *parser, ts_input_edit *edit) {
|
2014-03-21 12:46:23 -07:00
|
|
|
if (!edit) return 0;
|
|
|
|
|
|
2014-05-08 13:27:48 -07:00
|
|
|
ts_stack *stack = &parser->stack;
|
2014-03-21 13:02:25 -07:00
|
|
|
size_t position = 0;
|
2014-03-20 18:15:38 -07:00
|
|
|
|
2014-03-21 12:46:23 -07:00
|
|
|
for (;;) {
|
2014-05-08 13:27:48 -07:00
|
|
|
ts_tree *node = ts_stack_top_node(stack);
|
2014-03-21 12:46:23 -07:00
|
|
|
if (!node) break;
|
|
|
|
|
|
2014-03-21 13:02:25 -07:00
|
|
|
position = ts_stack_right_position(stack);
|
2014-05-08 13:27:48 -07:00
|
|
|
size_t child_count;
|
2014-03-24 00:36:47 -07:00
|
|
|
ts_tree **children = ts_tree_immediate_children(node, &child_count);
|
2014-03-21 12:46:23 -07:00
|
|
|
if (position <= edit->position && !children) break;
|
2014-03-18 13:23:21 -07:00
|
|
|
|
2014-03-21 13:02:25 -07:00
|
|
|
stack->size--;
|
2014-03-24 00:34:13 -07:00
|
|
|
position -= ts_tree_total_size(node);
|
2014-03-21 12:46:23 -07:00
|
|
|
|
|
|
|
|
for (size_t i = 0; i < child_count && position < edit->position; i++) {
|
|
|
|
|
ts_tree *child = children[i];
|
2014-05-08 13:20:05 -07:00
|
|
|
ts_state_id state = ts_stack_top_state(stack);
|
|
|
|
|
ts_state_id next_state = ts_lr_parser_table_actions(parser, state)[ts_tree_symbol(child)].data.to_state;
|
2014-03-21 13:02:25 -07:00
|
|
|
ts_stack_push(stack, next_state, child);
|
2014-03-19 19:27:31 -07:00
|
|
|
ts_tree_retain(child);
|
2014-03-24 00:34:13 -07:00
|
|
|
position += ts_tree_total_size(child);
|
2014-03-19 19:27:31 -07:00
|
|
|
}
|
2014-03-21 12:46:23 -07:00
|
|
|
|
2014-03-19 19:27:31 -07:00
|
|
|
ts_tree_release(node);
|
2014-03-18 13:23:21 -07:00
|
|
|
}
|
2014-03-21 12:46:23 -07:00
|
|
|
|
|
|
|
|
return position;
|
2014-03-18 13:23:21 -07:00
|
|
|
}
|
2014-01-11 18:14:24 -08:00
|
|
|
|
2014-03-15 16:55:25 -07:00
|
|
|
static void ts_lr_parser_initialize(ts_lr_parser *parser, ts_input input, ts_input_edit *edit) {
|
2014-03-19 19:27:31 -07:00
|
|
|
if (!edit) ts_stack_shrink(&parser->stack, 0);
|
2014-03-13 00:43:23 -07:00
|
|
|
parser->lookahead = NULL;
|
2014-03-15 15:15:59 -07:00
|
|
|
parser->next_lookahead = NULL;
|
2014-03-15 16:55:25 -07:00
|
|
|
|
2014-03-18 13:23:21 -07:00
|
|
|
size_t position = ts_lr_parser_breakdown_stack(parser, edit);
|
|
|
|
|
input.seek_fn(input.data, position);
|
|
|
|
|
|
2014-03-13 00:43:23 -07:00
|
|
|
parser->lexer = ts_lexer_make();
|
2014-03-15 16:55:25 -07:00
|
|
|
parser->lexer.input = input;
|
|
|
|
|
ts_lexer_advance(&parser->lexer);
|
2014-01-11 18:14:24 -08:00
|
|
|
}
|
2014-03-08 15:04:23 -08:00
|
|
|
|
2014-05-08 13:20:05 -07:00
|
|
|
static void ts_lr_parser_shift(ts_lr_parser *parser, ts_state_id parse_state) {
|
2014-03-13 00:43:23 -07:00
|
|
|
ts_stack_push(&parser->stack, parse_state, parser->lookahead);
|
2014-03-15 15:15:59 -07:00
|
|
|
parser->lookahead = parser->next_lookahead;
|
|
|
|
|
parser->next_lookahead = NULL;
|
2014-01-11 18:14:24 -08:00
|
|
|
}
|
2014-01-05 15:43:00 -08:00
|
|
|
|
2014-05-09 12:46:36 -07:00
|
|
|
static void ts_lr_parser_reduce(ts_lr_parser *parser, ts_symbol symbol, size_t child_count) {
|
2014-03-15 15:15:59 -07:00
|
|
|
parser->next_lookahead = parser->lookahead;
|
2014-05-06 12:54:04 -07:00
|
|
|
parser->lookahead = ts_stack_reduce(&parser->stack, symbol, child_count, parser->hidden_symbol_flags, parser->ubiquitous_symbol_flags);
|
2014-02-19 12:48:38 -08:00
|
|
|
}
|
2014-03-08 15:04:23 -08:00
|
|
|
|
2014-03-17 13:32:14 -07:00
|
|
|
static ts_symbol * ts_lr_parser_expected_symbols(ts_lr_parser *parser, size_t *count) {
|
|
|
|
|
*count = 0;
|
2014-03-25 19:34:17 -07:00
|
|
|
const ts_parse_action *actions = ts_lr_parser_table_actions(parser, ts_stack_top_state(&parser->stack));
|
|
|
|
|
for (size_t i = 0; i < parser->symbol_count; i++)
|
2014-03-17 13:32:14 -07:00
|
|
|
if (actions[i].type != ts_parse_action_type_error)
|
|
|
|
|
++(*count);
|
|
|
|
|
|
|
|
|
|
size_t n = 0;
|
|
|
|
|
ts_symbol *result = malloc(*count * sizeof(*result));
|
2014-05-08 13:27:48 -07:00
|
|
|
for (ts_symbol i = 0; i < parser->symbol_count; i++)
|
2014-03-17 13:32:14 -07:00
|
|
|
if (actions[i].type != ts_parse_action_type_error)
|
|
|
|
|
result[n++] = i;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int ts_lr_parser_handle_error(ts_lr_parser *parser) {
|
|
|
|
|
size_t count = 0;
|
|
|
|
|
ts_symbol *expected_symbols = ts_lr_parser_expected_symbols(parser, &count);
|
2014-03-13 00:43:23 -07:00
|
|
|
ts_tree *error = ts_tree_make_error(ts_lexer_lookahead_char(&parser->lexer), count, expected_symbols, 0, 0);
|
2014-02-27 00:06:22 -08:00
|
|
|
|
2014-03-13 00:43:23 -07:00
|
|
|
for (;;) {
|
|
|
|
|
ts_tree_release(parser->lookahead);
|
2014-03-22 15:46:58 -07:00
|
|
|
size_t position = ts_lexer_position(&parser->lexer);
|
2014-03-13 00:43:23 -07:00
|
|
|
parser->lookahead = ts_lex(&parser->lexer, ts_lex_state_error);
|
2014-03-22 15:46:58 -07:00
|
|
|
if (ts_lexer_position(&parser->lexer) == position)
|
|
|
|
|
ts_lexer_advance(&parser->lexer);
|
|
|
|
|
|
2014-03-24 00:34:13 -07:00
|
|
|
if (ts_tree_symbol(parser->lookahead) == ts_builtin_sym_end) {
|
2014-05-09 12:46:36 -07:00
|
|
|
ts_stack_push(&parser->stack, 0, error);
|
2014-03-15 15:15:59 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
2014-03-08 15:04:23 -08:00
|
|
|
|
2014-03-15 15:15:59 -07:00
|
|
|
/*
|
|
|
|
|
* Unwind the stack, looking for a state in which this token
|
|
|
|
|
* may appear after an error.
|
|
|
|
|
*/
|
2014-05-08 13:20:05 -07:00
|
|
|
for (size_t j = 0; j < parser->stack.size; j++) {
|
|
|
|
|
size_t i = parser->stack.size - 1 - j;
|
|
|
|
|
ts_state_id stack_state = parser->stack.entries[i].state;
|
2014-03-25 19:34:17 -07:00
|
|
|
ts_parse_action action_on_error = ts_lr_parser_table_actions(parser, stack_state)[ts_builtin_sym_error];
|
2014-03-17 13:32:14 -07:00
|
|
|
if (action_on_error.type == ts_parse_action_type_shift) {
|
2014-05-08 13:20:05 -07:00
|
|
|
ts_state_id state_after_error = action_on_error.data.to_state;
|
2014-03-25 19:34:17 -07:00
|
|
|
if (ts_lr_parser_table_actions(parser, state_after_error)[ts_tree_symbol(parser->lookahead)].type != ts_parse_action_type_error) {
|
2014-03-13 00:43:23 -07:00
|
|
|
ts_stack_shrink(&parser->stack, i + 1);
|
2014-03-17 13:32:14 -07:00
|
|
|
ts_stack_push(&parser->stack, state_after_error, error);
|
2014-02-26 19:03:43 -08:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-24 18:42:54 -08:00
|
|
|
}
|
2014-05-09 12:46:36 -07:00
|
|
|
|
|
|
|
|
static ts_tree * ts_lr_parser_tree_root(ts_lr_parser *parser) {
|
|
|
|
|
ts_stack *stack = &parser->stack;
|
|
|
|
|
ts_tree *top_node = ts_stack_top_node(stack);
|
|
|
|
|
if (stack->size <= 1)
|
|
|
|
|
return top_node;
|
|
|
|
|
|
|
|
|
|
size_t immediate_child_count;
|
|
|
|
|
ts_tree **immedate_children = ts_tree_immediate_children(top_node, &immediate_child_count);
|
|
|
|
|
|
|
|
|
|
stack->size--;
|
|
|
|
|
for (size_t i = 0; i < immediate_child_count; i++) {
|
|
|
|
|
ts_tree *child = immedate_children[i];
|
|
|
|
|
ts_tree_retain(child);
|
|
|
|
|
ts_state_id state = ts_stack_top_state(stack);
|
|
|
|
|
ts_state_id next_state = ts_lr_parser_table_actions(parser, state)[ts_tree_symbol(child)].data.to_state;
|
|
|
|
|
ts_stack_push(stack, next_state, child);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ts_tree *new_node = ts_stack_reduce(stack, ts_tree_symbol(top_node), stack->size, parser->hidden_symbol_flags, NULL);
|
|
|
|
|
ts_tree_release(top_node);
|
|
|
|
|
return new_node;
|
|
|
|
|
}
|
2014-02-19 12:48:38 -08:00
|
|
|
|
2014-03-17 13:32:14 -07:00
|
|
|
static const ts_tree * ts_parse(void *data, ts_input input, ts_input_edit *edit) {
|
|
|
|
|
ts_lr_parser *parser = (ts_lr_parser *)data;
|
|
|
|
|
ts_lr_parser_initialize(parser, input, edit);
|
2014-05-08 13:27:48 -07:00
|
|
|
|
|
|
|
|
int done = 0;
|
2014-03-17 13:32:14 -07:00
|
|
|
while (!done) {
|
2014-05-08 13:20:05 -07:00
|
|
|
ts_state_id state = ts_stack_top_state(&parser->stack);
|
2014-03-17 13:32:14 -07:00
|
|
|
if (!parser->lookahead)
|
2014-03-25 19:34:17 -07:00
|
|
|
parser->lookahead = ts_lex(&parser->lexer, parser->lex_states[state]);
|
|
|
|
|
ts_parse_action action = ts_lr_parser_table_actions(parser, state)[ts_tree_symbol(parser->lookahead)];
|
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-08 13:27:48 -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-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_
|