Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failure found by TestOperations.testGetRandomAcceptedString #14227

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,15 @@ final RegExp parseCharClasses() throws IllegalArgumentException {
do {
// look for escape
if (match('\\')) {
expandPreDefined(starts, ends);
if (peek("\\ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")) {
// special "escape" or invalid escape
expandPreDefined(starts, ends);
} else {
// escaped character, don't parse it
int c = next();
starts.add(c);
ends.add(c);
}
} else {
// parse a character
int c = parseCharExp();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ public void testGetRandomAcceptedString() throws Throwable {
final int ITER2 = atLeast(100);
for (int i = 0; i < ITER1; i++) {

final RegExp re = new RegExp(AutomatonTestUtil.randomRegexp(random()), RegExp.NONE);
final String text = AutomatonTestUtil.randomRegexp(random());
final RegExp re = new RegExp(text, RegExp.NONE);
// System.out.println("TEST i=" + i + " re=" + re);
final Automaton a = Operations.determinize(re.toAutomaton(), DEFAULT_DETERMINIZE_WORK_LIMIT);
assertFalse(Operations.isEmpty(a));
assertFalse("empty: " + text, Operations.isEmpty(a));

final AutomatonTestUtil.RandomAcceptedStrings rx =
new AutomatonTestUtil.RandomAcceptedStrings(a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,15 @@ public void testExcapedSlashNotCharClass() {
assertSameLanguage(expected, actual);
}

public void testEscapedDashCharClass() {
RegExp re = new RegExp("[\\-]");
assertEquals("REGEXP_CHAR char=-\n", re.toStringTree());
Automaton actual = re.toAutomaton();
assertTrue(actual.isDeterministic());
Automaton expected = Automata.makeChar('-');
assertSameLanguage(expected, actual);
}

public void testEmpty() {
RegExp re = new RegExp("#", RegExp.EMPTY);
assertEquals("#", re.toString());
Expand All @@ -391,6 +400,26 @@ public void testEmpty() {
assertSameLanguage(expected, actual);
}

public void testEmptyClass() {
Exception expected =
expectThrows(
IllegalArgumentException.class,
() -> {
new RegExp("[]");
});
assertEquals("expected ']' at position 2", expected.getMessage());
}

public void testEscapedInvalidClass() {
Exception expected =
expectThrows(
IllegalArgumentException.class,
() -> {
new RegExp("[\\]");
});
assertEquals("expected ']' at position 3", expected.getMessage());
}

public void testInterval() {
RegExp re = new RegExp("<5-40>");
assertEquals("<5-40>", re.toString());
Expand Down