Skip to content

Commit

Permalink
Proper "empty comments" fix (#161)
Browse files Browse the repository at this point in the history
* revert c19105f

* better fix for empty commits

* add tests regarding this issue
  • Loading branch information
fredg1 committed Oct 27, 2021
1 parent c19105f commit 0759421
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
9 changes: 2 additions & 7 deletions src/net/sourceforge/kolmafia/textui/Line.java
Expand Up @@ -139,12 +139,7 @@ public class Token {
final int restOfLineStart;

private Token(final int tokenLength) {
this(tokenLength, false);
}

private Token(final int tokenLength, final boolean allowEmptyToken) {
if ((tokenLength < 0 || (!allowEmptyToken && tokenLength == 0))
&& Line.this.content != null) {
if (tokenLength <= 0 && Line.this.content != null) {
throw new IllegalArgumentException();
}

Expand Down Expand Up @@ -220,7 +215,7 @@ public String toString() {

private class Comment extends Token {
private Comment(final int commentLength) {
super(commentLength, true);
super(commentLength);
}
}
}
8 changes: 6 additions & 2 deletions src/net/sourceforge/kolmafia/textui/Parser.java
Expand Up @@ -3616,7 +3616,9 @@ private Token currentToken() {
final int commentEnd = restOfLine.indexOf("*/");

if (commentEnd == -1) {
this.currentLine.makeComment(restOfLine.length());
if (!restOfLine.isEmpty()) {
this.currentLine.makeComment(restOfLine.length());
}

this.currentLine = this.currentLine.nextLine;
this.currentIndex = this.currentLine.offset;
Expand Down Expand Up @@ -3652,7 +3654,9 @@ private Token currentToken() {
final int commentEnd = restOfLine.indexOf("*/", 2);

if (commentEnd == -1) {
this.currentLine.makeComment(restOfLine.length());
if (!restOfLine.isEmpty()) {
this.currentLine.makeComment(restOfLine.length());
}

this.currentLine = this.currentLine.nextLine;
this.currentIndex = this.currentLine.offset;
Expand Down
13 changes: 0 additions & 13 deletions test/net/sourceforge/kolmafia/textui/LineTest.java
Expand Up @@ -383,17 +383,4 @@ public void testTokenSizeOverflow() {
assertSame(line3Token4, line3SurroundingWhitespace.removeLastToken());
assertThrows(IndexOutOfBoundsException.class, () -> line3SurroundingWhitespace.makeToken(50));
}

@Test
public void canHaveBlankComment() {
LineNumberReader commandStream =
new LineNumberReader(
new InputStreamReader(
new ByteArrayInputStream("/*\n\n*/".getBytes(StandardCharsets.UTF_8)),
StandardCharsets.UTF_8));

Line startCommentLine = new Line(commandStream);
Line blankCommentLine = new Line(commandStream, startCommentLine);
assertDoesNotThrow(() -> blankCommentLine.makeComment(0));
}
}
24 changes: 24 additions & 0 deletions test/net/sourceforge/kolmafia/textui/ParserTest.java
Expand Up @@ -201,6 +201,10 @@ public static Stream<Arguments> data() {
"Plural constant, comment",
"$booleans[tr//Comment\nue]",
Arrays.asList("$", "booleans", "[", "tr", "//Comment", "ue", "]")),
valid(
"Plural constant, empty comment",
"$booleans[tr//\nue]",
Arrays.asList("$", "booleans", "[", "tr", "//", "ue", "]")),
invalid(
"Plural constant, two line-separated slashes",
"$booleans[tr/\n/ue]",
Expand All @@ -213,21 +217,41 @@ public static Stream<Arguments> data() {
"Mid-line // comment",
"int x = // interrupting comment\n 5;",
Arrays.asList("int", "x", "=", "// interrupting comment", "5", ";")),
valid(
"Empty mid-line // comment",
"int x = //\n 5;",
Arrays.asList("int", "x", "=", "//", "5", ";")),
valid(
"Mid-line # comment",
// This ought to only accept full-line comments, but it's incorrectly implemented,
// and at this point, widely used enough that this isn't feasible to change.
"int x = # interrupting comment\n 5;",
Arrays.asList("int", "x", "=", "# interrupting comment", "5", ";")),
valid(
"Empty mid-line # comment",
"int x = #\n 5;",
Arrays.asList("int", "x", "=", "#", "5", ";")),
valid(
"Multiline comment",
"int x =/* this\n is a comment\n */ 5;",
// Note that this drops some leading whitespace.
Arrays.asList("int", "x", "=", "/* this", "is a comment", "*/", "5", ";")),
valid(
"Empty multiline comment",
"int x =/*\n\n*/ 5;",
Arrays.asList("int", "x", "=", "/*", "*/", "5", ";")),
valid(
"Multiline comment on one line",
"int x =/* this is a comment */ 5;",
Arrays.asList("int", "x", "=", "/* this is a comment */", "5", ";")),
valid(
"Empty multiline comment on one line",
"int x =/**/ 5;",
Arrays.asList("int", "x", "=", "/**/", "5", ";")),
invalid(
"Empty multiline comment on one line, single asterisk",
"int x =/*/ 5;",
"Expression expected"),
valid(
"Simple map literal",
"int[item] { $item[seal-clubbing club]: 1, $item[helmet turtle]: 2}",
Expand Down

0 comments on commit 0759421

Please sign in to comment.