Skip to content

Commit 5663603

Browse files
Parser: Report invalid syntax with IllegalArgumentException
1 parent 9255ecc commit 5663603

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

src/main/java/org/cornutum/regexpgen/js/Parser.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class Parser
3131
* Returns a {@link RegExpGen} that generates strings containing characters that match the given
3232
* JavaScript regular expression.
3333
*/
34-
public static RegExpGen parseRegExp( String regexp)
34+
public static RegExpGen parseRegExp( String regexp) throws IllegalArgumentException
3535
{
3636
return new Parser( regexp).parse( false);
3737
}
@@ -40,7 +40,7 @@ public static RegExpGen parseRegExp( String regexp)
4040
* Returns a {@link RegExpGen} that generates strings containing only characters that match the
4141
* given JavaScript regular expression.
4242
*/
43-
public static RegExpGen parseRegExpExact( String regexp)
43+
public static RegExpGen parseRegExpExact( String regexp) throws IllegalArgumentException
4444
{
4545
return new Parser( regexp).parse( true);
4646
}
@@ -57,11 +57,11 @@ private Parser( String regexp)
5757

5858
/**
5959
* Returns the {@link RegExpGen} represented by this JavaScript regular expression.
60-
* If <CODE>exact</CODE> is true, the result generates strings containin only
60+
* If <CODE>exact</CODE> is true, the result generates strings containing only
6161
* characters matching this regular expression. Otherwise, the result generates strings
6262
* that may contain other characters surrounding the matching characters.
6363
*/
64-
private RegExpGen parse( boolean exact)
64+
private RegExpGen parse( boolean exact) throws IllegalArgumentException
6565
{
6666
RegExpGen regExpGen = getNext();
6767

@@ -1020,7 +1020,7 @@ private void unexpectedChar( char c)
10201020
*/
10211021
private RuntimeException error( String reason)
10221022
{
1023-
return new IllegalStateException( String.format( "%s at position=%s", reason, cursor_));
1023+
return new IllegalArgumentException( String.format( "%s at position=%s", reason, cursor_));
10241024
}
10251025

10261026
/**

src/test/java/org/cornutum/regexpgen/js/ParserTest.java

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,7 @@ public void whenAlternativeMissing()
14361436
// Given...
14371437
String regexp = "A||C";
14381438

1439-
expectFailure( IllegalStateException.class)
1439+
expectFailure( IllegalArgumentException.class)
14401440
.when( () -> Parser.parseRegExp( regexp))
14411441
.then( failure -> {
14421442
assertThat( "Failure", failure.getMessage(), is( errorAt( "Alternative missing", 2)));
@@ -1449,7 +1449,7 @@ public void whenExtraStartAnchor()
14491449
// Given...
14501450
String regexp = "^A(B|^C)";
14511451

1452-
expectFailure( IllegalStateException.class)
1452+
expectFailure( IllegalArgumentException.class)
14531453
.when( () -> Parser.parseRegExp( regexp))
14541454
.then( failure -> {
14551455
assertThat( "Failure", failure.getMessage(), is( errorAt( "Start-anchored expression can be matched at most once", 8)));
@@ -1462,7 +1462,7 @@ public void whenFollowingEndAnchor()
14621462
// Given...
14631463
String regexp = "(ABC$)(DEF)";
14641464

1465-
expectFailure( IllegalStateException.class)
1465+
expectFailure( IllegalArgumentException.class)
14661466
.when( () -> Parser.parseRegExp( regexp))
14671467
.then( failure -> {
14681468
assertThat( "Failure", failure.getMessage(), is( errorAt( "Extra expressions not allowed after $ anchor", 11)));
@@ -1475,7 +1475,7 @@ public void whenWordBoundaryStart()
14751475
// Given...
14761476
String regexp = "Matchy (\\bMatchy)";
14771477

1478-
expectFailure( IllegalStateException.class)
1478+
expectFailure( IllegalArgumentException.class)
14791479
.when( () -> Parser.parseRegExp( regexp))
14801480
.then( failure -> {
14811481
assertThat( "Failure", failure.getMessage(), is( errorAt( "Unsupported word boundary assertion", 8)));
@@ -1488,7 +1488,7 @@ public void whenWordBoundaryEnd()
14881488
// Given...
14891489
String regexp = "Matchy\\B Matchy";
14901490

1491-
expectFailure( IllegalStateException.class)
1491+
expectFailure( IllegalArgumentException.class)
14921492
.when( () -> Parser.parseRegExp( regexp))
14931493
.then( failure -> {
14941494
assertThat( "Failure", failure.getMessage(), is( errorAt( "Unsupported word boundary assertion", 6)));
@@ -1501,7 +1501,7 @@ public void whenNegativeLookBehind()
15011501
// Given...
15021502
String regexp = "(?<!ABC)D";
15031503

1504-
expectFailure( IllegalStateException.class)
1504+
expectFailure( IllegalArgumentException.class)
15051505
.when( () -> Parser.parseRegExp( regexp))
15061506
.then( failure -> {
15071507
assertThat( "Failure", failure.getMessage(), is( errorAt( "Unsupported negative look-behind assertion", 0)));
@@ -1514,7 +1514,7 @@ public void whenLookBehindMissing()
15141514
// Given...
15151515
String regexp = "(?<=)ABC";
15161516

1517-
expectFailure( IllegalStateException.class)
1517+
expectFailure( IllegalArgumentException.class)
15181518
.when( () -> Parser.parseRegExp( regexp))
15191519
.then( failure -> {
15201520
assertThat( "Failure", failure.getMessage(), is( errorAt( "Missing look-behind expression", 4)));
@@ -1527,7 +1527,7 @@ public void whenLookBehindUnterminated()
15271527
// Given...
15281528
String regexp = "(?<=XYZ";
15291529

1530-
expectFailure( IllegalStateException.class)
1530+
expectFailure( IllegalArgumentException.class)
15311531
.when( () -> Parser.parseRegExp( regexp))
15321532
.then( failure -> {
15331533
assertThat( "Failure", failure.getMessage(), is( errorAt( "Missing ')'", 7)));
@@ -1540,7 +1540,7 @@ public void whenExtraStartAssertion()
15401540
// Given...
15411541
String regexp = "(?<=A)^Z";
15421542

1543-
expectFailure( IllegalStateException.class)
1543+
expectFailure( IllegalArgumentException.class)
15441544
.when( () -> Parser.parseRegExp( regexp))
15451545
.then( failure -> {
15461546
assertThat( "Failure", failure.getMessage(), is( errorAt( "Start assertion is inconsistent with look-behind assertion", 7)));
@@ -1553,7 +1553,7 @@ public void whenLookBehindIncomplete()
15531553
// Given...
15541554
String regexp = "A(?<=B)";
15551555

1556-
expectFailure( IllegalStateException.class)
1556+
expectFailure( IllegalArgumentException.class)
15571557
.when( () -> Parser.parseRegExp( regexp))
15581558
.then( failure -> {
15591559
assertThat( "Failure", failure.getMessage(), is( errorAt( "Missing regular expression for look-behind assertion", 7)));
@@ -1566,7 +1566,7 @@ public void whenNegativeLookAhead()
15661566
// Given...
15671567
String regexp = "ABC(?!D)";
15681568

1569-
expectFailure( IllegalStateException.class)
1569+
expectFailure( IllegalArgumentException.class)
15701570
.when( () -> Parser.parseRegExp( regexp))
15711571
.then( failure -> {
15721572
assertThat( "Failure", failure.getMessage(), is( errorAt( "Unsupported negative look-ahead assertion", 3)));
@@ -1579,7 +1579,7 @@ public void whenLookAheadMissing()
15791579
// Given...
15801580
String regexp = "(ABC)(?=)Z";
15811581

1582-
expectFailure( IllegalStateException.class)
1582+
expectFailure( IllegalArgumentException.class)
15831583
.when( () -> Parser.parseRegExp( regexp))
15841584
.then( failure -> {
15851585
assertThat( "Failure", failure.getMessage(), is( errorAt( "Missing look-ahead expression", 8)));
@@ -1592,7 +1592,7 @@ public void whenLookAheadUnterminated()
15921592
// Given...
15931593
String regexp = "ABC(?=DEF";
15941594

1595-
expectFailure( IllegalStateException.class)
1595+
expectFailure( IllegalArgumentException.class)
15961596
.when( () -> Parser.parseRegExp( regexp))
15971597
.then( failure -> {
15981598
assertThat( "Failure", failure.getMessage(), is( errorAt( "Missing ')'", 9)));
@@ -1605,7 +1605,7 @@ public void whenExtraEndAssertion()
16051605
// Given...
16061606
String regexp = "ABC$(?=DEF)";
16071607

1608-
expectFailure( IllegalStateException.class)
1608+
expectFailure( IllegalArgumentException.class)
16091609
.when( () -> Parser.parseRegExp( regexp))
16101610
.then( failure -> {
16111611
assertThat( "Failure", failure.getMessage(), is( errorAt( "End assertion is inconsistent with look-ahead assertion", 11)));
@@ -1618,7 +1618,7 @@ public void whenEndAnchorMultiple()
16181618
// Given...
16191619
String regexp = "(ABC$)+";
16201620

1621-
expectFailure( IllegalStateException.class)
1621+
expectFailure( IllegalArgumentException.class)
16221622
.when( () -> Parser.parseRegExp( regexp))
16231623
.then( failure -> {
16241624
assertThat( "Failure", failure.getMessage(), is( errorAt( "End-anchored expression can be matched at most once", 7)));
@@ -1631,7 +1631,7 @@ public void whenStartAnchorMultiple()
16311631
// Given...
16321632
String regexp = "(A|^B|C)+";
16331633

1634-
expectFailure( IllegalStateException.class)
1634+
expectFailure( IllegalArgumentException.class)
16351635
.when( () -> Parser.parseRegExp( regexp))
16361636
.then( failure -> {
16371637
assertThat( "Failure", failure.getMessage(), is( errorAt( "Start-anchored expression can be matched at most once", 9)));
@@ -1644,7 +1644,7 @@ public void whenQuantifierMinMissing()
16441644
// Given...
16451645
String regexp = "X{}";
16461646

1647-
expectFailure( IllegalStateException.class)
1647+
expectFailure( IllegalArgumentException.class)
16481648
.when( () -> Parser.parseRegExp( regexp))
16491649
.then( failure -> {
16501650
assertThat( "Failure", failure.getMessage(), is( errorAt( "Missing number", 2)));
@@ -1657,7 +1657,7 @@ public void whenQuantifierUnterminated()
16571657
// Given...
16581658
String regexp = "A{1,2BC";
16591659

1660-
expectFailure( IllegalStateException.class)
1660+
expectFailure( IllegalArgumentException.class)
16611661
.when( () -> Parser.parseRegExp( regexp))
16621662
.then( failure -> {
16631663
assertThat( "Failure", failure.getMessage(), is( errorAt( "Missing '}'", 5)));
@@ -1670,7 +1670,7 @@ public void whenGroupEmpty()
16701670
// Given...
16711671
String regexp = "A()B";
16721672

1673-
expectFailure( IllegalStateException.class)
1673+
expectFailure( IllegalArgumentException.class)
16741674
.when( () -> Parser.parseRegExp( regexp))
16751675
.then( failure -> {
16761676
assertThat( "Failure", failure.getMessage(), is( errorAt( "Incomplete group expression", 2)));
@@ -1683,7 +1683,7 @@ public void whenGroupUnterminated()
16831683
// Given...
16841684
String regexp = "(A|B";
16851685

1686-
expectFailure( IllegalStateException.class)
1686+
expectFailure( IllegalArgumentException.class)
16871687
.when( () -> Parser.parseRegExp( regexp))
16881688
.then( failure -> {
16891689
assertThat( "Failure", failure.getMessage(), is( errorAt( "Missing ')'", 4)));
@@ -1696,7 +1696,7 @@ public void whenCharRangeStartInvalid()
16961696
// Given...
16971697
String regexp = "[\\w-Z]";
16981698

1699-
expectFailure( IllegalStateException.class)
1699+
expectFailure( IllegalArgumentException.class)
17001700
.when( () -> Parser.parseRegExp( regexp))
17011701
.then( failure -> {
17021702
assertThat( "Failure", failure.getMessage(), is( errorAt( "Character range must begin with a specific character", 5)));
@@ -1709,7 +1709,7 @@ public void whenCharRangeEndInvalid()
17091709
// Given...
17101710
String regexp = "[d-\\S]";
17111711

1712-
expectFailure( IllegalStateException.class)
1712+
expectFailure( IllegalArgumentException.class)
17131713
.when( () -> Parser.parseRegExp( regexp))
17141714
.then( failure -> {
17151715
assertThat( "Failure", failure.getMessage(), is( errorAt( "Character range must end with a specific character", 5)));
@@ -1722,7 +1722,7 @@ public void whenCharClassUnterminated()
17221722
// Given...
17231723
String regexp = "AB[CD";
17241724

1725-
expectFailure( IllegalStateException.class)
1725+
expectFailure( IllegalArgumentException.class)
17261726
.when( () -> Parser.parseRegExp( regexp))
17271727
.then( failure -> {
17281728
assertThat( "Failure", failure.getMessage(), is( errorAt( "Missing ']'", 5)));
@@ -1735,7 +1735,7 @@ public void whenCharClassEmpty()
17351735
// Given...
17361736
String regexp = "A[^]Z";
17371737

1738-
expectFailure( IllegalStateException.class)
1738+
expectFailure( IllegalArgumentException.class)
17391739
.when( () -> Parser.parseRegExp( regexp))
17401740
.then( failure -> {
17411741
assertThat( "Failure", failure.getMessage(), is( errorAt( "Empty character class", 3)));
@@ -1748,7 +1748,7 @@ public void whenControlCharInvalid()
17481748
// Given...
17491749
String regexp = "\\c9";
17501750

1751-
expectFailure( IllegalStateException.class)
1751+
expectFailure( IllegalArgumentException.class)
17521752
.when( () -> Parser.parseRegExp( regexp))
17531753
.then( failure -> {
17541754
assertThat( "Failure", failure.getMessage(), is( errorAt( "Invalid control escape character='9'", 2)));
@@ -1761,7 +1761,7 @@ public void whenHexCharInvalid()
17611761
// Given...
17621762
String regexp = "\\x1G";
17631763

1764-
expectFailure( IllegalStateException.class)
1764+
expectFailure( IllegalArgumentException.class)
17651765
.when( () -> Parser.parseRegExp( regexp))
17661766
.then( failure -> {
17671767
assertThat( "Failure", failure.getMessage(), is( errorAt( "Invalid hex character='1G'", 2)));
@@ -1774,15 +1774,15 @@ public void whenUnicodeCharInvalid()
17741774
// Given...
17751775
String incomplete = "\\u123";
17761776

1777-
expectFailure( IllegalStateException.class)
1777+
expectFailure( IllegalArgumentException.class)
17781778
.when( () -> Parser.parseRegExp( incomplete))
17791779
.then( failure -> {
17801780
assertThat( "Failure", failure.getMessage(), is( errorAt( "Invalid Unicode character='123'", 2)));
17811781
});
17821782

17831783
String nonHex = "\\u123X";
17841784

1785-
expectFailure( IllegalStateException.class)
1785+
expectFailure( IllegalArgumentException.class)
17861786
.when( () -> Parser.parseRegExp( nonHex))
17871787
.then( failure -> {
17881788
assertThat( "Failure", failure.getMessage(), is( errorAt( "Invalid Unicode character='123X'", 2)));
@@ -1795,7 +1795,7 @@ public void whenPrecedingStartAnchor()
17951795
// Given...
17961796
String regexp = "ABC^DEF";
17971797

1798-
expectFailure( IllegalStateException.class)
1798+
expectFailure( IllegalArgumentException.class)
17991799
.when( () -> Parser.parseRegExp( regexp))
18001800
.then( failure -> {
18011801
assertThat( "Failure", failure.getMessage(), is( errorAt( "Extra expressions not allowed preceding ^ anchor", 5)));
@@ -1808,7 +1808,7 @@ public void whenBackReferenceNumber()
18081808
// Given...
18091809
String regexp = "<([a-z]+)>(.*?)</\\1>";
18101810

1811-
expectFailure( IllegalStateException.class)
1811+
expectFailure( IllegalArgumentException.class)
18121812
.when( () -> Parser.parseRegExp( regexp))
18131813
.then( failure -> {
18141814
assertThat( "Failure", failure.getMessage(), is( errorAt( "Unsupported back reference to capturing group", 18)));
@@ -1821,7 +1821,7 @@ public void whenBackReferenceName()
18211821
// Given...
18221822
String regexp = "<([a-z]+)>(?<tag>.*?)</\\k<tag>>";
18231823

1824-
expectFailure( IllegalStateException.class)
1824+
expectFailure( IllegalArgumentException.class)
18251825
.when( () -> Parser.parseRegExp( regexp))
18261826
.then( failure -> {
18271827
assertThat( "Failure", failure.getMessage(), is( errorAt( "Unsupported back reference to named group", 24)));

0 commit comments

Comments
 (0)