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) ⇒ Object



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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'ext/tree_sitter/language.c', line 67

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);
  const char *err = dlerror();
  if (err != NULL) {
    rb_raise(rb_eRuntimeError,
             "Could not load shared library `%s'.\nReason: %s", path_cstr, err);
  }

  char buf[256];
  snprintf(buf, sizeof(buf), "tree_sitter_%s", StringValueCStr(name));
  tree_sitter_lang *make_ts_language = dlsym(lib, buf);
  err = dlerror();
  if (err != NULL) {
    dlclose(lib);
    rb_raise(rb_eRuntimeError,
             "Could not load symbol `%s' from library `%s'.\nReason:%s",
             StringValueCStr(name), StringValueCStr(path), err);
  }

  TSLanguage *lang = make_ts_language();
  if (lang == NULL) {
    dlclose(lib);
    rb_raise(rb_eRuntimeError,
             "TSLanguage = NULL for language `%s' in library `%s'.\nCall your "
             "local TSLanguage supplier.",
             StringValueCStr(name), StringValueCStr(path));
  }

  uint32_t version = ts_language_version(lang);
  if (version < TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION) {
    rb_raise(rb_eRuntimeError,
             "Language %s (v%d) from `%s' is old.\nMinimum supported ABI: "
             "v%d.\nCurrent ABI: v%d.",
             StringValueCStr(name), version, StringValueCStr(path),
             TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION,
             TREE_SITTER_LANGUAGE_VERSION);
  }

  return new_language(lang);
}

Instance Method Details

#==(other) ⇒ Object



110
111
112
113
114
# File 'ext/tree_sitter/language.c', line 110

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

#field_countObject



44
45
46
# File 'ext/tree_sitter/language.c', line 44

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

#field_id_for_name(name) ⇒ Object



52
53
54
55
56
57
# File 'ext/tree_sitter/language.c', line 52

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) ⇒ Object



48
49
50
# File 'ext/tree_sitter/language.c', line 48

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)));
}

#symbol_countObject

Class methods



28
29
30
# File 'ext/tree_sitter/language.c', line 28

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

#symbol_for_name(string, is_named) ⇒ Object



36
37
38
39
40
41
42
# File 'ext/tree_sitter/language.c', line 36

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) ⇒ Object



32
33
34
# File 'ext/tree_sitter/language.c', line 32

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

#symbol_type(symbol) ⇒ Object



59
60
61
# File 'ext/tree_sitter/language.c', line 59

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

#versionObject



63
64
65
# File 'ext/tree_sitter/language.c', line 63

static VALUE language_version(VALUE self) {
  return UINT2NUM(ts_language_version(SELF));
}