A MoonBit binding to the tree-sitter parsing library
Dependencies
moon update # Update the mooncakes.io package index
moon add tonyfettes/tree_sitter{
"import": [
"tonyfettes/tree_sitter"
]
}moon add tonyfettes/tree_sitter_moonbitAnd then import the tonyfettes/tree_sitter_moonbit package in your
moon.pkg.json file as well:{
"import": [
"tonyfettes/tree_sitter",
"tonyfettes/tree_sitter_moonbit"
]
}fn main {
let moonbit = @tree_sitter_moonbit.language()
let parser = @tree_sitter.Parser::new()
parser.set_language(moonbit)
let source_code =
#|fn main {
#| println("Hello, World!")
#|}
let tree = parser.parse_string(None, source_code)
let root_node = tree.root_node()
println(root_node.string())
}# Initialize the tree-sitter submodule
git submodule update --init --recursive
moon update
moon build --target nativepython scripts/prepare.pycd test && moon test --target native[!NOTE] You may find that the LSP is not working when you are editing files under the test/ directory. This is because test/ is a MoonBit module itself, and therefore the LSP process has to be spawned in the test/ directory to work properly. In most cases, this means you need to spin up a new editor instance inside the test/ directory.
pub(open) trait DecodeFunction {
encoding(Self) -> InputEncoding = _
decode(BytesView) -> DecodeResult?
}type DecodeResulttype Input[DecodeFunction]type InputEditpub(all) enum InputEncoding {
UTF8
UTF16LE
UTF16BE
Custom
}impl DecodeFunction for InputEncodingtype LanguageMetadatatype Loggertype LookaheadIteratortype Nodetype ParseOptionspub struct ParseState {
current_byte_offset : Int
has_error : Bool
}type Parserfn[Encoding : DecodeFunction] Parser::parse(self : Parser, old_tree? : Tree, input : Input[Encoding], options? : ParseOptions) -> Tree raise ParseErrorfn Parser::parse_bytes(self : Parser, old_tree? : Tree, bytes : Bytes, encoding~ : InputEncoding) -> Tree raise ParseErrorfn Parser::parse_string(self : Parser, old_tree? : Tree, string : StringView) -> Tree raise ParseErrortype Pointtype Queryfn Query::capture_quantifier_for_id(self : Query, pattern_index : Int, capture_index : Int) -> Quantifierfn Query::predicates_for_pattern(self : Query, pattern_index : Int) -> Array[Array[QueryPredicateStep]]type QueryCursorfn QueryCursor::exec(self : QueryCursor, query : Query, node : Node, options? : QueryCursorOptions) -> Unittype QueryCursorOptionspub enum QueryPredicateStep {
Capture(String)
String(String)
}type Rangepub enum SymbolType {
Regular
Anonymous
Supertype
Auxiliary
}type Treetype TreeCursorlet LANGUAGE_VERSION : Intlet MIN_COMPATIBLE_LANGUAGE_VERSION : IntA MoonBit binding to the tree-sitter parsing library
Dependencies