A fast Java parser for Semantic Versioning with correct precedence ordering

I have a love/hate relationship with semantic versioning. Not because it uses three instead of just two version numbers, but because of the rattail of pre release and meta tags you can append to it and the insanely complex rules you have to follow when you do. I mean, take the following three examples:

All of them are valid semantic version identifiers. But what’s their correct order? You already need a background in compiler construction (always kind of a hobby of mine) to parse those strings, but sorting them, too? Total nightmare!

I found a couple of libraries that seem to do the trick, but boyah, talk about code bloat. Sure, the SemVer grammar isn’t exactly trivial, but you don’t have to take it to the YACC level and/or spread it across half a dozen classes. Not to mention that adding a dependency to your project just for parsing a single line string is an absolute overkill.

When you (ab)use Java’s native callstack to do recursive descend parsing, then the whole thing can easily be done in a single class with minimum overhead.

There, an efficient non bloated parser in only 132 lines of code. Here’s a JUnit testcase for it:


Posted in Tinkering