2018-05-10 22:22:37 -07:00
|
|
|
#ifndef RUNTIME_SUBTREE_H_
|
|
|
|
|
#define RUNTIME_SUBTREE_H_
|
2018-05-10 15:11:14 -07:00
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
2018-05-17 17:59:50 -07:00
|
|
|
#include <stdio.h>
|
2018-05-10 15:11:14 -07:00
|
|
|
#include "runtime/length.h"
|
|
|
|
|
#include "runtime/array.h"
|
2018-05-17 17:59:50 -07:00
|
|
|
#include "tree_sitter/runtime.h"
|
|
|
|
|
#include "tree_sitter/parser.h"
|
2018-05-10 15:11:14 -07:00
|
|
|
|
|
|
|
|
extern TSStateId TS_TREE_STATE_NONE;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
union {
|
|
|
|
|
char *long_data;
|
2018-09-14 11:02:11 -07:00
|
|
|
char short_data[24];
|
2018-05-10 15:11:14 -07:00
|
|
|
};
|
|
|
|
|
uint32_t length;
|
2018-05-11 12:57:41 -07:00
|
|
|
} ExternalScannerState;
|
2018-05-10 15:11:14 -07:00
|
|
|
|
|
|
|
|
typedef struct Subtree Subtree;
|
|
|
|
|
|
|
|
|
|
struct Subtree {
|
2018-09-14 11:02:11 -07:00
|
|
|
volatile uint32_t ref_count;
|
2018-05-10 15:11:14 -07:00
|
|
|
Length padding;
|
|
|
|
|
Length size;
|
|
|
|
|
uint32_t bytes_scanned;
|
|
|
|
|
uint32_t error_cost;
|
2018-08-31 10:39:14 -07:00
|
|
|
uint32_t child_count;
|
2018-09-14 11:02:11 -07:00
|
|
|
TSSymbol symbol;
|
|
|
|
|
TSStateId parse_state;
|
2018-05-10 15:11:14 -07:00
|
|
|
|
2018-09-13 16:13:49 -07:00
|
|
|
bool is_small : 1;
|
2018-05-10 15:11:14 -07:00
|
|
|
bool visible : 1;
|
|
|
|
|
bool named : 1;
|
|
|
|
|
bool extra : 1;
|
|
|
|
|
bool fragile_left : 1;
|
|
|
|
|
bool fragile_right : 1;
|
|
|
|
|
bool has_changes : 1;
|
|
|
|
|
bool has_external_tokens : 1;
|
|
|
|
|
bool is_missing : 1;
|
2018-06-15 12:25:17 -07:00
|
|
|
bool is_keyword : 1;
|
2018-05-10 15:11:14 -07:00
|
|
|
|
|
|
|
|
union {
|
2018-08-31 10:39:14 -07:00
|
|
|
// Non-terminal subtrees (`child_count > 0`)
|
2018-05-10 15:11:14 -07:00
|
|
|
struct {
|
2018-08-31 10:39:14 -07:00
|
|
|
const Subtree **children;
|
2018-05-10 15:11:14 -07:00
|
|
|
uint32_t visible_child_count;
|
|
|
|
|
uint32_t named_child_count;
|
2018-09-14 11:02:11 -07:00
|
|
|
uint32_t node_count;
|
2018-09-13 16:13:49 -07:00
|
|
|
uint32_t repeat_depth;
|
2018-09-14 11:02:11 -07:00
|
|
|
int32_t dynamic_precedence;
|
2018-05-10 15:11:14 -07:00
|
|
|
uint16_t alias_sequence_id;
|
2018-09-14 23:08:15 -07:00
|
|
|
struct {
|
|
|
|
|
TSSymbol symbol;
|
|
|
|
|
TSStateId parse_state;
|
|
|
|
|
} first_leaf;
|
2018-05-10 15:11:14 -07:00
|
|
|
};
|
2018-08-31 10:39:14 -07:00
|
|
|
|
2018-09-14 11:02:11 -07:00
|
|
|
// External terminal subtrees (`child_count == 0 && has_external_tokens`)
|
2018-08-31 10:39:14 -07:00
|
|
|
ExternalScannerState external_scanner_state;
|
2018-08-31 10:39:14 -07:00
|
|
|
|
|
|
|
|
// Error terminal subtrees (`child_count == 0 && symbol == ts_builtin_sym_error`)
|
2018-08-31 10:39:14 -07:00
|
|
|
int32_t lookahead_char;
|
2018-05-10 15:11:14 -07:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-31 13:30:59 -07:00
|
|
|
typedef Array(const Subtree *) SubtreeArray;
|
|
|
|
|
typedef Array(Subtree *) MutableSubtreeArray;
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
typedef struct {
|
2018-05-11 15:06:13 -07:00
|
|
|
MutableSubtreeArray free_trees;
|
2018-09-13 16:13:49 -07:00
|
|
|
MutableSubtreeArray free_small_trees;
|
2018-05-11 15:06:13 -07:00
|
|
|
MutableSubtreeArray tree_stack;
|
2018-05-10 15:11:14 -07:00
|
|
|
} SubtreePool;
|
|
|
|
|
|
2018-05-11 12:57:41 -07:00
|
|
|
void ts_external_scanner_state_init(ExternalScannerState *, const char *, unsigned);
|
|
|
|
|
const char *ts_external_scanner_state_data(const ExternalScannerState *);
|
2018-05-10 15:11:14 -07:00
|
|
|
|
2018-06-15 17:07:35 -07:00
|
|
|
void ts_subtree_array_copy(SubtreeArray, SubtreeArray *);
|
2018-05-10 15:11:14 -07:00
|
|
|
void ts_subtree_array_delete(SubtreePool *, SubtreeArray *);
|
|
|
|
|
SubtreeArray ts_subtree_array_remove_trailing_extras(SubtreeArray *);
|
|
|
|
|
void ts_subtree_array_reverse(SubtreeArray *);
|
|
|
|
|
|
2018-05-10 22:22:37 -07:00
|
|
|
SubtreePool ts_subtree_pool_new(uint32_t capacity);
|
2018-05-10 15:11:14 -07:00
|
|
|
void ts_subtree_pool_delete(SubtreePool *);
|
2018-09-13 16:13:49 -07:00
|
|
|
Subtree *ts_subtree_pool_allocate(SubtreePool *, bool);
|
2018-05-10 15:11:14 -07:00
|
|
|
void ts_subtree_pool_free(SubtreePool *, Subtree *);
|
|
|
|
|
|
2018-09-15 00:08:47 -07:00
|
|
|
Subtree *ts_subtree_new_leaf(
|
|
|
|
|
SubtreePool *, TSSymbol, Length, Length, uint32_t,
|
|
|
|
|
TSStateId, bool, bool, const TSLanguage *
|
|
|
|
|
);
|
2018-05-11 13:02:12 -07:00
|
|
|
Subtree *ts_subtree_new_node(SubtreePool *, TSSymbol, SubtreeArray *, unsigned, const TSLanguage *);
|
2018-05-11 15:06:13 -07:00
|
|
|
Subtree *ts_subtree_new_copy(SubtreePool *, const Subtree *);
|
2018-05-11 13:02:12 -07:00
|
|
|
Subtree *ts_subtree_new_error_node(SubtreePool *, SubtreeArray *, const TSLanguage *);
|
|
|
|
|
Subtree *ts_subtree_new_error(SubtreePool *, Length, Length, int32_t, const TSLanguage *);
|
2018-09-11 17:25:28 -07:00
|
|
|
Subtree *ts_subtree_new_missing_leaf(SubtreePool *, TSSymbol, Length, const TSLanguage *);
|
2018-05-11 15:06:13 -07:00
|
|
|
Subtree *ts_subtree_make_mut(SubtreePool *, const Subtree *);
|
|
|
|
|
void ts_subtree_retain(const Subtree *tree);
|
|
|
|
|
void ts_subtree_release(SubtreePool *, const Subtree *tree);
|
2018-05-10 15:11:14 -07:00
|
|
|
bool ts_subtree_eq(const Subtree *tree1, const Subtree *tree2);
|
|
|
|
|
int ts_subtree_compare(const Subtree *tree1, const Subtree *tree2);
|
2018-08-31 10:39:14 -07:00
|
|
|
void ts_subtree_set_children(Subtree *, const Subtree **, uint32_t, const TSLanguage *);
|
2018-05-11 15:06:13 -07:00
|
|
|
void ts_subtree_balance(const Subtree *, SubtreePool *, const TSLanguage *);
|
|
|
|
|
const Subtree *ts_subtree_edit(const Subtree *, const TSInputEdit *edit, SubtreePool *);
|
2018-05-10 15:11:14 -07:00
|
|
|
char *ts_subtree_string(const Subtree *, const TSLanguage *, bool include_all);
|
|
|
|
|
void ts_subtree_print_dot_graph(const Subtree *, const TSLanguage *, FILE *);
|
2018-05-11 15:06:13 -07:00
|
|
|
const Subtree *ts_subtree_last_external_token(const Subtree *);
|
2018-05-11 12:57:41 -07:00
|
|
|
bool ts_subtree_external_scanner_state_eq(const Subtree *, const Subtree *);
|
2018-05-10 15:11:14 -07:00
|
|
|
|
|
|
|
|
static inline uint32_t ts_subtree_total_bytes(const Subtree *self) {
|
|
|
|
|
return self->padding.bytes + self->size.bytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline Length ts_subtree_total_size(const Subtree *self) {
|
|
|
|
|
return length_add(self->padding, self->size);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 23:08:15 -07:00
|
|
|
static inline TSSymbol ts_subtree_leaf_symbol(const Subtree *self) {
|
|
|
|
|
return self->child_count > 0 ? self->first_leaf.symbol : self->symbol;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-05-10 22:22:37 -07:00
|
|
|
#endif // RUNTIME_SUBTREE_H_
|