Class: TreeSitter::Document

Inherits:
Object
  • Object
show all
Defined in:
ext/tree-sitter/document.c

Instance Method Summary collapse

Constructor Details

#initialize(rb_input_string, rb_options) ⇒ Object

Public: Creates a new document



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'ext/tree-sitter/document.c', line 24

VALUE rb_document_new(VALUE self, VALUE rb_input_string, VALUE rb_options)
{
  TSDocument *document;
  VALUE rb_language;

  Check_Type(rb_input_string, T_STRING);
  Check_Type (rb_options, T_HASH);

  rb_language = rb_hash_aref(rb_options, CSTR2SYM("language"));
  Check_Type(rb_language, T_STRING);

  Data_Get_Struct(self, TSDocument, document);

  rb_document_set_language(self, rb_language);
  rb_document_set_input_string(self, rb_input_string);
  rb_document_parse(self);

  return self;
}

Instance Method Details

#input_string=(rb_input_string) ⇒ Object

Public: Set the document string.

string - A String identifying the document contents.

Returns nothing.



92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/tree-sitter/document.c', line 92

VALUE rb_document_set_input_string(VALUE self, VALUE rb_input_string)
{
  TSDocument *document;
  Check_Type(rb_input_string, T_STRING);
  char *c_string = StringValueCStr(rb_input_string);

  Data_Get_Struct(self, TSDocument, document);

  ts_document_set_input_string(document, c_string);

  return Qnil;
}

#language=(lang) ⇒ Object

Public: Set the language type of a document.

lang - A String identifying the language.

Returns nothing.



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
# File 'ext/tree-sitter/document.c', line 52

VALUE rb_document_set_language(VALUE self, VALUE lang)
{
  TSDocument *document;
  char *language_name;
  void *handle;
  char *error;
  Check_Type(lang, T_STRING);

  language_name = StringValueCStr(lang);

  const TSLanguage * (*language_func)();

  handle = dlopen(BUNDLE_PATH, RTLD_LAZY);
  if (!handle) {
    rb_raise(rb_eDocumentError, "%s", dlerror());
  }

  dlerror();    /* Clear any existing error */

  *(void **) (&language_func) = dlsym(handle, language_name);

  if ((error = dlerror()) != NULL)  {
    rb_raise(rb_eDocumentError, "%s", error);
  }

  Data_Get_Struct(self, TSDocument, document);

  ts_document_set_language(document, (*language_func)());
  dlclose(handle);

  return Qnil;
}

#parseObject

Public: Parses the document string.

Returns true if successful.



110
111
112
113
114
115
116
117
118
119
# File 'ext/tree-sitter/document.c', line 110

VALUE rb_document_parse(VALUE self)
{
  TSDocument *document;

  Data_Get_Struct(self, TSDocument, document);

  ts_document_parse(document);

  return Qtrue;
}

#root_nodeObject

Public: Returns the document root node.

Returns a Node.



126
127
128
129
130
131
132
133
134
# File 'ext/tree-sitter/document.c', line 126

VALUE rb_document_root_node(VALUE self)
{
  TSDocument *document;

  Data_Get_Struct(self, TSDocument, document);
  TSNode ts_node = ts_document_root_node(document);

  return rb_new_node(ts_node, document);
}