-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.hpp
More file actions
65 lines (52 loc) · 2.16 KB
/
Copy pathsource.hpp
File metadata and controls
65 lines (52 loc) · 2.16 KB
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
// Kaynak dosya ve konum takibi.
//
// Her token, AST düğümü ve hata bir Span taşır. Span'i sonradan eklemek acı
// verdiği için en baştan, en alt katmana konuyor.
#pragma once
#include <cstdint>
#include <filesystem>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
namespace rs {
// Kaynaktaki bir aralık. Satır/sütun DEĞİL — bayt ofseti; çeviri Source'ta yapılır.
struct Span {
std::uint32_t offset = 0;
std::uint32_t length = 0;
[[nodiscard]] constexpr std::uint32_t end() const noexcept { return offset + length; }
// İki span'i kapsayan en küçük span (a'nın başından b'nin sonuna).
[[nodiscard]] static constexpr Span merge(Span a, Span b) noexcept {
const std::uint32_t bas = a.offset < b.offset ? a.offset : b.offset;
const std::uint32_t son = a.end() > b.end() ? a.end() : b.end();
return Span{bas, son - bas};
}
};
class Source {
public:
struct LineCol {
std::uint32_t line = 1; // 1 tabanlı
std::uint32_t col = 1; // 1 tabanlı, UTF-8 KARAKTER cinsinden (bayt değil)
};
Source(std::string name, std::string text);
// Dosyayı okur. Okunamazsa nullopt.
static std::optional<Source> fromFile(const std::filesystem::path& yol);
[[nodiscard]] std::string_view text() const noexcept { return text_; }
[[nodiscard]] std::string_view name() const noexcept { return name_; }
[[nodiscard]] std::uint32_t size() const noexcept {
return static_cast<std::uint32_t>(text_.size());
}
[[nodiscard]] std::uint32_t lineCount() const noexcept {
return static_cast<std::uint32_t>(lineStarts_.size());
}
// Bayt ofsetini satır/sütuna çevirir. Sütun UTF-8 karakter sayar, çünkü
// "Yıldırım" gibi bir satırda bayt sütunu yanlış yeri gösterir.
[[nodiscard]] LineCol lineCol(std::uint32_t offset) const;
// 1 tabanlı satır numarasının metni (sondaki \n hariç).
[[nodiscard]] std::string_view lineText(std::uint32_t line) const;
private:
std::string name_;
std::string text_;
std::vector<std::uint32_t> lineStarts_; // her satırın başlangıç ofseti
};
} // namespace rs