![]() |
Pitfalls of tree-sitter | ![]() |
The pitfalls which concern all the features of program code (builds without a feature, providers, folds, layers) are in the pitfalls of the features of program code. Those below are particular to tree-sitter.
A column of tree-sitter is a byte of the TeXmacs string of the line, not a character: <alpha> spans seven columns. This is what paths need, so the points of the glue go into paths unchanged; a column on the screen is code_column of it (an entity is one column, a tab goes to its stop), as the indentation computes it. The lexers and the scanners, which see decoded code points, count an entity as one character (lexer-column), and a tab <#9> is the character 9 for them.
The predicates of the queries (#eq?, #any-of?, #match?) compare the undecoded text: a query of TeXmacs writes "<less>", and the regular expressions of upstream queries see Cork bytes and entities, not UTF-8.
tree-sitter-parse-sexp and tree-sitter-query-captures parse their string argument as UTF-8, without the Cork decoding of the regions; the tests compare a region with them after cork->utf8.
The colour codes of the lines carry the bit TS_TOKEN_START (\(2^{24}\)) at the start of each token. Code which reads the stored colours of a line of tree-sitter must mask it (TS_COLOR_MASK), as get_color and tree-sitter-line-colors do; colour codes must stay below the bit.
The region of a line is found from its ip, in the edited tree. A line without ip, or in a detached tree (a copy typeset for an export, a test which typesets a detached tree), is parsed alone: a string or a comment over several lines is then coloured line by line. So is a string inside markup in a line.
A region is identified by its root tree, not by its content: a root replaced by an equal copy is a new region, parsed from scratch, and the old one stays until it is the least recently used.
At most 256 regions and 32 MB of text are kept; beyond that the least recently used region is dropped and its next request parses it in full. A test which makes many regions calls syntax-region-forget.
At most 32 languages can be loaded in a session (TSDYN_MAX_LANGUAGES: the lex functions are called without the language) and 32 languages can have a scanner. The registry loads a grammar once, but tree-sitter-language-new (and ts-c-language in the tests) loads a new language at each call, which is never freed.
A serialized scanner state longer than 1024 bytes (the buffer of tree-sitter) is dropped, the scanner then starting again from the initial state there; and a state must not be modified once serialized, since its text is mapped back to the same value.
The wrapper is registered when the list of the program languages is loaded. A program language made by prog_language before that, or not listed then, keeps its usual language for the session: the languages are made once per name.
When highlighting by tree-sitter is off, no highlighter is installed (the preference) or those which are installed are ignored (tree-sitter-set-highlighting), and syntax-cursor-language is #f: the editing by the tree is off too, not only the colours. The tests of the editing functions need the tables of their grammars and the highlighter installed.
A highlighter which arrives after the region was coloured (the tables of an injected language generated in the background) is seen only after tree-sitter-reset-highlights: the epoch makes every wrapped language take a new highlight code. The colours stored under the old codes stay on the lines until they are modified.
Lines whose colours change without being modified are typeset again only when the editor is idle (treesit-schedule-updates): their stored colours are new at once, their boxes only after that update.
The queries are compiled once per language and session (the highlighters, and those of
ts-edit.scm
); an edited query file is read at the next start. Only highlights.scm follows a first line ; inherits:.
A change of the generator or of the exported text needs a new treesit-generator-version: the cache is keyed by it and by the grammar, and old tables would otherwise be read. The shipped tables must then be made again (
ship-tables.sh
), or every grammar is generated at the first start; the suite treesit fails while they do not match.
The generation in the background runs another TeXmacs: the bin/texmacs.bin of $TEXMACS_PATH, the binary of the macOS bundle, or else the texmacs found in the path, which may be another version. The whole asynchronous path needs a running loop (-headless -X), so the suite calls treesit-generate-cache directly; Lean is tested only when its tables are in the cache.
The suite writes tables into the cache of $TEXMACS_HOME_PATH, and needs those of JSON, Scheme and C at least; corpus and capture checks against tree-sitter are the scripts of
misc/tree-sitter
, not the suite.
The suites run in one TeXmacs: a key typed in program code loads the keyboard of programs and its modules, which changed the behaviour of later suites. The tests call the editing functions instead of typing keys.
Under S7, call/cc is costly: scanners leave a scan with call-with-exit. Under femtolisp the escape must not be called return, a special form there, and a global macro wins over a local function of the same name.
Under S7, a do counter stored with ahash-set! ends as its last value; the loops which store their counter are named lets.
A call of a public function of another module costs about as much as the glue call behind it: the hot scanners keep the lookahead in a variable of their module, read once after each advance, and use the loops of
ts-scan.scm
, which run in C.
Measured on an Apple M-series machine and recorded in
src/docs/tree-sitter.md
: generating the tables takes 0.2 s for Scheme, 0.5 s for Python, 0.7 s for C and 6.5 s for C++ under S7 in TeXmacs, and Lean about a minute and gigabytes, hence the shipped tables (unpacked in about 0.1 s). A file of 100 KB of Python parses in about 40 ms; a scanner called at nearly every token costs more (254 KB of Bash in about 0.3 s). Typing in a C++ file of 13000 lines costs about 6 ms per edit, most of it the incremental parse, since only the lines out of date are coloured again.