Skip to content

Adam Chalmers

Tag: #parsing

Video: Parsing a programming language

KittyCAD's second Rust Club video is up! My [first video][/winnow-basics] covered the Winnow library for parsing text. Today's video shows you a real-world parser example. I'm working on the KittyCAD language (KCL) for designing CAD models, and this reviews the tokenizer and parser I wrote recently.

Apologies for the low video quality, I didn't know we'd be releasing this to YouTube, so it's just using Zoom's "record screen" feature. Starting from episode 5 we used a better-quality recording system.

Video: Parsing text with Winnow

I work at KittyCAD, and every Monday I teach Rust to my coworkers for an hour. We always record them, so that over time we build up an archive of Rust learning videos. Recently I realized we should put some of them on the internet, because other programmers outside KittyCAD might find them helpful too!

This is our first episode, and it's about building parsers with Winnow, a new fork of Nom. I'm a big fan of Nom, and I've written about it a lot. I've been interested in Winnow since it was announced, so here's an hour-long explanation of how to use Winnow.

Apologies for the low video quality, I didn't know we'd be releasing this to YouTube, so it's just using Zoom's "record screen" feature. Future episodes should have higher video quality!

Parsing bitstreams with Nom

Programming languages generally only manipulate bytes (groups of 8 bits). It can be pretty tricky to manipulate single bits. But sometimes you need to -- for example, a DNS header has some 4-bit numbers, and encodes some boolean flags into single bits. So we really need a way to parse binary data without chunking it up into bytes of 8 bits.

Luckily, Nom can do this! In the last blog post, we learned how to parse text files with Nom. The trick is to start with simple parsers that parse a few characters at a time. Then, using combinators, combine those simple parsers into more complex parsers that can deserialize an entire structured file. We can reuse this approach for parsing binary data too. Let's see how!

Parsing Text with Nom

"Parsing" is turning a stream of raw text or binary into some structured data types, i.e. a Rust type that your code can understand and use. This isn't the textbook definition of parsing, but damnit, this is my blog and my opinion. This tutorial is about nom, my favourite Rust parsing library. It uses a parser combinator approach: you start writing tiny parsers that match, say, a single number or a character. These become building blocks for larger parsers, that match, say, a date or a phone number. By combining many small parsers together, you can build a big parser that decodes a file or stream into nice Rust structs and enums. In this tutorial we'll use Nom to parse the input file to an Advent of Code puzzle.