Skip to content

Commit

Permalink
Merge pull request #79 from sentrysoftware/feature/78-regexp-constant…
Browse files Browse the repository at this point in the history
…-triggers-a-parserexception

Fixed issue #78 and parsing of regular expression constants (escaping)
  • Loading branch information
bertysentry authored Jan 12, 2024
2 parents c1ecad5 + 5b7634a commit ec9f791
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/main/java/org/sentrysoftware/jawk/frontend/AwkParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,24 @@ private void readRegexp()
// should only contain nothing or =, depending on whether
// starting with /... or /=...
assert regexp.length() == 0 || regexp.length() == 1;
while (c >= 0 && c != '\n' && (c != '/' || (regexp.length() > 0 && regexp.charAt(regexp.length() - 1) == '\\'))) {
regexp.append((char) c);
c = reader.read();
// completely bypass \r's
while (c >= 0 && c != '\n' && c != '/') {
if (c == '\\') {
c = reader.read();
if (c == '/') {
regexp.append('/');
c = reader.read();
continue;
} else if (c == '\\') {
regexp.append('\\');
}
}
while (c == '\r') {
c = reader.read();
}
if (c < 0 || c == '\n' || c == '/') break;

regexp.append((char) c);
c = reader.read();
}
if (c < 0 || c == '\n') {
throw new LexerException("Unterminated regular expression: " + regexp);
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/sentrysoftware/jawk/AwkParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,10 @@ public void testOperatorPrecedence() throws Exception {
assertEquals("* precedes +", "5", evalAwk("1 + 2 * 2"));
assertEquals("+ precedes string concat", "33", evalAwk("1 + 2 3"));
}

@Test
public void testRegExpConstant() throws Exception {
assertEquals("/\\\\/ must be supported", "success", runAwk("/\\\\/ { printf \"success\" }", "a\\b"));
assertEquals("/\\// must be supported", "success", runAwk("/\\// { printf \"success\" }", "a/b"));
}
}

0 comments on commit ec9f791

Please sign in to comment.