Skip to content

Commit e03b6a4

Browse files
authored
Fix Scanner.getNextToken0() to gracefully handle end of content (#4675)
- add checks to stop when the end of source is reached when handling a markdown comment - also do not attempt to access the source with the EOF index - add new test to ScannerTest - fixes #4674
1 parent f568d1a commit e03b6a4

File tree

2 files changed

+37
-3
lines changed
  • org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser
  • org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression

2 files changed

+37
-3
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/Scanner.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,7 @@ protected TerminalToken getNextToken0() throws InvalidInputException {
17301730
}
17311731
isUnicode = false;
17321732
previous = this.currentPosition;
1733-
if (((this.currentCharacter = this.source[this.currentPosition++]) == '\\')
1733+
if ((this.currentPosition < this.eofPosition && (this.currentCharacter = this.source[this.currentPosition++]) == '\\')
17341734
&& (this.source[this.currentPosition] == 'u')) {
17351735
//-------------unicode traitement ------------
17361736
getNextUnicodeChar();
@@ -1746,8 +1746,8 @@ protected TerminalToken getNextToken0() throws InvalidInputException {
17461746
//loop as long as lines start with ///
17471747
int firstTag = 0;
17481748
while(true) {
1749-
if (this.currentPosition > this.eofPosition) {
1750-
throw unterminatedComment();
1749+
if (this.currentPosition == this.eofPosition) {
1750+
break;
17511751
}
17521752
if ((this.currentCharacter == '\r') || (this.currentCharacter == '\n')) {
17531753
if (this.recordLineSeparator) {

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ScannerTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,4 +1848,38 @@ public void testTerminalTokensAPIs() {
18481848
assertTrue(TerminalToken.getRestrictedKeyword("When".toCharArray()) == TerminalToken.TokenNameNotAToken);
18491849
assertTrue(TerminalToken.getRestrictedKeyword("blah".toCharArray()) == TerminalToken.TokenNameNotAToken);
18501850
}
1851+
1852+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4674
1853+
public void testIssue4674() {
1854+
IScanner scanner = ToolFactory.createScanner(true, true, true, "23", "23");
1855+
final char[] source = "/// @return a string".toCharArray();
1856+
scanner.setSource(source);
1857+
final StringBuilder buffer = new StringBuilder();
1858+
try {
1859+
int token;
1860+
boolean foundMarkdown = false;
1861+
boolean foundOther = false;
1862+
while ((token = scanner.getNextToken()) != ITerminalSymbols.TokenNameEOF) {
1863+
try {
1864+
switch(token) {
1865+
case ITerminalSymbols.TokenNameCOMMENT_MARKDOWN :
1866+
foundMarkdown = true;
1867+
break;
1868+
default :
1869+
foundOther = true;
1870+
buffer.append(scanner.getCurrentTokenSource());
1871+
break;
1872+
}
1873+
} catch (ArrayIndexOutOfBoundsException e) {
1874+
e.printStackTrace();
1875+
}
1876+
}
1877+
assertTrue("Should have found markdown comment", foundMarkdown);
1878+
assertTrue("Should have found EOF token", token == ITerminalSymbols.TokenNameEOF);
1879+
assertFalse("Should not have found other", foundOther);
1880+
} catch (InvalidInputException e) {
1881+
assertTrue("Should not have InvalidInputException", false);
1882+
}
1883+
}
1884+
18511885
}

0 commit comments

Comments
 (0)