r/java • u/DelayLucky • 9d ago
Regex Parser for Static Analysis
https://google.github.io/mug/apidocs/com/google/common/labs/regex/RegexPattern.htmlThis is a parser that I created a while back, in order to build some regex static analysis tools (detecting ReDos, validating supported regex features etc.).
I built it because I didn't find an OSS regex parser that produces and exposes the AST for custom analysis (the regex engines I looked at all keep their parser internal, and only execute the regex).
The parser supports advanced features such as back references, lookarounds, unicode properties, posix char classes, inline and global comment mode (a.k.a free-spacing mode) etc.
Using it with pattern matching is easy:
switch (RegexPattern.of("\\w+:\\d+")) {
case Sequence(var elements) -> ...;
case Alternation(var alternatives) -> ...;
...
default -> ...;
}
To keep it simple, A few obscure features are omitted: such as \12 being parsed differently depending on the number of capture groups defined (\1 followed by literal 2 if no 12 capture groups are present).
The code is combinator-based, mostly pair-programmed by AI and me. It's actually what I built the dot-parse library for: to be able to have AI do the chores of generating readable parser for the wide surface area and arcane regex grammar rules; so I don't have to get a degree in regex.
It has served the purpose of static analysis well.
Feedbacks are welcome!
View all comments
Show parent comment
1
u/DelayLucky 8d ago edited 8d ago
Oh yeah it's a pain.
The regex has so many grammar rules and some very bizarre edge cases. What I know in my head is only a small subset. I wouldn't want to do it again if I can't offload the drudgery to AI.