package tree-sitter

  1. Overview
  2. Docs
OCaml bindings for Tree-sitter

Install

dune-project
 Dependency

Authors

Maintainers

Sources

mosaic-0.1.0.tbz
sha256=9e4e90d17f9b2af1b07071fe425bc2c519c849c4f1d1ab73cde512be2d874849
sha512=06e9c4a741590942e81a27738d0b5c0413fafec8cf3b7dae047ad69f155e7b718aa4223818dc161b7d028efffcfd3365905e264d6fd31d453910ddfa91dcf9b9

doc/tree-sitter/Tree_sitter/index.html

Module Tree_sitterSource

OCaml bindings for Tree-sitter.

Tree-sitter is an incremental parsing library that builds concrete syntax trees for source code and efficiently updates them as the code changes.

The main workflow is:

  1. Obtain a Language.t from a grammar package (e.g. Tree_sitter_json or Tree_sitter_ocaml).
  2. Create a Parser.t and call Parser.parse_string.
  3. Walk the resulting Tree.t via Node accessors or Tree_cursor.
  4. Optionally, compile a Query.t and run it with a Query_cursor for pattern matching.

Types

Sourcetype point = {
  1. row : int;
  2. column : int;
}

A position in source text. row and column are both zero-indexed. column is measured in bytes, not characters.

Sourcetype range = {
  1. start_byte : int;
  2. end_byte : int;
  3. start_point : point;
  4. end_point : point;
}

A contiguous range in source text, expressed as both byte offsets and row-column points.

Sourcetype symbol_type =
  1. | Regular
    (*

    Named grammar rules (e.g. string, object).

    *)
  2. | Anonymous
    (*

    Literal tokens (e.g. {, ,).

    *)
  3. | Supertype
    (*

    Abstract grouping rules.

    *)
  4. | Auxiliary
    (*

    Compiler-internal symbols.

    *)

The type of a grammar symbol.

Language grammars

Sourcemodule Language : sig ... end

Language grammars.

Syntax tree nodes

Sourcemodule Node : sig ... end

Syntax tree nodes.

Parsed syntax trees

Sourcemodule Tree : sig ... end

Parsed syntax trees.

Parsers

Sourcemodule Parser : sig ... end

Parsers.

Pattern queries

Sourcemodule Query : sig ... end

Compiled queries for pattern matching on syntax trees.

Query cursors

Sourcemodule Query_cursor : sig ... end

Query cursors for iterating over query results.

Tree traversal cursors

Sourcemodule Tree_cursor : sig ... end

Efficient tree traversal cursors.

Highlighting

Sourceval highlight : Query.t -> Tree.t -> (int * int * string) list

highlight query tree is the list of (start_byte, end_byte, group_name) triples for all captures in tree. Captures whose name starts with _ are filtered out. Empty ranges (where start_byte >= end_byte) are also dropped.

The result is sorted by ascending start_byte.

Sourceval highlight_range : Query.t -> Tree.t -> start_byte:int -> end_byte:int -> (int * int * string) list

highlight_range query tree ~start_byte ~end_byte is like highlight but restricted to nodes within the byte range [start_byte;end_byte].