1///|
2type Parser
3
4///|
5extern "c" fn ts_parser_new() -> type Parser
Parser = "moonbit_ts_parser_new"
6
7///|
8#borrow(self)
9extern "c" fn type Parser
Parser::is_null(self : type Parser
Parser) -> Bool
Bool = "moonbit_c_is_null"
10
11///|
12/// Create a new parser.
13pub fn type Parser
Parser::() -> Parser
Create a new parser.
new() -> type Parser
Parser {
14 () -> Parser
ts_parser_new()
15}
16
17///|
18pub fn (language : Language) -> Parser!LanguageError
parser(Language
language : type Language @tree_sitter_language.Language
Language) -> type Parser
Parser!type! LanguageError {
VersionMismatch(language_abi_version~ : Int, library_version~ : Int, min_compatible_version~ : Int)
}
LanguageError {
19 let Parser
parser = type Parser
Parser::() -> Parser
Create a new parser.
new()
20 Parser
parser.(self : Parser, language : Language) -> Unit!LanguageError
Set the language that the parser should use for parsing.
Returns a boolean indicating whether or not the language was successfully
assigned. True means assignment succeeded. False means there was a version
mismatch: the language was generated with an incompatible version of the
Tree-sitter CLI. Check the language's ABI version using
Language::abi_version and compare it to this library's LANGUAGE_VERSION
and MIN_COMPATIBLE_LANGUAGE_VERSION constants.
set_language!(Language
language)
21 return Parser
parser
22}
23
24///|
25#borrow(self)
26extern "c" fn ts_parser_language(self : type Parser
Parser) -> @c.Nullable[type Language @tree_sitter_language.Language
Language] = "moonbit_ts_parser_language"
27
28///|
29/// Get the parser's current language.
30pub fn type Parser
Parser::(self : Parser) -> Language?
Get the parser's current language.
language(Parser
self : type Parser
Parser) -> type Language @tree_sitter_language.Language
Language? {
31 (self : Parser) -> Language
ts_parser_language(Parser
self).() -> Language?
to()
32}
33
34///|
35#borrow(self)
36extern "c" fn ts_parser_set_language(
37 self : type Parser
Parser,
38 language : type Language @tree_sitter_language.Language
Language
39) -> Bool
Bool = "moonbit_ts_parser_set_language"
40
41///|
42type! LanguageError {
43 VersionMismatch(
44 language_abi_version~ : Int,
45 library_version~ : Int,
46 min_compatible_version~ : Int
47 )
48}
49
50///|
51impl trait ToJson {
to_json(Self) -> Json
}
ToJson for type! LanguageError {
VersionMismatch(language_abi_version~ : Int, library_version~ : Int, min_compatible_version~ : Int)
}
LanguageError with (self : LanguageError) -> Json
to_json(LanguageError
self) -> enum Json {
Null
True
False
Number(Double)
String(String)
Array(Array[Json])
Object(Map[String, Json])
}
Json {
52 match LanguageError
self {
53 (Int, Int, Int) -> LanguageError
VersionMismatch(
54 Int
language_abi_version~,
55 Int
library_version~,
56 Int
min_compatible_version~
57 ) =>
58 {
59 "language_abi_version": Int
language_abi_version.(self : Int) -> Json
to_json(),
60 "library_version": Int
library_version.(self : Int) -> Json
to_json(),
61 "min_compatible_version": Int
min_compatible_version.(self : Int) -> Json
to_json(),
62 }
63 }
64}
65
66///|
67/// Set the language that the parser should use for parsing.
68///
69/// Returns a boolean indicating whether or not the language was successfully
70/// assigned. True means assignment succeeded. False means there was a version
71/// mismatch: the language was generated with an incompatible version of the
72/// Tree-sitter CLI. Check the language's ABI version using
73/// `Language::abi_version` and compare it to this library's `LANGUAGE_VERSION`
74/// and `MIN_COMPATIBLE_LANGUAGE_VERSION` constants.
75pub fn type Parser
Parser::(self : Parser, language : Language) -> Unit!LanguageError
Set the language that the parser should use for parsing.
Returns a boolean indicating whether or not the language was successfully
assigned. True means assignment succeeded. False means there was a version
mismatch: the language was generated with an incompatible version of the
Tree-sitter CLI. Check the language's ABI version using
Language::abi_version and compare it to this library's LANGUAGE_VERSION
and MIN_COMPATIBLE_LANGUAGE_VERSION constants.
set_language(
76 Parser
self : type Parser
Parser,
77 Language
language : type Language @tree_sitter_language.Language
Language
78) -> Unit
Unit!type! LanguageError {
VersionMismatch(language_abi_version~ : Int, library_version~ : Int, min_compatible_version~ : Int)
}
LanguageError {
79 let Bool
succeed = (self : Parser, language : Language) -> Bool
ts_parser_set_language(Parser
self, Language
language)
80 if (x : Bool) -> Bool
Performs logical negation on a boolean value.
Parameters:
value : The boolean value to negate.
Returns the logical NOT of the input value: true if the input is false,
and false if the input is true.
Example:
test "not" {
inspect!(not(true), content="false")
inspect!(not(false), content="true")
}
not(Bool
succeed) {
81 raise (language_abi_version~ : Int, library_version~ : Int, min_compatible_version~ : Int) -> LanguageError
VersionMismatch(
82 Int
language_abi_version=Language
language.(self : Language) -> Int
Get the ABI version number for this language. This version number is used
to ensure that languages were generated by a compatible version of
Tree-sitter.
See also Parser::set_language.
abi_version(),
83 Int
library_version=Int
The latest ABI version that is supported by the current version of the
library. When Languages are generated by the Tree-sitter CLI, they are
assigned an ABI version number that corresponds to the current CLI version.
The Tree-sitter library is generally backwards-compatible with languages
generated using older CLI versions, but is not forwards-compatible.
LANGUAGE_VERSION,
84 Int
min_compatible_version=Int
The earliest ABI version that is supported by the current version of the
library.
MIN_COMPATIBLE_LANGUAGE_VERSION,
85 )
86 }
87}
88
89///|
90/// The latest ABI version that is supported by the current version of the
91/// library. When Languages are generated by the Tree-sitter CLI, they are
92/// assigned an ABI version number that corresponds to the current CLI version.
93/// The Tree-sitter library is generally backwards-compatible with languages
94/// generated using older CLI versions, but is not forwards-compatible.
95pub const Int
The latest ABI version that is supported by the current version of the
library. When Languages are generated by the Tree-sitter CLI, they are
assigned an ABI version number that corresponds to the current CLI version.
The Tree-sitter library is generally backwards-compatible with languages
generated using older CLI versions, but is not forwards-compatible.
LANGUAGE_VERSION = 15
96
97///|
98///
99/// The earliest ABI version that is supported by the current version of the
100/// library.
101pub const Int
The earliest ABI version that is supported by the current version of the
library.
MIN_COMPATIBLE_LANGUAGE_VERSION = 13
102
103///|
104#borrow(self, ranges)
105extern "c" fn ts_parser_set_included_ranges(
106 self : type Parser
Parser,
107 ranges : type FixedArray[A]
FixedArray[UInt
UInt]
108) -> Bool
Bool = "moonbit_ts_parser_set_included_ranges"
109
110///|
111/// Set the ranges of text that the parser should include when parsing.
112///
113/// By default, the parser will always include entire documents. This function
114/// allows you to parse only a *portion* of a document but still return a syntax
115/// tree whose ranges match up with the document as a whole. You can also pass
116/// multiple disjoint ranges.
117///
118/// The second and third parameters specify the location and length of an array
119/// of ranges.
120///
121/// If `count` is zero, then the entire document will be parsed. Otherwise,
122/// the given ranges must be ordered from earliest to latest in the document,
123/// and they must not overlap. That is, the following must hold for all:
124///
125/// `i < count - 1`: `ranges[i].end_byte <= ranges[i + 1].start_byte`
126///
127/// If this requirement is not satisfied, the operation will fail, the ranges
128/// will not be assigned, and this function will return `false`. On success,
129/// this function returns `true`
130pub fn type Parser
Parser::(self : Parser, ranges : Array[Range]) -> Bool
Set the ranges of text that the parser should include when parsing.
By default, the parser will always include entire documents. This function
allows you to parse only a portion of a document but still return a syntax
tree whose ranges match up with the document as a whole. You can also pass
multiple disjoint ranges.
The second and third parameters specify the location and length of an array
of ranges.
If count is zero, then the entire document will be parsed. Otherwise,
the given ranges must be ordered from earliest to latest in the document,
and they must not overlap. That is, the following must hold for all:
i < count - 1: ranges[i].end_byte <= ranges[i + 1].start_byte
If this requirement is not satisfied, the operation will fail, the ranges
will not be assigned, and this function will return false. On success,
this function returns true
set_included_ranges(
131 Parser
self : type Parser
Parser,
132 Array[Range]
ranges : type Array[T]
Array[type Range FixedArray[UInt]
Range]
133) -> Bool
Bool {
134 let FixedArray[UInt]
flatten_ranges = 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(Array[Range]
ranges.(self : Array[Range]) -> Int
Returns the number of elements in the array.
Parameters:
array : The array whose length is to be determined.
Returns the number of elements in the array as an integer.
Example:
test "Array::length" {
let arr = [1, 2, 3]
inspect!(arr.length(), content="3")
let empty : Array[Int] = []
inspect!(empty.length(), content="0")
}
length() (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
}
* 6, 0U)
135 for Int
i, Range
range in Array[Range]
ranges {
136 FixedArray[UInt]
flatten_ranges(FixedArray[UInt], Int, UInt) -> Unit
Sets a value at the specified index in a fixed-size array. The original value
at that index is overwritten.
Parameters:
array : The fixed-size array to modify.
index : The position in the array where the value will be set.
value : The new value to assign at the specified index.
Throws a runtime error if the index is out of bounds (less than 0 or greater
than or equal to the length of the array).
Example:
test "FixedArray::op_set" {
let arr = [1, 2, 3]
arr[1] = 42
inspect!(arr, content="[1, 42, 3]")
}
test "panic FixedArray::op_set/out_of_bounds" {
let arr = [1, 2, 3]
arr[3] = 42 // 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
}
* 6] = Range
range.FixedArray[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
}
_[0]
137 FixedArray[UInt]
flatten_ranges(FixedArray[UInt], Int, UInt) -> Unit
Sets a value at the specified index in a fixed-size array. The original value
at that index is overwritten.
Parameters:
array : The fixed-size array to modify.
index : The position in the array where the value will be set.
value : The new value to assign at the specified index.
Throws a runtime error if the index is out of bounds (less than 0 or greater
than or equal to the length of the array).
Example:
test "FixedArray::op_set" {
let arr = [1, 2, 3]
arr[1] = 42
inspect!(arr, content="[1, 42, 3]")
}
test "panic FixedArray::op_set/out_of_bounds" {
let arr = [1, 2, 3]
arr[3] = 42 // 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
}
* 6 (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] = Range
range.FixedArray[UInt]
_[1]
138 FixedArray[UInt]
flatten_ranges(FixedArray[UInt], Int, UInt) -> Unit
Sets a value at the specified index in a fixed-size array. The original value
at that index is overwritten.
Parameters:
array : The fixed-size array to modify.
index : The position in the array where the value will be set.
value : The new value to assign at the specified index.
Throws a runtime error if the index is out of bounds (less than 0 or greater
than or equal to the length of the array).
Example:
test "FixedArray::op_set" {
let arr = [1, 2, 3]
arr[1] = 42
inspect!(arr, content="[1, 42, 3]")
}
test "panic FixedArray::op_set/out_of_bounds" {
let arr = [1, 2, 3]
arr[3] = 42 // 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
}
* 6 (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
}
+ 2] = Range
range.FixedArray[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
}
_[2]
139 FixedArray[UInt]
flatten_ranges(FixedArray[UInt], Int, UInt) -> Unit
Sets a value at the specified index in a fixed-size array. The original value
at that index is overwritten.
Parameters:
array : The fixed-size array to modify.
index : The position in the array where the value will be set.
value : The new value to assign at the specified index.
Throws a runtime error if the index is out of bounds (less than 0 or greater
than or equal to the length of the array).
Example:
test "FixedArray::op_set" {
let arr = [1, 2, 3]
arr[1] = 42
inspect!(arr, content="[1, 42, 3]")
}
test "panic FixedArray::op_set/out_of_bounds" {
let arr = [1, 2, 3]
arr[3] = 42 // 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
}
* 6 (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
}
+ 3] = Range
range.FixedArray[UInt]
_[3]
140 FixedArray[UInt]
flatten_ranges(FixedArray[UInt], Int, UInt) -> Unit
Sets a value at the specified index in a fixed-size array. The original value
at that index is overwritten.
Parameters:
array : The fixed-size array to modify.
index : The position in the array where the value will be set.
value : The new value to assign at the specified index.
Throws a runtime error if the index is out of bounds (less than 0 or greater
than or equal to the length of the array).
Example:
test "FixedArray::op_set" {
let arr = [1, 2, 3]
arr[1] = 42
inspect!(arr, content="[1, 42, 3]")
}
test "panic FixedArray::op_set/out_of_bounds" {
let arr = [1, 2, 3]
arr[3] = 42 // 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
}
* 6 (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
}
+ 4] = Range
range.FixedArray[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
}
_[4]
141 FixedArray[UInt]
flatten_ranges(FixedArray[UInt], Int, UInt) -> Unit
Sets a value at the specified index in a fixed-size array. The original value
at that index is overwritten.
Parameters:
array : The fixed-size array to modify.
index : The position in the array where the value will be set.
value : The new value to assign at the specified index.
Throws a runtime error if the index is out of bounds (less than 0 or greater
than or equal to the length of the array).
Example:
test "FixedArray::op_set" {
let arr = [1, 2, 3]
arr[1] = 42
inspect!(arr, content="[1, 42, 3]")
}
test "panic FixedArray::op_set/out_of_bounds" {
let arr = [1, 2, 3]
arr[3] = 42 // 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
}
* 6 (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
}
+ 5] = Range
range.FixedArray[UInt]
_[5]
142 }
143 return (self : Parser, ranges : FixedArray[UInt]) -> Bool
ts_parser_set_included_ranges(Parser
self, FixedArray[UInt]
flatten_ranges)
144}
145
146///|
147#borrow(self)
148extern "c" fn ts_parser_included_ranges(self : type Parser
Parser) -> type FixedArray[A]
FixedArray[UInt
UInt] = "moonbit_ts_parser_included_ranges"
149
150///|
151/// Get the ranges of text that the parser will include when parsing.
152pub fn type Parser
Parser::(self : Parser) -> Array[Range]
Get the ranges of text that the parser will include when parsing.
included_ranges(Parser
self : type Parser
Parser) -> type Array[T]
Array[type Range FixedArray[UInt]
Range] {
153 let Array[Range]
flatten_ranges = (self : Parser) -> Array[Range]
ts_parser_included_ranges(Parser
self)
154 type Array[T]
Array::(Int, (Int) -> Range) -> Array[Range]
Creates a new array of the specified length, where each element is
initialized using an index-based initialization function.
Parameters:
length : The length of the new array. If length is less than or equal
to 0, returns an empty array.
initializer : A function that takes an index (starting from 0) and
returns a value of type T. This function is called for each index to
initialize the corresponding element.
Returns a new array of type Array[T] with the specified length, where each
element is initialized using the provided function.
Example:
test "Array::makei" {
let arr = Array::makei(3, fn(i) { i * 2 })
inspect!(arr, content="[0, 2, 4]")
}
makei(Array[Range]
flatten_ranges.(self : Array[Range]) -> Int
Returns the number of elements in the array.
Parameters:
array : The array whose length is to be determined.
Returns the number of elements in the array as an integer.
Example:
test "Array::length" {
let arr = [1, 2, 3]
inspect!(arr.length(), content="3")
let empty : Array[Int] = []
inspect!(empty.length(), content="0")
}
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
}
/ 6, fn(Int
i) {
155 (start_point : Point, end_point : Point, start_byte : UInt, end_byte : UInt) -> Range
ts_range_new(
156 (row : UInt, column : UInt) -> Point
ts_point_new(Array[Range]
flatten_ranges(Array[UInt], Int) -> UInt
Retrieves an element from the array at the specified index.
Parameters:
array : The array to get the element from.
index : The position in the array from which to retrieve the element.
Returns the element at the specified index.
Throws a panic if the index is negative or greater than or equal to the
length of the array.
Example:
test "Array::op_get" {
let arr = [1, 2, 3]
inspect!(arr[1], content="2")
}
test "panic Array::op_get/out_of_bounds" {
let arr = [1, 2, 3]
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
}
* 6], Array[Range]
flatten_ranges(Array[UInt], Int) -> UInt
Retrieves an element from the array at the specified index.
Parameters:
array : The array to get the element from.
index : The position in the array from which to retrieve the element.
Returns the element at the specified index.
Throws a panic if the index is negative or greater than or equal to the
length of the array.
Example:
test "Array::op_get" {
let arr = [1, 2, 3]
inspect!(arr[1], content="2")
}
test "panic Array::op_get/out_of_bounds" {
let arr = [1, 2, 3]
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
}
* 6 (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]),
157 (row : UInt, column : UInt) -> Point
ts_point_new(Array[Range]
flatten_ranges(Array[UInt], Int) -> UInt
Retrieves an element from the array at the specified index.
Parameters:
array : The array to get the element from.
index : The position in the array from which to retrieve the element.
Returns the element at the specified index.
Throws a panic if the index is negative or greater than or equal to the
length of the array.
Example:
test "Array::op_get" {
let arr = [1, 2, 3]
inspect!(arr[1], content="2")
}
test "panic Array::op_get/out_of_bounds" {
let arr = [1, 2, 3]
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
}
* 6 (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
}
+ 2], Array[Range]
flatten_ranges(Array[UInt], Int) -> UInt
Retrieves an element from the array at the specified index.
Parameters:
array : The array to get the element from.
index : The position in the array from which to retrieve the element.
Returns the element at the specified index.
Throws a panic if the index is negative or greater than or equal to the
length of the array.
Example:
test "Array::op_get" {
let arr = [1, 2, 3]
inspect!(arr[1], content="2")
}
test "panic Array::op_get/out_of_bounds" {
let arr = [1, 2, 3]
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
}
* 6 (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
}
+ 3]),
158 Array[Range]
flatten_ranges(Array[UInt], Int) -> UInt
Retrieves an element from the array at the specified index.
Parameters:
array : The array to get the element from.
index : The position in the array from which to retrieve the element.
Returns the element at the specified index.
Throws a panic if the index is negative or greater than or equal to the
length of the array.
Example:
test "Array::op_get" {
let arr = [1, 2, 3]
inspect!(arr[1], content="2")
}
test "panic Array::op_get/out_of_bounds" {
let arr = [1, 2, 3]
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
}
* 6 (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
}
+ 4],
159 Array[Range]
flatten_ranges(Array[UInt], Int) -> UInt
Retrieves an element from the array at the specified index.
Parameters:
array : The array to get the element from.
index : The position in the array from which to retrieve the element.
Returns the element at the specified index.
Throws a panic if the index is negative or greater than or equal to the
length of the array.
Example:
test "Array::op_get" {
let arr = [1, 2, 3]
inspect!(arr[1], content="2")
}
test "panic Array::op_get/out_of_bounds" {
let arr = [1, 2, 3]
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
}
* 6 (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
}
+ 5],
160 )
161 })
162}
163
164///|
165// @move input
166#borrow(self, old_tree)
167extern "c" fn ts_parser_parse(
168 self : type Parser
Parser,
169 old_tree : type TSTree
TSTree,
170 input : (UInt
UInt, type Point
Point, type FixedArray[A]
FixedArray[UInt
UInt]) -> Bytes
Bytes,
171 encoding : enum InputEncoding {
UTF8
UTF16LE
UTF16BE
Custom
}
InputEncoding,
172 decode : @c.Nullable[
173 FuncRef[(@c.Pointer[Byte
Byte], UInt
UInt, @c.Pointer[Int
Int]) -> Int
Int],
174 ]
175) -> type TSTree
TSTree = "moonbit_ts_parser_parse"
176
177///|
178/// The state of a parse operation.
179pub struct ParseState {
180 Int
current_byte_offset : Int
Int
181 Bool
has_error : Bool
Bool
182}
183
184///|
185/// Options for parsing.
186struct ParseOptions {
187 (ParseState) -> Bool
progress_callback : (struct ParseState {
current_byte_offset: Int
has_error: Bool
}
The state of a parse operation.
ParseState) -> Bool
Bool
188}
189
190///|
191/// Create new parse options with the given progress callback.
192pub fn struct ParseOptions {
progress_callback: (ParseState) -> Bool
}
Options for parsing.
ParseOptions::(progress_callback : (ParseState) -> Bool) -> ParseOptions
Create new parse options with the given progress callback.
new(
193 (ParseState) -> Bool
progress_callback : (struct ParseState {
current_byte_offset: Int
has_error: Bool
}
The state of a parse operation.
ParseState) -> Bool
Bool
194) -> struct ParseOptions {
progress_callback: (ParseState) -> Bool
}
Options for parsing.
ParseOptions {
195 struct ParseOptions {
progress_callback: (ParseState) -> Bool
}
Options for parsing.
ParseOptions::{ (ParseState) -> Bool
progress_callback, }
196}
197
198///|
199// @move input
200// @move progress_callback
201#borrow(self, old_tree)
202extern "c" fn ts_parser_parse_with_options(
203 self : type Parser
Parser,
204 old_tree : type TSTree
TSTree,
205 input : (UInt
UInt, type Point
Point, type FixedArray[A]
FixedArray[UInt
UInt]) -> Bytes
Bytes,
206 encoding : UInt
UInt,
207 decode : @c.Nullable[
208 FuncRef[(@c.Pointer[Byte
Byte], UInt
UInt, @c.Pointer[Int
Int]) -> Int
Int],
209 ],
210 progress_callback : (UInt
UInt, Bool
Bool) -> Bool
Bool
211) -> type TSTree
TSTree = "moonbit_ts_parser_parse_with_options"
212
213///|
214/// Use the parser to parse some source code and create a syntax tree.
215///
216/// If you are parsing this document for the first time, pass `None` for the
217/// `old_tree` parameter. Otherwise, if you have already parsed an earlier
218/// version of this document and the document has since been edited, pass the
219/// previous syntax tree so that the unchanged parts of it can be reused.
220/// This will save time and memory. For this to work correctly, you must have
221/// already edited the old syntax tree using the `Tree::edit` function in a
222/// way that exactly matches the source code changes.
223///
224/// The `Input` parameter lets you specify how to read the text. It has the
225/// following fields:
226/// 1. `read`: A function to retrieve a chunk of text at a given byte offset
227/// and (row, column) position. The function should return a pointer to the
228/// text and write its length to the `bytes_read` pointer. The parser does
229/// not take ownership of this buffer; it just borrows it until it has
230/// finished reading it. The function should write a zero value to the
231/// `bytes_read` pointer to indicate the end of the document.
232/// 2. `decode`: A function to decode the text. This is only used if the
233/// encoding is `Custom`.
234/// Additionally, you can pass `InputEncoding::UTF8` or `InputEncoding::UTF16`
235/// to the `Input` parameter to specify the encoding of the text.
236///
237/// This function returns a syntax tree on success, and `None` on failure. There
238/// are four possible reasons for failure:
239/// 1. The parser does not have a language assigned. Check for this using the
240/// `Parser::language` function.
241/// 2. Parsing was cancelled due to a timeout that was set by an earlier call to
242/// the `Parser::set_timeout_micros` function. You can resume parsing from
243/// where the parser left out by calling `Parser::parse` again with the
244/// same arguments. Or you can start parsing from scratch by first calling
245/// `Parser::reset`.
246/// 3. Parsing was cancelled using a cancellation flag that was set by an
247/// earlier call to `Parser::set_cancellation_flag`. You can resume parsing
248/// from where the parser left out by calling `Parser::parse` again with
249/// the same arguments.
250/// 4. Parsing was cancelled due to the progress callback returning true. This callback
251/// is passed as the `options` argument inside the `ParseOptions` struct.
252pub fn type Parser
Parser::(self : Parser, old_tree? : Tree, input : Input[Encoding], options? : ParseOptions) -> Tree!ParseError
Use the parser to parse some source code and create a syntax tree.
If you are parsing this document for the first time, pass None for the
old_tree parameter. Otherwise, if you have already parsed an earlier
version of this document and the document has since been edited, pass the
previous syntax tree so that the unchanged parts of it can be reused.
This will save time and memory. For this to work correctly, you must have
already edited the old syntax tree using the Tree::edit function in a
way that exactly matches the source code changes.
The Input parameter lets you specify how to read the text. It has the
following fields:
read: A function to retrieve a chunk of text at a given byte offset
and (row, column) position. The function should return a pointer to the
text and write its length to the bytes_read pointer. The parser does
not take ownership of this buffer; it just borrows it until it has
finished reading it. The function should write a zero value to the
bytes_read pointer to indicate the end of the document.
decode: A function to decode the text. This is only used if the
encoding is Custom.
Additionally, you can pass InputEncoding::UTF8 or InputEncoding::UTF16
to the Input parameter to specify the encoding of the text.
This function returns a syntax tree on success, and None on failure. There
are four possible reasons for failure:
- The parser does not have a language assigned. Check for this using the
Parser::language function.
- Parsing was cancelled due to a timeout that was set by an earlier call to
the
Parser::set_timeout_micros function. You can resume parsing from
where the parser left out by calling Parser::parse again with the
same arguments. Or you can start parsing from scratch by first calling
Parser::reset.
- Parsing was cancelled using a cancellation flag that was set by an
earlier call to
Parser::set_cancellation_flag. You can resume parsing
from where the parser left out by calling Parser::parse again with
the same arguments.
- Parsing was cancelled due to the progress callback returning true. This callback
is passed as the
options argument inside the ParseOptions struct.
parse[Encoding : trait DecodeFunction {
encoding(Self) -> InputEncoding
decode(@bytes.View) -> DecodeResult?
}
This function signature reads one code point from the given bytes,
returning the number of bytes consumed and the code point.
If the input is invalid, return None.
DecodeFunction](
253 Parser
self : type Parser
Parser,
254 Tree?
old_tree? : struct Tree {
tree: TSTree
text: @string.StringView
}
Tree,
255 Input[Encoding]
input : struct Input[DecodeFunction] {
read: (UInt, Point) -> @bytes.View
decode: DecodeFunction
}
Input[type parameter Encoding
Encoding],
256 ParseOptions?
options? : struct ParseOptions {
progress_callback: (ParseState) -> Bool
}
Options for parsing.
ParseOptions
257) -> struct Tree {
tree: TSTree
text: @string.StringView
}
Tree!type! ParseError {
MissingLanguage
Cancelled
}
ParseError {
258 let InputEncoding
encoding = Input[Encoding]
input.Encoding
decode.(Encoding) -> InputEncoding
encoding()
259 let ?
decode : @c.Nullable[
260 FuncRef[(@c.Pointer[Byte
Byte], UInt
UInt, @c.Pointer[Int
Int]) -> Int
Int],
261 ] = match InputEncoding
encoding {
262 InputEncoding
Custom =>
263 ((Unit, Unit, Unit) -> Int) -> ?
@c.Nullable::some(fn(Unit
bytes, Unit
length, Unit
code_point) {
264 let @buffer.T
buffer = (size_hint~ : Int = ..) -> @buffer.T
Creates a new extensible buffer with specified initial capacity. If the
initial capacity is less than 1, the buffer will be initialized with capacity
1.
Parameters:
size_hint : Initial capacity of the buffer in bytes. Defaults to 0.
Returns a new buffer of type T.
Example:
test "new" {
let buf = @buffer.new(size_hint=10)
inspect!(buf.length(), content="0")
buf.write_string("test")
inspect!(buf.length(), content="8")
}
@buffer.new()
265 for Int
i in Int
0..<Unit
length.() -> Int
reinterpret_as_int() {
266 @buffer.T
buffer.(self : @buffer.T, value : Byte) -> Unit
Writes a single byte to the end of the buffer. The buffer will automatically
grow if necessary to accommodate the new byte.
Parameters:
buffer : The buffer to write to.
byte : The byte value to be written.
Example:
test "write_byte" {
let buf = @buffer.new()
buf.write_byte(b'\x41')
inspect!(buf.contents(), content="b\"\\x41\"")
}
write_byte(Unit
bytes(Int) -> Byte
[i])
267 }
268 let Bytes
bytes = @buffer.T
buffer.(self : @buffer.T) -> Bytes
Returns a copy of the buffer's content as a sequence of bytes.
Parameters:
buffer : The buffer to read from.
Returns a Bytes object containing all bytes written to the buffer.
Example:
test "contents" {
let buf = @buffer.new()
buf.write_string("Test")
inspect!(
buf.contents(),
content=
#|b"\x54\x00\x65\x00\x73\x00\x74\x00"
,
)
}
contents()
269 match type parameter
Encoding::(@bytes.View) -> DecodeResult?
decode(Bytes
bytes[:]) {
270 (DecodeResult) -> DecodeResult?
Some(DecodeResult
result) => {
271 Unit
code_point.(Int) -> Unit
store(DecodeResult
result.Char
code_point.(self : Char) -> Int
Converts a character to its Unicode code point value as an integer.
Parameters:
self : The character to be converted.
Returns an integer representing the Unicode code point value of the
character.
Example:
test "Char::to_int" {
inspect!('A'.to_int(), content="65") // ASCII value of 'A'
inspect!('あ'.to_int(), content="12354") // Unicode code point of 'あ'
}
to_int())
272 DecodeResult
result.Int
bytes_read
273 }
274 DecodeResult?
None => -1
275 }
276 })
277 _ => () -> ?
@c.Nullable::none()
278 }
279 let mut @bytes.View
stage : type @bytes.View
A @bytes.View represents a view into a section of a Bytes without copying the data.
Example
let bs = b"\x00\x01\x02\x03\x04\x05"
let bv = bs[1:4]
assert_eq!(bv.length(), 3)
assert_eq!(bv[0], b'\x01')
assert_eq!(bv[1], b'\x02')
assert_eq!(bv[2], b'\x03')
@bytes.View = []
280 let StringBuilder
text = type StringBuilder
StringBuilder::(size_hint~ : Int = ..) -> StringBuilder
Creates a new string builder with an optional initial capacity hint.
Parameters:
size_hint : An optional initial capacity hint for the internal buffer. If
less than 1, a minimum capacity of 1 is used. Defaults to 0. It is the size of bytes,
not the size of characters. size_hint may be ignored on some platforms, JS for example.
Returns a new StringBuilder instance with the specified initial capacity.
new()
281 let mut Bytes?
chunk = Bytes?
None
282 let (UInt, Point, FixedArray[UInt]) -> Bytes
read : (UInt
UInt, type Point
Point, type FixedArray[A]
FixedArray[UInt
UInt]) -> Bytes
Bytes = fn(
283 UInt
offset : UInt
UInt,
284 Point
point : type Point
Point,
285 FixedArray[UInt]
range : type FixedArray[A]
FixedArray[UInt
UInt]
286 ) {
287 let @bytes.View
bytes_view = (Input[Encoding]
input.(UInt, Point) -> @bytes.View
read)(UInt
offset, Point
point)
288 @bytes.View
stage = [..@bytes.View
stage, ..@bytes.View
bytes_view]
289 loop type parameter
Encoding::(@bytes.View) -> DecodeResult?
decode(@bytes.View
stage) {
290 (DecodeResult) -> DecodeResult?
Some(DecodeResult
result) => {
291 StringBuilder
text.(self : StringBuilder, ch : Char) -> Unit
Writes a character to the StringBuilder.
write_char(DecodeResult
result.Char
code_point)
292 @bytes.View
stage = @bytes.View
stage[DecodeResult
result.Int
bytes_read:]
293 if @bytes.View
stage.(self : @bytes.View) -> Int
Returns the number of bytes in the view.
Parameters:
bytes_view : The view of a byte sequence.
Returns an integer representing the length of the view.
Example:
test "View::length" {
let bytes = b"\x00\x01\x02\x03\x04"
let view = bytes[2:4]
inspect!(view.length(), content="2")
}
length() (self : Int, other : Int) -> Bool
Compares two integers for equality.
Parameters:
self : The first integer to compare.
other : The second integer to compare.
Returns true if both integers have the same value, false otherwise.
Example:
test "Int::op_equal" {
inspect!(42 == 42, content="true")
inspect!(42 == -42, content="false")
}
== 0 {
294 break
295 } else {
296 continue type parameter
Encoding::(@bytes.View) -> DecodeResult?
decode(@bytes.View
stage)
297 }
298 }
299 DecodeResult?
None => ()
300 }
301 let Int
bytes_start = @bytes.View
bytes_view.(self : @bytes.View) -> Int
Retrieves the start index of the view.
start_offset()
302 let Int
bytes_length = @bytes.View
bytes_view.(self : @bytes.View) -> Int
Returns the number of bytes in the view.
Parameters:
bytes_view : The view of a byte sequence.
Returns an integer representing the length of the view.
Example:
test "View::length" {
let bytes = b"\x00\x01\x02\x03\x04"
let view = bytes[2:4]
inspect!(view.length(), content="2")
}
length()
303 FixedArray[UInt]
range[0] = (value : Int) -> UInt
int_to_uint(Int
bytes_start)
304 FixedArray[UInt]
range[1] = (value : Int) -> UInt
int_to_uint(Int
bytes_length)
305 let Bytes
bytes_data = @bytes.View
bytes_view.(self : @bytes.View) -> Bytes
Retrieves the underlying Bytes from a View.
data()
306 if Bytes?
chunk is (Bytes) -> Bytes?
Some(Bytes
chunk) (Bool, Bool) -> Bool
&& (a : Bytes, b : Bytes) -> Bool
Tests if two values are physically equal (i.e., point to the same memory
location). Unlike structural equality testing (==), this function checks if
two references point to exactly the same object in memory.
Parameters:
first : The first value to compare.
second : The second value to compare.
T : The type parameter representing the type of values being compared.
Returns true if both values refer to the same object in memory, false
otherwise.
Example:
test "physical_equal" {
let arr1 = [1, 2, 3]
let arr2 = arr1
let arr3 = [1, 2, 3]
inspect!(physical_equal(arr1, arr2), content="true") // Same object
inspect!(physical_equal(arr1, arr3), content="false") // Different objects with same content
}
physical_equal(Bytes
chunk, Bytes
bytes_data) {
307 return Bytes
bytes_data
308 }
309 if Int
bytes_length (x : Int, y : Int) -> Bool
!= 0 {
310 Bytes?
chunk = (Bytes) -> Bytes?
Some(Bytes
bytes_data)
311 } else {
312 Bytes?
chunk = Bytes?
None
313 }
314 return Bytes
bytes_data
315 }
316 let Tree
old_ts_tree = match Tree?
old_tree {
317 Tree?
None => () -> Tree
ts_tree_null()
318 (Tree) -> Tree?
Some(Tree
tree) => Tree
tree.TSTree
tree
319 }
320 guard ParseOptions?
options is (ParseOptions) -> ParseOptions?
Some(ParseOptions
options) else {
321 let TSTree?
tree = (self : Parser, old_tree : TSTree, input : (UInt, Point, FixedArray[UInt]) -> Bytes, encoding : InputEncoding, decode : ?) -> TSTree
ts_parser_parse(Parser
self, Tree
old_ts_tree, (UInt, Point, FixedArray[UInt]) -> Bytes
read, InputEncoding
encoding, ?
decode).(self : TSTree) -> TSTree?
to_option()
322 { TSTree
tree: Parser
self.(self : Parser, tree : TSTree?) -> TSTree!ParseError
raise_parse_error!(TSTree?
tree), @string.StringView
text: StringBuilder
text.(self : StringBuilder) -> String
Returns the current content of the StringBuilder as a string.
to_string() }
323 }
324 let TSTree?
tree = (self : Parser, old_tree : TSTree, input : (UInt, Point, FixedArray[UInt]) -> Bytes, encoding : UInt, decode : ?, progress_callback : (UInt, Bool) -> Bool) -> TSTree
ts_parser_parse_with_options(
325 Parser
self,
326 Tree
old_ts_tree,
327 (UInt, Point, FixedArray[UInt]) -> Bytes
read,
328 InputEncoding
encoding.(self : InputEncoding) -> UInt
to_uint(),
329 ?
decode,
330 fn(UInt
current_byte_offset, Bool
has_error) {
331 let Int
current_byte_offset = (value : UInt) -> Int
uint_to_int(UInt
current_byte_offset)
332 (ParseOptions
options.(ParseState) -> Bool
progress_callback)({ Int
current_byte_offset, Bool
has_error })
333 },
334 ).(self : TSTree) -> TSTree?
to_option()
335 { TSTree
tree: Parser
self.(self : Parser, tree : TSTree?) -> TSTree!ParseError
raise_parse_error!(TSTree?
tree), @string.StringView
text: StringBuilder
text.(self : StringBuilder) -> String
Returns the current content of the StringBuilder as a string.
to_string() }
336}
337
338///|
339#borrow(self, old_tree, string)
340extern "c" fn ts_parser_parse_bytes(
341 self : type Parser
Parser,
342 old_tree : type TSTree
TSTree,
343 string : Bytes
Bytes
344) -> type TSTree
TSTree = "moonbit_ts_parser_parse_string"
345
346///|
347#borrow(self, old_tree, string)
348extern "c" fn ts_parser_parse_bytes_encoding(
349 self : type Parser
Parser,
350 old_tree : type TSTree
TSTree,
351 string : Bytes
Bytes,
352 encoding : UInt
UInt
353) -> type TSTree
TSTree = "moonbit_ts_parser_parse_string_encoding"
354
355///|
356/// Use the parser to parse some source code stored in one contiguous buffer
357/// with a given encoding. The first three parameters work the same as in the
358/// `parse` method. The final parameter indicates whether the text is encoded as
359/// UTF8 or UTF16.
360pub fn type Parser
Parser::(self : Parser, old_tree? : Tree, bytes : Bytes, encoding~ : @encoding.Encoding) -> Tree!ParseError
Use the parser to parse some source code stored in one contiguous buffer
with a given encoding. The first three parameters work the same as in the
parse method. The final parameter indicates whether the text is encoded as
UTF8 or UTF16.
parse_bytes(
361 Parser
self : type Parser
Parser,
362 Tree?
old_tree? : struct Tree {
tree: TSTree
text: @string.StringView
}
Tree,
363 Bytes
bytes : Bytes
Bytes,
364 @encoding.Encoding
encoding~ : enum @encoding.Encoding {
UTF8
UTF16LE
UTF16BE
}
@encoding.Encoding
365) -> struct Tree {
tree: TSTree
text: @string.StringView
}
Tree!type! ParseError {
MissingLanguage
Cancelled
}
ParseError {
366 let Tree
old_ts_tree = match Tree?
old_tree {
367 Tree?
None => () -> Tree
ts_tree_null()
368 (Tree) -> Tree?
Some(Tree
tree) => Tree
tree.TSTree
tree
369 }
370 let TSTree?
tree = (self : Parser, old_tree : TSTree, string : Bytes, encoding : UInt) -> TSTree
ts_parser_parse_bytes_encoding(
371 Parser
self,
372 Tree
old_ts_tree,
373 Bytes
bytes,
374 @encoding.Encoding
encoding.(self : @encoding.Encoding) -> InputEncoding
encoding().(self : InputEncoding) -> UInt
to_uint(),
375 ).(self : TSTree) -> TSTree?
to_option()
376 {
377 TSTree
tree: Parser
self.(self : Parser, tree : TSTree?) -> TSTree!ParseError
raise_parse_error!(TSTree?
tree),
378 @string.StringView
text: (input : @bytes.View, encoding~ : @encoding.Encoding) -> String
@encoding.decode_lossy(Bytes
bytes, @encoding.Encoding
encoding~),
379 }
380}
381
382///|
383pub type! ParseError {
384 MissingLanguage
385 Cancelled
386} derive(Show)
387
388///|
389fn (self : Parser, tree : TSTree?) -> TSTree!ParseError
raise_parse_error(Parser
self : type Parser
Parser, TSTree?
tree : type TSTree
TSTree?) -> type TSTree
TSTree!type! ParseError {
MissingLanguage
Cancelled
}
ParseError {
390 match TSTree?
tree {
391 TSTree?
None =>
392 match Parser
self.(self : Parser) -> Language?
Get the parser's current language.
language() {
393 Language?
None => raise ParseError
MissingLanguage
394 Language?
Some(_) => raise ParseError
Cancelled
395 }
396 (TSTree) -> TSTree?
Some(TSTree
tree) => return TSTree
tree
397 }
398}
399
400///|
401/// Use the parser to parse some source code stored in one contiguous string buffer.
402/// The first two parameters are the same as in the `parse` function. The final
403/// parameter is the string to parse.
404pub fn type Parser
Parser::(self : Parser, old_tree? : Tree, string : @string.StringView) -> Tree!ParseError
Use the parser to parse some source code stored in one contiguous string buffer.
The first two parameters are the same as in the parse function. The final
parameter is the string to parse.
parse_string(
405 Parser
self : type Parser
Parser,
406 Tree?
old_tree? : struct Tree {
tree: TSTree
text: @string.StringView
}
Tree,
407 @string.StringView
string : 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
408) -> struct Tree {
tree: TSTree
text: @string.StringView
}
Tree!type! ParseError {
MissingLanguage
Cancelled
}
ParseError {
409 let Tree
old_ts_tree = match Tree?
old_tree {
410 Tree?
None => () -> Tree
ts_tree_null()
411 (Tree) -> Tree?
Some(Tree
tree) => Tree
tree.TSTree
tree
412 }
413 let TSTree?
tree = (self : Parser, old_tree : TSTree, string : Bytes) -> TSTree
ts_parser_parse_bytes(
414 Parser
self,
415 Tree
old_ts_tree,
416 (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
string),
417 ).(self : TSTree) -> TSTree?
to_option()
418 { TSTree
tree: Parser
self.(self : Parser, tree : TSTree?) -> TSTree!ParseError
raise_parse_error!(TSTree?
tree), @string.StringView
text: @string.StringView
string }
419}
420
421///|
422#borrow(self)
423extern "c" fn ts_parser_reset(self : type Parser
Parser) = "moonbit_ts_parser_reset"
424
425///|
426/// Instruct the parser to start the next parse from the beginning.
427///
428/// If the parser previously failed because of a timeout or a cancellation, then
429/// by default, it will resume where it left off on the next call to
430/// `parse` or other parsing functions. If you don't want to resume,
431/// and instead intend to use this parser to parse some other document, you must
432/// call `reset` first.
433pub fn type Parser
Parser::(self : Parser) -> Unit
Instruct the parser to start the next parse from the beginning.
If the parser previously failed because of a timeout or a cancellation, then
by default, it will resume where it left off on the next call to
parse or other parsing functions. If you don't want to resume,
and instead intend to use this parser to parse some other document, you must
call reset first.
reset(Parser
self : type Parser
Parser) -> Unit
Unit {
434 (self : Parser) -> Unit
ts_parser_reset(Parser
self)
435}
436
437///|
438pub enum LogType {
439 Parse
440 Lex
441}
442
443///|
444fn enum LogType {
Parse
Lex
}
LogType::(self : LogType) -> UInt
to_uint(LogType
self : enum LogType {
Parse
Lex
}
LogType) -> UInt
UInt {
445 match LogType
self {
446 LogType
Parse => 0
447 LogType
Lex => 1
448 }
449}
450
451///|
452fn enum LogType {
Parse
Lex
}
LogType::(value : UInt) -> LogType
of_uint(UInt
value : UInt
UInt) -> enum LogType {
Parse
Lex
}
LogType {
453 match UInt
value {
454 0 => LogType
Parse
455 1 => LogType
Lex
456 UInt
value => (msg : String) -> LogType
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 log type: \{UInt
value}")
457 }
458}
459
460///|
461type Logger (UInt, Bytes) -> Unit
462
463///|
464fn type Logger (String, Bool) -> Unit
Logger::(log : (LogType, @string.StringView) -> Unit) -> Logger
new((LogType, @string.StringView) -> Unit
log : (enum LogType {
Parse
Lex
}
LogType, 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) -> type Logger (String, Bool) -> Unit
Logger {
465 fn(String
log_type, Bool
message) {
466 (LogType, @string.StringView) -> Unit
log(
467 enum LogType {
Parse
Lex
}
LogType::(value : UInt) -> LogType
of_uint(String
log_type),
468 (encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy(@encoding.Encoding
encoding=@encoding.Encoding
UTF8, Bool
message),
469 )
470 }
471}
472
473///|
474// @move logger
475#borrow(self)
476extern "c" fn ts_parser_set_logger(self : type Parser
Parser, logger : type Logger (String, Bool) -> Unit
Logger) = "moonbit_ts_parser_set_logger"
477
478///|
479/// Set the logger that a parser should use during parsing.
480///
481/// The parser does not take ownership over the logger payload. If a logger was
482/// previously assigned, the caller is responsible for releasing any memory
483/// owned by the previous logger.
484pub fn type Parser
Parser::(self : Parser, logger : Logger) -> Unit
Set the logger that a parser should use during parsing.
The parser does not take ownership over the logger payload. If a logger was
previously assigned, the caller is responsible for releasing any memory
owned by the previous logger.
set_logger(Parser
self : type Parser
Parser, Logger
logger : type Logger (String, Bool) -> Unit
Logger) -> Unit
Unit {
485 (self : Parser, logger : (String, Bool) -> Unit) -> Unit
ts_parser_set_logger(Parser
self, Logger
logger)
486}
487
488///|
489#borrow(self)
490extern "c" fn ts_parser_logger(self : type Parser
Parser) -> type Logger (String, Bool) -> Unit
Logger = "moonbit_ts_parser_logger"
491
492///|
493/// Get the parser's current logger.
494pub fn type Parser
Parser::(self : Parser) -> Logger
Get the parser's current logger.
logger(Parser
self : type Parser
Parser) -> type Logger (String, Bool) -> Unit
Logger {
495 (self : Parser) -> (String, Bool) -> Unit
ts_parser_logger(Parser
self)
496}
497