Skip to content
This repository was archived by the owner on Oct 12, 2026. It is now read-only.

Commit 0190ccb

Browse files
committed
FIX: Report source locations instead of offsets in diagnostics.
- Replace byte offsets with source locations in diagnostic output. - Add missing support for Classic macOS (CR) line endings. Fixes #12
1 parent b8e3d49 commit 0190ccb

7 files changed

Lines changed: 37 additions & 6 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ dump/
99

1010
.lbd_history
1111
.direnv.ps1
12+
examples/debug.lbd

‎include/lbd/source/BufferManager.hpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string>
55
#include <unordered_map>
66
#include <lbd/source/Buffer.hpp>
7+
#include <lbd/source/Range.hpp>
78

89
namespace lbd::source
910
{
@@ -36,6 +37,10 @@ namespace lbd::source
3637

3738
[[nodiscard]] std::string_view getLine(BufferId id, Offset offset) const noexcept;
3839

40+
void printLocation(std::ostream &outputStream, BufferId id, Location location) const noexcept;
41+
42+
void printRange(std::ostream &outputStream, BufferId id, Range range) const noexcept;
43+
3944
// Optional utilities.
4045
[[nodiscard]] bool isBufferLoaded(const std::string &name) const;
4146

‎src/diagnostics/DiagnosticRenderer.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ namespace lbd::diagnostics
4949
// ReSharper disable once CppUseStructuredBinding
5050
const source::Location endLocation = diagnostic.getRange().getEnd();
5151
const source::BufferId bufferId = beginLocation.bufferId;
52-
outputStream << " " << topLeft << horizontal << " " << sourceManager.getBufferName(bufferId)
53-
<< ':' << diagnostic.getRange() << '\n';
52+
outputStream << " " << topLeft << horizontal << " " << sourceManager.getBufferName(bufferId) << ':';
53+
sourceManager.printRange(outputStream, bufferId, diagnostic.getRange());
54+
outputStream << '\n';
5455

5556
// Source line.
5657
outputStream << " " << vertical << '\n';

‎src/source/Buffer.cpp‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,20 @@ namespace lbd::source
1212
{
1313
m_lineOffsets.clear();
1414
m_lineOffsets.push_back(0);
15-
for (uint32_t i = 0; i < m_contents.size(); ++i)
15+
16+
for (Offset i = 0; i < m_contents.size(); ++i)
1617
{
17-
if (m_contents[i] == '\n') m_lineOffsets.push_back(i + 1);
18+
if (m_contents[i] == '\r')
19+
{
20+
// CRLF
21+
if (i + 1 < m_contents.size() && m_contents[i + 1] == '\n') ++i;
22+
// else CR
23+
m_lineOffsets.push_back(i + 1);
24+
} else if (m_contents[i] == '\n')
25+
{
26+
// LF
27+
m_lineOffsets.push_back(i + 1);
28+
}
1829
}
1930
}
2031
}

‎src/source/BufferManager.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ namespace lbd::source
6565
return contents.substr(lineStart, lineEnd - lineStart);
6666
}
6767

68+
void BufferManager::printLocation(std::ostream &outputStream, const BufferId id,
69+
const Location location) const noexcept
70+
{
71+
outputStream << getRow(id, location.offset) << ':' << getColumn(id, location.offset);
72+
}
73+
74+
void BufferManager::printRange(std::ostream &outputStream, const BufferId id, const Range range) const noexcept
75+
{
76+
printLocation(outputStream, id, range.getBegin());
77+
outputStream << "..";
78+
printLocation(outputStream, id, range.getEnd());
79+
}
80+
6881
bool BufferManager::isBufferLoaded(const std::string &name) const
6982
{
7083
const std::string absolutePath = getAbsolutePath(name);

‎tests/diagnostics/type_error.test.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ exit_code = 1
55
stderr_contains = [
66
"ERROR: Invalid inputs to native function `add`.",
77
"`add` signature: Float -> Float -> Float",
8-
"type_error.lbd:7..22",
8+
"type_error.lbd:1:8..1:23",
99
"(print (add \"hello\" 5))",
1010
]

‎tests/diagnostics/unexpected_token.test.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ file = "unexpected_token.lbd"
44
exit_code = 1
55
stderr_contains = [
66
"ERROR: Unexpected character `;`.",
7-
"unexpected_token.lbd:13..13",
7+
"unexpected_token.lbd:1:14..1:14",
88
"a: Number = 1;",
99
]

0 commit comments

Comments
 (0)