[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Tree-sitter maturity
From: |
Lynn Winebarger |
Subject: |
Re: Tree-sitter maturity |
Date: |
Tue, 31 Dec 2024 17:29:04 -0500 |
On Sun, Dec 29, 2024 at 3:37 PM Daniel Colascione <dancol@dancol.org> wrote:
>
> Thanks. Such an approach would let us treat tree-sitter grammars a lot
> more like font-lock-keywords, and I think for some modes, that'd be a
> good option. (Of course, SHTDI.)
The main blocking point for me is a primitive facility for describing
machine-level binary data structures, and operations for manipulating
data according to those specifications. The "bindat" facility is a
step in that direction, but its semantics lacks pointers, which is a
big limitation for simple translation of C data structures from source
code.
>
> Tree sitter, as wonderful as it is, strikes me as a bit of a Rube
> Goldberg machine architecturally: JS *and* Rust *and* C? Really? :-)
They evidently decided to use JSON and a simple schema to specify the
concrete grammar, instead of creating a DSL for the purpose.
Javascript is just a convenient way for embedding code into JSON the
same way LISP programmers use lisp to generate S-expressions. Once
you have the JSON format generated, javascript is not used.
The rest of the project is really composed of orthogonal components,
the GLR grammar compiler (written in Rust) and the run-time GLR
parsing engine, written in C. The grammar compiler produces the
parsing tables in the form of C source code that is compiled together
with the library for a single library per grammar, but the C library
does not actually require the parsing tables to be statically known at
compile-time, at least the last I looked, unless some really obscure
dependence. The procedural interface to the parser just takes a
pointer to the parser table data structure at run-time.
Since GLR grammars are basically arbitrary (ambiguous) LR(1) grammars,
the parser run-time has to implement a fairly sophisticated algorithm
(graph-stacks) to be efficient. Having implemented the LALR parser
generator at least 3 times in the last couple of decades (just for my
own use), generating the parse tables looks like a lot simpler (and
well-understood) problem to solve than the GLR run-time. More
importantly, the efficiency of the grammar compiler is not all that
critical compared to the run-time.
> Do you happen to know whether the subset of Rust that gccrs recognizes
> is sufficient to compile the tree sitter grammar compiler? If so, we
> could in principle combine gccrs with a bare-bones embedded JS
> interpreter like https://duckjs.org/ to produce a mechanism that would
> let us customize and rebuild tree sitter grammars as easily as we do
> elisp files, even on obscure platforms like DJGPP.
I have no idea. As I wrote above, replicating the calculation
performed by the grammar compiler is not that intimidating, if we had
a way of writing out the parse tables in the in-memory structures
understood by the runtime procedural interface. At least, replicating
the GLR grammar analysis is a lot simpler than implementing a compiler
for Rust, if that viewpoint makes sense. At worst, Emacs could move
from the generated C files to consuming the JSON files. There's no
requirement that parsers even have a JS form, that's just for
convenience of grammar writers.
I mean, look at
https://github.com/tree-sitter/tree-sitter-cpp/blob/master/grammar.js
. That uses a fairly limited subset of JS that has a straightforward
translation to lisp types. If we don't require replicating every
corner-case of Javascript, then the existing JS tree-sitter library
could probably be used to produce an S-expression that a simple set of
macros could translate into an S-expression equivalent of the
corresponding grammar.json. If you had a emacs-based grammar compiler
that could consume grammars in JSON format, with a generic tee-sitter
dynamic library (no fixed parse tables), you could even "bootstrap"
using the existing JSON from
https://github.com/tree-sitter/tree-sitter-javascript/blob/master/src/grammar.json,
so Rust was never involved (if that is important).
>
> Some Emacs modes could ship with .js grammars sourced from upstream
> editor-neutral projects. Other modes might just build tree sitter parse
> tables in elisp using something vaguely like SMIE syntax. Both styles
> of mode would be customizable by end users, and we'd (because, I'm a
> broken record, vendor vendor vendor) we'd maintain compatibility without
> mysterious AST-change-related breakages.
I agree, a generic grammar capturing the structures of most
programming languages would be useful. It is definitely possible to
extract the syntactic/semantic concepts from C++ and Python to create
such a grammar, if you are willing to allow nested grammars
appropriately delimited. For example, a constructor context would
delimit an expression in a data language that is embedded in a
constructor context that may itself have delimited value contexts
where the functional/procedural grammar may appear, ad infinitum. The
procedural and data grammars are distinct but mutually recursive.
That would be if the form appeared in an rvalue-context. For l-value
expressions, the same constructor delimiting syntax can become a
binding form, at least, with subexpressions of binding forms also
being binding forms. As long as the scanner is dynamically set
according to the grammar context (and recognizes/signals the closing
delimiter), the grammar can be made non-ambiguous because a given
character will produce context-appropriate terminal symbols.
As for vendoring, I just doubt you will get much buy-in in this forum.
There are corporate-type free/open-source software projects that
prioritize uniformity in build environments and limiting the scope of
bugs that can arise from the build process/dependencies that vendor at
the drop of the hat. Then there are "classic" free software projects
that have amalgamated the work of many individual contributors, and
those contributors often prioritize control of the software running on
their systems for whatever reason (but eliminating non-free software
is definitely one of them), and they often can/will contribute patches
for that purpose. The second camp *hates* vendoring because it
subverts their control of their computational resources. At least,
that's the dichotomy I see. There are probably finer points I'm
missing or mischaracterizing.
Lynn
- Re: Tree-sitter maturity, (continued)
- Re: Tree-sitter maturity, Lynn Winebarger, 2024/12/29
- Re: Tree-sitter maturity, Daniel Colascione, 2024/12/29
- Re: Tree-sitter maturity, Björn Bidar, 2024/12/29
- Message not available
- Re: Tree-sitter maturity, Yuan Fu, 2024/12/29
- Re: Tree-sitter maturity, Daniel Colascione, 2024/12/29
- Re: Tree-sitter maturity, Yuan Fu, 2024/12/29
- Re: Tree-sitter maturity, Philip Kaludercic, 2024/12/31
- Re: Tree-sitter maturity, Lynn Winebarger, 2024/12/29
- Re: Tree-sitter maturity, Björn Bidar, 2024/12/30
- Re: Tree-sitter maturity,
Lynn Winebarger <=
- Re: Tree-sitter maturity, Peter Oliver, 2024/12/28
- Re: Tree-sitter maturity, Philip Kaludercic, 2024/12/28
- Re: Tree-sitter maturity, Björn Bidar, 2024/12/29
- Re: Tree-sitter maturity, Eli Zaretskii, 2024/12/27
- Re: Tree-sitter maturity, Daniel Colascione, 2024/12/27
- Re: Tree-sitter maturity, Eli Zaretskii, 2024/12/27
- Re: Tree-sitter maturity, Daniel Colascione, 2024/12/27
- Re: Tree-sitter maturity, Stefan Kangas, 2024/12/27
- Re: Tree-sitter maturity, Richard Stallman, 2024/12/28
- Re: Tree-sitter maturity, Daniel Colascione, 2024/12/28