Class: TreeSitter::Query

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

Instance Method Summary collapse

Constructor Details

#initialize(language, source) ⇒ Object

Class methods



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'ext/tree_sitter/query.c', line 9

static VALUE query_initialize(VALUE self, VALUE language, VALUE source) {
  TSLanguage *lang = value_to_language(language);
  const char *src = StringValuePtr(source);
  uint32_t len = (uint32_t)RSTRING_LEN(source);
  uint32_t error_offset = 0;
  TSQueryError error_type;

  TSQuery *res = ts_query_new(lang, src, len, &error_offset, &error_type);

  if (res == NULL || error_offset > 0) {
    rb_raise(rb_eRuntimeError, "Could not create query: TSQueryError%s",
             query_error_str(error_type));
  } else {
    SELF = res;
  }

  return self;
}

Instance Method Details

#capture_countObject



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

static VALUE query_capture_count(VALUE self) {
  return UINT2NUM(ts_query_capture_count(SELF));
}

#capture_name_for_id(id) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/tree_sitter/query.c', line 72

static VALUE query_capture_name_for_id(VALUE self, VALUE id) {
  const TSQuery *query = SELF;
  uint32_t index = NUM2UINT(id);
  uint32_t range = ts_query_capture_count(query);

  if (index >= range) {
    rb_raise(rb_eIndexError, "Index %d out of range (len = %d)", index, range);
  } else {
    uint32_t length;
    const char *name = ts_query_capture_name_for_id(query, index, &length);
    return safe_str2(name, length);
  }
}

#capture_quantifier_for_id(id, capture_id) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'ext/tree_sitter/query.c', line 86

static VALUE query_capture_quantifier_for_id(VALUE self, VALUE id,
                                             VALUE capture_id) {
  const TSQuery *query = SELF;
  uint32_t pattern = NUM2UINT(id);
  uint32_t index = NUM2UINT(capture_id);
  uint32_t range = ts_query_capture_count(query);

  if (index >= range) {
    rb_raise(rb_eIndexError, "Capture ID %d out of range (len = %d)", index,
             range);
  } else {
    return UINT2NUM(ts_query_capture_quantifier_for_id(query, pattern, index));
  }
}

#disable_capture(capture) ⇒ Object



115
116
117
118
119
120
# File 'ext/tree_sitter/query.c', line 115

static VALUE query_disable_capture(VALUE self, VALUE capture) {
  const char *cap = StringValuePtr(capture);
  uint32_t length = (uint32_t)RSTRING_LEN(capture);
  ts_query_disable_capture(SELF, cap, length);
  return Qnil;
}

#disable_pattern(pattern) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'ext/tree_sitter/query.c', line 122

static VALUE query_disable_pattern(VALUE self, VALUE pattern) {
  TSQuery *query = SELF;
  uint32_t index = NUM2UINT(pattern);
  uint32_t range = ts_query_pattern_count(query);

  if (index >= range) {
    rb_raise(rb_eIndexError, "Index %d out of range (len = %d)", index, range);
  } else {
    ts_query_disable_pattern(query, index);
    return Qnil;
  }
}

#pattern_countObject



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

static VALUE query_pattern_count(VALUE self) {
  return UINT2NUM(ts_query_pattern_count(SELF));
}

#pattern_guaranteed_at_step?(byte_offset) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'ext/tree_sitter/query.c', line 67

static VALUE query_pattern_guaranteed_at_step(VALUE self, VALUE byte_offset) {
  return UINT2NUM(
      ts_query_is_pattern_guaranteed_at_step(SELF, NUM2UINT(byte_offset)));
}

#predicates_for_pattern(pattern_index) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'ext/tree_sitter/query.c', line 52

static VALUE query_predicates_for_pattern(VALUE self, VALUE pattern_index) {
  const TSQuery *query = SELF;
  uint32_t index = NUM2UINT(pattern_index);
  uint32_t length;
  const TSQueryPredicateStep *steps =
      ts_query_predicates_for_pattern(query, index, &length);
  VALUE res = rb_ary_new_capa(length);

  for (uint32_t i = 0; i < length; i++) {
    rb_ary_push(res, new_query_predicate_step(&steps[i]));
  }

  return res;
}

#start_byte_for_pattern(pattern_index) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'ext/tree_sitter/query.c', line 40

static VALUE query_start_byte_for_pattern(VALUE self, VALUE pattern_index) {
  const TSQuery *query = SELF;
  uint32_t index = NUM2UINT(pattern_index);
  uint32_t range = ts_query_pattern_count(query);

  if (index >= range) {
    rb_raise(rb_eIndexError, "Index %d out of range (len = %d)", index, range);
  } else {
    return UINT2NUM(ts_query_start_byte_for_pattern(SELF, index));
  }
}

#string_countObject



36
37
38
# File 'ext/tree_sitter/query.c', line 36

static VALUE query_string_count(VALUE self) {
  return UINT2NUM(ts_query_string_count(SELF));
}

#string_value_for_id(id) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'ext/tree_sitter/query.c', line 101

static VALUE query_string_value_for_id(VALUE self, VALUE id) {
  const TSQuery *query = SELF;
  uint32_t index = NUM2UINT(id);
  uint32_t range = ts_query_string_count(query);

  if (index >= range) {
    rb_raise(rb_eIndexError, "Index %d out of range (len = %d)", index, range);
  } else {
    uint32_t length;
    const char *string = ts_query_string_value_for_id(query, index, &length);
    return safe_str2(string, length);
  }
}