Asko Nõmm

3 min read

Dompa for Zig

Whenever I learn a new language, I do so by doing. No books, no blogs, no videos. I get a project I want to build, I go to the language's website, open up the docs, and start writing. My favorite project to do when learning a new language is to build a HTML document parser. I've written one for Rust and another one for Python.

I find it's easy enough that I won't ever need to spend more than a weekend on it (usually one evening is enough) and yet complex enough that I go through many crucial parts of what makes up the language to form an opinion and get a general feeling about it.

Recently, I was learning Zig, and thus, I created a HTML parser for Zig called Dompa. It parses a string of HTML into a tree of nodes, provides an API to traverse said tree and manipulate it programmatically, and also to then transform the tree of nodes back to an HTML string.

Use it in your Zig project:

Fetch the dependency:

zig fetch --save git+https://git.sr.ht/~asko/dompa

Import it in your build.zig:

const dompa = b.dependency("dompa", .{
  .target = target,
  .optimize = optimize,
});

// Add Dompa to lib
lib.root_module.addImport("dompa", dompa.module("dompa"));

// Add Dompa to exe
exe.root_module.addImport("dompa", dompa.module("dompa"));

Conclusion

Learning Zig was a bit of a challenge namely because it has pretty non-existent docs, especially around its new build system that allows for packages, but aside from that I really like the ergonomics of the language. It's not memory safe like Rust, but I don't get the same sweaty palms I get when writing C because Zig has made manual memory management very straight forward, and unlike C it has pretty decent stack traces if you do mess up somehow. Whilst I'm not a fan of Rust's syntax, I do really like Zig's. Overall it seems to be a promising thing.

I definitely still have some finishing touches I want to do to Dompa, such as instead of returning a std.ArrayList(Node) I would actually return my own struct so that I could provide more convenient API's (and deinit), and since I like the language so much perhaps I will also work on some other library in Zig. One such example I sometimes write is a templating language, which uses my own HTML parser, and always greatly enhances my learning experience because then I learn how library deployment and usage really works, and how nice or bad that whole thing is.