Refactor parser header
Make separate lexer, stack and parser structs.
This commit is contained in:
parent
05a5f9c124
commit
0dc3a95d0c
11 changed files with 361 additions and 276 deletions
|
|
@ -2,7 +2,7 @@
|
|||
#include <string.h>
|
||||
|
||||
struct ts_document {
|
||||
ts_parse_config parse_config;
|
||||
ts_parser parser;
|
||||
const ts_tree *tree;
|
||||
ts_input input;
|
||||
size_t error_count;
|
||||
|
|
@ -16,8 +16,8 @@ void ts_document_free(ts_document *document) {
|
|||
free(document);
|
||||
}
|
||||
|
||||
void ts_document_set_parser(ts_document *document, ts_parse_config config) {
|
||||
document->parse_config = config;
|
||||
void ts_document_set_parser(ts_document *document, ts_parser parser) {
|
||||
document->parser = parser;
|
||||
}
|
||||
|
||||
const ts_tree * ts_document_tree(const ts_document *document) {
|
||||
|
|
@ -25,17 +25,17 @@ const ts_tree * ts_document_tree(const ts_document *document) {
|
|||
}
|
||||
|
||||
const char * ts_document_string(const ts_document *document) {
|
||||
return ts_tree_string(document->tree, document->parse_config.symbol_names);
|
||||
return ts_tree_string(document->tree, document->parser.symbol_names);
|
||||
}
|
||||
|
||||
void ts_document_set_input(ts_document *document, ts_input input) {
|
||||
document->input = input;
|
||||
document->tree = document->parse_config.parse_fn(input);
|
||||
document->tree = ts_parser_parse(&document->parser, input);
|
||||
}
|
||||
|
||||
void ts_document_edit(ts_document *document, size_t position, size_t bytes_removed, size_t bytes_inserted) {
|
||||
document->input.seek_fn(document->input.data, 0);
|
||||
document->tree = document->parse_config.parse_fn(document->input);
|
||||
document->tree = ts_parser_parse(&document->parser, document->input);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
|
|
|
|||
10
src/runtime/parser.c
Normal file
10
src/runtime/parser.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#include "tree_sitter/runtime.h"
|
||||
|
||||
const ts_tree * ts_parser_parse(ts_parser *parser, ts_input input) {
|
||||
return parser->parse_fn(parser->data, input);
|
||||
}
|
||||
|
||||
void ts_parser_free(ts_parser *parser) {
|
||||
if (parser->free_fn != NULL)
|
||||
parser->free_fn(parser->data);
|
||||
}
|
||||
80
src/runtime/stack.c
Normal file
80
src/runtime/stack.c
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
#include "tree_sitter/runtime.h"
|
||||
#include <string.h>
|
||||
|
||||
typedef int state_id;
|
||||
static const state_id ts_lex_state_error = -1;
|
||||
|
||||
typedef struct {
|
||||
size_t size;
|
||||
struct {
|
||||
ts_tree *node;
|
||||
state_id state;
|
||||
} *entries;
|
||||
} ts_stack;
|
||||
|
||||
static int INITIAL_STACK_SIZE = 100;
|
||||
|
||||
ts_stack ts_stack_make() {
|
||||
ts_stack result = {
|
||||
.entries = calloc(INITIAL_STACK_SIZE, sizeof(*result.entries)),
|
||||
.size = 0,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
state_id ts_stack_top_state(const ts_stack *stack) {
|
||||
if (stack->size == 0) return 0;
|
||||
return stack->entries[stack->size - 1].state;
|
||||
}
|
||||
|
||||
ts_tree * ts_stack_root(ts_stack *stack) {
|
||||
return stack->entries[0].node;
|
||||
}
|
||||
|
||||
void ts_stack_push(ts_stack *stack, state_id state, ts_tree *node) {
|
||||
stack->entries[stack->size].state = state;
|
||||
stack->entries[stack->size].node = node;
|
||||
stack->size++;
|
||||
}
|
||||
|
||||
void ts_stack_shrink(ts_stack *stack, size_t new_size) {
|
||||
for (size_t i = new_size; i < stack->size; i++)
|
||||
ts_tree_release(stack->entries[i].node);
|
||||
stack->size = new_size;
|
||||
}
|
||||
|
||||
ts_tree * ts_stack_reduce(ts_stack *stack, ts_symbol symbol, int immediate_child_count, const int *collapse_flags) {
|
||||
size_t new_stack_size = stack->size - immediate_child_count;
|
||||
|
||||
int child_count = 0;
|
||||
for (int i = 0; i < immediate_child_count; i++) {
|
||||
ts_tree *child = stack->entries[new_stack_size + i].node;
|
||||
child_count += collapse_flags[i] ? ts_tree_child_count(child) : 1;
|
||||
}
|
||||
|
||||
int child_index = 0;
|
||||
size_t size = 0, offset = 0;
|
||||
ts_tree **children = malloc(child_count * sizeof(ts_tree *));
|
||||
for (int i = 0; i < immediate_child_count; i++) {
|
||||
ts_tree *child = stack->entries[new_stack_size + i].node;
|
||||
if (i == 0) {
|
||||
offset = child->offset;
|
||||
size = child->size;
|
||||
} else {
|
||||
size += child->offset + child->size;
|
||||
}
|
||||
|
||||
if (collapse_flags[i]) {
|
||||
size_t grandchild_count = ts_tree_child_count(child);
|
||||
memcpy(children + child_index, ts_tree_children(child), (grandchild_count * sizeof(ts_tree *)));
|
||||
child_index += grandchild_count;
|
||||
} else {
|
||||
memcpy(children + child_index, &child, sizeof(ts_tree *));
|
||||
child_index++;
|
||||
}
|
||||
}
|
||||
|
||||
ts_tree *lookahead = ts_tree_make_node(symbol, child_count, children, size, offset);
|
||||
ts_stack_shrink(stack, new_stack_size);
|
||||
return lookahead;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue