Fix line/col numbers in TokenMgrError

Ref #4635

Col number of zero may happen when the error is reported
on a newline. In the rest of PMD a newline is considered
to sit at the last index of the previous line. So the index
is now the first character of the next line, which is slightly
wrong but that doesn't matter much.
This commit is contained in:
Clément Fournier committed 2023-07-26 17:17:54 +02:00
1 parent 3e2de67190
commit 310f68aa52
1 file changed
+6 -4
@@ -4,6 +4,8 @@
package net.sourceforge.pmd.lang.ast;
import static java.lang.Math.max;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -31,8 +33,8 @@ public final class TokenMgrError extends FileAnalysisException {
*/
public TokenMgrError(int line, int column, @Nullable FileId filename, String message, @Nullable Throwable cause) {
super(message, cause);
this.line = line;
this.column = column;
this.line = max(line, 1);
this.column = max(column, 1);
if (filename != null) {
super.setFileId(filename);
}
@@ -44,8 +46,8 @@ public final class TokenMgrError extends FileAnalysisException {
@InternalApi
public TokenMgrError(boolean eofSeen, String lexStateName, int errorLine, int errorColumn, String errorAfter, char curChar) {
super(makeReason(eofSeen, lexStateName, errorAfter, curChar));
line = errorLine;
column = errorColumn;
line = max(errorLine, 1);
column = max(errorColumn, 1);
}
public int getLine() {