1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#include <cassert>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string_view>
#include <sourcemanager.hpp>
namespace willowc {
std::optional<FileID> SourceManager::addFile(std::string_view _path) {
std::error_code ec;
std::filesystem::path uncanonical_path{_path};
auto path = std::filesystem::weakly_canonical(uncanonical_path, ec);
if (ec) {
return std::nullopt;
}
std::filesystem::path display_path = path;
display_path.make_preferred();
std::string display = display_path.string();
assert(!getFileID(display));
if (!std::filesystem::exists(path, ec) || ec)
return std::nullopt;
if (!std::filesystem::is_regular_file(path, ec) || ec)
return std::nullopt;
std::size_t filesize = std::filesystem::file_size(path, ec);
if (ec)
return std::nullopt;
std::ifstream f{display_path, std::ios::binary};
if (!f)
return std::nullopt;
auto buf = std::make_unique<char[]>(filesize);
f.read(buf.get(), filesize);
const FileID id = file_table.size();
file_table.emplace_back(std::move(display), std::string(_path), std::move(buf), filesize);
ids[file_table.back().path] = id;
return id;
}
std::optional<FileID> SourceManager::getFileID(const std::string &path) {
auto it = ids.find(path);
if (it != ids.end())
return it->second;
return std::nullopt;
}
std::optional<FileID> SourceManager::addStdIn() {
std::string content{std::istreambuf_iterator<char>(std::cin),
std::istreambuf_iterator<char>()};
if (std::cin.bad())
return std::nullopt;
std::size_t size = content.size();
auto buf = std::make_unique<char[]>(size);
const FileID id = file_table.size();
file_table.emplace_back("<stdin>", "<stdin>", std::move(buf), size);
ids[file_table.back().path] = id;
return id;
}
willow::Location SourceManager::File::getLoc(std::size_t offset) {
size_t line_start = offset;
while (line_start != 0) {
if (this->buf[--line_start] != '\n')
continue;
line_start++;
break;
}
uint32_t col = offset - line_start + 1;
auto it = linecache_.find(line_start);
if (it != linecache_.end())
return willow::Location{display_path, it->second, col, offset};
auto back = linecache_.rbegin();
auto i = back->first;
auto line = back->second;
assert(i < line_start);
for (; i < line_start; i++) {
if (buf[i] == '\n') {
line = line + 1;
size_t next_start = i + 1;
if (next_start <= size)
linecache_.insert({next_start, ++line});
}
}
linecache_.insert({line_start, line});
return willow::Location{
display_path, line, static_cast<uint32_t>(offset - line_start + 1), offset};
}
} // namespace willowc
|