GNU TeXmacsUser manualReferenceAboutEnglish
Syntax trees of program code: tree-sitter

Tree-sitter is an incremental parser: from a grammar it makes a parser which gives a concrete syntax tree of a text, updates it after an edit by reusing the unchanged parts, and recovers from syntax errors (the tree always exists, with ERROR and MISSING nodes). TeXmacs keeps such a tree for every region of program code it typesets: a source file, a code block, the input of a session.

For the user, the tree gives the colours of the code (a string or a comment over several lines is coloured as such, the scripts of an HTML page in their own language, brackets which match nothing in the colour of errors) and the editing of programs: balanced brackets and quotes, closing keywords, indentation, structured moves and selections, structural edits, the outline and the definitions of a file, folds, and Return in a session input which is not complete yet. The user level description is editing program code. For the developer, the tree is a service of the C++ kernel which Scheme asks through glue routines, with positions which are positions in the TeXmacs strings of the lines.

1.The two halves

A grammar of tree-sitter is data: its grammar.json, which tree-sitter generate writes from grammar.js and which every grammar repository commits. Tree-sitter itself turns it into a parser.c of several megabytes per language. TeXmacs does not: it ships the grammar.json of each language and makes the tables itself.

The generator

A port to Scheme of the generator of tree-sitter 0.27.1 (

progs/treesit

) reads the grammar, builds its tokens and its LR(1) parse table with the conflict resolution of tree-sitter, merges the states and makes the lex tables. The stages which act on the states (about 100000 of them for C++ before merging) are also written in C++ (

ts_tables.cpp

), which gives the same tables byte for byte and is used when present. The result is a text, cached per grammar.

The runtime

The C runtime of tree-sitter 0.27.1 (MIT), vendored in

tree-sitter

, runs these tables:

ts_dynamic.c

fills a TSLanguage with them, so that no C code is generated per grammar. The external scanners of the grammars (indentation of Python, heredocs of Bash…) are ported to Scheme and called back by the runtime.

This choice avoids both the generated C of the grammars (more than 100 MB of sources for the languages of TeXmacs) and shared libraries per platform, which the browser build could not load. Its costs are the ports of the scanners and the time of the generation, which is why the tables of all the grammars are also shipped. The parser of

ts-parse.scm

, a port of the runtime to Scheme, is the reference for the tests; the editor always uses the C runtime.

The data flows as follows:

misc/tree-sitter/<grammar>/grammar.json
  --(ts-grammar, ts-nfa, ts-tables or ts_tables.cpp, ts-build)--> parse and lex tables
  --(ts-generate-tsdyn)--> <grammar>-<hash>.tsdyn in the cache (or the shipped .tsdyn.xz)
  --(tree-sitter-language-load, ts_dynamic.c)--> a TSLanguage, and its scanner (tree-sitter-set-scanner)
document of lines --(syntax_region.cpp)--> a TSTree, parsed again incrementally
  --(highlights.scm, ts_highlight.cpp)--> the colours of the lines
  --(glue syntax-region-…)--> ts-edit.scm, ts-code.scm

2.Source files

tree_sitter_glue.cpp

The loaded languages and queries (with the text predicates), the trampolines of the external scanners, the lexer functions which the Scheme scanners call, the unpacking of the shipped tables.

ts_dynamic.c

A TSLanguage from the exported tables: every state is a large one, the lex functions interpret the lex tables.

ts_tables.cpp

The C++ port of the stages of the generator which act on the states, and the writing of the exported text.

syntax_region.cpp

The syntax trees of the code regions, their incremental update, the colours of their lines, the requests of Scheme (nodes, errors, captures, indentation, moves).

ts_highlight.cpp

The language which wraps the usual language of a program, and the highlighters which Scheme installs.

tree_sitter_lib.c
,
ts_xz.c

The runtime of tree-sitter and the decoder of XZ Embedded (0BSD, in

xz

), each compiled as one unit.

build-glue-treesit.scm

The glue: tree-sitter-… and syntax-region-….

ts-registry.scm

The grammars, their scanners, the cache of the tables and their generation in the background.

ts-highlight.scm

The highlighters: the queries and the colour classes of their captures.

ts-edit.scm
,
ts-code.scm

The editing of programs by the tree, and its adapter for the features of program code.

tm-program-languages.scm

The one list of the program languages, with the grammar of each.

misc/tree-sitter

The grammars, their queries, licences and origins (SOURCE), and the shipped tables.

misc/tree-sitter (tools)

The tools: the shipped tables, the corpus and capture checks against tree-sitter, the generated queries.

treesit-test.scm

The suite treesit.

3.Builds without tree-sitter

The feature may be left out: --disable-tree-sitter of

tm_code.m4

(which otherwise defines USE_TREE_SITTER and adds the directory TreeSitter to the build), the option USE_TREE_SITTER of CMake (on by default), and the definition in

config.h

for the browser build, whose sources are listed in

sources.txt

. The glue is then not compiled, the program languages are not wrapped (the list of the program languages wraps them only when tree-sitter-wrap-language is defined), the usual highlighters colour all the code, and the editing of programs takes the answers of

code-syntax-none.scm

. How the features of program code are registered in the core is described in the features of program code.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".