File tree 4 files changed +45
-2
lines changed
src/edu/stanford/nlp/trees/tregex
test/src/edu/stanford/nlp/trees/tregex
4 files changed +45
-2
lines changed Original file line number Diff line number Diff line change @@ -36,6 +36,11 @@ public String localString() {
36
36
@ Override
37
37
public String toString () {
38
38
StringBuilder sb = new StringBuilder ();
39
+
40
+ if (isNegated () && isOptional ()) {
41
+ throw new AssertionError ("Shenanigans! The parser should not allow a pattern which is both negated and optional" );
42
+ }
43
+
39
44
if (isConj ) {
40
45
if (isNegated ()) {
41
46
sb .append ("!(" );
Original file line number Diff line number Diff line change @@ -365,14 +365,14 @@ public abstract class TregexPattern implements Serializable {
365
365
void negate () {
366
366
neg = true ;
367
367
if (opt ) {
368
- throw new RuntimeException ("Node cannot be both negated and optional." );
368
+ throw new IllegalStateException ("Node cannot be both negated and optional." );
369
369
}
370
370
}
371
371
372
372
void makeOptional () {
373
373
opt = true ;
374
374
if (neg ) {
375
- throw new RuntimeException ("Node cannot be both negated and optional." );
375
+ throw new IllegalStateException ("Node cannot be both negated and optional." );
376
376
}
377
377
}
378
378
Original file line number Diff line number Diff line change @@ -142,6 +142,8 @@ public TregexPattern compile(String tregex) {
142
142
throw new TregexParseException ("Could not parse " + tregex , tme );
143
143
} catch (ParseException e ) {
144
144
throw new TregexParseException ("Could not parse " + tregex , e );
145
+ } catch (IllegalStateException e ) {
146
+ throw new TregexParseException ("Could not parse " + tregex , e );
145
147
}
146
148
pattern .setPatternString (tregex );
147
149
return pattern ;
Original file line number Diff line number Diff line change @@ -1523,6 +1523,42 @@ public void testOptional() {
1523
1523
assertFalse (matcher .find ());
1524
1524
}
1525
1525
1526
+ /** The parser should not allow a pattern which is both negated and optional */
1527
+ public void testNegatedOptional () {
1528
+ TregexPattern pattern ;
1529
+ // these should be fine
1530
+ pattern = TregexPattern .compile ("A ?< B" );
1531
+ pattern = TregexPattern .compile ("A !< B" );
1532
+ // this should break
1533
+ try {
1534
+ pattern = TregexPattern .compile ("A !?< B" );
1535
+ } catch (TregexParseException e ) {
1536
+ // yay, passed
1537
+ }
1538
+ // this should also break
1539
+ try {
1540
+ pattern = TregexPattern .compile ("A ?!< B" );
1541
+ } catch (TregexParseException e ) {
1542
+ // yay, passed
1543
+ }
1544
+
1545
+ // these should be fine
1546
+ pattern = TregexPattern .compile ("A ?(< B < C)" );
1547
+ pattern = TregexPattern .compile ("A !(< B < C)" );
1548
+ // this should break
1549
+ try {
1550
+ pattern = TregexPattern .compile ("A !?(< B < C)" );
1551
+ } catch (TregexParseException e ) {
1552
+ // yay, passed
1553
+ }
1554
+ // this should also break
1555
+ try {
1556
+ pattern = TregexPattern .compile ("A ?!(< B < C)" );
1557
+ } catch (TregexParseException e ) {
1558
+ // yay, passed
1559
+ }
1560
+ }
1561
+
1526
1562
/**
1527
1563
* A user supplied an example of a negated disjunction which went into an infinite loop.
1528
1564
* Apparently no one had ever used a negated disjunction of tree structures before!
You can’t perform that action at this time.
0 commit comments