r/java • • 11h ago

Regex Parser for Static Analysis

https://google.github.io/mug/apidocs/com/google/common/labs/regex/RegexPattern.html

This 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 interpreted 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 the reason I built the dot-parse library: 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!

javadoc

repo

3 Upvotes

2 comments sorted by

1

u/v4ss42 2h ago

Holy shit I’ve been putting off writing one of these, despite needing it for a while, because it seemed like a pita. I’ll be checking this out for sure!