Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper "empty comments" fix #161

Merged
merged 4 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/net/sourceforge/kolmafia/textui/Line.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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