Add version number to TSLanguage structs
This commit is contained in:
parent
672d491775
commit
d853b6504d
6 changed files with 24 additions and 1 deletions
|
|
@ -65,6 +65,7 @@ typedef union {
|
|||
} TSParseActionEntry;
|
||||
|
||||
typedef struct TSLanguage {
|
||||
uint32_t version;
|
||||
uint32_t symbol_count;
|
||||
uint32_t token_count;
|
||||
uint32_t external_token_count;
|
||||
|
|
@ -166,6 +167,7 @@ typedef struct TSLanguage {
|
|||
|
||||
#define GET_LANGUAGE(...) \
|
||||
static TSLanguage language = { \
|
||||
.version = LANGUAGE_VERSION, \
|
||||
.symbol_count = SYMBOL_COUNT, \
|
||||
.token_count = TOKEN_COUNT, \
|
||||
.symbol_metadata = ts_symbol_metadata, \
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ extern "C" {
|
|||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define TREE_SITTER_LANGUAGE_VERSION 1
|
||||
|
||||
typedef unsigned short TSSymbol;
|
||||
typedef struct TSLanguage TSLanguage;
|
||||
typedef struct TSDocument TSDocument;
|
||||
|
|
@ -114,6 +116,7 @@ uint32_t ts_document_parse_count(const TSDocument *);
|
|||
|
||||
uint32_t ts_language_symbol_count(const TSLanguage *);
|
||||
const char *ts_language_symbol_name(const TSLanguage *, TSSymbol);
|
||||
uint32_t ts_language_version(const TSLanguage *);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -164,6 +164,17 @@ describe("Document", [&]() {
|
|||
"(program (expression_statement "
|
||||
"(object (pair (string) (array (number) (number))))))");
|
||||
});
|
||||
|
||||
it("does not allow setting a language with a different version number", [&]() {
|
||||
TSLanguage language = *get_test_language("json");
|
||||
AssertThat(ts_language_version(&language), Equals<uint32_t>(TREE_SITTER_LANGUAGE_VERSION));
|
||||
|
||||
language.version++;
|
||||
AssertThat(ts_language_version(&language), !Equals<uint32_t>(TREE_SITTER_LANGUAGE_VERSION));
|
||||
|
||||
ts_document_set_language(document, &language);
|
||||
AssertThat(ts_document_language(document), IsNull());
|
||||
});
|
||||
});
|
||||
|
||||
describe("set_logger(TSLogger)", [&]() {
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
#include "compiler/lexical_grammar.h"
|
||||
#include "compiler/rules/built_in_symbols.h"
|
||||
#include "compiler/util/string_helpers.h"
|
||||
#include "tree_sitter/runtime.h"
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace generate_code {
|
||||
|
|
@ -134,6 +135,7 @@ class CCodeGenerator {
|
|||
}
|
||||
}
|
||||
|
||||
line("#define LANGUAGE_VERSION " + to_string(TREE_SITTER_LANGUAGE_VERSION));
|
||||
line("#define STATE_COUNT " + to_string(parse_table.states.size()));
|
||||
line("#define SYMBOL_COUNT " + to_string(parse_table.symbols.size()));
|
||||
line("#define TOKEN_COUNT " + to_string(token_count));
|
||||
|
|
@ -227,7 +229,7 @@ class CCodeGenerator {
|
|||
for (size_t i = 0, n = lexical_grammar.variables.size(); i < n; i++) {
|
||||
for (size_t j = 0; j < syntax_grammar.external_tokens.size(); j++) {
|
||||
const ExternalToken &external_token = syntax_grammar.external_tokens[j];
|
||||
if (external_token.corresponding_internal_token.index == i) {
|
||||
if (external_token.corresponding_internal_token.index == Symbol::Index(i)) {
|
||||
external_tokens_by_corresponding_internal_token.insert({i, j});
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ const TSLanguage *ts_document_language(TSDocument *self) {
|
|||
}
|
||||
|
||||
void ts_document_set_language(TSDocument *self, const TSLanguage *language) {
|
||||
if (language->version != TREE_SITTER_LANGUAGE_VERSION) return;
|
||||
ts_document_invalidate(self);
|
||||
parser_set_language(&self->parser, language);
|
||||
if (self->tree) {
|
||||
|
|
|
|||
|
|
@ -34,6 +34,10 @@ uint32_t ts_language_symbol_count(const TSLanguage *language) {
|
|||
return language->symbol_count;
|
||||
}
|
||||
|
||||
uint32_t ts_language_version(const TSLanguage *language) {
|
||||
return language->version;
|
||||
}
|
||||
|
||||
TSSymbolMetadata ts_language_symbol_metadata(const TSLanguage *language,
|
||||
TSSymbol symbol) {
|
||||
if (symbol == ts_builtin_sym_error)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue