Skip to content

Commit

Permalink
Merge pull request #67 from Gedochao/maintenance/remove-comments-support
Browse files Browse the repository at this point in the history
Remove `/* (...) */` comments support
  • Loading branch information
tgodzik authored Dec 19, 2024
2 parents f904a1c + d387684 commit 96aa8ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
34 changes: 3 additions & 31 deletions src/main/java/com/virtuslab/using_directives/custom/Scanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private boolean doFetchToken() {
reader.nextChar();
getOperatorRest();
} else if (ch == '/') {
if (skipComment()) return true;
if (skipLine()) return true;
else {
putChar('/');
getOperatorRest();
Expand Down Expand Up @@ -272,46 +272,18 @@ private boolean doFetchToken() {
}

// Unsupported: Keeping comments
private boolean skipComment() {
private boolean skipLine() {
Runnable skipLine =
() -> {
reader.nextChar();
while (reader.ch != CR && reader.ch != LF && reader.ch != SU) {
reader.nextChar();
}
};
Runnable skipComment =
() -> {
int nested = 0;
boolean flag = true;
while (flag) {
if (reader.ch == '/') {
reader.nextChar();
if (reader.ch == '*') {
nested += 1;
reader.nextChar();
}
} else if (reader.ch == '*') {
reader.nextChar();
if (reader.ch == '/') {
if (nested > 0) {
nested -= 1;
} else flag = false;
reader.nextChar();
}
} else {
reader.nextChar();
}
}
};
reader.nextChar();
if (reader.ch == '/') {
skipLine.run();
return true;
} else if (reader.ch == '*') {
reader.nextChar();
skipComment.run();
return true;
} else {
return false;
}
Expand Down Expand Up @@ -378,7 +350,7 @@ public void getOperatorRest() {
break;
case '/':
char nxch = reader.lookaheadChar();
if (nxch == '/' || nxch == '*') finishNamed(Tokens.IDENTIFIER, td);
if (nxch == '/') finishNamed(Tokens.IDENTIFIER, td);
else {
putChar(reader.ch);
reader.nextChar();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,23 @@ public void testListJustComma() {
assertValueListSize(parsedDirective, "keyA", 1);
assertValueListAtPath(parsedDirective, "keyA", List.of("0,2,3"));
}

@Test
public void testAsterisksAndSlashes() {
String input = "//> using exclude */*/Foo.scala";
UsingDirectives parsedDirective = testCode(1, input);
assertValueListSize(parsedDirective, "exclude", 1);
assertValueListAtPath(parsedDirective, "exclude", List.of("*/*/Foo.scala"));
}

@Test
public void oldCommentsArentSkipped() {
String input = "//> using /* comment1 */ notTheKeyActually /* comment2 */ 42";
UsingDirectives parsedDirective = testCode(1, input);
assertValueListSize(parsedDirective, "/*", 7);
assertValueListAtPath(
parsedDirective,
"/*",
List.of("comment1", "*/", "notTheKeyActually", "/*", "comment2", "*/", "42"));
}
}

0 comments on commit 96aa8ca

Please # to comment.