Skip to content

Commit

Permalink
Revert "Proper "empty comments" fix (#161)"
Browse files Browse the repository at this point in the history
This reverts commit 0759421.
  • Loading branch information
heeheehee-kolmafia committed Oct 27, 2021
1 parent 0759421 commit 7897952
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
9 changes: 7 additions & 2 deletions src/net/sourceforge/kolmafia/textui/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ public class Token {
final int restOfLineStart;

private Token(final int tokenLength) {
if (tokenLength <= 0 && Line.this.content != null) {
this(tokenLength, false);
}

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

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

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

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

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

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

this.currentLine = this.currentLine.nextLine;
this.currentIndex = this.currentLine.offset;
Expand Down
13 changes: 13 additions & 0 deletions test/net/sourceforge/kolmafia/textui/LineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,17 @@ 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: 0 additions & 24 deletions test/net/sourceforge/kolmafia/textui/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,6 @@ 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 @@ -217,41 +213,21 @@ 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 7897952

Please sign in to comment.