Zig AST explorer website?

Hi!

I used this website to get a visual representation of JavaScript AST. I found it useful when I was working on static analyzers.

There are lots of languages on this site, but not Zig.

Do you think it would be a good idea to write a zig parser in JS and create such a website? Is Zig based on a clear and public EBNF grammar or some other specification?

1 Like

There is a grammar posted on the docs, though I don’t know if it fits well known format.

3 Likes

There would be a lot of work involved, but Zig targets WASM, so it should be possible to use the Zig parser and jsonify the AST to pass it to a visualizer.

Pretty neat project if someone wants to tackle it.

1 Like

Maybe it’s simpler to write a parser in vanilla js?

Hard to say, but I would guess not.

A significant amount of the work involved is displaying the parse tree, and that’s the same effort for either approach.

If the parser is in JavaScript, that would have to be written from scratch, and to be useful it would have to be faithful enough to render the same AST as Zig’s parser, not just recognize the same strings. That can be tricky to get right.

The Zig code to parse a Zig string and then stringify the AST into JSON is not a lot of code. There would need to be an adapter between the WASM and the JavaScript, and that project would be using two languages instead of just one, so there’s complexity there.

I don’t think all of that adds up to more work than writing a faithful Zig parser in JavaScript though.

1 Like

You could also use a treesitter grammar for zig.

4 Likes

Just want to add here that Zig dropped js code for parsing from the old documentation in favor of wasm, so creating a vanilla js one manually would seem to me like going back to an earlier time and re-adding a burden of having to track and maintain javascript code that was already eliminated for the documentation.

I think a better option would be to implement it in Zig like @mnemnion suggested.
And then maybe something like this could be used in places that only can run javascript but not wasm? Usage · webassemblyjs


That said I won’t stop anyone from doing this manually, but I doubt it would be a satisfying experience to have to maintain something that also could be automated away in different ways.

3 Likes