Skip to content

Commit a53b184

Browse files
committed
{libutil,libexpr}: Move pos-idx,pos-table code to libutil
All of this code doesn't actually depend on anything from libexpr. Because Pos is so tigtly coupled with Error, it makes sense to have in the same library.
1 parent cac1168 commit a53b184

File tree

7 files changed

+48
-41
lines changed

7 files changed

+48
-41
lines changed

maintainers/flake-module.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@
127127
''^src/libexpr/nixexpr\.cc$''
128128
''^src/libexpr/nixexpr\.hh$''
129129
''^src/libexpr/parser-state\.hh$''
130-
''^src/libexpr/pos-table\.hh$''
131130
''^src/libexpr/primops\.cc$''
132131
''^src/libexpr/primops\.hh$''
133132
''^src/libexpr/primops/context\.cc$''

src/libexpr/meson.build

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ headers = [config_h] + files(
172172
# internal: 'lexer-helpers.hh',
173173
'nixexpr.hh',
174174
'parser-state.hh',
175-
'pos-idx.hh',
176-
'pos-table.hh',
177175
'primops.hh',
178176
'print-ambiguous.hh',
179177
'print-options.hh',

src/libexpr/nixexpr.cc

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -601,41 +601,6 @@ void ExprLambda::setDocComment(DocComment docComment) {
601601
}
602602
};
603603

604-
605-
606-
/* Position table. */
607-
608-
Pos PosTable::operator[](PosIdx p) const
609-
{
610-
auto origin = resolve(p);
611-
if (!origin)
612-
return {};
613-
614-
const auto offset = origin->offsetOf(p);
615-
616-
Pos result{0, 0, origin->origin};
617-
auto lines = this->lines.lock();
618-
auto linesForInput = (*lines)[origin->offset];
619-
620-
if (linesForInput.empty()) {
621-
auto source = result.getSource().value_or("");
622-
const char * begin = source.data();
623-
for (Pos::LinesIterator it(source), end; it != end; it++)
624-
linesForInput.push_back(it->data() - begin);
625-
if (linesForInput.empty())
626-
linesForInput.push_back(0);
627-
}
628-
// as above: the first line starts at byte 0 and is always present
629-
auto lineStartOffset = std::prev(
630-
std::upper_bound(linesForInput.begin(), linesForInput.end(), offset));
631-
632-
result.line = 1 + (lineStartOffset - linesForInput.begin());
633-
result.column = 1 + (offset - *lineStartOffset);
634-
return result;
635-
}
636-
637-
638-
639604
/* Symbol table. */
640605

641606
size_t SymbolTable::totalSize() const

src/libutil/meson.build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ sources = files(
155155
'memory-source-accessor.cc',
156156
'mounted-source-accessor.cc',
157157
'position.cc',
158+
'pos-table.cc',
158159
'posix-source-accessor.cc',
159160
'references.cc',
160161
'serialise.cc',
@@ -225,6 +226,8 @@ headers = [config_h] + files(
225226
'muxable-pipe.hh',
226227
'os-string.hh',
227228
'pool.hh',
229+
'pos-idx.hh',
230+
'pos-table.hh',
228231
'position.hh',
229232
'posix-source-accessor.hh',
230233
'processes.hh',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
///@file
23

34
#include <cinttypes>
45
#include <functional>

src/libutil/pos-table.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "pos-table.hh"
2+
3+
#include <algorithm>
4+
5+
namespace nix {
6+
7+
/* Position table. */
8+
9+
Pos PosTable::operator[](PosIdx p) const
10+
{
11+
auto origin = resolve(p);
12+
if (!origin)
13+
return {};
14+
15+
const auto offset = origin->offsetOf(p);
16+
17+
Pos result{0, 0, origin->origin};
18+
auto lines = this->lines.lock();
19+
auto linesForInput = (*lines)[origin->offset];
20+
21+
if (linesForInput.empty()) {
22+
auto source = result.getSource().value_or("");
23+
const char * begin = source.data();
24+
for (Pos::LinesIterator it(source), end; it != end; it++)
25+
linesForInput.push_back(it->data() - begin);
26+
if (linesForInput.empty())
27+
linesForInput.push_back(0);
28+
}
29+
// as above: the first line starts at byte 0 and is always present
30+
auto lineStartOffset = std::prev(std::upper_bound(linesForInput.begin(), linesForInput.end(), offset));
31+
32+
result.line = 1 + (lineStartOffset - linesForInput.begin());
33+
result.column = 1 + (offset - *lineStartOffset);
34+
return result;
35+
}
36+
37+
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
///@file
23

34
#include <cstdint>
45
#include <vector>
@@ -18,9 +19,12 @@ public:
1819
private:
1920
uint32_t offset;
2021

21-
Origin(Pos::Origin origin, uint32_t offset, size_t size):
22-
offset(offset), origin(origin), size(size)
23-
{}
22+
Origin(Pos::Origin origin, uint32_t offset, size_t size)
23+
: offset(offset)
24+
, origin(origin)
25+
, size(size)
26+
{
27+
}
2428

2529
public:
2630
const Pos::Origin origin;

0 commit comments

Comments
 (0)