Split parse stack when there are multiple parse actions
This commit is contained in:
parent
f26ddf5187
commit
aff8bc3266
12 changed files with 1563 additions and 907 deletions
|
|
@ -40,7 +40,7 @@ typedef struct TSLexer {
|
|||
} TSLexer;
|
||||
|
||||
typedef enum {
|
||||
TSParseActionTypeError,
|
||||
TSParseActionTypeError = 1,
|
||||
TSParseActionTypeShift,
|
||||
TSParseActionTypeShiftExtra,
|
||||
TSParseActionTypeReduce,
|
||||
|
|
@ -113,7 +113,7 @@ struct TSLanguage {
|
|||
*/
|
||||
|
||||
#define ACTIONS(...) \
|
||||
(TSParseAction[]) {__VA_ARGS__}
|
||||
(TSParseAction[]) {__VA_ARGS__, {.type = 0}}
|
||||
|
||||
#define SHIFT(to_state_value) \
|
||||
{ \
|
||||
|
|
|
|||
7
spec/fixtures/grammars/c.cc
vendored
7
spec/fixtures/grammars/c.cc
vendored
|
|
@ -132,6 +132,7 @@ extern const Grammar c = Grammar({
|
|||
|
||||
{ "expression", choice({
|
||||
sym("math_expression"),
|
||||
sym("call_expression"),
|
||||
sym("string"),
|
||||
sym("identifier"),
|
||||
sym("number") }) },
|
||||
|
|
@ -140,6 +141,12 @@ extern const Grammar c = Grammar({
|
|||
prec(1, seq({ sym("expression"), str("+"), sym("expression") })),
|
||||
prec(2, seq({ sym("expression"), sym("star"), sym("expression") })) }) },
|
||||
|
||||
{ "call_expression", prec(3, seq({
|
||||
sym("expression"),
|
||||
str("("),
|
||||
comma_sep(sym("expression")),
|
||||
str(")") })) },
|
||||
|
||||
{ "statement", choice({
|
||||
sym("expression_statement") }) },
|
||||
|
||||
|
|
|
|||
2038
spec/fixtures/parsers/c.c
vendored
2038
spec/fixtures/parsers/c.c
vendored
File diff suppressed because it is too large
Load diff
|
|
@ -22,17 +22,20 @@ int main() {
|
|||
(compound_statement))
|
||||
|
||||
==========================================
|
||||
simple declarations in functions
|
||||
ambiguous declarations
|
||||
==========================================
|
||||
|
||||
int main() {
|
||||
x + y;
|
||||
someTypeOrValue * pointerOrMultiplicand();
|
||||
}
|
||||
|
||||
---
|
||||
|
||||
(function_definition
|
||||
(identifier)
|
||||
(direct_declarator (identifier))
|
||||
(compound_statement
|
||||
(expression_statement (math_expression (identifier) (identifier)))))
|
||||
(identifier)
|
||||
(direct_declarator (identifier))
|
||||
(AMBIGUITY
|
||||
(ALTERNATIVE (compound_statement
|
||||
(declaration (identifier) (declarator (star) (direct_declarator (identifier))))))
|
||||
(ALTERNATIVE (compound_statement
|
||||
(expression_statement (math_expression (identifier) (star) (call_expression (identifier))))))))
|
||||
|
|
|
|||
|
|
@ -185,7 +185,7 @@ describe("ParseStack", [&]() {
|
|||
});
|
||||
|
||||
describe("when the trees are identical", [&]() {
|
||||
it("merges the heads, and removes nodes along both heads on subsequent pop operations", [&]() {
|
||||
before_each([&]() {
|
||||
/*
|
||||
* A0__B1__C2__D3__G6.
|
||||
* \__E4__F5__/
|
||||
|
|
@ -194,14 +194,18 @@ describe("ParseStack", [&]() {
|
|||
AssertThat(merged, IsFalse());
|
||||
merged = ts_parse_stack_push(stack, 1, stateG, trees[6]);
|
||||
AssertThat(merged, IsTrue());
|
||||
});
|
||||
|
||||
it("merges the heads", [&]() {
|
||||
AssertThat(ts_parse_stack_head_count(stack), Equals(1));
|
||||
const ParseStackEntry *entry1 = ts_parse_stack_head(stack, 0);
|
||||
AssertThat(*entry1, Equals<ParseStackEntry>({trees[6], stateG}));
|
||||
AssertThat(ts_parse_stack_entry_next_count(entry1), Equals(2));
|
||||
AssertThat(*ts_parse_stack_entry_next(entry1, 0), Equals<ParseStackEntry>({trees[3], stateD}));
|
||||
AssertThat(*ts_parse_stack_entry_next(entry1, 1), Equals<ParseStackEntry>({trees[5], stateF}));
|
||||
});
|
||||
|
||||
it("removes nodes along both heads on subsequent pop operations", [&]() {
|
||||
/*
|
||||
* A0__B1__C2.
|
||||
* \__E4.
|
||||
|
|
@ -220,6 +224,56 @@ describe("ParseStack", [&]() {
|
|||
AssertThat(*ts_parse_stack_head(stack, 0), Equals<ParseStackEntry>({trees[2], stateC}));
|
||||
AssertThat(*ts_parse_stack_head(stack, 1), Equals<ParseStackEntry>({trees[4], stateE}));
|
||||
});
|
||||
|
||||
it("pops only one path if the split is hidden under sufficiently many nodes", [&]() {
|
||||
/*
|
||||
* A0__B1__C2__D3__G6__H7.
|
||||
* \__E4__F5__/
|
||||
*/
|
||||
merged = ts_parse_stack_push(stack, 0, stateH, trees[7]);
|
||||
AssertThat(merged, IsFalse());
|
||||
AssertThat(ts_parse_stack_head_count(stack), Equals(1));
|
||||
|
||||
/*
|
||||
* A0__B1__C2__D3__G6.
|
||||
* \__E4__F5__/
|
||||
*/
|
||||
ParseStackPopResultList pop = ts_parse_stack_pop(stack, 0, 1, false);
|
||||
|
||||
AssertThat(pop.size, Equals(1));
|
||||
AssertThat(ts_parse_stack_head_count(stack), Equals(1));
|
||||
});
|
||||
|
||||
it("can pop one branch that reveals two head", [&]() {
|
||||
/*
|
||||
* A0__B1__C2__D3.
|
||||
* \__E4__F5.
|
||||
*/
|
||||
ParseStackPopResultList pop = ts_parse_stack_pop(stack, 0, 1, false);
|
||||
AssertThat(ts_parse_stack_head_count(stack), Equals(2));
|
||||
|
||||
AssertThat(pop.size, Equals(2));
|
||||
AssertThat(pop.contents[0].index, Equals(0));
|
||||
AssertThat(pop.contents[1].index, Equals(1));
|
||||
AssertThat(pop.contents[0].trees, Equals(pop.contents[1].trees));
|
||||
});
|
||||
|
||||
it("can pop two branches that converge at the same head", [&]() {
|
||||
/*
|
||||
* A0__B1.
|
||||
*/
|
||||
ParseStackPopResultList pop = ts_parse_stack_pop(stack, 0, 3, false);
|
||||
AssertThat(ts_parse_stack_head_count(stack), Equals(1));
|
||||
AssertThat(*ts_parse_stack_head(stack, 0), Equals<ParseStackEntry>({trees[1], stateB}));
|
||||
|
||||
AssertThat(pop.size, Equals(2));
|
||||
AssertThat(pop.contents[0].tree_count, Equals(3));
|
||||
AssertThat(pop.contents[0].index, Equals(0));
|
||||
AssertThat(pop.contents[0].trees[0], Equals(trees[2]));
|
||||
AssertThat(pop.contents[1].tree_count, Equals(3));
|
||||
AssertThat(pop.contents[1].index, Equals(0));
|
||||
AssertThat(pop.contents[1].trees[0], Equals(trees[4]));
|
||||
});
|
||||
});
|
||||
|
||||
describe("when the trees are different", [&]() {
|
||||
|
|
@ -235,7 +289,7 @@ describe("ParseStack", [&]() {
|
|||
|
||||
AssertThat(ts_parse_stack_head_count(stack), Equals(1));
|
||||
AssertThat(*ts_parse_stack_head(stack, 0), Equals<ParseStackEntry>({
|
||||
ts_tree_make_ambiguity(2, tree_array({ trees[6], trees[7] })),
|
||||
ts_tree_make_ambiguity(trees[6], trees[7]),
|
||||
stateG
|
||||
}));
|
||||
});
|
||||
|
|
|
|||
|
|
@ -5,17 +5,19 @@
|
|||
START_TEST
|
||||
|
||||
enum {
|
||||
cat = 2,
|
||||
dog = 3,
|
||||
pig = 4,
|
||||
cat = ts_builtin_sym_start,
|
||||
dog,
|
||||
pig,
|
||||
};
|
||||
|
||||
static const char *names[] = {
|
||||
"error",
|
||||
"end",
|
||||
"cat",
|
||||
"dog",
|
||||
"pig",
|
||||
"AMBIGUITY",
|
||||
"DOCUMENT",
|
||||
"ERROR",
|
||||
"END",
|
||||
"cat",
|
||||
"dog",
|
||||
"pig",
|
||||
};
|
||||
|
||||
describe("Tree", []() {
|
||||
|
|
|
|||
|
|
@ -139,7 +139,15 @@ static int ts_parse_stack_add_head(ParseStack *this, ParseStackNode *node) {
|
|||
return new_index;
|
||||
}
|
||||
|
||||
static void ts_parse_stack_remove_head(ParseStack *this, int head_index) {
|
||||
static int ts_parse_stack_find_or_add_head(ParseStack *this, ParseStackNode *node) {
|
||||
for (int i = 0; i < this->head_count; i++)
|
||||
if (this->heads[i] == node) {
|
||||
return i;
|
||||
}
|
||||
return ts_parse_stack_add_head(this, node);
|
||||
}
|
||||
|
||||
void ts_parse_stack_remove_head(ParseStack *this, int head_index) {
|
||||
stack_node_release(this->heads[head_index]);
|
||||
for (int i = head_index; i < this->head_count - 1; i++) {
|
||||
this->heads[head_index] = this->heads[head_index + 1];
|
||||
|
|
@ -156,10 +164,7 @@ static bool ts_parse_stack_merge_head(ParseStack *this, int head_index, TSStateI
|
|||
ts_parse_stack_remove_head(this, head_index);
|
||||
return true;
|
||||
} else {
|
||||
TSTree **options = malloc(2 * sizeof(TSTree *));
|
||||
options[0] = head->entry.tree;
|
||||
options[1] = tree;
|
||||
head->entry.tree = ts_tree_make_ambiguity(2, options);
|
||||
head->entry.tree = ts_tree_add_alternative(head->entry.tree, tree);
|
||||
stack_node_add_successor(head, this->heads[head_index]);
|
||||
ts_parse_stack_remove_head(this, head_index);
|
||||
return true;
|
||||
|
|
@ -181,6 +186,12 @@ bool ts_parse_stack_push(ParseStack *this, int head_index, TSStateId state, TSTr
|
|||
return false;
|
||||
}
|
||||
|
||||
void ts_parse_stack_add_alternative(ParseStack *this, int head_index, TSTree *tree) {
|
||||
assert(head_index < this->head_count);
|
||||
ParseStackEntry *entry = &this->heads[head_index]->entry;
|
||||
entry->tree = ts_tree_add_alternative(entry->tree, tree);
|
||||
}
|
||||
|
||||
int ts_parse_stack_split(ParseStack *this, int head_index) {
|
||||
assert(head_index < this->head_count);
|
||||
return ts_parse_stack_add_head(this, this->heads[head_index]);
|
||||
|
|
@ -194,6 +205,7 @@ ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index, int
|
|||
size_t tree_counts_by_path[MAX_POP_PATH_COUNT] = {child_count};
|
||||
ParseStackNode *nodes_by_path[MAX_POP_PATH_COUNT] = {previous_head};
|
||||
TreeVector trees_by_path[MAX_POP_PATH_COUNT] = {tree_vector_new(capacity)};
|
||||
bool is_shared_by_path[MAX_POP_PATH_COUNT] = {false};
|
||||
|
||||
/*
|
||||
* Reduce along every possible path in parallel. Stop when the given number
|
||||
|
|
@ -219,6 +231,10 @@ ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index, int
|
|||
* If a node has more than one successor, create new paths for each of
|
||||
* the additional successors.
|
||||
*/
|
||||
if (is_shared_by_path[path]) {
|
||||
trees_by_path[path] = tree_vector_copy(&trees_by_path[path]);
|
||||
is_shared_by_path[path] = false;
|
||||
}
|
||||
tree_vector_push(&trees_by_path[path], node->entry.tree);
|
||||
|
||||
for (int i = 0; i < node->successor_count; i++) {
|
||||
|
|
@ -227,7 +243,8 @@ ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index, int
|
|||
if (path_count == MAX_POP_PATH_COUNT) break;
|
||||
next_path = path_count;
|
||||
tree_counts_by_path[next_path] = tree_counts_by_path[path];
|
||||
trees_by_path[next_path] = tree_vector_copy(&trees_by_path[path]);
|
||||
trees_by_path[next_path] = trees_by_path[path];
|
||||
is_shared_by_path[next_path] = true;
|
||||
path_count++;
|
||||
} else {
|
||||
next_path = path;
|
||||
|
|
@ -240,13 +257,13 @@ ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index, int
|
|||
|
||||
for (int path = 0; path < path_count; path++) {
|
||||
tree_vector_reverse(&trees_by_path[path]);
|
||||
int index;
|
||||
int index = -1;
|
||||
if (path == 0) {
|
||||
stack_node_retain(nodes_by_path[path]);
|
||||
this->heads[head_index] = nodes_by_path[path];
|
||||
index = head_index;
|
||||
} else {
|
||||
index = ts_parse_stack_add_head(this, nodes_by_path[path]);
|
||||
index = ts_parse_stack_find_or_add_head(this, nodes_by_path[path]);
|
||||
}
|
||||
|
||||
this->last_pop_results[path] = (ParseStackPopResult) {
|
||||
|
|
|
|||
|
|
@ -74,6 +74,11 @@ ParseStackEntry *ts_parse_stack_entry_next(const ParseStackEntry *, int);
|
|||
*/
|
||||
bool ts_parse_stack_push(ParseStack *, int head, TSStateId, TSTree *);
|
||||
|
||||
/*
|
||||
* Add an alternative tree for the given head of the stack.
|
||||
*/
|
||||
void ts_parse_stack_add_alternative(ParseStack *, int head, TSTree *);
|
||||
|
||||
/*
|
||||
* Pop the given number of entries from the given head of the stack. This
|
||||
* operation can increase the number of stack heads by revealing multiple heads
|
||||
|
|
@ -94,6 +99,11 @@ void ts_parse_stack_shrink(ParseStack *, int head, int count);
|
|||
*/
|
||||
int ts_parse_stack_split(ParseStack *, int head);
|
||||
|
||||
/*
|
||||
* Remove the given head from the stack.
|
||||
*/
|
||||
void ts_parse_stack_remove_head(ParseStack *, int head);
|
||||
|
||||
/*
|
||||
* Remove all entries from the stack.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include "tree_sitter/runtime.h"
|
||||
|
|
@ -27,14 +28,20 @@
|
|||
* Private
|
||||
*/
|
||||
|
||||
static const TSParseAction ERROR_ACTION = {
|
||||
.type = TSParseActionTypeError
|
||||
static const TSParseAction ERROR_ACTIONS[2] = {
|
||||
{.type = TSParseActionTypeError},
|
||||
{.type = 0}
|
||||
};
|
||||
|
||||
static const TSParseAction *get_actions(const TSLanguage *language, TSStateId state,
|
||||
TSSymbol sym) {
|
||||
const TSParseAction *actions = (language->parse_table + (state * language->symbol_count))[sym];
|
||||
return actions ? actions : ERROR_ACTIONS;
|
||||
}
|
||||
|
||||
static TSParseAction get_action(const TSLanguage *language, TSStateId state,
|
||||
TSSymbol sym) {
|
||||
const TSParseAction *actions = (language->parse_table + (state * language->symbol_count))[sym];
|
||||
return actions ? actions[0] : ERROR_ACTION;
|
||||
return get_actions(language, state, sym)[0];
|
||||
}
|
||||
|
||||
static TSLength break_down_left_stack(TSParser *parser, TSInputEdit edit) {
|
||||
|
|
@ -168,56 +175,78 @@ static TSTree *get_next_node(TSParser *parser, TSStateId lex_state) {
|
|||
* Parse Actions
|
||||
*/
|
||||
|
||||
static void shift(TSParser *parser, TSStateId parse_state) {
|
||||
ts_parse_stack_push(parser->stack, 0, parse_state, parser->lookahead);
|
||||
parser->lookahead = NULL;
|
||||
static void shift(TSParser *parser, int head, TSStateId parse_state) {
|
||||
ts_parse_stack_push(parser->stack, head, parse_state, parser->lookahead);
|
||||
}
|
||||
|
||||
static void shift_extra(TSParser *parser, TSStateId state) {
|
||||
static void shift_extra(TSParser *parser, int head, TSStateId state) {
|
||||
ts_tree_set_extra(parser->lookahead);
|
||||
shift(parser, state);
|
||||
shift(parser, head, state);
|
||||
}
|
||||
|
||||
static TSTree * reduce_helper(TSParser *parser, TSSymbol symbol, size_t child_count, bool extra, bool count_extra) {
|
||||
ParseStackPopResultList pop_results = ts_parse_stack_pop(parser->stack, 0, child_count, count_extra);
|
||||
TSTree **children = pop_results.contents[0].trees;
|
||||
|
||||
static TSTree * reduce_helper(TSParser *parser, int head, TSSymbol symbol, size_t child_count, bool extra, bool count_extra) {
|
||||
bool hidden = parser->language->hidden_symbol_flags[symbol];
|
||||
TSTree *parent = ts_tree_make_node(symbol, pop_results.contents[0].tree_count, children, hidden);
|
||||
ParseStackPopResultList pop_results = ts_parse_stack_pop(parser->stack, head, child_count, count_extra);
|
||||
|
||||
TSStateId top_state = ts_parse_stack_top_state(parser->stack, 0);
|
||||
TSStateId state = extra ? top_state : get_action(parser->language, top_state, symbol).data.to_state;
|
||||
TSTree *parent = NULL;
|
||||
TSTree **last_children = NULL;
|
||||
int last_index = -1;
|
||||
|
||||
for (int i = 0; i < pop_results.size; i++) {
|
||||
ParseStackPopResult pop_result = pop_results.contents[i];
|
||||
|
||||
if (pop_result.trees != last_children) {
|
||||
parent = ts_tree_make_node(symbol, pop_result.tree_count, pop_result.trees, hidden);
|
||||
}
|
||||
|
||||
if (pop_result.index == last_index) {
|
||||
ts_parse_stack_add_alternative(parser->stack, pop_result.index, parent);
|
||||
} else {
|
||||
TSStateId top_state = ts_parse_stack_top_state(parser->stack, pop_result.index);
|
||||
TSStateId state;
|
||||
|
||||
if (extra) {
|
||||
ts_tree_set_extra(parent);
|
||||
state = top_state;
|
||||
} else {
|
||||
state = get_action(parser->language, top_state, symbol).data.to_state;
|
||||
}
|
||||
|
||||
ts_parse_stack_push(parser->stack, pop_result.index, state, parent);
|
||||
}
|
||||
|
||||
last_index = pop_result.index;
|
||||
last_children = pop_result.trees;
|
||||
}
|
||||
|
||||
ts_parse_stack_push(parser->stack, 0, state, parent);
|
||||
return parent;
|
||||
}
|
||||
|
||||
static void reduce(TSParser *parser, TSSymbol symbol, size_t child_count) {
|
||||
reduce_helper(parser, symbol, child_count, false, false);
|
||||
static void reduce(TSParser *parser, int head, TSSymbol symbol, size_t child_count) {
|
||||
reduce_helper(parser, head, symbol, child_count, false, false);
|
||||
}
|
||||
|
||||
static void reduce_extra(TSParser *parser, TSSymbol symbol) {
|
||||
TSTree *reduced = reduce_helper(parser, symbol, 1, true, false);
|
||||
ts_tree_set_extra(reduced);
|
||||
static void reduce_extra(TSParser *parser, int head, TSSymbol symbol) {
|
||||
reduce_helper(parser, head, symbol, 1, true, false);
|
||||
}
|
||||
|
||||
static void reduce_fragile(TSParser *parser, TSSymbol symbol, size_t child_count) {
|
||||
TSTree *reduced = reduce_helper(parser, symbol, child_count, false, false);
|
||||
static void reduce_fragile(TSParser *parser, int head, TSSymbol symbol, size_t child_count) {
|
||||
TSTree *reduced = reduce_helper(parser, head, symbol, child_count, false, false);
|
||||
ts_tree_set_fragile_left(reduced);
|
||||
ts_tree_set_fragile_right(reduced);
|
||||
}
|
||||
|
||||
static void reduce_error(TSParser *parser, size_t child_count) {
|
||||
TSTree *reduced = reduce_helper(parser, ts_builtin_sym_error, child_count, false, true);
|
||||
static void reduce_error(TSParser *parser, int head, size_t child_count) {
|
||||
TSTree *reduced = reduce_helper(parser, head, ts_builtin_sym_error, child_count, false, true);
|
||||
reduced->size = ts_length_add(reduced->size, parser->lookahead->padding);
|
||||
parser->lookahead->padding = ts_length_zero();
|
||||
ts_tree_set_fragile_left(reduced);
|
||||
ts_tree_set_fragile_right(reduced);
|
||||
}
|
||||
|
||||
static int handle_error(TSParser *parser) {
|
||||
static bool handle_error(TSParser *parser, int head) {
|
||||
size_t error_token_count = 1;
|
||||
ParseStackEntry *entry_before_error = ts_parse_stack_head(parser->stack, 0);
|
||||
ParseStackEntry *entry_before_error = ts_parse_stack_head(parser->stack, head);
|
||||
|
||||
for (;;) {
|
||||
|
||||
|
|
@ -240,8 +269,8 @@ static int handle_error(TSParser *parser) {
|
|||
|
||||
if (action_after_error.type != TSParseActionTypeError) {
|
||||
DEBUG("recover state:%u, count:%lu", state_after_error, error_token_count + i);
|
||||
reduce_error(parser, error_token_count + i);
|
||||
return 1;
|
||||
reduce_error(parser, head, error_token_count + i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -251,7 +280,7 @@ static int handle_error(TSParser *parser) {
|
|||
* current lookahead token, advance to the next token.
|
||||
*/
|
||||
DEBUG("skip token:%s", SYM_NAME(parser->lookahead->symbol));
|
||||
shift(parser, ts_parse_stack_top_state(parser->stack, 0));
|
||||
shift(parser, head, ts_parse_stack_top_state(parser->stack, 0));
|
||||
parser->lookahead = get_next_node(parser, ts_lex_state_error);
|
||||
error_token_count++;
|
||||
|
||||
|
|
@ -260,14 +289,14 @@ static int handle_error(TSParser *parser) {
|
|||
*/
|
||||
if (parser->lookahead->symbol == ts_builtin_sym_end) {
|
||||
DEBUG("fail_to_recover");
|
||||
reduce_error(parser, error_token_count - 1);
|
||||
return 0;
|
||||
reduce_error(parser, head, error_token_count - 1);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static TSTree *finish(TSParser *parser) {
|
||||
return reduce_helper(parser, ts_builtin_sym_document, -1, false, true);
|
||||
return reduce_helper(parser, 0, ts_builtin_sym_document, -1, false, true);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -301,6 +330,89 @@ void ts_parser_set_debugger(TSParser *parser, TSDebugger debugger) {
|
|||
parser->lexer.debugger = debugger;
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
ParserNextResultNone,
|
||||
ParserNextResultAdvanced,
|
||||
ParserNextResultRemoved,
|
||||
ParserNextResultFinished
|
||||
} ParserNextResult;
|
||||
|
||||
ParserNextResult ts_parser_next(TSParser *parser, int head_to_advance) {
|
||||
TSStateId state = ts_parse_stack_top_state(parser->stack, head_to_advance);
|
||||
const TSParseAction *next_action = get_actions(parser->language, state, parser->lookahead->symbol);
|
||||
int head, next_head = head_to_advance;
|
||||
|
||||
ParserNextResult result = ParserNextResultNone;
|
||||
|
||||
while (next_action) {
|
||||
TSParseAction action = *next_action;
|
||||
head = next_head;
|
||||
|
||||
next_action++;
|
||||
if (next_action->type == 0) {
|
||||
next_action = NULL;
|
||||
} else {
|
||||
next_head = ts_parse_stack_split(parser->stack, head);
|
||||
DEBUG("split head:%d, created:%d", head, next_head);
|
||||
}
|
||||
|
||||
DEBUG("iteration state:%d, head:%d", state, head);
|
||||
|
||||
// TODO: Remove this by making a separate symbol for errors returned from
|
||||
// the lexer.
|
||||
if (parser->lookahead->symbol == ts_builtin_sym_error)
|
||||
action.type = TSParseActionTypeError;
|
||||
|
||||
switch (action.type) {
|
||||
case TSParseActionTypeError:
|
||||
DEBUG("error_sym");
|
||||
if (ts_parse_stack_head_count(parser->stack) == 1) {
|
||||
if (handle_error(parser, head))
|
||||
break;
|
||||
else
|
||||
return ParserNextResultFinished;
|
||||
} else {
|
||||
DEBUG("bail head:%d", head);
|
||||
ts_parse_stack_remove_head(parser->stack, head);
|
||||
return ParserNextResultRemoved;
|
||||
}
|
||||
|
||||
case TSParseActionTypeShift:
|
||||
DEBUG("shift state:%u", action.data.to_state);
|
||||
shift(parser, head, action.data.to_state);
|
||||
result = ParserNextResultAdvanced;
|
||||
break;
|
||||
|
||||
case TSParseActionTypeShiftExtra:
|
||||
DEBUG("shift_extra");
|
||||
shift_extra(parser, head, state);
|
||||
result = ParserNextResultAdvanced;
|
||||
break;
|
||||
|
||||
case TSParseActionTypeReduce:
|
||||
DEBUG("reduce sym:%s, count:%u", SYM_NAME(action.data.symbol), action.data.child_count);
|
||||
reduce(parser, head, action.data.symbol, action.data.child_count);
|
||||
break;
|
||||
|
||||
case TSParseActionTypeReduceExtra:
|
||||
DEBUG("reduce_extra sym:%s", SYM_NAME(action.data.symbol));
|
||||
reduce_extra(parser, head, action.data.symbol);
|
||||
break;
|
||||
|
||||
case TSParseActionTypeReduceFragile:
|
||||
DEBUG("reduce_fragile sym:%s, count:%u", SYM_NAME(action.data.symbol), action.data.child_count);
|
||||
reduce_fragile(parser, head, action.data.symbol, action.data.child_count);
|
||||
break;
|
||||
|
||||
case TSParseActionTypeAccept:
|
||||
DEBUG("accept");
|
||||
return ParserNextResultFinished;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const TSTree *ts_parser_parse(TSParser *parser, TSInput input,
|
||||
TSInputEdit *edit) {
|
||||
TSLength position;
|
||||
|
|
@ -314,61 +426,37 @@ const TSTree *ts_parser_parse(TSParser *parser, TSInput input,
|
|||
position = ts_length_zero();
|
||||
}
|
||||
|
||||
parser->lookahead = NULL;
|
||||
parser->lexer.input = input;
|
||||
ts_lexer_reset(&parser->lexer, position);
|
||||
|
||||
for (;;) {
|
||||
TSStateId state = ts_parse_stack_top_state(parser->stack, 0);
|
||||
if (!parser->lookahead)
|
||||
parser->lookahead = get_next_node(parser, parser->language->lex_states[state]);
|
||||
TSParseAction action = get_action(parser->language, state, parser->lookahead->symbol);
|
||||
parser->lookahead = get_next_node(parser, parser->language->lex_states[state]);
|
||||
|
||||
DEBUG("lookahead state:%d, sym:%s", state, SYM_NAME(parser->lookahead->symbol));
|
||||
switch (action.type) {
|
||||
case TSParseActionTypeShift:
|
||||
if (parser->lookahead->symbol == ts_builtin_sym_error) {
|
||||
DEBUG("error_sym");
|
||||
if (!handle_error(parser))
|
||||
DEBUG("lookahead sym:%s", SYM_NAME(parser->lookahead->symbol));
|
||||
DEBUG("head_count: %d", ts_parse_stack_head_count(parser->stack));
|
||||
|
||||
int head = 0;
|
||||
while (head < ts_parse_stack_head_count(parser->stack)) {
|
||||
bool removed = false, advanced = false;
|
||||
|
||||
while (!(advanced || removed)) {
|
||||
switch (ts_parser_next(parser, head)) {
|
||||
case ParserNextResultNone:
|
||||
break;
|
||||
case ParserNextResultRemoved:
|
||||
removed = true;
|
||||
break;
|
||||
case ParserNextResultAdvanced:
|
||||
advanced = true;
|
||||
break;
|
||||
case ParserNextResultFinished:
|
||||
return finish(parser);
|
||||
} else {
|
||||
DEBUG("shift state:%u", action.data.to_state);
|
||||
shift(parser, action.data.to_state);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case TSParseActionTypeShiftExtra:
|
||||
DEBUG("shift_extra");
|
||||
shift_extra(parser, state);
|
||||
break;
|
||||
|
||||
case TSParseActionTypeReduce:
|
||||
DEBUG("reduce sym:%s, count:%u", SYM_NAME(action.data.symbol), action.data.child_count);
|
||||
reduce(parser, action.data.symbol, action.data.child_count);
|
||||
break;
|
||||
|
||||
case TSParseActionTypeReduceExtra:
|
||||
DEBUG("reduce_extra sym:%s", SYM_NAME(action.data.symbol));
|
||||
reduce_extra(parser, action.data.symbol);
|
||||
break;
|
||||
|
||||
case TSParseActionTypeReduceFragile:
|
||||
DEBUG("reduce_fragile sym:%s, count:%u", SYM_NAME(action.data.symbol), action.data.child_count);
|
||||
reduce_fragile(parser, action.data.symbol, action.data.child_count);
|
||||
break;
|
||||
|
||||
case TSParseActionTypeAccept:
|
||||
DEBUG("accept");
|
||||
return finish(parser);
|
||||
|
||||
case TSParseActionTypeError:
|
||||
DEBUG("error_sym");
|
||||
if (!handle_error(parser))
|
||||
return finish(parser);
|
||||
break;
|
||||
|
||||
default:
|
||||
return NULL;
|
||||
if (!removed)
|
||||
head++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
|
@ -120,21 +121,44 @@ TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
|
|||
return result;
|
||||
}
|
||||
|
||||
TSTree *ts_tree_make_ambiguity(size_t alternative_count, TSTree **alternatives) {
|
||||
TSTree *ts_tree_make_ambiguity(TSTree *left, TSTree *right) {
|
||||
TSTree *result = malloc(sizeof(TSTree));
|
||||
TSTree **alternatives = malloc(2 * sizeof(TSTree *));
|
||||
alternatives[0] = left;
|
||||
alternatives[1] = right;
|
||||
ts_tree_retain(left);
|
||||
ts_tree_retain(right);
|
||||
*result = (TSTree) { .ref_count = 1,
|
||||
.symbol = ts_builtin_sym_ambiguity,
|
||||
.size = alternatives[0]->size,
|
||||
.padding = alternatives[0]->padding,
|
||||
.child_count = alternative_count,
|
||||
.child_count = 2,
|
||||
.children = alternatives,
|
||||
.options = 0 };
|
||||
return result;
|
||||
}
|
||||
|
||||
void ts_tree_retain(TSTree *tree) { tree->ref_count++; }
|
||||
TSTree *ts_tree_add_alternative(TSTree *left, TSTree *right) {
|
||||
if (left->symbol == ts_builtin_sym_ambiguity) {
|
||||
size_t index = left->child_count++;
|
||||
left->children = realloc(left->children, left->child_count * sizeof(TSTree *));
|
||||
left->children[index] = right;
|
||||
ts_tree_retain(right);
|
||||
return left;
|
||||
} else {
|
||||
TSTree *result = ts_tree_make_ambiguity(left, right);
|
||||
ts_tree_release(left);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
void ts_tree_retain(TSTree *tree) {
|
||||
assert(tree->ref_count > 0);
|
||||
tree->ref_count++;
|
||||
}
|
||||
|
||||
void ts_tree_release(TSTree *tree) {
|
||||
assert(tree->ref_count > 0);
|
||||
tree->ref_count--;
|
||||
if (tree->ref_count == 0) {
|
||||
size_t count;
|
||||
|
|
@ -228,10 +252,13 @@ static size_t tree_write_to_string(const TSTree *tree, const char **symbol_names
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
for (size_t i = 0; i < tree->child_count; i++) {
|
||||
if (tree->symbol == ts_builtin_sym_ambiguity)
|
||||
cursor += snprintf(*writer, limit, " (ALTERNATIVE");
|
||||
TSTree *child = tree->children[i];
|
||||
cursor += tree_write_to_string(child, symbol_names, *writer, limit, 0);
|
||||
if (tree->symbol == ts_builtin_sym_ambiguity)
|
||||
cursor += snprintf(*writer, limit, ")");
|
||||
}
|
||||
|
||||
if (visible)
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ static inline bool ts_tree_is_fragile_right(TSTree *tree) {
|
|||
TSTree *ts_tree_make_leaf(TSSymbol, TSLength, TSLength, bool);
|
||||
TSTree *ts_tree_make_node(TSSymbol, size_t, TSTree **, bool);
|
||||
TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char);
|
||||
TSTree *ts_tree_make_ambiguity(size_t, TSTree **);
|
||||
TSTree *ts_tree_make_ambiguity(TSTree *, TSTree *);
|
||||
void ts_tree_retain(TSTree *tree);
|
||||
void ts_tree_release(TSTree *tree);
|
||||
bool ts_tree_eq(const TSTree *tree1, const TSTree *tree2);
|
||||
|
|
@ -85,6 +85,7 @@ char *ts_tree_error_string(const TSTree *tree, const char **names);
|
|||
TSTree **ts_tree_children(const TSTree *tree, size_t *count);
|
||||
TSTreeChild *ts_tree_visible_children(const TSTree *tree, size_t *count);
|
||||
TSLength ts_tree_total_size(const TSTree *tree);
|
||||
TSTree *ts_tree_add_alternative(TSTree *tree, TSTree *alternative);
|
||||
|
||||
static inline bool ts_tree_is_empty(TSTree *tree) {
|
||||
return ts_tree_total_size(tree).bytes == 0;
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ static inline void tree_vector_push(TreeVector *this, TSTree *tree) {
|
|||
this->capacity += 4;
|
||||
this->contents = realloc(this->contents, this->capacity * sizeof(TSTree *));
|
||||
}
|
||||
ts_tree_retain(tree);
|
||||
this->contents[this->size++] = tree;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue