Class: TreeSitter::Language

Inherits:
Object
  • Object
show all
Defined in:
ext/tree_sitter/language.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load(name, path) ⇒ Language

Load a language parser from disk.

with this gem.

Parameters:

  • name (String)

    the parser’s name.

  • path (String, Pathname)

    the parser’s shared library (so, dylib) path on disk.

Returns:

Raises:

  • (RuntimeError)

    if the parser was not found, or if it’s incompatible



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'ext/tree_sitter/language.c', line 40

static VALUE language_load(VALUE self, VALUE name, VALUE path) {
  VALUE path_s = rb_funcall(path, rb_intern("to_s"), 0);
  char *path_cstr = StringValueCStr(path_s);
  void *lib = dlopen(path_cstr, RTLD_NOW);
  if (lib == NULL) {
    const char *err = dlerror();
    VALUE parser_not_found = rb_const_get(mTreeSitter, rb_intern("ParserNotFoundError"));
    rb_raise(parser_not_found,
             "Could not load shared library `%s'.\nReason: %s", path_cstr, err ? err : "unknown error");
  }

  VALUE symbol_name = rb_sprintf("tree_sitter_%s", StringValueCStr(name));
  char *buf = StringValueCStr(symbol_name);

  // Clear any previous error before dlsym (POSIX requirement)
  dlerror();

  tree_sitter_lang *make_ts_language = dlsym(lib, buf);

  // Only check dlerror if dlsym returned NULL
  if (make_ts_language == NULL) {
    const char *err = dlerror();
    dlclose(lib);
    VALUE symbol_not_found = rb_const_get(mTreeSitter, rb_intern("SymbolNotFoundError"));
    rb_raise(symbol_not_found,
             "Could not load symbol `%s' from library `%s'.\nReason: %s",
             buf, path_cstr, err ? err : "symbol not found");
  }

  const TSLanguage *lang = make_ts_language();
  if (lang == NULL) {
    dlclose(lib);
    VALUE language_load_error = rb_const_get(mTreeSitter, rb_intern("LanguageLoadError"));
    rb_raise(language_load_error,
             "TSLanguage = NULL for language `%s' in library `%s'.\nCall your "
             "local TSLanguage supplier.",
             StringValueCStr(name), path_cstr);
  }

  // tree-sitter 0.26+ renamed ts_language_version to ts_language_abi_version
  uint32_t version = ts_language_abi_version(lang);
  if (version < TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION) {
    VALUE version_error = rb_const_get(mTreeSitter, rb_intern("ParserVersionError"));
    rb_raise(version_error,
             "Language %s (v%d) from `%s' is old.\nMinimum supported ABI: "
             "v%d.\nCurrent ABI: v%d.",
             StringValueCStr(name), version, path_cstr,
             TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION,
             TREE_SITTER_LANGUAGE_VERSION);
  }

  return new_language(lang);
}

Instance Method Details

#==(other) ⇒ Object

Operators



94
95
96
97
98
# File 'ext/tree_sitter/language.c', line 94

static VALUE language_equal(VALUE self, VALUE other) {
  TSLanguage *this = SELF;
  TSLanguage *that = unwrap(other)->data;
  return this == that ? Qtrue : Qfalse;
}

#abi_versionObject

Get the ABI version number for this language. This version number is used to ensure that languages were generated by a compatible version of Tree-sitter.

See Also:



204
205
206
207
# File 'ext/tree_sitter/language.c', line 204

static VALUE language_version(VALUE self) {
  // tree-sitter 0.26+ renamed ts_language_version to ts_language_abi_version
  return UINT2NUM(ts_language_abi_version(SELF));
}

#field_countInteger

Get the number of distinct field names in the language.

Returns:

  • (Integer)


105
106
107
# File 'ext/tree_sitter/language.c', line 105

static VALUE language_field_count(VALUE self) {
  return UINT2NUM(ts_language_field_count(SELF));
}

#field_id_for_name(name) ⇒ Integer

Get the numerical id for the given field name string.

Parameters:

  • name (String)

Returns:

  • (Integer)


116
117
118
119
120
121
# File 'ext/tree_sitter/language.c', line 116

static VALUE language_field_id_for_name(VALUE self, VALUE name) {
  TSLanguage *language = SELF;
  const char *str = StringValuePtr(name);
  uint32_t length = (uint32_t)RSTRING_LEN(name);
  return UINT2NUM(ts_language_field_id_for_name(language, str, length));
}

#field_name_for_id(field_id) ⇒ String

Get the field name string for the given numerical id.

Parameters:

  • field_id (Integer)

Returns:

  • (String)


130
131
132
# File 'ext/tree_sitter/language.c', line 130

static VALUE language_field_name_for_id(VALUE self, VALUE field_id) {
  return safe_str(ts_language_field_name_for_id(SELF, NUM2UINT(field_id)));
}

#next_state(state, symbol) ⇒ Object

Get the next parse state. Combine this with lookahead iterators to generate completion suggestions or valid symbols in error nodes. Use Node#grammar_symbol for valid symbols.



139
140
141
142
143
# File 'ext/tree_sitter/language.c', line 139

static VALUE language_next_state(VALUE self, VALUE state, VALUE symbol) {
  uint16_t sta = (uint16_t)NUM2UINT(state);
  uint16_t sym = (uint16_t)NUM2UINT(symbol);
  return UINT2NUM(ts_language_next_state(SELF, sta, sym));
}

#symbol_countInteger

Get the number of distinct node types in the language.

Returns:

  • (Integer)


150
151
152
# File 'ext/tree_sitter/language.c', line 150

static VALUE language_symbol_count(VALUE self) {
  return UINT2NUM(ts_language_symbol_count(SELF));
}

#symbol_for_name(string, is_named) ⇒ Integer

Get the numerical id for the given node type string.

Parameters:

  • string (Symbol)
  • is_named (Boolean)

Returns:

  • (Integer)


173
174
175
176
177
178
179
# File 'ext/tree_sitter/language.c', line 173

static VALUE language_symbol_for_name(VALUE self, VALUE string,
                                      VALUE is_named) {
  const char *str = rb_id2name(SYM2ID(string));
  uint32_t length = (uint32_t)strlen(str);
  bool named = RTEST(is_named);
  return UINT2NUM(ts_language_symbol_for_name(SELF, str, length, named));
}

#symbol_name(symbol) ⇒ String

Get a node type string for the given numerical id.

Parameters:

  • symbol (Integer)

Returns:

  • (String)


161
162
163
# File 'ext/tree_sitter/language.c', line 161

static VALUE language_symbol_name(VALUE self, VALUE symbol) {
  return safe_str(ts_language_symbol_name(SELF, NUM2UINT(symbol)));
}

#symbol_type(symbol) ⇒ SymbolType

Check whether the given node type id belongs to named nodes, anonymous nodes, or a hidden nodes.

Hidden nodes are never returned from the API.

Parameters:

  • symbol (Integer)

Returns:

See Also:



193
194
195
# File 'ext/tree_sitter/language.c', line 193

static VALUE language_symbol_type(VALUE self, VALUE symbol) {
  return new_symbol_type(ts_language_symbol_type(SELF, NUM2UINT(symbol)));
}

#versionObject

Get the ABI version number for this language. This version number is used to ensure that languages were generated by a compatible version of Tree-sitter.

See Also:



204
205
206
207
# File 'ext/tree_sitter/language.c', line 204

static VALUE language_version(VALUE self) {
  // tree-sitter 0.26+ renamed ts_language_version to ts_language_abi_version
  return UINT2NUM(ts_language_abi_version(SELF));
}