re2c lexical analyzer generator now supports Rust!

45 points permetz

I write a lot of parsers. Ever since I started using Rust, I've missed my favorite lexical analyzer generator, re2c, which supported only C, C++, and Go. I asked the maintainer if he would add Rust support, and he added it, so re2c now can generate lexers written in Rust.

You might wonder why you would want to use re2c. The rest of this post is a sales job.

re2c has a bunch of great features:

  1. It is extremely flexible to use. The big issue I usually have with other lexical analyzer tools is that they get in your way, sometimes enough so that you give up and write a lexer by hand. For most such tools, if your use case doesn't exactly conform with the use case that the creators had in mind, you're stuck and have to give up on using the tool. re2c, by contrast, mostly stays out of your way, doing the tedious work of building a big state machine for you without restricting how you use that state machine at all.
  2. It is "zero cost". The lexical analyzers it produces are as fast as any code you could possibly write manually (and in practice are faster because building a large inline-coded state machine by hand is simply too tedious). There is never a performance advantage to rolling your own lexer over using re2c (and in the unlikely event you can find one, the maintainer is really good about incorporating performance improvements for the generated code).
  3. It comes with really extensive documentation and a lot of good code examples you can steal.
  4. It supports creating lexers for ASCII, UTF-8, UTF-16, UTF-32, and EBCDIC (not that you probably care about EBCIDIC) so you're pretty much set on all modern text encoding schemes.
  5. It does extremely efficient submatch extraction, partially by virtue of some original research the current maintainer did (and published) on submatches in regular expressions.
  6. And of course, it's open source (it's in fact public domain, which is as unrestricted as things get.)

I used to use re2c for most of my language processing projects and I've already been using it with Rust even before the official release. It's fantastic. Most people don't use a lexical analyzer generator every day, but if you need one, you should try it.