API extensions
This commit is contained in:
parent
08f4e82bb2
commit
c47e217e73
14 changed files with 741 additions and 29 deletions
|
|
@ -7,6 +7,10 @@ uint32_t ts_language_symbol_count(const TSLanguage *self) {
|
|||
return self->symbol_count + self->alias_count;
|
||||
}
|
||||
|
||||
uint32_t ts_language_state_count(const TSLanguage *self) {
|
||||
return self->state_count;
|
||||
}
|
||||
|
||||
uint32_t ts_language_version(const TSLanguage *self) {
|
||||
return self->version;
|
||||
}
|
||||
|
|
@ -56,6 +60,28 @@ TSSymbol ts_language_public_symbol(
|
|||
return self->public_symbol_map[symbol];
|
||||
}
|
||||
|
||||
TSStateId ts_language_next_state(
|
||||
const TSLanguage *self,
|
||||
TSStateId state,
|
||||
TSSymbol symbol
|
||||
) {
|
||||
if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) {
|
||||
return 0;
|
||||
} else if (symbol < self->token_count) {
|
||||
uint32_t count;
|
||||
const TSParseAction *actions = ts_language_actions(self, state, symbol, &count);
|
||||
if (count > 0) {
|
||||
TSParseAction action = actions[count - 1];
|
||||
if (action.type == TSParseActionTypeShift) {
|
||||
return action.shift.extra ? state : action.shift.state;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
return ts_language_lookup(self, state, symbol);
|
||||
}
|
||||
}
|
||||
|
||||
const char *ts_language_symbol_name(
|
||||
const TSLanguage *self,
|
||||
TSSymbol symbol
|
||||
|
|
@ -135,3 +161,43 @@ TSFieldId ts_language_field_id_for_name(
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
TSLookaheadIterator *ts_lookahead_iterator_new(const TSLanguage *self, TSStateId state) {
|
||||
if (state >= self->state_count) return NULL;
|
||||
LookaheadIterator *iterator = ts_malloc(sizeof(LookaheadIterator));
|
||||
*iterator = ts_language_lookaheads(self, state);
|
||||
return (TSLookaheadIterator *)iterator;
|
||||
}
|
||||
|
||||
void ts_lookahead_iterator_delete(TSLookaheadIterator *self) {
|
||||
ts_free(self);
|
||||
}
|
||||
|
||||
bool ts_lookahead_iterator_reset_state(TSLookaheadIterator * self, TSStateId state) {
|
||||
LookaheadIterator *iterator = (LookaheadIterator *)self;
|
||||
if (state >= iterator->language->state_count) return false;
|
||||
*iterator = ts_language_lookaheads(iterator->language, state);
|
||||
return true;
|
||||
}
|
||||
|
||||
const TSLanguage * ts_lookahead_iterator_language(const TSLookaheadIterator *self) {
|
||||
LookaheadIterator *iterator = (LookaheadIterator *)self;
|
||||
return iterator->language;
|
||||
}
|
||||
|
||||
bool ts_lookahead_iterator_reset(TSLookaheadIterator *self, const TSLanguage *language, TSStateId state) {
|
||||
if (state >= language->state_count) return false;
|
||||
LookaheadIterator *iterator = (LookaheadIterator *)self;
|
||||
*iterator = ts_language_lookaheads(language, state);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ts_lookahead_iterator_advance(TSLookaheadIterator *self) {
|
||||
LookaheadIterator *iterator = (LookaheadIterator *)self;
|
||||
return ts_lookahead_iterator_next(iterator);
|
||||
}
|
||||
|
||||
TSSymbol ts_lookahead_iterator_current_symbol(const TSLookaheadIterator *self) {
|
||||
LookaheadIterator *iterator = (LookaheadIterator *)self;
|
||||
return iterator->symbol;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,6 +38,8 @@ TSSymbolMetadata ts_language_symbol_metadata(const TSLanguage *, TSSymbol);
|
|||
|
||||
TSSymbol ts_language_public_symbol(const TSLanguage *, TSSymbol);
|
||||
|
||||
TSStateId ts_language_next_state(const TSLanguage *self, TSStateId state, TSSymbol symbol);
|
||||
|
||||
static inline bool ts_language_is_symbol_external(const TSLanguage *self, TSSymbol symbol) {
|
||||
return 0 < symbol && symbol < self->external_token_count + 1;
|
||||
}
|
||||
|
|
@ -178,28 +180,6 @@ static inline bool ts_lookahead_iterator_next(LookaheadIterator *self) {
|
|||
return true;
|
||||
}
|
||||
|
||||
static inline TSStateId ts_language_next_state(
|
||||
const TSLanguage *self,
|
||||
TSStateId state,
|
||||
TSSymbol symbol
|
||||
) {
|
||||
if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) {
|
||||
return 0;
|
||||
} else if (symbol < self->token_count) {
|
||||
uint32_t count;
|
||||
const TSParseAction *actions = ts_language_actions(self, state, symbol, &count);
|
||||
if (count > 0) {
|
||||
TSParseAction action = actions[count - 1];
|
||||
if (action.type == TSParseActionTypeShift) {
|
||||
return action.shift.extra ? state : action.shift.state;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
return ts_language_lookup(self, state, symbol);
|
||||
}
|
||||
}
|
||||
|
||||
// Whether the state is a "primary state". If this returns false, it indicates that there exists
|
||||
// another state that behaves identically to this one with respect to query analysis.
|
||||
static inline bool ts_language_state_is_primary(
|
||||
|
|
|
|||
|
|
@ -468,6 +468,15 @@ uint32_t ts_node_descendant_count(TSNode self) {
|
|||
return ts_subtree_visible_descendant_count(ts_node__subtree(self)) + 1;
|
||||
}
|
||||
|
||||
bool ts_node_is_error(TSNode self) {
|
||||
TSSymbol symbol = ts_node_symbol(self);
|
||||
return symbol == ts_builtin_sym_error;
|
||||
}
|
||||
|
||||
TSStateId ts_node_parse_state(TSNode self) {
|
||||
return ts_subtree_parse_state(ts_node__subtree(self));
|
||||
}
|
||||
|
||||
TSNode ts_node_parent(TSNode self) {
|
||||
TSNode node = ts_tree_root_node(self.tree);
|
||||
uint32_t end_byte = ts_node_end_byte(self);
|
||||
|
|
|
|||
|
|
@ -97,6 +97,40 @@ static inline bool ts_tree_cursor_child_iterator_next(
|
|||
return true;
|
||||
}
|
||||
|
||||
static inline bool ts_tree_cursor_child_iterator_previous(
|
||||
CursorChildIterator *self,
|
||||
TreeCursorEntry *result,
|
||||
bool *visible
|
||||
) {
|
||||
// this is mostly a reverse `ts_tree_cursor_child_iterator_next` taking into
|
||||
// account unsigned underflow
|
||||
if (!self->parent.ptr || (int8_t)self->child_index == -1) return false;
|
||||
const Subtree *child = &ts_subtree_children(self->parent)[self->child_index];
|
||||
*result = (TreeCursorEntry) {
|
||||
.subtree = child,
|
||||
.position = self->position,
|
||||
.child_index = self->child_index,
|
||||
.structural_child_index = self->structural_child_index,
|
||||
};
|
||||
*visible = ts_subtree_visible(*child);
|
||||
bool extra = ts_subtree_extra(*child);
|
||||
if (!extra && self->alias_sequence) {
|
||||
*visible |= self->alias_sequence[self->structural_child_index];
|
||||
self->structural_child_index--;
|
||||
}
|
||||
|
||||
self->position = length_sub(self->position, ts_subtree_padding(*child));
|
||||
self->child_index--;
|
||||
|
||||
// unsigned can underflow so compare it to child_count
|
||||
if (self->child_index < self->parent.ptr->child_count) {
|
||||
Subtree previous_child = ts_subtree_children(self->parent)[self->child_index];
|
||||
self->position = length_sub(self->position, ts_subtree_size(previous_child));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// TSTreeCursor - lifecycle
|
||||
|
||||
TSTreeCursor ts_tree_cursor_new(TSNode node) {
|
||||
|
|
@ -163,6 +197,47 @@ bool ts_tree_cursor_goto_first_child(TSTreeCursor *self) {
|
|||
return false;
|
||||
}
|
||||
|
||||
TreeCursorStep ts_tree_cursor_goto_last_child_internal(TSTreeCursor *_self) {
|
||||
TreeCursor *self = (TreeCursor *)_self;
|
||||
bool visible;
|
||||
TreeCursorEntry entry;
|
||||
CursorChildIterator iterator = ts_tree_cursor_iterate_children(self);
|
||||
if (!iterator.parent.ptr || iterator.parent.ptr->child_count == 0) return TreeCursorStepNone;
|
||||
|
||||
TreeCursorEntry last_entry;
|
||||
bool last_visible;
|
||||
while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) {
|
||||
if (visible) {
|
||||
last_entry = entry;
|
||||
last_visible = true;
|
||||
}
|
||||
else if (ts_subtree_visible_child_count(*entry.subtree) > 0) {
|
||||
last_entry = entry;
|
||||
last_visible = false;
|
||||
}
|
||||
}
|
||||
if (last_entry.subtree) {
|
||||
array_push(&self->stack, last_entry);
|
||||
return last_visible? TreeCursorStepVisible : TreeCursorStepHidden;
|
||||
}
|
||||
|
||||
return TreeCursorStepNone;
|
||||
}
|
||||
|
||||
bool ts_tree_cursor_goto_last_child(TSTreeCursor *self) {
|
||||
for (;;) {
|
||||
switch (ts_tree_cursor_goto_last_child_internal(self)) {
|
||||
case TreeCursorStepHidden:
|
||||
continue;
|
||||
case TreeCursorStepVisible:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline int64_t ts_tree_cursor_goto_first_child_for_byte_and_point(
|
||||
TSTreeCursor *_self,
|
||||
uint32_t goal_byte,
|
||||
|
|
@ -213,7 +288,9 @@ int64_t ts_tree_cursor_goto_first_child_for_point(TSTreeCursor *self, TSPoint go
|
|||
return ts_tree_cursor_goto_first_child_for_byte_and_point(self, 0, goal_point);
|
||||
}
|
||||
|
||||
TreeCursorStep ts_tree_cursor_goto_next_sibling_internal(TSTreeCursor *_self) {
|
||||
TreeCursorStep ts_tree_cursor_goto_sibling_internal(
|
||||
TSTreeCursor *_self,
|
||||
bool (*advance)(CursorChildIterator *, TreeCursorEntry *, bool *)) {
|
||||
TreeCursor *self = (TreeCursor *)_self;
|
||||
uint32_t initial_size = self->stack.size;
|
||||
|
||||
|
|
@ -226,10 +303,10 @@ TreeCursorStep ts_tree_cursor_goto_next_sibling_internal(TSTreeCursor *_self) {
|
|||
iterator.descendant_index = entry.descendant_index;
|
||||
|
||||
bool visible = false;
|
||||
ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible);
|
||||
advance(&iterator, &entry, &visible);
|
||||
if (visible && self->stack.size + 1 < initial_size) break;
|
||||
|
||||
while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) {
|
||||
while (advance(&iterator, &entry, &visible)) {
|
||||
if (visible) {
|
||||
array_push(&self->stack, entry);
|
||||
return TreeCursorStepVisible;
|
||||
|
|
@ -246,6 +323,10 @@ TreeCursorStep ts_tree_cursor_goto_next_sibling_internal(TSTreeCursor *_self) {
|
|||
return TreeCursorStepNone;
|
||||
}
|
||||
|
||||
TreeCursorStep ts_tree_cursor_goto_next_sibling_internal(TSTreeCursor *_self) {
|
||||
return ts_tree_cursor_goto_sibling_internal(_self, ts_tree_cursor_child_iterator_next);
|
||||
}
|
||||
|
||||
bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *self) {
|
||||
switch (ts_tree_cursor_goto_next_sibling_internal(self)) {
|
||||
case TreeCursorStepHidden:
|
||||
|
|
@ -258,6 +339,50 @@ bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *self) {
|
|||
}
|
||||
}
|
||||
|
||||
TreeCursorStep ts_tree_cursor_goto_previous_sibling_internal(TSTreeCursor *_self) {
|
||||
// since subtracting across row loses column information, we may have to
|
||||
// restore it
|
||||
TreeCursor *self = (TreeCursor *)_self;
|
||||
|
||||
// for that, save current position before traversing
|
||||
Length position = array_back(&self->stack)->position;
|
||||
TreeCursorStep step = ts_tree_cursor_goto_sibling_internal(
|
||||
_self, ts_tree_cursor_child_iterator_previous);
|
||||
if (step == TreeCursorStepNone)
|
||||
return step;
|
||||
|
||||
// if row has not changed, column is still valid
|
||||
if (array_back(&self->stack)->position.extent.row == position.extent.row)
|
||||
return step;
|
||||
|
||||
// restore position from the parent node
|
||||
const TreeCursorEntry *parent = &self->stack.contents[self->stack.size - 2];
|
||||
position = parent->position;
|
||||
uint32_t child_index = array_back(&self->stack)->child_index;
|
||||
const Subtree *children = ts_subtree_children((*(parent->subtree)));
|
||||
for (uint32_t i = 0; i < child_index; ++i) {
|
||||
position = length_add(position, ts_subtree_total_size(children[i]));
|
||||
}
|
||||
if (child_index > 0)
|
||||
position = length_add(position, ts_subtree_padding(children[child_index]));
|
||||
|
||||
array_back(&self->stack)->position = position;
|
||||
|
||||
return step;
|
||||
}
|
||||
|
||||
bool ts_tree_cursor_goto_previous_sibling(TSTreeCursor *self) {
|
||||
switch (ts_tree_cursor_goto_previous_sibling_internal(self)) {
|
||||
case TreeCursorStepHidden:
|
||||
ts_tree_cursor_goto_last_child(self);
|
||||
return true;
|
||||
case TreeCursorStepVisible:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ts_tree_cursor_goto_parent(TSTreeCursor *_self) {
|
||||
TreeCursor *self = (TreeCursor *)_self;
|
||||
for (unsigned i = self->stack.size - 2; i + 1 > 0; i--) {
|
||||
|
|
@ -556,3 +681,11 @@ TSTreeCursor ts_tree_cursor_copy(const TSTreeCursor *_cursor) {
|
|||
array_push_all(©->stack, &cursor->stack);
|
||||
return res;
|
||||
}
|
||||
|
||||
void ts_tree_cursor_reset_to(TSTreeCursor *_dst, const TSTreeCursor *_src) {
|
||||
const TreeCursor *cursor = (const TreeCursor *)_src;
|
||||
TreeCursor *copy = (TreeCursor *)_dst;
|
||||
copy->tree = cursor->tree;
|
||||
array_clear(©->stack);
|
||||
array_push_all(©->stack, &cursor->stack);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue