@yalehwang/archguard 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/README.md +33 -23
  2. package/dist/cli/analyze/normalize-to-diagrams.d.ts.map +1 -1
  3. package/dist/cli/analyze/normalize-to-diagrams.js +34 -4
  4. package/dist/cli/analyze/normalize-to-diagrams.js.map +1 -1
  5. package/dist/cli/analyze/run-analysis.d.ts.map +1 -1
  6. package/dist/cli/analyze/run-analysis.js +4 -1
  7. package/dist/cli/analyze/run-analysis.js.map +1 -1
  8. package/dist/cli/commands/mcp.d.ts.map +1 -1
  9. package/dist/cli/commands/mcp.js +2 -6
  10. package/dist/cli/commands/mcp.js.map +1 -1
  11. package/dist/cli/index.js +0 -0
  12. package/dist/cli/mcp/analyze-tool.d.ts +1 -5
  13. package/dist/cli/mcp/analyze-tool.d.ts.map +1 -1
  14. package/dist/cli/mcp/analyze-tool.js +48 -52
  15. package/dist/cli/mcp/analyze-tool.js.map +1 -1
  16. package/dist/cli/mcp/mcp-server.d.ts +4 -4
  17. package/dist/cli/mcp/mcp-server.d.ts.map +1 -1
  18. package/dist/cli/mcp/mcp-server.js +108 -57
  19. package/dist/cli/mcp/mcp-server.js.map +1 -1
  20. package/dist/cli/processors/arch-json-provider.d.ts.map +1 -1
  21. package/dist/cli/processors/arch-json-provider.js +8 -2
  22. package/dist/cli/processors/arch-json-provider.js.map +1 -1
  23. package/dist/cli/processors/diagram-output-router.d.ts.map +1 -1
  24. package/dist/cli/processors/diagram-output-router.js +1 -0
  25. package/dist/cli/processors/diagram-output-router.js.map +1 -1
  26. package/dist/cli/query/engine-loader.js +2 -2
  27. package/dist/cli/query/engine-loader.js.map +1 -1
  28. package/dist/plugins/golang/atlas/index.d.ts.map +1 -1
  29. package/dist/plugins/golang/atlas/index.js +3 -1
  30. package/dist/plugins/golang/atlas/index.js.map +1 -1
  31. package/dist/plugins/golang/atlas/types.d.ts +1 -0
  32. package/dist/plugins/golang/atlas/types.d.ts.map +1 -1
  33. package/dist/plugins/golang/index.d.ts.map +1 -1
  34. package/dist/plugins/golang/index.js +15 -6
  35. package/dist/plugins/golang/index.js.map +1 -1
  36. package/dist/plugins/golang/source-scope.d.ts +7 -0
  37. package/dist/plugins/golang/source-scope.d.ts.map +1 -0
  38. package/dist/plugins/golang/source-scope.js +83 -0
  39. package/dist/plugins/golang/source-scope.js.map +1 -0
  40. package/node_modules/node-addon-api/LICENSE.md +9 -0
  41. package/node_modules/node-addon-api/README.md +95 -0
  42. package/node_modules/node-addon-api/common.gypi +21 -0
  43. package/node_modules/node-addon-api/except.gypi +25 -0
  44. package/node_modules/node-addon-api/index.js +14 -0
  45. package/node_modules/node-addon-api/napi-inl.deprecated.h +186 -0
  46. package/node_modules/node-addon-api/napi-inl.h +7033 -0
  47. package/node_modules/node-addon-api/napi.h +3309 -0
  48. package/node_modules/node-addon-api/node_addon_api.gyp +42 -0
  49. package/node_modules/node-addon-api/node_api.gyp +9 -0
  50. package/node_modules/node-addon-api/noexcept.gypi +26 -0
  51. package/node_modules/node-addon-api/nothing.c +0 -0
  52. package/node_modules/node-addon-api/package-support.json +21 -0
  53. package/node_modules/node-addon-api/package.json +480 -0
  54. package/node_modules/node-addon-api/tools/README.md +73 -0
  55. package/node_modules/node-addon-api/tools/check-napi.js +99 -0
  56. package/node_modules/node-addon-api/tools/clang-format.js +71 -0
  57. package/node_modules/node-addon-api/tools/conversion.js +301 -0
  58. package/node_modules/node-gyp-build/LICENSE +21 -0
  59. package/node_modules/node-gyp-build/README.md +58 -0
  60. package/node_modules/node-gyp-build/SECURITY.md +5 -0
  61. package/node_modules/node-gyp-build/bin.js +84 -0
  62. package/node_modules/node-gyp-build/build-test.js +19 -0
  63. package/node_modules/node-gyp-build/index.js +6 -0
  64. package/node_modules/node-gyp-build/node-gyp-build.js +207 -0
  65. package/node_modules/node-gyp-build/optional.js +7 -0
  66. package/node_modules/node-gyp-build/package.json +43 -0
  67. package/node_modules/tree-sitter/LICENSE +21 -0
  68. package/node_modules/tree-sitter/README.md +128 -0
  69. package/node_modules/tree-sitter/binding.gyp +80 -0
  70. package/node_modules/tree-sitter/index.js +916 -0
  71. package/node_modules/tree-sitter/package.json +76 -0
  72. package/node_modules/tree-sitter/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  73. package/node_modules/tree-sitter/src/addon_data.h +47 -0
  74. package/node_modules/tree-sitter/src/binding.cc +35 -0
  75. package/node_modules/tree-sitter/src/conversions.cc +140 -0
  76. package/node_modules/tree-sitter/src/conversions.h +22 -0
  77. package/node_modules/tree-sitter/src/language.cc +106 -0
  78. package/node_modules/tree-sitter/src/language.h +17 -0
  79. package/node_modules/tree-sitter/src/logger.cc +70 -0
  80. package/node_modules/tree-sitter/src/logger.h +19 -0
  81. package/node_modules/tree-sitter/src/lookaheaditerator.cc +122 -0
  82. package/node_modules/tree-sitter/src/lookaheaditerator.h +33 -0
  83. package/node_modules/tree-sitter/src/node.cc +1088 -0
  84. package/node_modules/tree-sitter/src/node.h +30 -0
  85. package/node_modules/tree-sitter/src/parser.cc +306 -0
  86. package/node_modules/tree-sitter/src/parser.h +35 -0
  87. package/node_modules/tree-sitter/src/query.cc +397 -0
  88. package/node_modules/tree-sitter/src/query.h +40 -0
  89. package/node_modules/tree-sitter/src/tree.cc +316 -0
  90. package/node_modules/tree-sitter/src/tree.h +45 -0
  91. package/node_modules/tree-sitter/src/tree_cursor.cc +213 -0
  92. package/node_modules/tree-sitter/src/tree_cursor.h +52 -0
  93. package/node_modules/tree-sitter/tree-sitter.d.ts +1042 -0
  94. package/node_modules/tree-sitter/vendor/tree-sitter/lib/include/tree_sitter/api.h +1478 -0
  95. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/alloc.c +48 -0
  96. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/alloc.h +41 -0
  97. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/array.h +291 -0
  98. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/atomic.h +68 -0
  99. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/clock.h +146 -0
  100. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/error_costs.h +11 -0
  101. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/get_changed_ranges.c +523 -0
  102. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/get_changed_ranges.h +36 -0
  103. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/host.h +21 -0
  104. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/language.c +293 -0
  105. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/language.h +293 -0
  106. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/length.h +52 -0
  107. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/lexer.c +483 -0
  108. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/lexer.h +54 -0
  109. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/lib.c +12 -0
  110. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/node.c +875 -0
  111. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/parser.c +2293 -0
  112. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/parser.h +286 -0
  113. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/point.h +48 -0
  114. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/portable/endian.h +239 -0
  115. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/query.c +4350 -0
  116. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/reduce_action.h +34 -0
  117. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/reusable_node.h +95 -0
  118. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/stack.c +911 -0
  119. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/stack.h +133 -0
  120. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/subtree.c +1034 -0
  121. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/subtree.h +399 -0
  122. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/tree.c +170 -0
  123. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/tree.h +31 -0
  124. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/tree_cursor.c +717 -0
  125. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/tree_cursor.h +48 -0
  126. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/ts_assert.h +11 -0
  127. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/ICU_SHA +1 -0
  128. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/LICENSE +414 -0
  129. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/README.md +29 -0
  130. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/ptypes.h +1 -0
  131. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/umachine.h +448 -0
  132. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/urename.h +1 -0
  133. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/utf.h +1 -0
  134. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/utf16.h +733 -0
  135. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/utf8.h +881 -0
  136. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode.h +75 -0
  137. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/wasm/stdlib-symbols.txt +24 -0
  138. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/wasm/stdlib.c +113 -0
  139. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/wasm/wasm-stdlib.h +1314 -0
  140. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/wasm_store.c +1935 -0
  141. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/wasm_store.h +31 -0
  142. package/package.json +8 -2
  143. package/scripts/postinstall-tree-sitter.mjs +28 -0
