Skip to content

Commit

Permalink
Ensure Scanner adds line ends for text block continuation lines (ecli…
Browse files Browse the repository at this point in the history
…pse-jdt#2339)

- fixes eclipse-jdt#2338
- add new test to ScannerTest
  • Loading branch information
jjohnstn authored Apr 13, 2024
1 parent 7bb452a commit 9512d3f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2057,8 +2057,11 @@ protected int scanForTextBlock() throws InvalidInputException {
break outer;
case '\n' :
case '\r' :
this.currentCharacter = this.source[this.currentPosition++];
if (this.recordLineSeparator) {
pushLineSeparator();
}
this.currentCharacter = '\\';
this.currentPosition++;
break;
case '\"' :
this.currentPosition++;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2019 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -1582,6 +1582,42 @@ public void testBug575556_at_15() {
}
}

public void testIssue2338_001_since_14() {
char[] source = ("class X {\n" +
" String s = \"\"\"\nThis is the new\\\n String\"\"\";\n" +
"}").toCharArray();
Scanner scanner = new Scanner(false, false, false, ClassFileConstants.MAJOR_LATEST_VERSION, null, null, false);
scanner.previewEnabled = true;
scanner.recordLineSeparator = true;
scanner.setSource(source);
scanner.resetTo(0, source.length - 1);
try {
int token;
StringBuilder buffer = new StringBuilder();
while ((token = scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
try {
switch(token) {
case TerminalTokens.TokenNameTextBlock :
buffer.append( new String(scanner.getCurrentTextBlock()));
break;
case TerminalTokens.TokenNameStringLiteral :
break;
case TerminalTokens.TokenNameEOF :
break;
default :
break;
}
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
assertEquals("Wrong contents", "This is the new String", String.valueOf(buffer));
assertEquals("Missing line end for continuation", 44, scanner.lineEnds[2]);
} catch (InvalidInputException e) {
assertTrue(false);
}
}

public void testSealed() {
char[] source = ("sealed class X { }").toCharArray();
IScanner scanner = ToolFactory.createScanner(false, true, false, "17", "17", false);
Expand Down

0 comments on commit 9512d3f

Please sign in to comment.