Skip to content

Commit 9c67d39

Browse files
committed
Add a test that patterns are not both negated and optional. The logic is hard to define (perhaps someone will eventually come up with a use case)
1 parent 357b1bb commit 9c67d39

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

src/edu/stanford/nlp/trees/tregex/CoordinationPattern.java

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public String localString() {
3636
@Override
3737
public String toString() {
3838
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+
3944
if (isConj) {
4045
if (isNegated()) {
4146
sb.append("!(");

src/edu/stanford/nlp/trees/tregex/TregexPattern.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -365,14 +365,14 @@ public abstract class TregexPattern implements Serializable {
365365
void negate() {
366366
neg = true;
367367
if (opt) {
368-
throw new RuntimeException("Node cannot be both negated and optional.");
368+
throw new IllegalStateException("Node cannot be both negated and optional.");
369369
}
370370
}
371371

372372
void makeOptional() {
373373
opt = true;
374374
if (neg) {
375-
throw new RuntimeException("Node cannot be both negated and optional.");
375+
throw new IllegalStateException("Node cannot be both negated and optional.");
376376
}
377377
}
378378

src/edu/stanford/nlp/trees/tregex/TregexPatternCompiler.java

+2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ public TregexPattern compile(String tregex) {
142142
throw new TregexParseException("Could not parse " + tregex, tme);
143143
} catch (ParseException e) {
144144
throw new TregexParseException("Could not parse " + tregex, e);
145+
} catch (IllegalStateException e) {
146+
throw new TregexParseException("Could not parse " + tregex, e);
145147
}
146148
pattern.setPatternString(tregex);
147149
return pattern;

test/src/edu/stanford/nlp/trees/tregex/TregexTest.java

+36
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,42 @@ public void testOptional() {
15231523
assertFalse(matcher.find());
15241524
}
15251525

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+
15261562
/**
15271563
* A user supplied an example of a negated disjunction which went into an infinite loop.
15281564
* Apparently no one had ever used a negated disjunction of tree structures before!

0 commit comments

Comments
 (0)