@@ -0,0 +1,397 @@
1
+ #include "./query.h"
2
+ #include "./language.h"
3
+ #include "./node.h"
4
+
5
+ #include <napi.h>
6
+ #include <string>
7
+ #include <vector>
8
+
9
+ using std::vector;
10
+ using namespace Napi;
11
+
12
+ namespace node_tree_sitter {
13
+
14
+ /*
15
+ tstag() {
16
+ b2sum -l64 <(printf tree-sitter) <(printf "$1") | \
17
+ awk '{printf "0x" toupper($1) (NR == 1 ? ", " : "\n")}'
18
+ }
19
+ tstag query # => 0x8AF2E5212AD58ABF, 0x7B1FAB666CBD6803
20
+ */
21
+ const napi_type_tag QUERY_TYPE_TAG = {
22
+ 0x8AF2E5212AD58ABF, 0x7B1FAB666CBD6803
23
+ };
24
+
25
+ const char *query_error_names[] = {
26
+ "TSQueryErrorNone",
27
+ "TSQueryErrorSyntax",
28
+ "TSQueryErrorNodeType",
29
+ "TSQueryErrorField",
30
+ "TSQueryErrorCapture",
31
+ "TSQueryErrorStructure",
32
+ };
33
+
34
+ void Query::Init(Napi::Env env, Napi::Object exports) {
35
+ auto *data = env.GetInstanceData<AddonData>();
36
+ data->ts_query_cursor = ts_query_cursor_new();
37
+
38
+ Function ctor = DefineClass(env, "Query", {
39
+ InstanceAccessor("matchLimit", &Query::MatchLimit, nullptr, napi_default_method),
40
+
41
+ InstanceMethod("_matches", &Query::Matches, napi_default_method),
42
+ InstanceMethod("_captures", &Query::Captures, napi_default_method),
43
+ InstanceMethod("_getPredicates", &Query::GetPredicates, napi_default_method),
44
+ InstanceMethod("disableCapture", &Query::DisableCapture, napi_default_method),
45
+ InstanceMethod("disablePattern", &Query::DisablePattern, napi_default_method),
46
+ InstanceMethod("isPatternGuaranteedAtStep", &Query::IsPatternGuaranteedAtStep, napi_default_method),
47
+ InstanceMethod("isPatternRooted", &Query::IsPatternRooted, napi_default_method),
48
+ InstanceMethod("isPatternNonLocal", &Query::IsPatternNonLocal, napi_default_method),
49
+ InstanceMethod("startIndexForPattern", &Query::StartIndexForPattern, napi_default_method),
50
+ InstanceMethod("endIndexForPattern", &Query::EndIndexForPattern, napi_default_method),
51
+ InstanceMethod("didExceedMatchLimit", &Query::DidExceedMatchLimit, napi_default_method),
52
+ });
53
+
54
+ data->query_constructor = Napi::Persistent(ctor);
55
+ exports["Query"] = ctor;
56
+ }
57
+
58
+ Query::Query(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Query>(info) , query_(nullptr) {
59
+ Napi::Env env = info.Env();
60
+
61
+ Value().TypeTag(&QUERY_TYPE_TAG);
62
+
63
+ const TSLanguage *language = language_methods::UnwrapLanguage(info[0]);
64
+ const char *source;
65
+ uint32_t source_len;
66
+ uint32_t error_offset = 0;
67
+ TSQueryError error_type = TSQueryErrorNone;
68
+
69
+ if (language == nullptr) {
70
+ throw Error::New(env, "Missing language argument");
71
+ }
72
+
73
+ if (info[1].IsString()) {
74
+ auto string = info[1].As<String>();
75
+ std::string utf8_string = string.Utf8Value();
76
+ source = utf8_string.data();
77
+ source_len = utf8_string.length();
78
+ query_ = ts_query_new(language, source, source_len, &error_offset, &error_type);
79
+ } else if (info[1].IsBuffer()) {
80
+ auto buf = info[1].As<Buffer<char>>();
81
+ source = buf.Data();
82
+ source_len = buf.Length();
83
+ query_ = ts_query_new(language, source, source_len, &error_offset, &error_type);
84
+ }
85
+ else {
86
+ throw Error::New(env, "Missing source argument");
87
+ }
88
+
89
+ if (error_offset > 0) {
90
+ const char *error_name = query_error_names[error_type];
91
+ std::string message = "Query error of type ";
92
+ message += error_name;
93
+ message += " at position ";
94
+ message += std::to_string(error_offset);
95
+ throw Error::New(env, message.c_str());
96
+ }
97
+
98
+ info.This().As<Napi::Object>().Get("_init").As<Napi::Function>().Call(info.This(), {});
99
+ }
100
+
101
+ Query::~Query() {
102
+ ts_query_delete(query_);
103
+ }
104
+
105
+ Query *Query::UnwrapQuery(const Napi::Value &value) {
106
+ if (!value.IsObject()) {
107
+ return nullptr;
108
+ }
109
+ auto js_query = value.As<Object>();
110
+ if (!js_query.CheckTypeTag(&QUERY_TYPE_TAG)) {
111
+ return nullptr;
112
+ }
113
+ return Query::Unwrap(js_query);
114
+ }
115
+
116
+ Napi::Value Query::GetPredicates(const Napi::CallbackInfo &info) {
117
+ Napi::Env env = info.Env();
118
+ Query *query = Query::UnwrapQuery(info.This());
119
+ auto *ts_query = query->query_;
120
+
121
+ auto pattern_len = ts_query_pattern_count(ts_query);
122
+
123
+ Array js_predicates = Array::New(env);
124
+
125
+ for (size_t pattern_index = 0; pattern_index < pattern_len; pattern_index++) {
126
+ uint32_t predicates_len;
127
+ const TSQueryPredicateStep *predicates = ts_query_predicates_for_pattern(
128
+ ts_query, pattern_index, &predicates_len);
129
+
130
+ Array js_pattern_predicates = Array::New(env);
131
+
132
+ if (predicates_len > 0) {
133
+ Array js_predicate = Array::New(env);
134
+
135
+ size_t a_index = 0;
136
+ size_t p_index = 0;
137
+ for (size_t i = 0; i < predicates_len; i++) {
138
+ const TSQueryPredicateStep predicate = predicates[i];
139
+ uint32_t len;
140
+ switch (predicate.type) {
141
+ case TSQueryPredicateStepTypeCapture:
142
+ js_predicate[p_index++] = Number::New(env, TSQueryPredicateStepTypeCapture);
143
+ js_predicate[p_index++] = String::New(env,
144
+ ts_query_capture_name_for_id(ts_query, predicate.value_id, &len)
145
+ );
146
+ break;
147
+ case TSQueryPredicateStepTypeString:
148
+ js_predicate[p_index++] = Number::New(env, TSQueryPredicateStepTypeString);
149
+ js_predicate[p_index++] = String::New(env,
150
+ ts_query_string_value_for_id(ts_query, predicate.value_id, &len)
151
+ );
152
+ break;
153
+ case TSQueryPredicateStepTypeDone:
154
+ js_pattern_predicates[a_index++] = js_predicate;
155
+ js_predicate = Array::New(env);
156
+ p_index = 0;
157
+ break;
158
+ }
159
+ }
160
+ }
161
+
162
+ js_predicates[pattern_index] = js_pattern_predicates;
163
+ }
164
+
165
+ return js_predicates;
166
+ }
167
+
168
+ Napi::Value Query::Matches(const Napi::CallbackInfo &info) {
169
+ Napi::Env env = info.Env();
170
+ auto *data = env.GetInstanceData<AddonData>();
171
+ Query *query = Query::UnwrapQuery(info.This());
172
+ const Tree *tree = Tree::UnwrapTree(info[0]);
173
+
174
+ uint32_t start_row = 0, start_column = 0, end_row = 0, end_column = 0, start_index = 0, end_index = 0,
175
+ match_limit = UINT32_MAX, max_start_depth = UINT32_MAX, timeout_micros = 0;
176
+
177
+ if (info.Length() > 1 && info[1].IsNumber()) {
178
+ start_row = info[1].As<Number>().Uint32Value();
179
+ }
180
+ if (info.Length() > 2 && info[2].IsNumber()) {
181
+ start_column = info[2].As<Number>().Uint32Value() << 1;
182
+ }
183
+ if (info.Length() > 3 && info[3].IsNumber()) {
184
+ end_row = info[3].As<Number>().Uint32Value();
185
+ }
186
+ if (info.Length() > 4 && info[4].IsNumber()) {
187
+ end_column = info[4].As<Number>().Uint32Value() << 1;
188
+ }
189
+ if (info.Length() > 5 && info[5].IsNumber()) {
190
+ start_index = info[5].As<Number>().Uint32Value();
191
+ }
192
+ if (info.Length() > 6 && info[6].IsNumber()) {
193
+ end_index = info[6].As<Number>().Uint32Value() << 1;
194
+ }
195
+ if (info.Length() > 7 && info[7].IsNumber()) {
196
+ match_limit = info[7].As<Number>().Uint32Value();
197
+ }
198
+ if (info.Length() > 8 && info[8].IsNumber()) {
199
+ max_start_depth = info[8].As<Number>().Uint32Value();
200
+ }
201
+ if (info.Length() > 9 && info[9].IsNumber()) {
202
+ timeout_micros = info[9].As<Number>().Uint32Value();
203
+ }
204
+
205
+ if (query == nullptr) {
206
+ throw Error::New(env, "Missing argument query");
207
+ }
208
+
209
+ if (tree == nullptr) {
210
+ throw Error::New(env, "Missing argument tree");
211
+ }
212
+
213
+ TSQuery *ts_query = query->query_;
214
+ TSNode root_node = node_methods::UnmarshalNode(env, tree);
215
+ TSPoint start_point = {start_row, start_column};
216
+ TSPoint end_point = {end_row, end_column};
217
+ ts_query_cursor_set_point_range(data->ts_query_cursor, start_point, end_point);
218
+ ts_query_cursor_set_byte_range(data->ts_query_cursor, start_index, end_index);
219
+ ts_query_cursor_set_match_limit(data->ts_query_cursor, match_limit);
220
+ ts_query_cursor_set_max_start_depth(data->ts_query_cursor, max_start_depth);
221
+ ts_query_cursor_set_timeout_micros(data->ts_query_cursor, timeout_micros);
222
+ ts_query_cursor_exec(data->ts_query_cursor, ts_query, root_node);
223
+
224
+ Array js_matches = Array::New(env);
225
+ unsigned index = 0;
226
+ vector<TSNode> nodes;
227
+ TSQueryMatch match;
228
+
229
+ while (ts_query_cursor_next_match(data->ts_query_cursor, &match)) {
230
+ js_matches[index++] = Number::New(env, match.pattern_index);
231
+
232
+ for (uint16_t i = 0; i < match.capture_count; i++) {
233
+ const TSQueryCapture &capture = match.captures[i];
234
+
235
+ uint32_t capture_name_len = 0;
236
+ const char *capture_name = ts_query_capture_name_for_id(
237
+ ts_query, capture.index, &capture_name_len);
238
+
239
+ TSNode node = capture.node;
240
+ nodes.push_back(node);
241
+
242
+ String js_capture = String::New(env, capture_name);;
243
+ js_matches[index++] = js_capture;
244
+ }
245
+ }
246
+
247
+ auto js_nodes = node_methods::GetMarshalNodes(info, tree, nodes.data(), nodes.size());
248
+
249
+ auto result = Array::New(env);
250
+ result[0U] = js_matches;
251
+ result[1] = js_nodes;
252
+ return result;
253
+ }
254
+
255
+ Napi::Value Query::Captures(const Napi::CallbackInfo &info) {
256
+ Napi::Env env = info.Env();
257
+ auto *data = env.GetInstanceData<AddonData>();
258
+ Query *query = Query::UnwrapQuery(info.This());
259
+ const Tree *tree = Tree::UnwrapTree(info[0]);
260
+
261
+ uint32_t start_row = 0, start_column = 0, end_row = 0, end_column = 0, start_index = 0, end_index = 0,
262
+ match_limit = UINT32_MAX, max_start_depth = UINT32_MAX, timeout_micros = 0;
263
+
264
+ if (info.Length() > 1 && info[1].IsNumber()) {
265
+ start_row = info[1].As<Number>().Uint32Value();
266
+ }
267
+ if (info.Length() > 2 && info[2].IsNumber()) {
268
+ start_column = info[2].As<Number>().Uint32Value() << 1;
269
+ }
270
+ if (info.Length() > 3 && info[3].IsNumber()) {
271
+ end_row = info[3].As<Number>().Uint32Value();
272
+ }
273
+ if (info.Length() > 4 && info[4].IsNumber()) {
274
+ end_column = info[4].As<Number>().Uint32Value() << 1;
275
+ }
276
+ if (info.Length() > 5 && info[5].IsNumber()) {
277
+ start_index = info[5].As<Number>().Uint32Value();
278
+ }
279
+ if (info.Length() > 6 && info[6].IsNumber()) {
280
+ end_index = info[6].As<Number>().Uint32Value() << 1;
281
+ }
282
+ if (info.Length() > 7 && info[7].IsNumber()) {
283
+ match_limit = info[7].As<Number>().Uint32Value();
284
+ }
285
+ if (info.Length() > 8 && info[8].IsNumber()) {
286
+ max_start_depth = info[8].As<Number>().Uint32Value();
287
+ }
288
+ if (info.Length() > 9 && info[9].IsNumber()) {
289
+ timeout_micros = info[9].As<Number>().Uint32Value();
290
+ }
291
+
292
+ if (query == nullptr) {
293
+ throw Error::New(env, "Missing argument query");
294
+ }
295
+
296
+ if (tree == nullptr) {
297
+ throw Error::New(env, "Missing argument tree");
298
+ }
299
+
300
+ TSQuery *ts_query = query->query_;
301
+ TSNode root_node = node_methods::UnmarshalNode(env, tree);
302
+ TSPoint start_point = {start_row, start_column};
303
+ TSPoint end_point = {end_row, end_column};
304
+ ts_query_cursor_set_point_range(data->ts_query_cursor, start_point, end_point);
305
+ ts_query_cursor_set_byte_range(data->ts_query_cursor, start_index, end_index);
306
+ ts_query_cursor_set_match_limit(data->ts_query_cursor, match_limit);
307
+ ts_query_cursor_set_max_start_depth(data->ts_query_cursor, max_start_depth);
308
+ ts_query_cursor_set_timeout_micros(data->ts_query_cursor, timeout_micros);
309
+ ts_query_cursor_exec(data->ts_query_cursor, ts_query, root_node);
310
+
311
+ Array js_matches = Array::New(env);
312
+ unsigned index = 0;
313
+ vector<TSNode> nodes;
314
+ TSQueryMatch match;
315
+ uint32_t capture_index;
316
+
317
+ while (ts_query_cursor_next_capture(
318
+ data->ts_query_cursor,
319
+ &match,
320
+ &capture_index
321
+ )) {
322
+
323
+ js_matches[index++] = Number::New(env, match.pattern_index);
324
+ js_matches[index++] = Number::New(env, capture_index);
325
+
326
+ for (uint16_t i = 0; i < match.capture_count; i++) {
327
+ const TSQueryCapture &capture = match.captures[i];
328
+
329
+ uint32_t capture_name_len = 0;
330
+ const char *capture_name = ts_query_capture_name_for_id(
331
+ ts_query, capture.index, &capture_name_len);
332
+
333
+ TSNode node = capture.node;
334
+ nodes.push_back(node);
335
+
336
+ String js_capture = String::New(env, capture_name);;
337
+ js_matches[index++] = js_capture;
338
+ }
339
+ }
340
+
341
+ auto js_nodes = node_methods::GetMarshalNodes(info, tree, nodes.data(), nodes.size());
342
+
343
+ auto result = Array::New(env);
344
+ result[0U] = js_matches;
345
+ result[1] = js_nodes;
346
+ return result;
347
+ }
348
+
349
+ Napi::Value Query::DisableCapture(const Napi::CallbackInfo &info) {
350
+ std::string string = info[0].As<String>().Utf8Value();
351
+ const char *capture_name = string.c_str();
352
+ ts_query_disable_capture(query_, capture_name, string.length());
353
+ return info.Env().Undefined();
354
+ }
355
+
356
+ Napi::Value Query::DisablePattern(const Napi::CallbackInfo &info) {
357
+ uint32_t pattern_index = info[0].As<Number>().Uint32Value();
358
+ ts_query_disable_pattern(query_, pattern_index);
359
+ return info.Env().Undefined();
360
+ }
361
+
362
+ Napi::Value Query::IsPatternGuaranteedAtStep(const Napi::CallbackInfo &info) {
363
+ uint32_t byte_offset = info[0].As<Number>().Uint32Value();
364
+ return Boolean::New(info.Env(), ts_query_is_pattern_guaranteed_at_step(query_, byte_offset));
365
+ }
366
+
367
+ Napi::Value Query::IsPatternRooted(const Napi::CallbackInfo &info) {
368
+ uint32_t pattern_index = info[0].As<Number>().Uint32Value();
369
+ return Boolean::New(info.Env(), ts_query_is_pattern_rooted(query_, pattern_index));
370
+ }
371
+
372
+ Napi::Value Query::IsPatternNonLocal(const Napi::CallbackInfo &info) {
373
+ uint32_t pattern_index = info[0].As<Number>().Uint32Value();
374
+ return Boolean::New(info.Env(), ts_query_is_pattern_non_local(query_, pattern_index));
375
+ }
376
+
377
+ Napi::Value Query::StartIndexForPattern(const Napi::CallbackInfo &info) {
378
+ uint32_t pattern_index = info[0].As<Number>().Uint32Value();
379
+ return Number::New(info.Env(), ts_query_start_byte_for_pattern(query_, pattern_index));
380
+ }
381
+
382
+ Napi::Value Query::EndIndexForPattern(const Napi::CallbackInfo &info) {
383
+ uint32_t pattern_index = info[0].As<Number>().Uint32Value();
384
+ return Number::New(info.Env(), ts_query_end_byte_for_pattern(query_, pattern_index));
385
+ }
386
+
387
+ Napi::Value Query::DidExceedMatchLimit(const Napi::CallbackInfo &info) {
388
+ auto *data = info.Env().GetInstanceData<AddonData>();
389
+ return Boolean::New(info.Env(), ts_query_cursor_did_exceed_match_limit(data->ts_query_cursor));
390
+ }
391
+
392
+ Napi::Value Query::MatchLimit(const Napi::CallbackInfo &info) {
393
+ auto *data = info.Env().GetInstanceData<AddonData>();
394
+ return Number::New(info.Env(), ts_query_cursor_match_limit(data->ts_query_cursor));
395
+ }
396
+
397
+ } // namespace node_tree_sitter
@@ -0,0 +1,40 @@
1
+ #ifndef NODE_TREE_SITTER_QUERY_H_
2
+ #define NODE_TREE_SITTER_QUERY_H_
3
+
4
+ #include "./addon_data.h"
5
+ #include "tree_sitter/api.h"
6
+
7
+ #include <napi.h>
8
+ #include <node_object_wrap.h>
9
+
10
+ namespace node_tree_sitter {
11
+
12
+ class Query final : public Napi::ObjectWrap<Query> {
13
+ public:
14
+ static void Init(Napi::Env env, Napi::Object exports);
15
+ static Query *UnwrapQuery(const Napi::Value &);
16
+
17
+ explicit Query(const Napi::CallbackInfo &info);
18
+ ~Query() final;
19
+
20
+ private:
21
+ TSQuery *query_;
22
+
23
+ Napi::Value New(const Napi::CallbackInfo &);
24
+ Napi::Value Matches(const Napi::CallbackInfo &);
25
+ Napi::Value Captures(const Napi::CallbackInfo &);
26
+ Napi::Value GetPredicates(const Napi::CallbackInfo &);
27
+ Napi::Value DisableCapture(const Napi::CallbackInfo &);
28
+ Napi::Value DisablePattern(const Napi::CallbackInfo &);
29
+ Napi::Value IsPatternGuaranteedAtStep(const Napi::CallbackInfo &);
30
+ Napi::Value IsPatternRooted(const Napi::CallbackInfo &);
31
+ Napi::Value IsPatternNonLocal(const Napi::CallbackInfo &);
32
+ Napi::Value StartIndexForPattern(const Napi::CallbackInfo &);
33
+ Napi::Value EndIndexForPattern(const Napi::CallbackInfo &);
34
+ Napi::Value DidExceedMatchLimit(const Napi::CallbackInfo &);
35
+ Napi::Value MatchLimit(const Napi::CallbackInfo &);
36
+ };
37
+
38
+ } // namespace node_tree_sitter
39
+
40
+ #endif // NODE_TREE_SITTER_QUERY_H_