8312821: Javac accepts char literal as template

Reviewed-by: jlahoda
This commit is contained in:
Jim Laskey 2023-08-02 21:01:44 +00:00
parent cff25dd574
commit bc1d2eac9a
3 changed files with 26 additions and 15 deletions

View File

@ -425,9 +425,10 @@ public class JavaTokenizer extends UnicodeReader {
* escape character. Actual conversion of escape sequences takes place
* during at the end of readToken.
*
* @param pos position of the first character in literal.
* @param pos position of the first character in literal.
* @param isString true if is a string literal
*/
private void scanLitChar(int pos) {
private void scanLitChar(int pos, boolean isString) {
int backslash = position();
if (acceptThenPut('\\')) {
hasEscapeSequences = true;
@ -480,9 +481,13 @@ public class JavaTokenizer extends UnicodeReader {
break;
case '{':
scanEmbeddedExpression(pos, backslash);
if (hasStringTemplateErrors) {
return;
if (isString) {
scanEmbeddedExpression(pos, backslash);
if (hasStringTemplateErrors) {
return;
}
} else {
lexError(position(), Errors.IllegalEscChar);
}
break;
@ -546,7 +551,7 @@ public class JavaTokenizer extends UnicodeReader {
}
} else {
// Add character to string buffer.
scanLitChar(pos);
scanLitChar(pos, true);
}
}
} else {
@ -570,7 +575,7 @@ public class JavaTokenizer extends UnicodeReader {
break;
} else {
// Add character to string buffer.
scanLitChar(pos);
scanLitChar(pos, true);
}
}
}
@ -1115,10 +1120,17 @@ public class JavaTokenizer extends UnicodeReader {
lexError(pos, Errors.IllegalLineEndInCharLit);
}
int errorPos = position();
scanLitChar(pos);
scanLitChar(pos, false);
if (accept('\'')) {
if (tk == TokenKind.ERROR) {
while (isAvailable() && !is('\'')) {
if (is('\\')) {
next();
}
next();
}
accept('\'');
} else if (accept('\'')) {
tk = TokenKind.CHARLITERAL;
} else {
lexError(pos, Errors.UnclosedCharLit);

View File

@ -102,7 +102,8 @@ public class JavaLexerTest {
new TestTuple(EOF, "\\u", ""),
new TestTuple(ERROR, "\'\'"),
new TestTuple(ERROR, "\'\\q\'", "\'\\"),
new TestTuple(ERROR, "\'\\q\'", "\'\\q\'"),
new TestTuple(ERROR, "\'\\{1+2}\'", "\'\\{1+2}\'"),
};
static class TestTuple {

View File

@ -1,5 +1,3 @@
TripleQuote.java:12:14: compiler.err.empty.char.lit
TripleQuote.java:12:21: compiler.err.unclosed.char.lit
TripleQuote.java:13:14: compiler.err.empty.char.lit
TripleQuote.java:13:16: compiler.err.unclosed.char.lit
4 errors
TripleQuote.java:13:15: compiler.err.empty.char.lit
2 errors