The Parsing Paradox: How We Wrote a Doxygen Filter Without Doxygen

 · 4 min read

Transitioning a large project to new tracks is always an archaeologist’s job. Before we started implementing the native AST parser, the project lived on Doxygen, and even earlier—on an old proprietary parser.

The developers were not going to rewrite gigabytes of source code, so the code was historically littered with thousands of legacy markers like //HIDE-BEGIN and //HIDE-END. So that our pipeline (and Doxygen, which we still used as an intermediate layer back then) could understand these tags without touching the C++ code itself, we wrote a preprocessing script in Python. It was supposed to convert the legacy markers into native commands on the fly: \cond and \endcond.

Sounds like a one-liner task with re.sub? Not quite.

If the Regex Makes a Mistake — The API Disappears

If we had applied a simple regex for auto-replacement, the project would have collapsed. After running the basic script across the corpus, we discovered a harsh reality: developers are human, and they often made typos. They would put an opening //HIDE-BEGIN and forget the closing //HIDE-END.

If a naive regex stumbles upon such a typo and inserts a single \cond (opening a hidden Doxygen block), the parser will silently execute the order and hide the rest of the file all the way to the end! Hundreds of working functions would simply disappear from the documentation, and no tool would even throw an error—to Doxygen, a single \cond is absolutely legal, it just quietly writes a warning to the log.

Why did developers make these typos for years without noticing the losses? It’s about the difference in architectures. The old proprietary parser tied the hide marker to the current structural block: upon hitting the end of a class (the closing brace }), it automatically reset the hide mode, forgiving the forgotten END. But Doxygen processes \cond at the level of a primitive text preprocessor (like #ifdef 0 in C++). For it, this is a dumb switch: it “closes its eyes” and blindly discards all text until EOF, ignoring any class or namespace boundaries.

Legacy Tags Parser

We had to write a smart preprocessor with a stack. It honestly parsed the markers, checked their pairing, and even counted the balance of curly braces {} inside the hidden block of C++ code! If the braces or tags didn’t match, the script went into a “Fail-Open” mode: it gave up and returned the original untouched text with a warning in the log, thereby saving the file from complete amputation.

The Parser Bites Itself

Legacy Tags Parser Part 2

We were so proud of this clever Python script that we wrote huge, detailed documentation for it right in the source code. In the text comments to the code, we explained exactly how we change the old tags to \cond.

And then our CI updated the Doxygen version.

The new version became much more aggressive and started looking for its tags everywhere—even in the comments of Python files (after the # symbol). Doxygen read our text comments for the script, saw the word \cond in the explanation, decided it was a real command to act upon, and… silently deleted a piece of our own code from the documentation about the generator itself!

Documentation coverage dropped sharply because the parser applied its rules to the text that explained how to parse the parser. We had to urgently release a patch escaping our own text comments with double slashes (\\cond).

Conclusion

Legacy never disappears, it only changes form. We spent a ton of time writing a complex parser for old comments just to feed them to a mediocre parser, which we then replaced with an AST anyway.

But parsing text comments turned out to be child’s play compared to the quirks of real C++ syntax. In the next episode, we will tell you how we had to teach the native parser to simulate madness and invent wild names for nameless anonymous structs to save the architecture.