Regex is not always the best solution: How to build a recursive parser

Tired of regex? We built a recursive parser in TypeScript for nodejs/doc-kit, handling nested generics and arrow functions.

jueves, 2 de julio de 2026 • 2 min read • Q2BSTUDIO Team

From regex to recursive parser: handling complex types in TypeScript

When a developer reads technical API documentation and clicks on a data type, they expect to jump directly to its definition. Behind that apparent simplicity lies a parsing problem that has plagued many engineering teams: how to correctly interpret nested generic types, arrow functions, and complex unions written in TypeScript. The initial temptation is often to look for a quick solution with regular expressions. However, as demonstrated by the real experience of optimizing Webpack documentation, regexes are pattern-matching tools, not syntactic analyzers. When a type like Promise<Map<string, number & string>> appears, any attempt to use a regex fails miserably, because regular expressions cannot reliably handle infinitely nested structures. The solid alternative is to build a recursive descent parser, a classic compiler technique that breaks down the expression from the lowest precedence operators to the atomic ones. This approach, based on traversing the string character by character while maintaining a depth counter for parentheses, brackets, and angle brackets, allows detecting the top-level operator (such as the function arrow, union, or intersection) and then splitting the expression into parts that are processed recursively. Each fragment is cleaned up until reaching the base types, which are the only ones linked to documentation anchors. The implementation requires three pillars: an iterator that respects nesting depth, a finder of the highest precedence operator at the root level, and a recursive engine that resolves each subexpression and recombines the results with the correct separators. This type of development is not just an academic exercise; it has a direct impact on the quality of the tools that programmers use every day. At Q2BSTUDIO, we understand that quality software engineering requires going beyond superficial patches. That is why we offer custom applications and bespoke software that integrate parsing, compilation, and syntactic analysis solutions when circumstances require it. Our teams apply principles of artificial intelligence, cybersecurity, AWS and Azure cloud services, as well as business intelligence services, to build robust and maintainable systems. Whether you need AI for businesses, AI agents, or dashboards with Power BI, at Q2BSTUDIO we design solutions that address the root of problems, not just the symptoms. Because as the recursive parser case demonstrates, taking the time to understand the true complexity of a problem and building a mathematically solid solution is always the best long-term investment.

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.