Skip to main content

Workaround for Differences Between Node.js and Web Tree-sitter APIs

ยท 2 min read
Mohamad Omar Nachawati

Tree-sitter is a great tool for building incremental and error-tolerant parsers for use in compilers, IDEs and code analyzers, which can also run directly in the browser. Unlike ANTLR, which is a popular open-source LL(*) parser generator, tree-sitter is a GLR parser generator that's similar but more powerful than the LALR parser generator YACC that we all were forced to learn back in CS 440 ๐Ÿ˜….

It's also nice that tree-sitter is open-source, unlike the closed-source REx Parser Generator that I used to generate the JSONiq parser for the Unity DGMS analytics engine (from the ebnf grammar). I guess it can be a bit disquieting for a project to depend on Gunther Rademacher to continuing maintaining his REx web service ๐Ÿ˜ฌ.

Recently, however, I ran into a problem with tree-sitter when trying to reuse TypeScript code for both Node.js and the Web. Although both of these tree-sitter API implementations can be called separately from TypeScript (or JavaScript), slight differences prevents seamlessly interchanging them in a cross-platform build. Until this problem is properly fixed, I've developed a simple workaround that provides a unified tree-sitter API for both Node.js and the Web.

To use it, simply import this Gist in any common module that processes tree-sitter SyntaxNodes. Please note that this workaround does not eliminate the need for Node.js- and Web- specific loading and initialization of the tree-sitter API and languages.