Skip to main content

· 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.

· 2 min read
Mohamad Omar Nachawati

Today I revisited CasADi to see if I could use emscripten to compile it to a WebAssembly and run it inside of a Web browser. CasADi is an awesome tool for performing symbolic computation and automatic differentiation, among other things. It also provides interfaces to popular open-source solvers, such as the MINLP solver BONMIN.

I had previously used CasADi during my PhD to implement a grey-box algorithmic framework that I had designed and aptly named GreyOpt (paper, slides). I also used CasADi for implementing the main backend of Unity DGMS, an open-source JSONiq analytics engine. I thought it would be interesting to see if at least some functionality could be easily ported to run directly in the browser.

It was fairly easy to add a new executable target in CMAKE, along with a simple C API that wrapped some functions for constructing symbolic variables and for performing basic symbolic arithmetic. I initially got some compilation errors stemming from an issue with some preprocessor directives that conditionally enabled the pack and unpack methods for unsigned int& arguments based on the value of SIZE_MAX (WASM support is currently only 32-bit).

After resolving those errors, the build and linking of the CasADi WebAssembly completed successfully. Now I can use CasADi to do computer algebra in Chromium ;-)