Create ambiguity nodes when joining stack heads

This commit is contained in:
Max Brunsfeld 2015-05-28 15:06:39 -07:00
parent 1ca5f4ab15
commit 381f89f8ba
13 changed files with 130 additions and 72 deletions

View file

@ -69,6 +69,7 @@ class ParseTableBuilder {
parse_table.symbols.insert(rules::ERROR());
parse_table.symbols.insert(rules::DOCUMENT());
parse_table.symbols.insert(rules::AMBIGUITY());
return { parse_table, nullptr };
}

View file

@ -315,8 +315,12 @@ class CCodeGenerator {
return "ts_builtin_sym_error";
else if (symbol == rules::END_OF_INPUT())
return "ts_builtin_sym_end";
else
else if (symbol == rules::DOCUMENT())
return "ts_builtin_sym_document";
else if (symbol == rules::AMBIGUITY())
return "ts_builtin_sym_ambiguity";
else
return "";
} else {
string name = sanitize_name(rule_name(symbol));
if (symbol.is_auxiliary())

View file

@ -7,6 +7,7 @@ Symbol END_OF_INPUT() { return Symbol(-1, SymbolOptionToken); }
Symbol ERROR() { return Symbol(-2, SymbolOptionToken); }
Symbol START() { return Symbol(-3); }
Symbol DOCUMENT() { return Symbol(-4); }
Symbol AMBIGUITY() { return Symbol(-5); }
} // namespace rules
} // namespace tree_sitter

View file

@ -10,6 +10,7 @@ Symbol ERROR();
Symbol START();
Symbol END_OF_INPUT();
Symbol DOCUMENT();
Symbol AMBIGUITY();
} // namespace rules
} // namespace tree_sitter

View file

@ -1,6 +1,7 @@
#include "tree_sitter/parser.h"
#include "runtime/tree.h"
#include "runtime/parse_stack.h"
#include "runtime/length.h"
#include <assert.h>
static const size_t INITIAL_HEAD_CAPACITY = 3;
@ -90,13 +91,14 @@ bool ts_parse_stack_reduce(ParseStack *this, int head_index, TSStateId state,
}
TSTree *parent = ts_tree_make_node(symbol, child_count, children, false);
if (parse_stack_merge_head(this, head_index, state, parent)) {
ts_tree_release(parent);
return true;
}
stack_node_retain(next_node);
stack_node_release(this->heads[head_index]);
this->heads[head_index] = next_node;
if (parse_stack_merge_head(this, head_index, state, parent))
return true;
this->heads[head_index] = stack_node_new(next_node, state, parent);
return false;
}
@ -152,6 +154,9 @@ static bool stack_node_release(ParseStackNode *this) {
static void stack_node_add_successor(ParseStackNode *this, ParseStackNode *successor) {
stack_node_retain(successor);
for (int i = 0; i < this->successor_count; i++)
if (this->successors[i] == successor)
return;
this->successors[this->successor_count] = successor;
this->successor_count++;
}
@ -159,10 +164,23 @@ static void stack_node_add_successor(ParseStackNode *this, ParseStackNode *succe
static bool parse_stack_merge_head(ParseStack *this, int head_index, TSStateId state, TSTree *tree) {
for (int i = 0; i < head_index; i++) {
ParseStackNode *head = this->heads[i];
if (head->state == state && ts_tree_eq(head->tree, tree)) {
stack_node_add_successor(head, this->heads[head_index]);
parse_stack_remove_head(this, head_index);
return true;
if (head->state == state) {
if (head->tree == tree) {
stack_node_add_successor(head, this->heads[head_index]);
parse_stack_remove_head(this, head_index);
return true;
}
if (head->tree->symbol == tree->symbol &&
ts_length_eq(head->tree->size, tree->size)) {
TSTree **options = malloc(2 * sizeof(TSTree *));
options[0] = head->tree;
options[1] = tree;
head->tree = ts_tree_make_ambiguity(2, options);
stack_node_add_successor(head, this->heads[head_index]);
parse_stack_remove_head(this, head_index);
return true;
}
}
}
return false;

View file

@ -120,6 +120,18 @@ 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 *result = malloc(sizeof(TSTree));
*result = (TSTree) { .ref_count = 1,
.symbol = ts_builtin_sym_ambiguity,
.size = alternatives[0]->size,
.padding = alternatives[0]->padding,
.child_count = alternative_count,
.children = alternatives,
.options = 0 };
return result;
}
void ts_tree_retain(TSTree *tree) { tree->ref_count++; }
void ts_tree_release(TSTree *tree) {

View file

@ -76,6 +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 **);
void ts_tree_retain(TSTree *tree);
void ts_tree_release(TSTree *tree);
bool ts_tree_eq(const TSTree *tree1, const TSTree *tree2);