From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Daniel Colascione Newsgroups: gmane.emacs.devel Subject: Re: Tree-sitter maturity Date: Sat, 04 Jan 2025 12:39:44 -0500 Message-ID: <87h66emqan.fsf@dancol.org> References: <67428b3d.c80a0220.2f3036.adbdSMTPIN_ADDED_BROKEN@mx.google.com> <86ldwdm7xg.fsf@gnu.org> <6765355b.c80a0220.1a6b24.3117SMTPIN_ADDED_BROKEN@mx.google.com> <00554790-CACA-4233-8846-9E091CF1F7AA@gmail.com> <86msgl2red.fsf@gnu.org> <87o710sr7y.fsf@debian-hx90.lan> <8734i9tmze.fsf@posteo.net> <86plldwb7w.fsf@gnu.org> <87ttapryxr.fsf@posteo.net> <0883EB00-3BB2-4BC8-95D1-45F4497C0526@dancol.org> <87msge8bv8.fsf@dancol.org> <6775a459.170a0220.2f3d1e.1897SMTPIN_ADDED_BROKEN@mx.google.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="27414"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.12.8; emacs 31.0.50 Cc: =?utf-8?Q?Bj=C3=B6rn?= Bidar , Philip Kaludercic , emacs-devel , Eli Zaretskii , Richard Stallman , manphiz@gmail.com To: Lynn Winebarger Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Jan 04 18:40:24 2025 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1tU88N-0006yO-RT for ged-emacs-devel@m.gmane-mx.org; Sat, 04 Jan 2025 18:40:24 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tU880-0006Pz-G7; Sat, 04 Jan 2025 12:40:00 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tU87y-0006Pp-M8 for emacs-devel@gnu.org; Sat, 04 Jan 2025 12:39:58 -0500 Original-Received: from dancol.org ([2600:3c01:e000:3d8::1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tU87u-0005el-8k; Sat, 04 Jan 2025 12:39:56 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=dancol.org; s=x; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID:Date: References:In-Reply-To:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=IQv9rz5L8wnnJRPyOzIS0jnrXrwLLgaj/WT66acP7GU=; b=JwotZ4HhNV1Ytl12YOpm3jIOeJ /MTwXSqZspsnXDfZqqI5EUL2lnU2WneZ2FA7OqF0wudcvZZO+lMT3GAOYkTtzFWZd8zke9zwMmPCV JSq947VTemPBHBIuhugc5c6dzCiJnlk/NhG6UcIqZoxVHcCfOopl/AiK1OGqRJLhSPrgyz1ufaLuS 8LX9ev9VKsW+yMBG0J4GRmoyEIeZ0h+h0atn0kz5YOoL99B3WwK93huulyNSP2BXaGsMdkVR/TvBW L8ZYmuO4FQYEyp5HA8boat8QYoCUYrg5ihXDoaapITB8YXXvD0+qLTlcKPOF36eV6o2VHEoKV3Icy KWwqysVA==; Original-Received: from 2603-9001-4203-1ab2-88cb-7eba-2381-0587.inf6.spectrum.com ([2603:9001:4203:1ab2:88cb:7eba:2381:587]:39850 helo=localhost) by dancol.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tU87m-0005vd-0s; Sat, 04 Jan 2025 12:39:46 -0500 In-Reply-To: (Lynn Winebarger's message of "Sat, 4 Jan 2025 11:15:22 -0500") Received-SPF: pass client-ip=2600:3c01:e000:3d8::1; envelope-from=dancol@dancol.org; helo=dancol.org X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:327676 Archived-At: Lynn Winebarger writes: > On Wed, Jan 1, 2025 at 3:23=E2=80=AFPM Bj=C3=B6rn Bidar wrote: >> Lynn Winebarger writes: >> >> 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. >> > >> >> Additional alernatives instead of Node are already a good alternative. >> Using WASM as the output format also does not sound bad assuming their >> is some abstraction from the tree-sitter library side. > > I'm not sure why WASM would be interesting. AFAICT, it's just another > set of bindings to the C library, maybe with the tables compiled into > WASM binary module (or whatever the correct term should be - I'm not a > WASM expert). In any case, AFAIK Emacs has no particular capability > for using WASM files as dynamic libraries in general. Maybe if Emacs > itself was compiled to WASM, in which case I suppose the function for > dynamically loading libraries would implicitly load such modules. > > OTOH, the generated WASM bindings might provide an example of using > the tree-sitter DLL with the in-memory parse table structure not > embedded in the tree-sitter DLL. Is that what you meant? I think people get too excited about WASM. It's just a 1) portable, 2) sandboxed mechanism for running the same programs you could compile to native code. What's in it for us? We don't need a security sandbox for parsers. If we want to sandbox, we should do it at a higher level. The portability aspect seems like only a minor benefit: sure, it's less of a logistical headache to ship one prebuilt binary than to ship N for N different architectures, but either way, you're opting into the headache of prebuilt binaries. I'd rather dynamically build from source, TBH. >> > 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. >> >> What kind of scanner are you referring to? Something that works like a >> binding generator but for AST? > > A few years ago, I wanted a template system for this terrible > proprietary language I was working with, so I wrote this grammar that > could encompass that language (which, AFAICT, was only defined by > company programmers hacking additional patterns directly into their > hand-written parser, for which I reverse-engineered a LALR(1) > grammar), a shell-type interpolation sublanguage, and other languages > that stuck to the syntactic constructs allowed by Python and C++. It > was a bear to work out, and I ended up throwing it away, anyway. But > the point is, at the start of an interpolation context, the parser > would switch scanner and parser tables to the language assigned to the > scope of that interpolation context (associated with a particular > terminal introducing that context in the "current" parser table). So > while parsing language A, "${" might introduce an interpolation > context for language B, "$!{" for language C, "$[" for language D, > etc. As long as the new scanner or parser could discriminate the > closing terminal as ending the sublanguage program and returning to > language A context, it should work. > > Anyway, for that purpose, I wanted a grammar that would be flexible > enough that I could just switch the bindings for the actions and > mapping of terminals, not change the whole grammar, so I would only > need to do the grammar analysis once. That being said, I never > actually showed it could be done with multiple real terminals for a > single meta-terminal. That is, in the previous paragraph there might > have been a "meta-terminal" "START_INTERPOLATION_CONTEXT" that would > expand to 3 concrete terminals (in the grammar for language A) > "START_INTERPOLATION_B", "START_INTERPOLATION_C", > "START_INTERPOLATION_D", so the parser would have to know which of > those concrete terminals was being reduced to choose the right action. > I've been waiting for the details to rot from my memory so I can start > from scratch on a concrete grammar. ANTLR's lexer modes gives you a similarly powerful capability, FWIW. > Aside from being useful for generic templating purposes, Such a > generic grammar would be of use for the purpose Daniel described, i.e. > a layer of abstraction usable for almost any modern language, even in > polyglot texts. Arbitrary language composition has been the holy grail for a while, yes? GLR grammars are closed under composition too. Making it easier to define tree-sitter grammars and lexers that refer to each other would be nice. At this point, though, I think it's more important to finish the task of making tree-sitter-based modes as usable and Emacs-y as traditional ones than to imagine new meta-parser description abstractions. >> > 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. >> >> From my point as a distribution packager there are several reason why >> vendoring can be bad or in some context keeping them is the better >> decision. >> >> But in this context it complicates the build process as now each grammar >> has to be built for Emacs in addition to another editors. >> The Emacs package now pulls in more build dependencies at built time >> which complicates the built process as the dependency grows. >> >> Besides bundled dependencies are not allowed unless there's no way to >> avoid them. It is not about control or anything. > > That sounds like something I would interpret as control. Distro > creators/maintainers are prime candidates for wanting to maintain > control of the build/run-time environment, as they are responsible for > everything they bundle working together. Perhaps "control of their > computational resources" is more specific than I intended in my > previous posting. The point I keep trying to make is that you can't safely update a foo-ts-mode tree sitter grammar without updating the corresponding foo-ts-mode Lisp. They're tightly coupled. They're not separate programs. Same goes for nvim or whatever using TS grammars. Even distribution packagers understand the futility of consolidating dependencies with unstable interfaces. When it comes to Emacs, we either 1) treat grammars as part of Emacs and build them with Emacs, or 2) try to take a runtime dependency on grammars that can be updated independently of Emacs. Compatibility considerations mean #2 can't work, so we're left with doing #1 somehow. We're not talking about something like libpng, which could in principle be updated without Emacs having to know about the update. We're talking about something that's an implementation detail of Emacs, one that just so happens to have begun life outside Emacs. The simplest possible way to implement #1 is to just check the grammars into the Emacs repository and build them with Emacs using the normal build system. Trying to check in hashes and download the hash-named grammar versions during the build and *then* build them with Emacs --- why bother? Because of the hash-locking, a download-at-build-time scheme doesn't actually add any flexibility relative to just checking in the code. It's just a more complicated and error-prone way of doing the same thing as checking in the code. The same goes for other forms of downloading dependencies, e.g. via git submodules.