Skip to content

Java Regex Refactor - #1745

Open
lmasroca wants to merge 5 commits into
masterfrom
java-regex-refactor
Open

Java Regex Refactor#1745
lmasroca wants to merge 5 commits into
masterfrom
java-regex-refactor

Conversation

@lmasroca

@lmasroca lmasroca commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

Refactored Java regex character classes in both grammar and visitor:

  • Used new lexer mode (CHAR_CLASS_MODE) to avoid having to manually enumerate all the default lexer tokens allowed in classAtomNoDash in the Java parser. This simplifies that parser rule and allows us to remove code in visitNonemptyClassRanges that was breaking those default tokens back down into literal characters. This also allows us to remove && as a default mode lexer token, making it only a token within character classes.
  • Moved \p{...} escapes from CharacterEscape (which represents one character) into CharacterClassEscape (which represents predefined character classes), since \p{...} escapes represent predefined character classes.
  • Made classEscape have its own behavior instead of wrapping atomEscape, and extracted shared code into a method used by both. This simplifies visitClassEscape by skipping having to throw on back references since those are now rejected at lexer level (see related note below) as now back references no longer have a token on that context (CHAR_CLASS_MODE).
  • Added a test to check that character classes and related features are still working as intended.

Note: Since one of the new tests checks that [\1] is rejected at the lexer level, RegexHandler.ThrowingErrorListener.syntaxError(...) had to be updated. That method declared offendingSymbol: Any, but it is actually null when a lexer error is thrown (as there is no symbol at that point), so we changed it to Any?.

@lmasroca
lmasroca marked this pull request as ready for review September 9, 2026 01:05
@lmasroca
lmasroca requested a review from jgaleotti September 9, 2026 01:05
@jgaleotti
jgaleotti requested a review from arcuri82 September 9, 2026 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants