Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
pos-table.hh
1#pragma once
2
3#include <cstdint>
4#include <vector>
5
6#include "pos-idx.hh"
7#include "position.hh"
8#include "sync.hh"
9
10namespace nix {
11
13{
14public:
15 class Origin
16 {
17 friend PosTable;
18 private:
19 uint32_t offset;
20
21 Origin(Pos::Origin origin, uint32_t offset, size_t size):
22 offset(offset), origin(origin), size(size)
23 {}
24
25 public:
26 const Pos::Origin origin;
27 const size_t size;
28
29 uint32_t offsetOf(PosIdx p) const
30 {
31 return p.id - 1 - offset;
32 }
33 };
34
35private:
36 using Lines = std::vector<uint32_t>;
37
38 std::map<uint32_t, Origin> origins;
39 mutable Sync<std::map<uint32_t, Lines>> lines;
40
41 const Origin * resolve(PosIdx p) const
42 {
43 if (p.id == 0)
44 return nullptr;
45
46 const auto idx = p.id - 1;
47 /* we want the last key <= idx, so we'll take prev(first key > idx).
48 this is guaranteed to never rewind origin.begin because the first
49 key is always 0. */
50 const auto pastOrigin = origins.upper_bound(idx);
51 return &std::prev(pastOrigin)->second;
52 }
53
54public:
55 Origin addOrigin(Pos::Origin origin, size_t size)
56 {
57 uint32_t offset = 0;
58 if (auto it = origins.rbegin(); it != origins.rend())
59 offset = it->first + it->second.size;
60 // +1 because all PosIdx are offset by 1 to begin with, and
61 // another +1 to ensure that all origins can point to EOF, eg
62 // on (invalid) empty inputs.
63 if (2 + offset + size < offset)
64 return Origin{origin, offset, 0};
65 return origins.emplace(offset, Origin{origin, offset, size}).first->second;
66 }
67
68 PosIdx add(const Origin & origin, size_t offset)
69 {
70 if (offset > origin.size)
71 return PosIdx();
72 return PosIdx(1 + origin.offset + offset);
73 }
74
75 Pos operator[](PosIdx p) const;
76
77 Pos::Origin originOf(PosIdx p) const
78 {
79 if (auto o = resolve(p))
80 return o->origin;
81 return std::monostate{};
82 }
83};
84
85}
Definition lexer.l:6769
Definition pos-idx.hh:9
Definition pos-table.hh:16
Definition pos-table.hh:13
std::shared_ptr< T > p
Definition lexer.l:1269
Pos and AbstractPos.