An experimental pure MoonBit port of Tree-sitter, with mbtx grammar definitions.
moon add moonbit-community/treesitter@0.1.0
moon add moonbit-community/tree-sitter-json@0.24.8 # or any module in grammars/
moon install moonbit-community/treesitter/cmd/treesitter@0.1.0 # the CLI (native)///|
test "generate a grammar, parse and query" {
// A grammar.json document (here built with the @grammar DSL; upstream's
// `tree-sitter generate` writes the same format to src/grammar.json).
let grammar = @grammar.Grammar::new("words", [
("document", Repeat(Symbol("word"))),
("word", @grammar.pattern("[a-z]+")),
])
let generated = @generate.generate_from_grammar_json(
grammar.to_json().stringify(),
)
// `generated.c_code` is the parser.c upstream would emit, byte for byte.
let language = @treesitter.Language::new(generated.language)
let parser = @treesitter.Parser::new()
parser.set_language(language)
let source = @utf8.encode("hello moonbit")
guard parser.parse(source) is Some(tree) else { fail("parse failed") }
inspect(tree.root_node().to_sexp(), content="(document (word) (word))")
let query = @treesitter.Query::new(language, "(word) @w")
let cursor = @treesitter.QueryCursor::new()
let matches = cursor.matches(query, tree.root_node(), source)
let mut count = 0
while matches.next() is Some(_) {
count += 1
}
inspect(count, content="2")
}| Package | Upstream source |
|---|---|
| moonbit-community/treesitter | lib/binding_rust/lib.rs |
| runtime | lib/src (parser, lexer, stack, subtree, tree, node, tree cursor, changed ranges) |
| language | TSLanguage (lib/src/parser.h, language.c) as portable tables |
| query | lib/src/query.c |
| generate | crates/generate |
| regex_syntax, regex | the regex-syntax 0.8 subset the generator and tags use, and a matcher |
| highlight, tags | crates/highlight, crates/tags |
| scanners | the C external scanners of the upstream fixture and test grammars and of the grammars in grammars/ |
| query_testing, cli_test | the CLI's assertion and corpus testing |
| cli, loader, config, clap, clap_complete, cmd/treesitter | crates/cli, crates/loader, crates/config, and the clap subset they use (native only) |
| grammar | a MoonBit DSL producing grammar.json (no upstream equivalent; stands in for grammar.js) |
import {
"moonbit-community/treesitter",
"moonbit-community/tree-sitter-json" @tree_sitter_json,
}moon build --target native --release cmd/treesitter
_build/native/release/build/moonbit-community/treesitter/cmd/treesitter/treesitter.exe --helpCARGO="cargo +1.97.1" sh tools/oracle/oracle_setup.sh # pinned upstream + reference outputs
sh tools/oracle/oracle.sh # generator, runtime, queries
bash tools/oracle/diff_cli.sh # CLI commands
python3 tools/oracle/compare_modules.py # grammar modules' tables
moon test --target native # ported upstream test suitespub(open) trait DotGraphOutput {
fn write_dot_graph(Self, Bytes) -> Unit
}impl DotGraphOutput for Bufferimpl DotGraphOutput for StringBuilderimpl TextProvider for Bytesimpl TextProvider for BytesViewimpl Show for IncludedRangesErrorimpl Show for LanguageErrorpub(all) suberror QueryError {
QueryError(row~ : Int, column~ : Int, offset~ : Int, message~ : String, kind~ : QueryErrorKind)
} derive(Eq, Debug)impl Show for QueryErrorpub struct Language {
// private fields
}pub struct LookaheadIterator {
// private fields
}pub struct LossyUtf8 {
// private fields
}pub struct Node {
// private fields
}fn Node::children_by_field_name(self : Node, field_name : StringView, cursor : TreeCursor) -> Iter[Node]pub struct ParseState {
// private fields
}pub struct Parser {
// private fields
}fn Parser::set_included_ranges(self : Parser, ranges : ArrayView[Range]) -> Unit raise IncludedRangesErrorpub struct Query {
// private fields
}impl Debug for QueryCapturepub struct QueryCaptures[T] {
// private fields
}pub struct QueryCursor {
// private fields
}fn[T] QueryCursor::captures(self : QueryCursor, query : Query, node : Node, text_provider : T) -> QueryCaptures[T]fn[T] QueryCursor::captures_with_options(self : QueryCursor, query : Query, node : Node, text_provider : T, options : QueryCursorOptions) -> QueryCaptures[T]fn[T] QueryCursor::matches(self : QueryCursor, query : Query, node : Node, text_provider : T) -> QueryMatches[T]fn[T] QueryCursor::matches_with_options(self : QueryCursor, query : Query, node : Node, text_provider : T, options : QueryCursorOptions) -> QueryMatches[T]fn QueryCursor::set_containing_byte_range(self : QueryCursor, start : Int, end : Int) -> QueryCursorfn QueryCursor::set_containing_point_range(self : QueryCursor, start : Point, end : Point) -> QueryCursorfn QueryCursorOptions::new(progress_callback? : (QueryCursorState) -> ControlFlow) -> QueryCursorOptionspub struct QueryCursorState {
// private fields
}pub struct QueryMatch {
pattern_index : Int
// private fields
}impl Debug for QueryMatchfn[T : TextProvider] QueryMatch::satisfies_text_predicates(self : QueryMatch, query : Query, text_provider : T) -> Boolpub struct QueryMatches[T] {
// private fields
}pub(all) struct QueryPredicate {
operator : String
args : Array[QueryPredicateArg]
} derive(Eq, Debug)pub struct TextCallback {
// private fields
}impl TextProvider for TextCallbackpub struct Tree {
// private fields
}pub struct TreeCursor {
// private fields
}fn format_sexp(sexp : String, initial_indent_level : Int) -> Stringlet language_version : Intlet min_compatible_language_version : Int