From f1c47cb7262e6156de090c35b06c5f2b607c768c Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Sat, 21 Sep 2024 02:19:16 +0200 Subject: [PATCH] Fix line numbers for invariant test failures --- .../text/UCD/TestUnicodeInvariants.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/unicodetools/src/main/java/org/unicode/text/UCD/TestUnicodeInvariants.java b/unicodetools/src/main/java/org/unicode/text/UCD/TestUnicodeInvariants.java index ac507c58ed..946b513184 100644 --- a/unicodetools/src/main/java/org/unicode/text/UCD/TestUnicodeInvariants.java +++ b/unicodetools/src/main/java/org/unicode/text/UCD/TestUnicodeInvariants.java @@ -184,7 +184,7 @@ public static int testInvariants(String inputFile, String suffix, boolean doRang } final var noComments = new StringBuilder(); final List lines = new ArrayList<>(); - final List lineBeginnings = new ArrayList(); + final List lineBeginnings = new ArrayList<>(); try (final BufferedReader in = getInputReader(inputFile)) { in.lines() .forEach( @@ -234,7 +234,26 @@ public static int testInvariants(String inputFile, String suffix, boolean doRang position -> { for (int i = 0; i < lineBeginnings.size(); ++i) { if (lineBeginnings.get(i) > position.getIndex()) { - return i; // 1-based line number. + // The error is before the beginning of line i (0-based), thus + // on line i + // (1-based). + return i; + } else if (lineBeginnings.get(i) == position.getIndex()) { + // The position in a beginning of line; this happens when an + // statement has been + // successfully parsed, but then fails for nonsyntactic reasons. + // The parse position is then the beginning of the next + // statement. + // Backtrack to the last nonempty line (ignoring comments), + // which is the last + // line of the failing statement. + int indexInTrimmedSource = position.getIndex(); + while (lineBeginnings.get(i) == indexInTrimmedSource + && indexInTrimmedSource > 0) { + --indexInTrimmedSource; + --i; + } + return i + 1; } } return lineBeginnings.size();