1///|
2priv type TSQueryCursor
3
4///|
5struct QueryCursor {
6 cursor : TSQueryCursor
7 mut query : Query
8 mut tree : TSTree
9 mut text : @string.View
10}
11
12///|
13extern "c" fn ts_query_cursor_new() -> type TSQueryCursor
TSQueryCursor = "moonbit_ts_query_cursor_new"
14
15///|
16/// Create a new cursor for executing a given query.
17///
18/// The cursor stores the state that is needed to iteratively search for
19/// matches. To use the query cursor, first call `QueryCursor::exec`
20/// to start running a given query on a given syntax node. Then, there are
21/// two options for consuming the results of the query:
22/// 1. Repeatedly call `QueryCursor::next_match` to iterate over all of the
23/// *matches* in the order that they were found. Each match contains the
24/// index of the pattern that matched, and an array of captures. Because
25/// multiple patterns can match the same set of nodes, one match may contain
26/// captures that appear *before* some of the captures from a previous match.
27/// 2. Repeatedly call `QueryCursor::next_capture` to iterate over all of the
28/// individual *captures* in the order that they appear. This is useful if
29/// don't care about which pattern matched, and just want a single ordered
30/// sequence of captures.
31///
32/// If you don't care about consuming all of the results, you can stop calling
33/// `QueryCursor::next_match` or `QueryCursor::next_capture` at any point.
34/// You can then start executing another query on another node by calling
35/// `QueryCursor::exec` again.
36pub fn struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor::() -> QueryCursor
Create a new cursor for executing a given query.
The cursor stores the state that is needed to iteratively search for
matches. To use the query cursor, first call QueryCursor::exec
to start running a given query on a given syntax node. Then, there are
two options for consuming the results of the query:
- Repeatedly call
QueryCursor::next_match to iterate over all of the
matches in the order that they were found. Each match contains the
index of the pattern that matched, and an array of captures. Because
multiple patterns can match the same set of nodes, one match may contain
captures that appear before some of the captures from a previous match.
- Repeatedly call
QueryCursor::next_capture to iterate over all of the
individual captures in the order that they appear. This is useful if
don't care about which pattern matched, and just want a single ordered
sequence of captures.
If you don't care about consuming all of the results, you can stop calling
QueryCursor::next_match or QueryCursor::next_capture at any point.
You can then start executing another query on another node by calling
QueryCursor::exec again.
new() -> struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor {
37 let TSQueryCursor
cursor = () -> TSQueryCursor
ts_query_cursor_new()
38 let QueryCursor
cursor = struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor::{
39 TSQueryCursor
cursor,
40 Query
query: () -> Query
ts_query_null(),
41 Tree
tree: () -> Tree
ts_tree_null(),
42 String
text: "",
43 }
44 QueryCursor
cursor
45}
46
47///|
48#borrow(self, query, node, tree)
49extern "c" fn ts_query_cursor_exec(
50 self : type TSQueryCursor
TSQueryCursor,
51 query : type Query
Query,
52 node : type TSNode Bytes
TSNode,
53 tree : type TSTree
TSTree
54) = "moonbit_ts_query_cursor_exec"
55
56///|
57// @move progress_callback
58#borrow(self, query, node, tree)
59extern "c" fn ts_query_cursor_exec_with_options(
60 self : type TSQueryCursor
TSQueryCursor,
61 query : type Query
Query,
62 node : type TSNode Bytes
TSNode,
63 tree : type TSTree
TSTree,
64 progress_callback : (UInt
UInt) -> Unit
Unit
65) = "moonbit_ts_query_cursor_exec_with_options"
66
67///|
68pub struct QueryCursorState {
69 Int
current_byte_offset : Int
Int
70}
71
72///|
73struct QueryCursorOptions {
74 (QueryCursorState) -> Unit
progress_callback : (struct QueryCursorState {
current_byte_offset: Int
}
QueryCursorState) -> Unit
Unit
75}
76
77///|
78pub fn struct QueryCursorOptions {
progress_callback: (QueryCursorState) -> Unit
}
QueryCursorOptions::(progress_callback~ : (QueryCursorState) -> Unit) -> QueryCursorOptions
new(
79 (QueryCursorState) -> Unit
progress_callback~ : (struct QueryCursorState {
current_byte_offset: Int
}
QueryCursorState) -> Unit
Unit
80) -> struct QueryCursorOptions {
progress_callback: (QueryCursorState) -> Unit
}
QueryCursorOptions {
81 struct QueryCursorOptions {
progress_callback: (QueryCursorState) -> Unit
}
QueryCursorOptions::{ (QueryCursorState) -> Unit
progress_callback, }
82}
83
84///|
85/// Start running a given query on a given node, with optional options.
86pub fn struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor::(self : QueryCursor, query : Query, node : Node, options? : QueryCursorOptions) -> Unit
Start running a given query on a given node, with optional options.
exec(
87 QueryCursor
self : struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor,
88 Query
query : type Query
Query,
89 Node
node : struct Node {
node: TSNode
tree: TSTree
text: @string.StringView
}
Node,
90 QueryCursorOptions?
options? : struct QueryCursorOptions {
progress_callback: (QueryCursorState) -> Unit
}
QueryCursorOptions
91) -> Unit
Unit {
92 match QueryCursorOptions?
options {
93 QueryCursorOptions?
None => (self : TSQueryCursor, query : Query, node : TSNode, tree : TSTree) -> Unit
ts_query_cursor_exec(QueryCursor
self.TSTreeCursor
cursor, Query
query, Node
node.TSNode
node, Node
node.TSTree
tree)
94 (QueryCursorOptions) -> QueryCursorOptions?
Some(QueryCursorOptions
options) =>
95 (self : TSQueryCursor, query : Query, node : TSNode, tree : TSTree, progress_callback : (UInt) -> Unit) -> Unit
ts_query_cursor_exec_with_options(
96 QueryCursor
self.TSTreeCursor
cursor,
97 Query
query,
98 Node
node.TSNode
node,
99 Node
node.TSTree
tree,
100 fn(UInt
current_byte_offset) {
101 let Int
current_byte_offset = (value : UInt) -> Int
uint_to_int(UInt
current_byte_offset)
102 (QueryCursorOptions
options.(QueryCursorState) -> Unit
progress_callback)(struct QueryCursorState {
current_byte_offset: Int
}
QueryCursorState::{ Int
current_byte_offset, })
103 },
104 )
105 }
106 QueryCursor
self.Query?
query = Query
query
107 QueryCursor
selfUnit
.tree = Node
nodeUnit
.TSTree
tree
108 QueryCursor
selfUnit
.text = Node
nodeUnit
.@string.StringView
text
109}
110
111///|
112#borrow(self)
113extern "c" fn ts_query_cursor_did_exceed_match_limit(
114 self : type TSQueryCursor
TSQueryCursor
115) -> Bool
Bool = "moonbit_ts_query_cursor_did_exceed_match_limit"
116
117///|
118/// Check if the query cursor exceeded its match limit.
119///
120/// Query cursors have an optional maximum capacity for storing lists of
121/// in-progress captures. If this capacity is exceeded, then the
122/// earliest-starting match will silently be dropped to make room for further
123/// matches. This maximum capacity is optional — by default, query cursors allow
124/// any number of pending matches, dynamically allocating new space for them as
125/// needed as the query is executed.
126pub fn struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor::(self : QueryCursor) -> Bool
Check if the query cursor exceeded its match limit.
Query cursors have an optional maximum capacity for storing lists of
in-progress captures. If this capacity is exceeded, then the
earliest-starting match will silently be dropped to make room for further
matches. This maximum capacity is optional — by default, query cursors allow
any number of pending matches, dynamically allocating new space for them as
needed as the query is executed.
did_exceed_match_limit(QueryCursor
self : struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor) -> Bool
Bool {
127 (self : TSQueryCursor) -> Bool
ts_query_cursor_did_exceed_match_limit(QueryCursor
self.TSTreeCursor
cursor)
128}
129
130///|
131#borrow(self)
132extern "c" fn ts_query_cursor_match_limit(self : type TSQueryCursor
TSQueryCursor) -> UInt
UInt = "moonbit_ts_query_cursor_match_limit"
133
134///|
135/// Get the maximum number of in-progress matches allowed by this query cursor.
136///
137/// See `QueryCursor::did_exceed_match_limit`.
138pub fn struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor::(self : QueryCursor) -> Int
Get the maximum number of in-progress matches allowed by this query cursor.
See QueryCursor::did_exceed_match_limit.
match_limit(QueryCursor
self : struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor) -> Int
Int {
139 (self : TSQueryCursor) -> UInt
ts_query_cursor_match_limit(QueryCursor
self.TSTreeCursor
cursor) |> (value : UInt) -> Int
uint_to_int()
140}
141
142///|
143#borrow(self)
144extern "c" fn ts_query_cursor_set_match_limit(
145 self : type TSQueryCursor
TSQueryCursor,
146 limit : UInt
UInt
147) = "moonbit_ts_query_cursor_set_match_limit"
148
149///|
150/// Set the maximum number of in-progress matches allowed by this query cursor.
151///
152/// See `QueryCursor::did_exceed_match_limit`.
153pub fn struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor::(self : QueryCursor, limit : Int) -> Unit
Set the maximum number of in-progress matches allowed by this query cursor.
See QueryCursor::did_exceed_match_limit.
set_match_limit(QueryCursor
self : struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor, Int
limit : Int
Int) -> Unit
Unit {
154 (self : TSQueryCursor, limit : UInt) -> Unit
ts_query_cursor_set_match_limit(QueryCursor
self.TSTreeCursor
cursor, (value : Int) -> UInt
int_to_uint(Int
limit))
155}
156
157///|
158#borrow(self)
159extern "c" fn ts_query_cursor_set_byte_range(
160 self : type TSQueryCursor
TSQueryCursor,
161 start_byte : UInt
UInt,
162 end_byte : UInt
UInt
163) = "moonbit_ts_query_cursor_set_byte_range"
164
165///|
166/// Set the range of bytes in which the query will be executed.
167///
168/// The query cursor will return matches that intersect with the given byte range.
169/// This means that a match may be returned even if some of its captures fall
170/// outside the specified range, as long as at least part of the match
171/// overlaps with the range.
172pub fn struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor::(self : QueryCursor, start_byte : Int, end_byte : Int) -> Unit
Set the range of bytes in which the query will be executed.
The query cursor will return matches that intersect with the given byte range.
This means that a match may be returned even if some of its captures fall
outside the specified range, as long as at least part of the match
overlaps with the range.
set_byte_range(
173 QueryCursor
self : struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor,
174 Int
start_byte : Int
Int,
175 Int
end_byte : Int
Int
176) -> Unit
Unit {
177 (self : TSQueryCursor, start_byte : UInt, end_byte : UInt) -> Unit
ts_query_cursor_set_byte_range(
178 QueryCursor
self.TSTreeCursor
cursor,
179 (value : Int) -> UInt
int_to_uint(Int
start_byte),
180 (value : Int) -> UInt
int_to_uint(Int
end_byte),
181 )
182}
183
184///|
185#borrow(self, start_point, end_point)
186extern "c" fn ts_query_cursor_set_point_range(
187 self : type TSQueryCursor
TSQueryCursor,
188 start_point : type Point
Point,
189 end_point : type Point
Point
190) = "moonbit_ts_query_cursor_set_point_range"
191
192///|
193/// Set the range of (row, column) positions in which the query will be executed.
194///
195/// The query cursor will return matches that intersect with the given point range.
196/// This means that a match may be returned even if some of its captures fall
197/// outside the specified range, as long as at least part of the match
198/// overlaps with the range.
199pub fn struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor::(self : QueryCursor, start_point : Point, end_point : Point) -> Unit
Set the range of (row, column) positions in which the query will be executed.
The query cursor will return matches that intersect with the given point range.
This means that a match may be returned even if some of its captures fall
outside the specified range, as long as at least part of the match
overlaps with the range.
set_point_range(
200 QueryCursor
self : struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor,
201 Point
start_point : type Point
Point,
202 Point
end_point : type Point
Point
203) -> Unit
Unit {
204 (self : TSQueryCursor, start_point : Point, end_point : Point) -> Unit
ts_query_cursor_set_point_range(QueryCursor
self.TSTreeCursor
cursor, Point
start_point, Point
end_point)
205}
206
207///|
208struct QueryCapture {
209 query : Query
210 node : Node
211 index : Int
212}
213
214///|
215pub impl trait Show {
output(Self, &Logger) -> Unit
to_string(Self) -> String
}
Show for type QueryCapture
QueryCapture with (self : QueryCapture, logger : &Logger) -> Unit
output(QueryCapture
self, &Logger
logger) -> Unit
Unit {
216 &Logger
logger.(&Logger, String) -> Unit
write_string(QueryCapture
self.Node?
node.() -> Unit
string().() -> String
to_string())
217 &Logger
logger.(&Logger, String) -> Unit
write_string(" @")
218 &Logger
logger.(self : &Logger, obj : String) -> Unit
write_object(QueryCapture
self.(self : QueryCapture) -> String
name())
219}
220
221///|
222pub impl trait ToJson {
to_json(Self) -> Json
}
ToJson for type QueryCapture
QueryCapture with (self : QueryCapture) -> Json
to_json(QueryCapture
self) -> enum Json {
Null
True
False
Number(Double)
String(String)
Array(Array[Json])
Object(Map[String, Json])
}
Json {
223 enum Json {
Null
True
False
Number(Double)
String(String)
Array(Array[Json])
Object(Map[String, Json])
}
Json::(Map[String, Json]) -> Json
Creates a JSON object value from a MoonBit map.
Parameters:
map : A map from strings to JSON values to be converted to a JSON object
value.
Returns a JSON value representing the given map.
Example:
test "object" {
let map : Map[String, Json] = { "name": "John", "age": 42.0 }
inspect!(
Json::object(map),
content="Object({\"name\": String(\"John\"), \"age\": Number(42)})",
)
}
object({
224 "name": QueryCapture
self.(self : QueryCapture) -> String
name().(self : String) -> Json
to_json(),
225 "node": QueryCapture
self.Node?
node.(self : Node?) -> String
to_string().(self : String) -> Json
to_json(),
226 "index": QueryCapture
self.Unit
index.() -> Json
to_json(),
227 })
228}
229
230///|
231pub fn type QueryCapture
QueryCapture::(self : QueryCapture) -> Node
node(QueryCapture
self : type QueryCapture
QueryCapture) -> struct Node {
node: TSNode
tree: TSTree
text: @string.StringView
}
Node {
232 QueryCapture
self.Node?
node
233}
234
235///|
236pub fn type QueryCapture
QueryCapture::(self : QueryCapture) -> Int
index(QueryCapture
self : type QueryCapture
QueryCapture) -> Int
Int {
237 QueryCapture
self.Int
index
238}
239
240///|
241pub fn type QueryCapture
QueryCapture::(self : QueryCapture) -> String
name(QueryCapture
self : type QueryCapture
QueryCapture) -> String
String {
242 QueryCapture
self.Query?
query.(Unit) -> String
capture_name_for_id(QueryCapture
self.Unit
index)
243}
244
245///|
246struct QueryMatch {
247 query : Query
248 id : Int
249 pattern_index : Int
250 captures : FixedArray[QueryCapture]
251}
252
253///|
254pub impl trait ToJson {
to_json(Self) -> Json
}
ToJson for struct QueryMatch {
query: Query
query_match: TSQueryMatch
}
QueryMatch with (self : QueryMatch) -> Json
to_json(QueryMatch
self) -> enum Json {
Null
True
False
Number(Double)
String(String)
Array(Array[Json])
Object(Map[String, Json])
}
Json {
255 enum Json {
Null
True
False
Number(Double)
String(String)
Array(Array[Json])
Object(Map[String, Json])
}
Json::(Map[String, Json]) -> Json
Creates a JSON object value from a MoonBit map.
Parameters:
map : A map from strings to JSON values to be converted to a JSON object
value.
Returns a JSON value representing the given map.
Example:
test "object" {
let map : Map[String, Json] = { "name": "John", "age": 42.0 }
inspect!(
Json::object(map),
content="Object({\"name\": String(\"John\"), \"age\": Number(42)})",
)
}
object({
256 "id": QueryMatch
self.Unit
id.() -> Json
to_json(),
257 "pattern_index": QueryMatch
self.Unit
pattern_index.() -> Json
to_json(),
258 "captures": QueryMatch
self.Unit
captures.() -> Json
to_json(),
259 })
260}
261
262///|
263pub fn struct QueryMatch {
query: Query
query_match: TSQueryMatch
}
QueryMatch::(self : QueryMatch) -> Int
id(QueryMatch
self : struct QueryMatch {
query: Query
query_match: TSQueryMatch
}
QueryMatch) -> Int
Int {
264 QueryMatch
self.Int
id
265}
266
267///|
268pub fn struct QueryMatch {
query: Query
query_match: TSQueryMatch
}
QueryMatch::(self : QueryMatch) -> Int
pattern_index(QueryMatch
self : struct QueryMatch {
query: Query
query_match: TSQueryMatch
}
QueryMatch) -> Int
Int {
269 QueryMatch
self.Int
pattern_index
270}
271
272///|
273pub fn struct QueryMatch {
query: Query
query_match: TSQueryMatch
}
QueryMatch::(self : QueryMatch) -> Iter[QueryCapture]
captures(QueryMatch
self : struct QueryMatch {
query: Query
query_match: TSQueryMatch
}
QueryMatch) -> type Iter[T]
Iter[type QueryCapture
QueryCapture] {
274 QueryMatch
self.Unit
captures.() -> Iter[QueryCapture]
iter()
275}
276
277///|
278pub fn struct QueryMatch {
query: Query
query_match: TSQueryMatch
}
QueryMatch::(self : QueryMatch) -> Array[Array[QueryPredicateStep]]
predicates(QueryMatch
self : struct QueryMatch {
query: Query
query_match: TSQueryMatch
}
QueryMatch) -> type Array[T]
Array[type Array[T]
An Array is a collection of values that supports random access and can
grow in size.
QueryPredicate] {
279 QueryMatch
self.Query
query.(self : Query, pattern_index : Int) -> Array[Array[QueryPredicateStep]]
predicates_for_pattern(QueryMatch
self.Int
pattern_index)
280}
281
282///|
283priv type TSQueryMatch
284
285///|
286#borrow(self)
287extern "c" fn ts_query_match_is_null(self : type TSQueryMatch
TSQueryMatch) -> Bool
Bool = "moonbit_c_is_null"
288
289///|
290impl trait Nullable {
is_null(Self) -> Bool
to_option(Self) -> Self?
}
Nullable for type TSQueryMatch
TSQueryMatch with (self : TSQueryMatch) -> Bool
is_null(TSQueryMatch
self : type TSQueryMatch
TSQueryMatch) -> Bool
Bool {
291 return (self : TSQueryMatch) -> Bool
ts_query_match_is_null(TSQueryMatch
self)
292}
293
294///|
295#borrow(self)
296extern "c" fn ts_query_match_id(self : type TSQueryMatch
TSQueryMatch) -> UInt
UInt = "moonbit_ts_query_match_id"
297
298///|
299#borrow(self)
300extern "c" fn ts_query_match_pattern_index(self : type TSQueryMatch
TSQueryMatch) -> UInt
UInt = "moonbit_ts_query_match_pattern_index"
301
302///|
303#borrow(self)
304extern "c" fn ts_query_match_capture_count(self : type TSQueryMatch
TSQueryMatch) -> UInt16
UInt16 = "moonbit_ts_query_match_capture_count"
305
306///|
307#borrow(self)
308extern "c" fn ts_query_match_captures_get_node(
309 self : type TSQueryMatch
TSQueryMatch,
310 index : UInt
UInt
311) -> type TSNode Bytes
TSNode = "moonbit_ts_query_match_captures_get_node"
312
313///|
314#borrow(self)
315extern "c" fn ts_query_match_captures_get_index(
316 self : type TSQueryMatch
TSQueryMatch,
317 index : UInt
UInt
318) -> UInt
UInt = "moonbit_ts_query_match_captures_get_index"
319
320///|
321#borrow(self, query, tree)
322extern "c" fn ts_query_cursor_next_match(
323 self : type TSQueryCursor
TSQueryCursor,
324 query : type Query
Query,
325 tree : type TSTree
TSTree
326) -> type TSQueryMatch
TSQueryMatch = "moonbit_ts_query_cursor_next_match"
327
328///|
329/// Advance to the next match of the currently running query.
330///
331/// If there is a match, returns Some(match).
332/// Otherwise, returns None.
333pub fn struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor::(self : QueryCursor) -> QueryMatch?
Advance to the next match of the currently running query.
If there is a match, returns Some(match).
Otherwise, returns None.
next_match(QueryCursor
self : struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor) -> struct QueryMatch {
query: Query
query_match: TSQueryMatch
}
QueryMatch? {
334 let TSQueryMatch?
ts_match = (self : TSQueryCursor, query : Query, tree : TSTree) -> TSQueryMatch
ts_query_cursor_next_match(QueryCursor
self.TSTreeCursor
cursor, QueryCursor
self.Query?
query, QueryCursor
self.TSTree
tree).(self : TSQueryMatch) -> TSQueryMatch?
to_option()
335 guard TSQueryMatch?
ts_match is (TSQueryMatch) -> TSQueryMatch?
Some(TSQueryMatch
ts_match) else { return QueryMatch?
None }
336 let UInt16
capture_count = (self : TSQueryMatch) -> UInt16
ts_query_match_capture_count(TSQueryMatch
ts_match)
337 let FixedArray[Unit]
captures = type FixedArray[A]
FixedArray::(length : Int, value : (Int) -> Unit) -> FixedArray[Unit]
Creates a new fixed-size array of the specified length, where each element is
initialized using a function that maps indices to values.
Parameters:
length : The length of the array to create. If length is less than or
equal to 0, returns an empty array.
initializer : A function that takes an index (from 0 to length - 1) and
returns a value of type T for that position.
Returns a new fixed array containing the values produced by applying the
initializer function to each index.
Example:
test "FixedArray::makei" {
let arr = FixedArray::makei(3, fn(i) { i * 2 })
inspect!(arr, content="[0, 2, 4]")
}
makei(UInt16
capture_count.(self : UInt16) -> Int
Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is
zero-extended to fill the higher bits.
Parameters:
value : The unsigned 16-bit integer to be converted.
Returns a 32-bit signed integer. Since the input value is always non-negative
and less than 65536, the conversion never results in overflow.
Example:
test "UInt16::to_int" {
let x = Int::to_uint16(42)
inspect!(x.to_int(), content="42")
let max = Int::to_uint16(65535) // maximum value of UInt16
inspect!(max.to_int(), content="65535")
}
to_int(), fn(Int
i) {
338 let UInt
i = Int
i.(self : Int) -> UInt
reinterpret the signed int as unsigned int, when the value is
non-negative, i.e, 0..=2^31-1, the value is the same. When the
value is negative, it turns into a large number,
for example, -1 turns into 2^32-1
reinterpret_as_uint()
339 let TSNode
node = (self : TSQueryMatch, index : UInt) -> TSNode
ts_query_match_captures_get_node(TSQueryMatch
ts_match, UInt
i)
340 let UInt
index = (self : TSQueryMatch, index : UInt) -> UInt
ts_query_match_captures_get_index(TSQueryMatch
ts_match, UInt
i)
341 let Int
index = (value : UInt) -> Int
uint_to_int(UInt
index)
342 type QueryCapture
QueryCapture::{
343 Query?
query: QueryCursor
self.Query?
query,
344 Node
node: { TSNode
node, TSTree
tree: QueryCursor
self.TSTree
tree, @string.StringView
text: QueryCursor
self.@string.StringView
text },
345 Int
index,
346 }
347 })
348 let Int
id = (value : UInt) -> Int
uint_to_int((self : TSQueryMatch) -> UInt
ts_query_match_id(TSQueryMatch
ts_match))
349 let Int
pattern_index = (value : UInt) -> Int
uint_to_int((self : TSQueryMatch) -> UInt
ts_query_match_pattern_index(TSQueryMatch
ts_match))
350 (QueryMatch) -> QueryMatch?
Some(struct QueryMatch {
query: Query
query_match: TSQueryMatch
}
QueryMatch::{ Query?
query: QueryCursor
self.Query?
query, Int
id, Int
pattern_index, FixedArray[Unit]
captures })
351}
352
353///|
354pub fn struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor::(self : QueryCursor) -> Iter[QueryMatch]
matches(QueryCursor
self : struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor) -> type Iter[T]
Iter[struct QueryMatch {
query: Query
query_match: TSQueryMatch
}
QueryMatch] {
355 type Iter[T]
Iter::(((QueryMatch) -> IterResult) -> IterResult) -> Iter[QueryMatch]
Do not use this method, it is for internal use only.
new(fn((QueryMatch) -> IterResult
yield_) {
356 while QueryCursor
self.(self : QueryCursor) -> QueryMatch?
Advance to the next match of the currently running query.
If there is a match, returns Some(match).
Otherwise, returns None.
next_match() is (QueryMatch) -> QueryMatch?
Some(QueryMatch
match_) {
357 match (QueryMatch) -> IterResult
yield_(QueryMatch
match_) {
358 IterResult
IterContinue => continue
359 IterResult
IterEnd => break IterResult
IterEnd
360 }
361 } else {
362 IterResult
IterContinue
363 }
364 })
365}
366
367///|
368#borrow(self, query, tree)
369extern "c" fn ts_query_cursor_remove_match(
370 self : type TSQueryCursor
TSQueryCursor,
371 query : type Query
Query,
372 tree : type TSTree
TSTree,
373 match_id : UInt
UInt
374) = "moonbit_ts_query_cursor_remove_match"
375
376///|
377/// Remove a match from the query cursor's results.
378pub fn struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor::(self : QueryCursor, match_id : Int) -> Unit
Remove a match from the query cursor's results.
remove_match(QueryCursor
self : struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor, Int
match_id : Int
Int) -> Unit
Unit {
379 (self : TSQueryCursor, query : Query, tree : TSTree, match_id : UInt) -> Unit
ts_query_cursor_remove_match(
380 QueryCursor
self.TSTreeCursor
cursor,
381 QueryCursor
self.Query?
query,
382 QueryCursor
self.TSTree
tree,
383 (value : Int) -> UInt
int_to_uint(Int
match_id),
384 )
385}
386
387///|
388#borrow(self, query, tree, match_id)
389extern "c" fn ts_query_cursor_next_capture(
390 self : type TSQueryCursor
TSQueryCursor,
391 query : type Query
Query,
392 tree : type TSTree
TSTree,
393 match_id : type FixedArray[A]
FixedArray[UInt
UInt]
394) -> @c.Nullable[type TSQueryMatch
TSQueryMatch] = "moonbit_ts_query_cursor_next_capture"
395
396///|
397/// Advance to the next capture of the currently running query.
398///
399/// If there is a capture, returns Some(capture).
400/// Otherwise, returns None.
401pub fn struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor::(self : QueryCursor) -> QueryCapture?
Advance to the next capture of the currently running query.
If there is a capture, returns Some(capture).
Otherwise, returns None.
next_capture(QueryCursor
self : struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor) -> type QueryCapture
QueryCapture? {
402 let FixedArray[UInt]
match_id = type FixedArray[A]
FixedArray::(len : Int, init : UInt) -> FixedArray[UInt]
Creates a new fixed-size array with the specified length, initializing all
elements with the given value.
Parameters:
length : The length of the array to create. Must be non-negative.
initial_value : The value used to initialize all elements in the array.
Returns a new fixed-size array of type FixedArray[T] with length
elements, where each element is initialized to initial_value.
Throws a panic if length is negative.
Example:
test "FixedArray::make" {
let arr = FixedArray::make(3, 42)
inspect!(arr[0], content="42")
inspect!(arr.length(), content="3")
}
test "panic FixedArray::make/negative_length" {
ignore(FixedArray::make(-1, 0))
}
WARNING: A common pitfall is creating with the same initial value, for example:
test {
let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
two_dimension_array[0][5] = 10
assert_eq!(two_dimension_array[5][5], 10)
}
This is because all the cells reference to the same object (the FixedArray[Int] in this case).
One should use makei() instead which creates an object for each index.
make(1, 0U)
403 let TSQueryMatch?
ts_match = (self : TSQueryCursor, query : Query, tree : TSTree, match_id : FixedArray[UInt]) -> ?
ts_query_cursor_next_capture(
404 QueryCursor
self.TSTreeCursor
cursor,
405 QueryCursor
self.Query?
query,
406 QueryCursor
self.TSTree
tree,
407 FixedArray[UInt]
match_id,
408 ).() -> TSQueryMatch?
to()
409 guard TSQueryMatch?
ts_match is (TSQueryMatch) -> TSQueryMatch?
Some(TSQueryMatch
ts_match) else { return QueryCapture?
None }
410 let UInt
match_id = FixedArray[UInt]
match_id[0]
411 let TSNode
node = (self : TSQueryMatch, index : UInt) -> TSNode
ts_query_match_captures_get_node(TSQueryMatch
ts_match, UInt
match_id)
412 let Node
node = { TSNode
node, TSTree
tree: QueryCursor
self.TSTree
tree, @string.StringView
text: QueryCursor
self.@string.StringView
text }
413 let UInt
index = (self : TSQueryMatch, index : UInt) -> UInt
ts_query_match_captures_get_index(TSQueryMatch
ts_match, UInt
match_id)
414 let Int
index = (value : UInt) -> Int
uint_to_int(UInt
index)
415 (QueryCapture) -> QueryCapture?
Some(type QueryCapture
QueryCapture::{ Query?
query: QueryCursor
self.Query?
query, Node
node, Int
index })
416}
417
418///|
419pub fn struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor::(self : QueryCursor) -> Iter[QueryCapture]
captures(QueryCursor
self : struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor) -> type Iter[T]
Iter[type QueryCapture
QueryCapture] {
420 type Iter[T]
Iter::(((QueryCapture) -> IterResult) -> IterResult) -> Iter[QueryCapture]
Do not use this method, it is for internal use only.
new(fn((QueryCapture) -> IterResult
yield_) {
421 while QueryCursor
self.(self : QueryCursor) -> QueryCapture?
Advance to the next capture of the currently running query.
If there is a capture, returns Some(capture).
Otherwise, returns None.
next_capture() is (QueryCapture) -> QueryCapture?
Some(QueryCapture
capture) {
422 match (QueryCapture) -> IterResult
yield_(QueryCapture
capture) {
423 IterResult
IterContinue => continue
424 IterResult
IterEnd => break IterResult
IterEnd
425 }
426 } else {
427 IterResult
IterContinue
428 }
429 })
430}
431
432///|
433#borrow(self)
434extern "c" fn ts_query_cursor_set_max_start_depth(
435 self : type TSQueryCursor
TSQueryCursor,
436 max_start_depth : UInt
UInt
437) = "moonbit_ts_query_cursor_set_max_start_depth"
438
439///|
440/// Set the maximum start depth for a query cursor.
441///
442/// This prevents cursors from exploring children nodes at a certain depth.
443/// Note if a pattern includes many children, then they will still be checked.
444///
445/// The zero max start depth value can be used as a special behavior and
446/// it helps to destructure a subtree by staying on a node and using captures
447/// for interested parts. Note that the zero max start depth only limit a search
448/// depth for a pattern's root node but other nodes that are parts of the pattern
449/// may be searched at any depth what defined by the pattern structure.
450///
451/// Set to `@uint.max_value` to remove the maximum start depth.
452pub fn struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor::(self : QueryCursor, max_start_depth : Int) -> Unit
Set the maximum start depth for a query cursor.
This prevents cursors from exploring children nodes at a certain depth.
Note if a pattern includes many children, then they will still be checked.
The zero max start depth value can be used as a special behavior and
it helps to destructure a subtree by staying on a node and using captures
for interested parts. Note that the zero max start depth only limit a search
depth for a pattern's root node but other nodes that are parts of the pattern
may be searched at any depth what defined by the pattern structure.
Set to @uint.max_value to remove the maximum start depth.
set_max_start_depth(
453 QueryCursor
self : struct QueryCursor {
mut query: Query?
mut node: Node?
}
QueryCursor,
454 Int
max_start_depth : Int
Int
455) -> Unit
Unit {
456 (self : TSQueryCursor, max_start_depth : UInt) -> Unit
ts_query_cursor_set_max_start_depth(QueryCursor
self.TSTreeCursor
cursor, (value : Int) -> UInt
int_to_uint(Int
max_start_depth))
457}
458