219 points · by thunderbong · 2y ago · 46 comments
- Convert tree-sitter grammar.json to EBNF https://meimporta.eu/tree-sitter/json2ebnf.html
- An alternative syntax to write tree-sitter grammars https://mingodad.github.io/lua-wasm-playground/ (select "Tree-sitter-ebnf-generator" from "Examples" then click "Run" to see the tree-sitter grammar for the content in "Input Text (arg[1])")
- An Yacc/Lex online editor/tester with several example grammars https://mingodad.github.io/parsertl-playground/playground/
Tree-sitter is great until you inevitably run into a problem with it and find that it is impossible to fix properly if you can even figure out why the problem even exists.
The idea of a uniform api for querying syntax trees is a good one and tree-sitter deserves credit for popularizing it. It's unfortunately not a great implementation of the idea.
The wasm bindings are a necessity if you want to be anywhere near "stable", but they run 3x slower.
^ the JS/TS parser when invoked via the Node bindings, at least. Others are potentially better. The GH issues are rife with similar complaints.
Or is it an issue with poorly-written grammars? The second you need to write an external parser with tree sitter, you have to start writing C, so I can see how that could lead to segfaults very quickly.
I dunno, I guess I'm just a bit surprised to hear a bunch of negative feedback towards tree-sitter, because up until now most of what I've heard has been pretty positive.
It's not really accurate to say that Tree-sitter segfaults constantly. The Tree-sitter CI performs randomized fuzz testing based on a bunch of popular grammars, randomly mutating entries from their test corpus. If you have a reproduction case for the segfault, it'd be useful to report.
Since you mention NPM, it sounds like you may be talking about a segfault that's specific to the Tree-sitter Node.js bindings, but it's hard to be sure.
It's not surprising to me that there are issues with generated parsers, partly because many languages need external scanners to work, which have to be written in C and use a rather clunky API. tree-sitter-javascript also uses an external scanner so you might want to look there for whatever is causing the problem.
Announcement: https://marijnhaverbeke.nl/blog/lezer.html
Homepage: https://lezer.codemirror.net/
Link in the post is currently broken, but here’s the author’s grammar on GitHub: https://github.com/treeman/tree-sitter-djot
> But, you might wonder how does a Tree-sitter grammar look like,
How does $thing look?
or
What does $thing look like?
Similarly, one often hears "Where are we at?" It is common enough to be seen as a regionalism.
-- Your friendly tree-sitting grammarian
I wonder if it's one of those little things that's surprisingly rare, like the th sound.
1. But you might wonder: what does a Tree-sitter grammar look like, and how do you create one for a language?
2. But you might wonder what a Tree-sitter looks like and how to create one for a language.
In either case, I agree the right word is "what" not "how". The comma after "but" bugs me tremendously. The colon can be replaced by an em dash (—) if the writer prefers it—but I would not use a question mark for the second clause without separating it from the first "but you might wonder" in some way.
The comma left in alternative #1 is to needed (recommended?) because the "and" is joining two clauses with different subjects. I think it's not strictly needed in alternative #2 because it's really "(but you might wonder (what a tree sitter looks like and how to create one for a language))" where the inner term is a list of things the reader might wonder, and lists with only two items do not take a comma before "and"
I would also prefer "Where are we?" over "Where are we at?"
Not an English speaker, so just my $0.02.
I’m a native English speaker who says “Where are we at”. I think it’s informal, but I never considered it to be wrong or awkward like the first example. Perhaps I’m in the region where it’s common.
Often elided to "Where we at?".
It's dialect, I wouldn't use either in formal/technical writing, but I would unselfconsciously say the latter, as in "ok, moving down the agenda, where we at with item 3?".
One issue that I remember was the web application to test the grammar needed an exact version of the emscripten stuff. And I had to sort out what that version was myself.
In the footer there’s a link to OPs source code. The whole site gets compiled with Rust. Interesting gem to find.
Also, the bold version of iosevka is an order of magnitud heavier than the other fonts :-).
--
1: https://docs.aws.amazon.com/AmazonCloudFront/latest/Develope...
I'm wondering why isn't there any language agnostic AST, commonly referred to as intermediate representation, and higher level logic such as data flow graph built on Tree-sitter. I mean writing the Grammer is of course something that is mandatory but it seems like programming languages are too much different in the architectural level of Tree-sitter
Where we've found success is in stepping back and creating formalisms that are language-agnostic to begin with, and then using tree-sitter to manage the language-specific translations into that formalism. A good example is how we use stack graphs for precise code navigation [1], which uses a graph structure to encode a language's name binding semantics, and which we build up for several languages from tree-sitter parse trees [2].
[1] https://dcreager.net/talks/stack-graphs/
[2] https://github.com/github/stack-graphs/tree/main/languages
For your data flow graph, are you going to handle copy constructors, assignment operator overloads, implicit conversions etc. like you see in C++? Or how about overload resolution: figuring out which method wins out over all candidates requires details about the convertibility of types, and how the language ranks candidates based on conversions required. And let's not forget Koenig lookup.
Things like Tree-sitter can discover definitions, declarations and invocations, but matching the right declaration for an invocation isn't trivial in the presence of overloads.
Consider three languages, one with binary operators but no function application, and another with only function application, and a third with both. What would the "agnostic" AST be, except a union of the AST of the first two?
Is the plan to at some point deprecate the existing vim-style syntax highlighting (syn match/region etc.) in favor of treesitter-based syntax highlighting? Where can I find this discussion if it is happening?