Skip to content

Commit

Permalink
feat(lib): add the language name
Browse files Browse the repository at this point in the history
  • Loading branch information
ObserverOfTime committed May 6, 2024
1 parent 6e6dcf1 commit ee7a88e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cli/src/generate/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const SMALL_STATE_THRESHOLD: usize = 64;
const ABI_VERSION_MIN: usize = 13;
const ABI_VERSION_MAX: usize = tree_sitter::LANGUAGE_VERSION;
const ABI_VERSION_WITH_PRIMARY_STATES: usize = 14;
const ABI_VERSION_WITH_NAMES: usize = 15;

macro_rules! add {
($this: tt, $($arg: tt)*) => {{
Expand Down Expand Up @@ -1438,6 +1439,9 @@ impl Generator {
if self.abi_version >= ABI_VERSION_WITH_PRIMARY_STATES {
add_line!(self, ".primary_state_ids = ts_primary_state_ids,");
}
if self.abi_version >= ABI_VERSION_WITH_NAMES {
add_line!(self, ".name = \"{}\",", self.language_name);
}

dedent!(self);
add_line!(self, "}};");
Expand Down
5 changes: 5 additions & 0 deletions lib/include/tree_sitter/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,11 @@ uint32_t ts_language_version(const TSLanguage *self);
*/
TSStateId ts_language_next_state(const TSLanguage *self, TSStateId state, TSSymbol symbol);

/**
* Get the name of this language. This returns `NULL` in older parsers.
*/
const char *ts_language_name(const TSLanguage *self);

/********************************/
/* Section - Lookahead Iterator */
/********************************/
Expand Down
4 changes: 4 additions & 0 deletions lib/src/language.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ uint32_t ts_language_version(const TSLanguage *self) {
return self->version;
}

const char *ts_language_name(const TSLanguage *self) {
return self->version >= LANGUAGE_VERSION_WITH_NAMES ? self->name : NULL;
}

uint32_t ts_language_field_count(const TSLanguage *self) {
return self->field_count;
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/language.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern "C" {

#define ts_builtin_sym_error_repeat (ts_builtin_sym_error - 1)

#define LANGUAGE_VERSION_WITH_NAMES 15
#define LANGUAGE_VERSION_WITH_PRIMARY_STATES 14
#define LANGUAGE_VERSION_USABLE_VIA_WASM 13

Expand Down
1 change: 1 addition & 0 deletions lib/src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ struct TSLanguage {
void (*deserialize)(void *, const char *, unsigned);
} external_scanner;
const TSStateId *primary_state_ids;
const char *name;
};

static inline bool set_contains(TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
Expand Down

0 comments on commit ee7a88e

Please sign in to comment.