Skip to content

Commit

Permalink
Merge pull request #70 from sentrysoftware/feature/issue-67-multiline…
Browse files Browse the repository at this point in the history
…-operations

Fixed issue #67 where eol wasn't supported after && || ? : ,
  • Loading branch information
bertysentry authored Jan 9, 2024
2 parents fa19b94 + e98b702 commit 15fd905
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
30 changes: 22 additions & 8 deletions src/main/java/org/sentrysoftware/jawk/frontend/AwkParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,21 @@ private void read()
}
}
}

/**
* Skip all whitespaces and comments
* @throws IOException
*/
private void skipWhitespaces() throws IOException {
while (c == ' ' || c == '\t' || c == '#' || c == '\n') {
if (c == '#') {
while (c >= 0 && c != '\n') {
read();
}
}
read();
}
}

/**
* Logs a warning about the syntax of the script (at which line, of which file)
Expand Down Expand Up @@ -477,9 +492,6 @@ private int lexer()
while (c >= 0 && c != '\n') {
read();
}
//// Causes failure when comments are embedded within lines of code...
//// if (c == '\n')
//// read();
} else {
read();
}
Expand All @@ -490,6 +502,7 @@ private int lexer()
}
if (c == ',') {
read();
skipWhitespaces();
return token = _COMMA_;
}
if (c == '(') {
Expand All @@ -502,6 +515,7 @@ private int lexer()
}
if (c == '{') {
read();
skipWhitespaces();
return token = _OPEN_BRACE_;
}
if (c == '}') {
Expand All @@ -526,16 +540,19 @@ private int lexer()
}
if (c == '?') {
read();
skipWhitespaces();
return token = _QUESTION_MARK_;
}
if (c == ':') {
read();
skipWhitespaces();
return token = _COLON_;
}
if (c == '&') {
read();
if (c == '&') {
read();
skipWhitespaces();
return token = _AND_;
}
throw new LexerException("use && for logical and");
Expand All @@ -544,6 +561,7 @@ private int lexer()
read();
if (c == '|') {
read();
skipWhitespaces();
return token = _OR_;
}
return token = _PIPE_;
Expand Down Expand Up @@ -730,11 +748,7 @@ private int lexer()
read();
}
}
if (c == '\n') {
read();
} else {
read();
}
read();
}
return token = _NEWLINE_;
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/sentrysoftware/jawk/AwkParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,16 @@ public void testStringParsing() throws Exception {
assertThrows("Interrupted hex number in string by EOF must throw", AwkParser.LexerException.class, () -> AwkTestHelper.runAwk("BEGIN { printf \"unfinished\\xF", null));
assertThrows("Interrupted hex number in string by EOL must throw", AwkParser.LexerException.class, () -> AwkTestHelper.runAwk("BEGIN { printf \"unfinished\\xf\n\"}", null));
}

@Test
public void testMultiLineStatement() throws Exception {
assertEquals("|| must allow eol", "success", AwkTestHelper.runAwk("BEGIN { if (0 || \n 1) { printf \"success\" } }", null));
assertEquals("&& must allow eol", "success", AwkTestHelper.runAwk("BEGIN { if (1 && \n 1) { printf \"success\" } }", null));
assertEquals("? must allow eol", "success", AwkTestHelper.runAwk("BEGIN { printf 1 ?\n\"success\" : \"failed\" }", null));
assertEquals(": must allow eol", "success", AwkTestHelper.runAwk("BEGIN { printf 1 ? \"success\" :\n\"failed\" }", null));
assertEquals(", must allow eol", "success", AwkTestHelper.runAwk("BEGIN { printf(\"%s\", \n\"success\") }", null));
assertEquals("do must allow eol", "success", AwkTestHelper.runAwk("BEGIN { do\n printf \"success\"; while (0) }", null));
assertEquals("else must allow eol", "success", AwkTestHelper.runAwk("BEGIN { if (0) { printf \"failure\" } else \n printf \"success\" }", null));
}

}
22 changes: 0 additions & 22 deletions src/test/resources/gawk/checknegtime.awk

This file was deleted.

0 comments on commit 15fd905

Please sign in to comment.