1///|
2type Query
3
4///|
5extern "c" fn ts_query_null() -> type Query
Query = "moonbit_c_null"
6
7///|
8type! QueryError {
9 (UInt) -> QueryError
Syntax(UInt
UInt)
10 (UInt) -> QueryError
NodeType(UInt
UInt)
11 (UInt) -> QueryError
Field(UInt
UInt)
12 (UInt) -> QueryError
Capture(UInt
UInt)
13 (UInt) -> QueryError
Structure(UInt
UInt)
14 (UInt) -> QueryError
Language(UInt
UInt)
15} derive((QueryError, &Logger) -> Unit
Show)
16
17///|
18fn type! QueryError {
Syntax(UInt)
NodeType(UInt)
Field(UInt)
Capture(UInt)
Structure(UInt)
Language(UInt)
}
QueryError::(self : QueryError) -> UInt
offset(QueryError
self : type! QueryError {
Syntax(UInt)
NodeType(UInt)
Field(UInt)
Capture(UInt)
Structure(UInt)
Language(UInt)
}
QueryError) -> UInt
UInt {
19 match QueryError
self {
20 QueryError::(UInt) -> QueryError
Syntax(UInt
offset) => UInt
offset
21 QueryError::(UInt) -> QueryError
NodeType(UInt
offset) => UInt
offset
22 QueryError::(UInt) -> QueryError
Field(UInt
offset) => UInt
offset
23 QueryError::(UInt) -> QueryError
Capture(UInt
offset) => UInt
offset
24 QueryError::(UInt) -> QueryError
Structure(UInt
offset) => UInt
offset
25 QueryError::(UInt) -> QueryError
Language(UInt
offset) => UInt
offset
26 }
27}
28
29///|
30fn type! QueryError {
Syntax(UInt)
NodeType(UInt)
Field(UInt)
Capture(UInt)
Structure(UInt)
Language(UInt)
}
QueryError::(offset : UInt, type_ : UInt) -> QueryError
from(UInt
offset : UInt
UInt, UInt
type_ : UInt
UInt) -> type! QueryError {
Syntax(UInt)
NodeType(UInt)
Field(UInt)
Capture(UInt)
Structure(UInt)
Language(UInt)
}
QueryError {
31 match UInt
type_ {
32 1 => QueryError::(UInt) -> QueryError
Syntax(UInt
offset)
33 2 => QueryError::(UInt) -> QueryError
NodeType(UInt
offset)
34 3 => QueryError::(UInt) -> QueryError
Field(UInt
offset)
35 4 => QueryError::(UInt) -> QueryError
Capture(UInt
offset)
36 5 => QueryError::(UInt) -> QueryError
Structure(UInt
offset)
37 6 => QueryError::(UInt) -> QueryError
Language(UInt
offset)
38 UInt
type_ => (msg : String) -> QueryError
Aborts the program with an error message. Always causes a panic, regardless
of the message provided.
Parameters:
message : A string containing the error message to be displayed when
aborting.
Returns a value of type T. However, this function never actually returns a
value as it always causes a panic.
abort("Invalid QueryError type: \{UInt
type_}")
39 }
40}
41
42///|
43#borrow(source, error)
44extern "c" fn ts_query_new(
45 language : type Language @tree_sitter_language.Language
Language,
46 source : Bytes
Bytes,
47 error : type FixedArray[A]
FixedArray[UInt
UInt]
48) -> type Query
Query = "moonbit_ts_query_new"
49
50///|
51/// Create a new query from a string containing one or more S-expression
52/// patterns. The query is associated with a particular language, and can
53/// only be run on syntax nodes parsed with that language.
54///
55/// If all of the given patterns are valid, this returns a `Query`.
56/// If a pattern is invalid, this raises a `QueryError`.
57pub fn type Query
Query::(language : Language, source : @string.StringView) -> Query!QueryError
Create a new query from a string containing one or more S-expression
patterns. The query is associated with a particular language, and can
only be run on syntax nodes parsed with that language.
If all of the given patterns are valid, this returns a Query.
If a pattern is invalid, this raises a QueryError.
new(
58 Language
language : type Language @tree_sitter_language.Language
Language,
59 @string.StringView
source : type @string.StringView
A @string.View represents a view of a String that maintains proper Unicode
character boundaries. It allows safe access to a substring while handling
multi-byte characters correctly.
@string.View
60) -> type Query
Query!type! QueryError {
Syntax(UInt)
NodeType(UInt)
Field(UInt)
Capture(UInt)
Structure(UInt)
Language(UInt)
}
QueryError {
61 let FixedArray[UInt]
error = 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(2, 0U)
62 let Bytes
source = (encoding~ : @encoding.Encoding, string : @string.StringView) -> Bytes
Encode a given string to the specified character encoding and returns the resulting bytes.
Parameters
encoding : The target encoding format.
src: The input string to be encoded.
Returns
A bytes representing the encoded string in the selected format.
Examples
let src = "Hello, World!"
let encoded_bytes = encode(UTF8, src)
@encoding.encode(@encoding.Encoding
encoding=@encoding.Encoding
UTF8, @string.StringView
source)
63 let Query
query = (ts : TS, language : Language, source : String, error : FixedArray[UInt]) -> Query
ts_query_new(Language
language, Bytes
source, FixedArray[UInt]
error)
64 if FixedArray[UInt]
error[1] (self : UInt, other : UInt) -> Bool
Compares two unsigned 32-bit integers for equality.
Parameters:
self : The first unsigned integer operand.
other : The second unsigned integer operand to compare with.
Returns true if both integers have the same value, false otherwise.
Example:
test "UInt::op_equal" {
let a = 42U
let b = 42U
let c = 24U
inspect!(a == b, content="true")
inspect!(a == c, content="false")
}
== 0 {
65 Query
query
66 } else {
67 raise type! QueryError {
Syntax(UInt)
NodeType(UInt)
Field(UInt)
Capture(UInt)
Structure(UInt)
Language(UInt)
}
QueryError::(offset : UInt, type_ : UInt) -> QueryError
from(FixedArray[UInt]
error[0], FixedArray[UInt]
error[1])
68 }
69}
70
71///|
72/// Get the number of patterns in the query.
73#borrow(self)
74extern "c" fn ts_query_pattern_count(self : type Query
Query) -> UInt
UInt = "moonbit_ts_query_pattern_count"
75
76///|
77/// Get the number of patterns in the query.
78pub fn type Query
Query::(self : Query) -> Int
Get the number of patterns in the query.
pattern_count(Query
self : type Query
Query) -> Int
Int {
79 (value : UInt) -> Int
uint_to_int((self : Query) -> UInt
ts_query_pattern_count(Query
self))
80}
81
82///|
83/// Get the number of captures in the query.
84#borrow(self)
85extern "c" fn ts_query_capture_count(self : type Query
Query) -> UInt
UInt = "moonbit_ts_query_capture_count"
86
87///|
88/// Get the number of captures in the query.
89pub fn type Query
Query::(self : Query) -> Int
Get the number of captures in the query.
capture_count(Query
self : type Query
Query) -> Int
Int {
90 (value : UInt) -> Int
uint_to_int((self : Query) -> UInt
ts_query_capture_count(Query
self))
91}
92
93///|
94/// Get the number of string literals in the query.
95#borrow(self)
96extern "c" fn ts_query_string_count(self : type Query
Query) -> UInt
UInt = "moonbit_ts_query_string_count"
97
98///|
99/// Get the number of string literals in the query.
100pub fn type Query
Query::(self : Query) -> Int
Get the number of string literals in the query.
string_count(Query
self : type Query
Query) -> Int
Int {
101 (value : UInt) -> Int
uint_to_int((self : Query) -> UInt
ts_query_string_count(Query
self))
102}
103
104///|
105#borrow(self)
106extern "c" fn ts_query_start_byte_for_pattern(
107 self : type Query
Query,
108 pattern_index : UInt
UInt
109) -> UInt
UInt = "moonbit_ts_query_start_byte_for_pattern"
110
111///|
112/// Get the byte offset where the given pattern starts in the query's source.
113///
114/// This can be useful when combining queries by concatenating their source
115/// code strings.
116pub fn type Query
Query::(self : Query, pattern_index : Int) -> Int
Get the byte offset where the given pattern starts in the query's source.
This can be useful when combining queries by concatenating their source
code strings.
start_byte_for_pattern(Query
self : type Query
Query, Int
pattern_index : Int
Int) -> Int
Int {
117 let UInt
pattern_index = (value : Int) -> UInt
int_to_uint(Int
pattern_index)
118 (self : Query, pattern_index : UInt) -> UInt
ts_query_start_byte_for_pattern(Query
self, UInt
pattern_index) |> (value : UInt) -> Int
uint_to_int()
119}
120
121///|
122#borrow(self)
123extern "c" fn ts_query_end_byte_for_pattern(
124 self : type Query
Query,
125 pattern_index : UInt
UInt
126) -> UInt
UInt = "moonbit_ts_query_end_byte_for_pattern"
127
128///|
129/// Get the byte offset where the given pattern ends in the query's source.
130///
131/// This can be useful when combining queries by concatenating their source
132/// code strings.
133pub fn type Query
Query::(self : Query, pattern_index : Int) -> Int
Get the byte offset where the given pattern ends in the query's source.
This can be useful when combining queries by concatenating their source
code strings.
end_byte_for_pattern(Query
self : type Query
Query, Int
pattern_index : Int
Int) -> Int
Int {
134 let UInt
pattern_index = (value : Int) -> UInt
int_to_uint(Int
pattern_index)
135 (self : Query, pattern_index : UInt) -> UInt
ts_query_end_byte_for_pattern(Query
self, UInt
pattern_index) |> (value : UInt) -> Int
uint_to_int()
136}
137
138///|
139#borrow(self)
140extern "c" fn ts_query_predicates_for_pattern(
141 self : type Query
Query,
142 pattern_index : UInt
UInt
143) -> type FixedArray[A]
FixedArray[UInt
UInt] = "moonbit_ts_query_predicates_for_pattern"
144
145///|
146priv enum QueryPredicateStepType {
147 QueryPredicateStepType
Done
148 QueryPredicateStepType
Capture
149 QueryPredicateStepType
String
150} derive((QueryPredicateStepType, &Logger) -> Unit
Show)
151
152///|
153fn enum QueryPredicateStepType {
Done
Capture
String
}
QueryPredicateStepType::(value : UInt) -> QueryPredicateStepType
of_uint(UInt
value : UInt
UInt) -> enum QueryPredicateStepType {
Done
Capture
String
}
QueryPredicateStepType {
154 match UInt
value {
155 0 => QueryPredicateStepType::QueryPredicateStepType
Done
156 1 => QueryPredicateStepType::QueryPredicateStepType
Capture
157 2 => QueryPredicateStepType::QueryPredicateStepType
String
158 UInt
value => (msg : String) -> QueryPredicateStepType
Aborts the program with an error message. Always causes a panic, regardless
of the message provided.
Parameters:
message : A string containing the error message to be displayed when
aborting.
Returns a value of type T. However, this function never actually returns a
value as it always causes a panic.
abort("Invalid QueryPredicateStepType: \{UInt
value}")
159 }
160}
161
162///|
163pub enum QueryPredicateStep {
164 (String) -> QueryPredicateStep
Capture(String
String)
165 (String) -> QueryPredicateStep
String(String
String)
166}
167
168///|
169pub impl trait Show {
output(Self, &Logger) -> Unit
to_string(Self) -> String
}
Show for enum QueryPredicateStep {
Capture(String)
String(String)
}
QueryPredicateStep with (self : QueryPredicateStep, logger : &Logger) -> Unit
output(
170 QueryPredicateStep
self,
171 &Logger
logger : &trait Logger {
write_string(Self, String) -> Unit
write_substring(Self, String, Int, Int) -> Unit
write_char(Self, Char) -> Unit
}
Trait for a logger, where debug logs can be written into
@builtin.Logger
172) -> Unit
Unit {
173 match QueryPredicateStep
self {
174 (String) -> QueryPredicateStep
Capture(String
name) => &Logger
logger.(&Logger, String) -> Unit
write_string("@\{String
name}")
175 (String) -> QueryPredicateStep
String(String
value) => &Logger
logger.(&Logger, String) -> Unit
write_string(String
value.(self : String) -> String
Returns a valid MoonBit string literal representation of a string,
add quotes and escape special characters.
escape())
176 }
177}
178
179///|
180pub impl trait ToJson {
to_json(Self) -> Json
}
ToJson for enum QueryPredicateStep {
Capture(String)
String(String)
}
QueryPredicateStep with (self : QueryPredicateStep) -> Json
to_json(QueryPredicateStep
self) -> enum Json {
Null
True
False
Number(Double)
String(String)
Array(Array[Json])
Object(Map[String, Json])
}
Json {
181 QueryPredicateStep
self.(self : QueryPredicateStep) -> String
to_string().(self : String) -> Json
to_json()
182}
183
184///|
185pub typealias QueryPredicate = type Array[T]
Array[enum QueryPredicateStep {
Capture(String)
String(String)
}
QueryPredicateStep]
186
187///|
188/// Get all of the predicates for the given pattern in the query.
189///
190/// The predicates are represented as a single array of array of steps.
191/// There are two types of steps in this array:
192/// - `QueryPredicateStep::Capture` - Steps with this type represent names
193/// of captures. Their `value_id` can be used with the
194/// `Query::capture_name_for_id` function to obtain the name of the capture.
195/// - `QueryPredicateStep::String` - Steps with this type represent literal
196/// strings. Their `value_id` can be used with the
197/// `Query::string_value_for_id` function to obtain their string value.
198pub fn type Query
Query::(self : Query, pattern_index : Int) -> Array[Array[QueryPredicateStep]]
predicates_for_pattern(
199 Query
self : type Query
Query,
200 Int
pattern_index : Int
Int
201) -> type Array[T]
Array[type Array[T]
An Array is a collection of values that supports random access and can
grow in size.
QueryPredicate] {
202 let UInt
pattern_index = (value : Int) -> UInt
int_to_uint(Int
pattern_index)
203 let FixedArray[UInt]
flatten_predicates = (self : Query, pattern_index : UInt) -> FixedArray[UInt]
ts_query_predicates_for_pattern(Query
self, UInt
pattern_index)
204 let Array[Array[QueryPredicateStep]]
predicates = []
205 let mut Array[QueryPredicateStep]
predicate = []
206 for Int
i in Int
0..<(FixedArray[UInt]
flatten_predicates.(self : FixedArray[UInt]) -> Int
Returns the number of elements in a fixed-size array.
Parameters:
array : The fixed-size array whose length is to be determined.
Returns an integer representing the number of elements in the array.
Example:
test "FixedArray::length" {
let arr = FixedArray::make(3, 42)
inspect!(arr.length(), content="3")
}
length() (self : Int, other : Int) -> Int
Performs integer division between two 32-bit integers. The result is
truncated towards zero (rounds down for positive numbers and up for negative
numbers).
Parameters:
dividend : The first integer operand to be divided.
divisor : The second integer operand that divides the dividend.
Returns the quotient of the division operation.
Throws a panic if divisor is zero.
Example:
test "Int::op_div" {
inspect!(10 / 3, content="3") // truncates towards zero
inspect!(-10 / 3, content="-3")
inspect!(10 / -3, content="-3")
}
test "panic Int::op_div/division_by_zero" {
ignore(42 / 0) // Panics with division by zero
}
/ 2) {
207 let UInt
value = FixedArray[UInt]
flatten_predicates(FixedArray[UInt], Int) -> UInt
Retrieves an element at the specified index from a fixed-size array. This
function implements the array indexing operator [].
Parameters:
array : The fixed-size array to access.
index : The position in the array from which to retrieve the element.
Returns the element at the specified index.
Throws a runtime error if the index is out of bounds (negative or greater
than or equal to the length of the array).
Example:
test "FixedArray::op_get" {
let arr = FixedArray::make(3, 42)
inspect!(arr[1], content="42")
}
test "panic FixedArray::op_get/out_of_bounds" {
let arr = FixedArray::make(3, 0)
ignore(arr[3]) // Index out of bounds
}
[i (self : Int, other : Int) -> Int
Multiplies two 32-bit integers. This is the implementation of the *
operator for Int.
Parameters:
self : The first integer operand.
other : The second integer operand.
Returns the product of the two integers. If the result overflows the range of
Int, it wraps around according to two's complement arithmetic.
Example:
test "Int::op_mul" {
inspect!(42 * 2, content="84")
inspect!(-10 * 3, content="-30")
let max = 2147483647 // Int.max_value
inspect!(max * 2, content="-2") // Overflow wraps around
}
* 2 (self : Int, other : Int) -> Int
Adds two 32-bit signed integers. Performs two's complement arithmetic, which
means the operation will wrap around if the result exceeds the range of a
32-bit integer.
Parameters:
self : The first integer operand.
other : The second integer operand.
Returns a new integer that is the sum of the two operands. If the
mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to
2,147,483,647), the result wraps around according to two's complement rules.
Example:
test "Int::op_add" {
inspect!(42 + 1, content="43")
inspect!(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+ 1]
208 let Int
value = (value : UInt) -> Int
uint_to_int(UInt
value)
209 let QueryPredicateStepType
type_ = enum QueryPredicateStepType {
Done
Capture
String
}
QueryPredicateStepType::(value : UInt) -> QueryPredicateStepType
of_uint(FixedArray[UInt]
flatten_predicates(FixedArray[UInt], Int) -> UInt
Retrieves an element at the specified index from a fixed-size array. This
function implements the array indexing operator [].
Parameters:
array : The fixed-size array to access.
index : The position in the array from which to retrieve the element.
Returns the element at the specified index.
Throws a runtime error if the index is out of bounds (negative or greater
than or equal to the length of the array).
Example:
test "FixedArray::op_get" {
let arr = FixedArray::make(3, 42)
inspect!(arr[1], content="42")
}
test "panic FixedArray::op_get/out_of_bounds" {
let arr = FixedArray::make(3, 0)
ignore(arr[3]) // Index out of bounds
}
[i (self : Int, other : Int) -> Int
Multiplies two 32-bit integers. This is the implementation of the *
operator for Int.
Parameters:
self : The first integer operand.
other : The second integer operand.
Returns the product of the two integers. If the result overflows the range of
Int, it wraps around according to two's complement arithmetic.
Example:
test "Int::op_mul" {
inspect!(42 * 2, content="84")
inspect!(-10 * 3, content="-30")
let max = 2147483647 // Int.max_value
inspect!(max * 2, content="-2") // Overflow wraps around
}
* 2])
210 match QueryPredicateStepType
type_ {
211 QueryPredicateStepType
Capture => {
212 let @string.StringView
name = type Query
Query::(self : Query, capture_id : Int) -> @string.StringView
Get the name of one of the query's captures. Each capture is associated with a
numeric id based on the order that it appeared in the query's source.
capture_name_for_id(Query
self, Int
value)
213 Array[QueryPredicateStep]
predicate.(self : Array[QueryPredicateStep], value : QueryPredicateStep) -> Unit
Adds an element to the end of the array.
If the array is at capacity, it will be reallocated.
Example
let v = []
v.push(3)
push(QueryPredicateStep::(String) -> QueryPredicateStep
Capture(@string.StringView
name))
214 }
215 QueryPredicateStepType
String => {
216 let String
value = type Query
Query::(self : Query, string_id : Int) -> String?
Get the value of one of the query's string literals. Each string is associated with a
numeric id based on the order that it appeared in the query's source.
string_value_for_id(Query
self, Int
value).(self : String?) -> String
Extract the value in Some.
If the value is None, it throws a panic.
unwrap()
217 Array[QueryPredicateStep]
predicate.(self : Array[QueryPredicateStep], value : QueryPredicateStep) -> Unit
Adds an element to the end of the array.
If the array is at capacity, it will be reallocated.
Example
let v = []
v.push(3)
push(QueryPredicateStep::(String) -> QueryPredicateStep
String(String
value))
218 }
219 QueryPredicateStepType
Done => {
220 Array[Array[QueryPredicateStep]]
predicates.(self : Array[Array[QueryPredicateStep]], value : Array[QueryPredicateStep]) -> Unit
Adds an element to the end of the array.
If the array is at capacity, it will be reallocated.
Example
let v = []
v.push(3)
push(Array[QueryPredicateStep]
predicate)
221 Array[QueryPredicateStep]
predicate = []
222 }
223 }
224 }
225 Array[Array[QueryPredicateStep]]
predicates
226}
227
228///|
229#borrow(self)
230extern "c" fn ts_query_is_pattern_rooted(
231 self : type Query
Query,
232 pattern_index : UInt
UInt
233) -> Bool
Bool = "moonbit_ts_query_is_pattern_rooted"
234
235///|
236/// Check if the given pattern in the query has a single root node.
237pub fn type Query
Query::(self : Query, pattern_index : Int) -> Bool
Check if the given pattern in the query has a single root node.
is_pattern_rooted(Query
self : type Query
Query, Int
pattern_index : Int
Int) -> Bool
Bool {
238 (self : Query, pattern_index : UInt) -> Bool
ts_query_is_pattern_rooted(Query
self, (value : Int) -> UInt
int_to_uint(Int
pattern_index))
239}
240
241///|
242#borrow(self)
243extern "c" fn ts_query_is_pattern_non_local(
244 self : type Query
Query,
245 pattern_index : UInt
UInt
246) -> Bool
Bool = "moonbit_ts_query_is_pattern_non_local"
247
248///|
249/// Check if the given pattern in the query is 'non local'.
250///
251/// A non-local pattern has multiple root nodes and can match within a
252/// repeating sequence of nodes, as specified by the grammar. Non-local
253/// patterns disable certain optimizations that would otherwise be possible
254/// when executing a query on a specific range of a syntax tree.
255pub fn type Query
Query::(self : Query, pattern_index : Int) -> Bool
Check if the given pattern in the query is 'non local'.
A non-local pattern has multiple root nodes and can match within a
repeating sequence of nodes, as specified by the grammar. Non-local
patterns disable certain optimizations that would otherwise be possible
when executing a query on a specific range of a syntax tree.
is_pattern_non_local(Query
self : type Query
Query, Int
pattern_index : Int
Int) -> Bool
Bool {
256 (self : Query, pattern_index : UInt) -> Bool
ts_query_is_pattern_non_local(Query
self, (value : Int) -> UInt
int_to_uint(Int
pattern_index))
257}
258
259///|
260#borrow(self)
261extern "c" fn ts_query_is_pattern_guaranteed_at_step(
262 self : type Query
Query,
263 byte_offset : UInt
UInt
264) -> Bool
Bool = "moonbit_ts_query_is_pattern_guaranteed_at_step"
265
266///|
267/// Check if a given pattern is guaranteed to match once a given step is reached.
268/// The step is specified by its byte offset in the query's source code.
269pub fn type Query
Query::(self : Query, byte_offset : Int) -> Bool
Check if a given pattern is guaranteed to match once a given step is reached.
The step is specified by its byte offset in the query's source code.
is_pattern_guaranteed_at_step(
270 Query
self : type Query
Query,
271 Int
byte_offset : Int
Int
272) -> Bool
Bool {
273 (self : Query, byte_offset : UInt) -> Bool
ts_query_is_pattern_guaranteed_at_step(Query
self, (value : Int) -> UInt
int_to_uint(Int
byte_offset))
274}
275
276///|
277#borrow(self, length)
278extern "c" fn ts_query_capture_name_for_id(
279 self : type Query
Query,
280 capture_id : UInt
UInt,
281 length : type FixedArray[A]
FixedArray[UInt
UInt]
282) -> @c.Pointer[Byte
Byte] = "moonbit_ts_query_capture_name_for_id"
283
284///|
285/// Get the name of one of the query's captures. Each capture is associated with a
286/// numeric id based on the order that it appeared in the query's source.
287pub fn type Query
Query::(self : Query, capture_id : Int) -> String
Get the name of one of the query's captures. Each capture is associated with a
numeric id based on the order that it appeared in the query's source.
capture_name_for_id(Query
self : type Query
Query, Int
capture_id : Int
Int) -> String
String {
288 let UInt
capture_id = (value : Int) -> UInt
int_to_uint(Int
capture_id)
289 let FixedArray[UInt]
length : type FixedArray[A]
FixedArray[UInt
UInt] = [0U]
290 let ?
name = (self : Query, capture_id : UInt, FixedArray[UInt]) -> ?
ts_query_capture_name_for_id(Query
self, UInt
capture_id, FixedArray[UInt]
length)
291 return (c_string : ?, length~ : Int) -> String?
decode_c_string(?
name, Int
length=FixedArray[UInt]
length[0].(self : UInt) -> Int
reinterpret the unsigned int as signed int
For number within the range of 0..=2^31-1,
the value is the same. For number within the range of 2^31..=2^32-1,
the value is negative
reinterpret_as_int()).(self : String?) -> String
Extract the value in Some.
If the value is None, it throws a panic.
unwrap()
292}
293
294///|
295pub enum Quantifier {
296 Quantifier
Zero
297 Quantifier
ZeroOrOne
298 Quantifier
ZeroOrMore
299 Quantifier
One
300 Quantifier
OneOrMore
301} derive((Quantifier, &Logger) -> Unit
Show)
302
303///|
304fn enum Quantifier {
Zero
ZeroOrOne
ZeroOrMore
One
OneOrMore
}
Quantifier::(value : UInt) -> Quantifier
of_uint(UInt
value : UInt
UInt) -> enum Quantifier {
Zero
ZeroOrOne
ZeroOrMore
One
OneOrMore
}
Quantifier {
305 match UInt
value {
306 0 => Quantifier::Quantifier
Zero
307 1 => Quantifier::Quantifier
ZeroOrOne
308 2 => Quantifier::Quantifier
ZeroOrMore
309 3 => Quantifier::Quantifier
One
310 4 => Quantifier::Quantifier
OneOrMore
311 UInt
value => (msg : String) -> Quantifier
Aborts the program with an error message. Always causes a panic, regardless
of the message provided.
Parameters:
message : A string containing the error message to be displayed when
aborting.
Returns a value of type T. However, this function never actually returns a
value as it always causes a panic.
abort("Invalid Quantifier: \{UInt
value}")
312 }
313}
314
315///|
316#borrow(self)
317extern "c" fn ts_query_capture_quantifier_for_id(
318 self : type Query
Query,
319 pattern_index : UInt
UInt,
320 capture_index : UInt
UInt
321) -> UInt
UInt = "moonbit_ts_query_capture_quantifier_for_id"
322
323///|
324/// Get the quantifier of the query's captures. Each capture is
325/// associated with a numeric id based on the order that it appeared in the query's source.
326pub fn type Query
Query::(self : Query, pattern_index : Int, capture_index : Int) -> Quantifier
Get the quantifier of the query's captures. Each capture is
associated with a numeric id based on the order that it appeared in the query's source.
capture_quantifier_for_id(
327 Query
self : type Query
Query,
328 Int
pattern_index : Int
Int,
329 Int
capture_index : Int
Int
330) -> enum Quantifier {
Zero
ZeroOrOne
ZeroOrMore
One
OneOrMore
}
Quantifier {
331 let UInt
pattern_index = (value : Int) -> UInt
int_to_uint(Int
pattern_index)
332 let UInt
capture_index = (value : Int) -> UInt
int_to_uint(Int
capture_index)
333 enum Quantifier {
Zero
ZeroOrOne
ZeroOrMore
One
OneOrMore
}
Quantifier::(value : UInt) -> Quantifier
of_uint(
334 (self : Query, pattern_index : UInt, capture_index : UInt) -> UInt
ts_query_capture_quantifier_for_id(Query
self, UInt
pattern_index, UInt
capture_index),
335 )
336}
337
338///|
339#borrow(self)
340extern "c" fn ts_query_string_value_for_id(
341 self : type Query
Query,
342 string_id : UInt
UInt
343) -> @c.Nullable[Bytes
Bytes] = "moonbit_ts_query_string_value_for_id"
344
345///|
346/// Get the value of one of the query's string literals. Each string is associated with a
347/// numeric id based on the order that it appeared in the query's source.
348pub fn type Query
Query::(self : Query, string_id : Int) -> String?
Get the value of one of the query's string literals. Each string is associated with a
numeric id based on the order that it appeared in the query's source.
string_value_for_id(Query
self : type Query
Query, Int
string_id : Int
Int) -> String
String? {
349 let UInt
string_id = (value : Int) -> UInt
int_to_uint(Int
string_id)
350 let @bytes.View?
value = (self : Query, string_id : UInt) -> ?
ts_query_string_value_for_id(Query
self, UInt
string_id).() -> @bytes.View?
to()
351 guard @bytes.View?
value is (@bytes.View) -> @bytes.View?
Some(@bytes.View
value) else { return String?
None }
352 (String) -> String?
Some((input : @bytes.View, encoding~ : @encoding.Encoding) -> String
@encoding.decode_lossy(@bytes.View
value, @encoding.Encoding
encoding=@encoding.Encoding
UTF8))
353}
354
355///|
356#borrow(self, name)
357extern "c" fn ts_query_disable_capture(self : type Query
Query, name : Bytes
Bytes) = "moonbit_ts_query_disable_capture"
358
359///|
360/// Disable a certain capture within a query.
361///
362/// This prevents the capture from being returned in matches, and also avoids
363/// any resource usage associated with recording the capture. Currently, there
364/// is no way to undo this.
365pub fn type Query
Query::(self : Query, name : @string.StringView) -> Unit
Disable a certain capture within a query.
This prevents the capture from being returned in matches, and also avoids
any resource usage associated with recording the capture. Currently, there
is no way to undo this.
disable_capture(Query
self : type Query
Query, @string.StringView
name : type @string.StringView
A @string.View represents a view of a String that maintains proper Unicode
character boundaries. It allows safe access to a substring while handling
multi-byte characters correctly.
@string.View) -> Unit
Unit {
366 let Bytes
name = (encoding~ : @encoding.Encoding, string : @string.StringView) -> Bytes
Encode a given string to the specified character encoding and returns the resulting bytes.
Parameters
encoding : The target encoding format.
src: The input string to be encoded.
Returns
A bytes representing the encoded string in the selected format.
Examples
let src = "Hello, World!"
let encoded_bytes = encode(UTF8, src)
@encoding.encode(@encoding.Encoding
encoding=@encoding.Encoding
UTF8, @string.StringView
name)
367 (self : Query, name : Bytes) -> Unit
ts_query_disable_capture(Query
self, Bytes
name)
368}
369
370///|
371#borrow(self)
372extern "c" fn ts_query_disable_pattern(self : type Query
Query, pattern_index : UInt
UInt) = "moonbit_ts_query_disable_pattern"
373
374///|
375/// Disable a certain pattern within a query.
376///
377/// This prevents the pattern from matching and removes most of the overhead
378/// associated with the pattern. Currently, there is no way to undo this.
379pub fn type Query
Query::(self : Query, pattern_index : Int) -> Unit
Disable a certain pattern within a query.
This prevents the pattern from matching and removes most of the overhead
associated with the pattern. Currently, there is no way to undo this.
disable_pattern(Query
self : type Query
Query, Int
pattern_index : Int
Int) -> Unit
Unit {
380 (self : Query, pattern_index : UInt) -> Unit
ts_query_disable_pattern(Query
self, (value : Int) -> UInt
int_to_uint(Int
pattern_index))
381}
382
383///|
384pub fn type Query
Query::(self : Query, node : Node) -> Iter[QueryMatch]
matches(Query
self : type Query
Query, Node
node : struct Node {
node: TSNode
tree: TSTree
text: @string.StringView
}
Node) -> type Iter[T]
Iter[struct QueryMatch {
query: Query
query_match: TSQueryMatch
}
QueryMatch] {
385 let QueryCursor
cursor = 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()
386 QueryCursor
cursor.(self : QueryCursor, query : Query, node : Node, options? : QueryCursorOptions) -> Unit
Start running a given query on a given node, with optional options.
exec(Query
self, Node
node)
387 QueryCursor
cursor.(self : QueryCursor) -> Iter[QueryMatch]
matches()
388}
389
390///|
391pub fn type Query
Query::(self : Query, node : Node) -> Iter[QueryCapture]
captures(Query
self : type Query
Query, Node
node : struct Node {
node: TSNode
tree: TSTree
text: @string.StringView
}
Node) -> type Iter[T]
Iter[type QueryCapture
QueryCapture] {
392 let QueryCursor
cursor = 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()
393 QueryCursor
cursor.(self : QueryCursor, query : Query, node : Node, options? : QueryCursorOptions) -> Unit
Start running a given query on a given node, with optional options.
exec(Query
self, Node
node)
394 QueryCursor
cursor.(self : QueryCursor) -> Iter[QueryCapture]
captures()
395}
396