Skip to content

Commit

Permalink
Fix the semgrex parser usage of includes
Browse files Browse the repository at this point in the history
  • Loading branch information
AngledLuffa committed Feb 27, 2025
1 parent 33de4e1 commit 3063b26
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
30 changes: 15 additions & 15 deletions src/edu/stanford/nlp/semgraph/semgrex/SemgrexParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,11 @@ final public SemgrexPattern Root() throws ParseException {// Root pattern for th
Token value = null;
Token attrType = null;
switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
case ALIGNRELN:
case IDENTIFIER:{
attr = jj_consume_token(IDENTIFIER);
switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
case IDENTIFIER:{
attr = jj_consume_token(IDENTIFIER);
case 10:
case 22:{
switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
case 10:{
attrType = jj_consume_token(10);
Expand Down Expand Up @@ -563,6 +563,10 @@ final public SemgrexPattern Root() throws ParseException {// Root pattern for th
jj_consume_token(-1);
throw new ParseException();
}
if (attr != null && value != null) {
boolean negated = attrType.image.equals("!:");
attributes.setAttribute(attr.image, value.image, negated);
}
break;
}
case ALIGNRELN:{
Expand All @@ -583,23 +587,20 @@ final public SemgrexPattern Root() throws ParseException {// Root pattern for th
jj_consume_token(-1);
throw new ParseException();
}
if (attrType.image.equals("@")) {
if (attr == null || key == null || value == null) {
{if (true) throw new SemgrexParseException("null while parsing semgrex expression: attr=" + attr +
" key=" + key + " value=" + value);}
}
attributes.addContains(attr.image, key.image, value.image);
}
break;
}
default:
jj_la1[26] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
if (attrType.image.equals("@")) {
if (attr == null || key == null || value == null) {
{if (true) throw new SemgrexParseException("null while parsing semgrex expression: attr=" + attr +
" key=" + key + " value=" + value);}
}
attributes.addContains(attr.image, key.image, value.image);
} else if (attr != null && value != null) {
boolean negated = attrType.image.equals("!:");
attributes.setAttribute(attr.image, value.image, negated);
}
break;
}
case ROOT:{
Expand All @@ -625,7 +626,6 @@ final public SemgrexPattern Root() throws ParseException {// Root pattern for th
NodePattern pat;
jj_consume_token(23);
switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
case ALIGNRELN:
case IDENTIFIER:
case EMPTY:
case ROOT:{
Expand Down Expand Up @@ -690,7 +690,7 @@ final public SemgrexPattern Root() throws ParseException {// Root pattern for th
jj_la1_init_0();
}
private static void jj_la1_init_0() {
jj_la1_0 = new int[] {0x400,0x828808,0x3801c,0x3801c,0x828800,0x2000,0x3c01c,0x4000,0x3801c,0x2001c,0x80000,0x10,0x110,0x110,0x100000,0x200000,0x1c,0x828800,0x2000,0x82c000,0x4000,0x828000,0x820000,0x400400,0x110,0x110,0x18,0xd8,0x1000000,0xd8,0x200000,};
jj_la1_0 = new int[] {0x400,0x828808,0x3801c,0x3801c,0x828800,0x2000,0x3c01c,0x4000,0x3801c,0x2001c,0x80000,0x10,0x110,0x110,0x100000,0x200000,0x1c,0x828800,0x2000,0x82c000,0x4000,0x828000,0x820000,0x400400,0x110,0x110,0x400408,0xd0,0x1000000,0xd0,0x200000,};
}

/** Constructor with InputStream. */
Expand Down
32 changes: 18 additions & 14 deletions src/edu/stanford/nlp/semgraph/semgrex/SemgrexParser.jj
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,25 @@ void AddAttribute(NodeAttributes attributes) : {
Token value = null;
Token attrType = null;
} {
((attr = <IDENTIFIER>
( (attrType = ":" | attrType = "!:") (value = <IDENTIFIER> | value = <REGEX>) ) |
( (attrType = "@") (key = <IDENTIFIER>) "=" (value = <IDENTIFIER> | value = <REGEX>) ) )
{
if (attrType.image.equals("@")) {
if (attr == null || key == null || value == null) {
throw new SemgrexParseException("null while parsing semgrex expression: attr=" + attr +
" key=" + key + " value=" + value);
(attr = <IDENTIFIER>
(( (attrType = ":" | attrType = "!:") (value = <IDENTIFIER> | value = <REGEX>) {
if (attr != null && value != null) {
boolean negated = attrType.image.equals("!:");
attributes.setAttribute(attr.image, value.image, negated);
}
attributes.addContains(attr.image, key.image, value.image);
} else if (attr != null && value != null) {
boolean negated = attrType.image.equals("!:");
attributes.setAttribute(attr.image, value.image, negated);
}
})
})
|
(attrType = "@") (key = <IDENTIFIER>) "=" (value = <IDENTIFIER> | value = <REGEX>)
{
if (attrType.image.equals("@")) {
if (attr == null || key == null || value == null) {
throw new SemgrexParseException("null while parsing semgrex expression: attr=" + attr +
" key=" + key + " value=" + value);
}
attributes.addContains(attr.image, key.image, value.image);
}
})
)
|
( attr = <ROOT> { attributes.setRoot(true); } )
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void testBrokenContainsExpression() {

public void testContainsExpression() {
// morphofeatures is a Map, so this should work
//SemgrexPattern pattern = SemgrexPattern.compile("{morphofeatures@foo=bar}");
SemgrexPattern pattern = SemgrexPattern.compile("{morphofeatures@foo=bar}");
}

public void testReferencedRegex() {
Expand Down

0 comments on commit 3063b26

Please sign in to comment.