Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,41 +57,75 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int
/**
* Rewrite the error message.
*/
private static String formatMessage(Object offendingSymbol, int line, int charPositionInLine, String message,
RecognitionException e, String query) {

String errorText = "At " + line + ":" + charPositionInLine;

if (offendingSymbol instanceof CommonToken ct) {

String token = ct.getText();
if (!ObjectUtils.isEmpty(token)) {
errorText += " and token '" + token + "'";
}
}
errorText += ", ";

if (e instanceof NoViableAltException) {

errorText += message.substring(0, message.indexOf('\''));
if (query.isEmpty()) {
errorText += "'*' (empty query string)";
} else {

List<String> list = query.lines().toList();
String lineText = list.get(line - 1);
String text = lineText.substring(0, charPositionInLine) + "*" + lineText.substring(charPositionInLine);
errorText += "'" + text + "'";
}

} else if (e instanceof InputMismatchException) {
errorText += message.substring(0, message.length() - 1).replace(" expecting {",
", expecting one of the following tokens: ");
} else {
errorText += message;
}

return errorText;
}

private static String formatMessage(Object offendingSymbol,
int line,
int charPositionInLine,
String message,
RecognitionException e,
String query) {

StringBuilder errorText = new StringBuilder(256)
.append("At ")
.append(line)
.append(":")
.append(charPositionInLine);

if (offendingSymbol instanceof CommonToken ct) {
String token = ct.getText();
if (!ObjectUtils.isEmpty(token)) {
errorText.append(" and token '").append(token).append("'");
}
}
errorText.append(", ");

if (e instanceof NoViableAltException) {
int idx = message.indexOf('\'');
if (idx >= 0) {
errorText.append(message, 0, idx);
} else {
errorText.append(message);
}

if (query.isEmpty()) {
errorText.append("'*' (empty query string)");
} else {
String lineText = getLineAt(query, line);

StringBuilder lineSb = new StringBuilder(lineText.length() + 1); // 삽입 고려
lineSb.append(lineText);
if (charPositionInLine >= 0 && charPositionInLine <= lineSb.length()) {
lineSb.insert(charPositionInLine, '*');
} else {
lineSb.append('*');
}

errorText.append("'").append(lineSb).append("'");
}

} else if (e instanceof InputMismatchException) {
errorText.append(
message.substring(0, message.length() - 1)
.replace(" expecting {", ", expecting one of the following tokens: ")
);
} else {
errorText.append(message);
}

return errorText.toString();
}

private static String getLineAt(String query, int line) {
if (query == null || query.isEmpty()) {
return "";
}

String[] lines = query.split("\\r?\\n");
int lineIndex = line - 1; // 라인 번호는 1-based이므로 0-based 인덱스로 변환

if (lineIndex >= 0 && lineIndex < lines.length) {
return lines[lineIndex];
}

return ""; // 라인 번호가 범위를 벗어나면 빈 문자열 반환
}
}