SILICON
Loading...
Searching...
No Matches
projectDocument.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "projectDocument.hpp"
19
20#include <algorithm>
21#include <cctype>
22#include <format>
23#include <ranges>
24#include <stdexcept>
25#include <unordered_set>
26#include <utility>
27
29
30#include <nlohmann/json.hpp>
31
32namespace SILICON::project {
33namespace {
34
35 [[nodiscard]] bool containsControlCharacter(const std::string_view value)
36 {
37 return std::ranges::any_of(value, [](const unsigned char character) {
38 return character < 0x20 || character == 0x7f;
39 });
40 }
41
42 [[nodiscard]] bool isValidDocumentPath(const std::string_view path,
43 const DocumentTypeInfo& info)
44 {
45 if (!path.starts_with(info.root) || !path.ends_with(info.suffix))
46 return false;
47
48 if (path.size() < info.root.size() + info.suffix.size())
49 return false;
50
51 const auto slug = path.substr(
52 info.root.size(), path.size() - info.root.size() - info.suffix.size());
53 return info.category == DocumentCategory::Architecture
55 : isValidDocumentSlug(slug);
56 }
57
58 [[nodiscard]] std::string_view leafFileName(const std::string_view path)
59 {
60 const auto separator = path.find_last_of("/\\");
61 return separator == std::string_view::npos ? path : path.substr(separator + 1);
62 }
63
64 [[nodiscard]] std::string lowerCaseExtension(const std::string_view fileName)
65 {
66 const auto dot = fileName.find_last_of('.');
67 if (dot == std::string_view::npos || dot == 0)
68 return {};
69
70 auto extension = std::string(fileName.substr(dot));
71 std::ranges::transform(extension, extension.begin(), [](const unsigned char value) {
72 return static_cast<char>(std::tolower(value));
73 });
74 return extension;
75 }
76
77 [[nodiscard]] std::string fileStem(const std::string_view fileName)
78 {
79 const auto dot = fileName.find_last_of('.');
80 if (dot == std::string_view::npos || dot == 0)
81 return std::string(fileName);
82 return std::string(fileName.substr(0, dot));
83 }
84
85 [[nodiscard]] bool isCircuitContents(const std::string_view contents)
86 {
87 const auto json = nlohmann::json::parse(contents, nullptr, false);
88 if (!json.is_object())
89 return false;
90
91 const auto circuit = json.find("circuit");
92 const auto visual = json.find("visual");
93 return (circuit != json.end() && circuit->is_object())
94 || (visual != json.end() && visual->is_object());
95 }
96
97 [[nodiscard]] bool isTextContents(const std::string_view contents)
98 {
99 for (std::size_t index = 0; index < contents.size();) {
100 const auto byte = static_cast<unsigned char>(contents[index]);
101 if (byte < 0x80) {
102 if ((byte < 0x20 && byte != '\t' && byte != '\n' && byte != '\r') || byte == 0x7f)
103 return false;
104 ++index;
105 continue;
106 }
107
108 std::size_t length;
109 std::uint32_t codePoint;
110 std::uint32_t minimumCodePoint;
111 if (byte >= 0xc2 && byte <= 0xdf) {
112 length = 2;
113 codePoint = byte & 0x1f;
114 minimumCodePoint = 0x80;
115 } else if (byte >= 0xe0 && byte <= 0xef) {
116 length = 3;
117 codePoint = byte & 0x0f;
118 minimumCodePoint = 0x800;
119 } else if (byte >= 0xf0 && byte <= 0xf4) {
120 length = 4;
121 codePoint = byte & 0x07;
122 minimumCodePoint = 0x10000;
123 } else {
124 return false;
125 }
126
127 if (index + length > contents.size())
128 return false;
129 for (std::size_t offset = 1; offset < length; ++offset) {
130 const auto continuation = static_cast<unsigned char>(contents[index + offset]);
131 if ((continuation & 0xc0) != 0x80)
132 return false;
133 codePoint = (codePoint << 6) | (continuation & 0x3f);
134 }
135
136 if (codePoint < minimumCodePoint
137 || (length == 3 && codePoint >= 0xd800 && codePoint <= 0xdfff)
138 || (length == 4 && codePoint > 0x10ffff))
139 return false;
140 index += length;
141 }
142 return true;
143 }
144
145 [[nodiscard]] std::optional<DocumentType>
146 documentTypeForFile(const std::string_view fileName, const std::string_view contents)
147 {
148 const auto extension = lowerCaseExtension(fileName);
149 if (extension == ".v")
151 if (extension == ".sisl")
152 return DocumentType::Sisl;
153 if (extension == ".bin")
155
156 if (isCircuitContents(contents))
158 if (!contents.empty() && !isTextContents(contents))
160 return std::nullopt;
161 }
162
163} // namespace
164
165std::optional<DocumentType> documentTypeForPath(const std::string_view path)
166{
167 const auto it =
168 std::ranges::find_if(DOCUMENT_TYPE_INFO, [path](const DocumentTypeInfo& info) {
169 return isValidDocumentPath(path, info);
170 });
171 return it == DOCUMENT_TYPE_INFO.end() ? std::nullopt : std::optional(it->type);
172}
173
174std::optional<std::string> documentSlugForPath(const std::string_view path)
175{
176 const auto type = documentTypeForPath(path);
177 if (!type)
178 return std::nullopt;
179
180 const auto& info = documentTypeInfo(*type);
181 return std::string(
182 path.substr(info.root.size(), path.size() - info.root.size() - info.suffix.size()));
183}
184
185bool isValidDocumentSlug(const std::string_view slug)
186{
187 return !slug.empty() && slug != "." && slug != ".." && !slug.contains('/')
188 && !slug.contains('\\') && !containsControlCharacter(slug);
189}
190
191std::string documentPathForSlug(const DocumentType type, const std::string_view slug)
192{
193 const auto& info = documentTypeInfo(type);
194 if (info.category == DocumentCategory::Architecture
196 : !isValidDocumentSlug(slug))
197 throw std::invalid_argument("Invalid document slug");
198
199 return std::format("{}{}{}", info.root, slug, info.suffix);
200}
201
202Document::Document(std::string path, std::string contents)
203 : path(std::move(path)), contents(std::move(contents))
204{
205 const auto type = documentTypeForPath(this->path);
206 if (!type)
207 throw std::invalid_argument("Document path is invalid");
208}
209
210const std::string& Document::getPath() const noexcept
211{
212 return path;
213}
214
215const std::string& Document::getContents() const noexcept
216{
217 return contents;
218}
219
221{
222 return *documentTypeForPath(path);
223}
224
226{
227 this->contents = std::move(contents);
228}
229
230Document importDocument(const std::string_view sourcePath, std::string contents)
231{
232 const auto fileName = leafFileName(sourcePath);
233 if (fileName.empty() || fileName == "." || fileName == "..")
234 throw std::invalid_argument("Document file name is invalid");
235
236 const auto type = documentTypeForFile(fileName, contents);
237 if (!type)
238 throw std::invalid_argument(std::format("Unsupported document file: {}", fileName));
239
240 std::string slug;
241 if (*type == DocumentType::Sisl) {
242 const auto name = declaredArchitectureName(contents);
243 if (!name)
244 throw std::invalid_argument("SISL file must begin with a valid arch declaration");
245 slug = *name;
246 } else {
247 slug = *type == DocumentType::RawBinary ? std::string(fileName)
248 : fileStem(fileName);
249 }
250 if (!isValidDocumentSlug(slug))
251 throw std::invalid_argument("Document file name is invalid");
252
253 return Document(documentPathForSlug(*type, slug), std::move(contents));
254}
255
256std::string documentFileName(const Document& document)
257{
258 return std::string(leafFileName(document.getPath()));
259}
260
261const Document* DocumentStore::find(const std::string_view documentPath) const noexcept
262{
263 const auto it = std::ranges::find(documents, documentPath, &Document::getPath);
264 return it == documents.end() ? nullptr : &*it;
265}
266
267bool DocumentStore::contains(const std::string_view documentPath) const noexcept
268{
269 return find(documentPath) != nullptr;
270}
271
272bool DocumentStore::contains(const DocumentType type) const noexcept
273{
274 return std::ranges::any_of(
275 documents, [type](const Document& document) { return document.getType() == type; });
276}
277
278const std::vector<Document>& DocumentStore::getDocuments() const noexcept
279{
280 return documents;
281}
282
283std::optional<std::size_t>
284DocumentStore::indexOf(const std::string_view documentPath) const
285{
286 const auto it = std::ranges::find(documents, documentPath, &Document::getPath);
287 if (it == documents.end())
288 return std::nullopt;
289 return static_cast<std::size_t>(std::distance(documents.begin(), it));
290}
291
292std::uint64_t DocumentStore::addListener(Listener listener) const
293{
294 return listeners.add(std::move(listener));
295}
296
297void DocumentStore::removeListener(const std::uint64_t id) const
298{
299 listeners.remove(id);
300}
301
302void DocumentStore::commitDocuments(std::vector<Document> documents,
303 const DocumentChange& change) noexcept
304{
305 this->documents.swap(documents);
306 listeners.notify(change);
307}
308
309std::shared_ptr<const std::string> binaryContentsSnapshot(const DocumentStore& documents,
310 const std::string_view slug)
311{
312 if (!isValidDocumentSlug(slug))
313 return nullptr;
314
315 const auto* document =
317 if (!document)
318 return nullptr;
319
320 return std::make_shared<const std::string>(document->getContents());
321}
322
323} // namespace SILICON::project
SILICON::core::CallbackRegistry< const DocumentChange & > listeners
const std::vector< Document > & getDocuments() const noexcept
std::uint64_t addListener(Listener listener) const
std::optional< std::size_t > indexOf(std::string_view documentPath) const
bool contains(std::string_view documentPath) const noexcept
void removeListener(std::uint64_t id) const
const Document * find(std::string_view documentPath) const noexcept
void commitDocuments(std::vector< Document > documents, const DocumentChange &change) noexcept
std::function< void(const DocumentChange &)> Listener
std::vector< Document > documents
void setContents(std::string contents)
DocumentType getType() const noexcept
Document(std::string path, std::string contents)
const std::string & getPath() const noexcept
const std::string & getContents() const noexcept
std::string documentPathForSlug(const DocumentType type, const std::string_view slug)
constexpr const DocumentTypeInfo & documentTypeInfo(const DocumentType type)
constexpr std::array< DocumentTypeInfo, 4 > DOCUMENT_TYPE_INFO
std::string documentFileName(const Document &document)
std::optional< std::string > declaredArchitectureName(const std::string_view source)
std::optional< std::string > documentSlugForPath(const std::string_view path)
Document importDocument(const std::string_view sourcePath, std::string contents)
bool isValidDocumentSlug(const std::string_view slug)
std::optional< DocumentType > documentTypeForPath(const std::string_view path)
bool isValidArchitectureName(const std::string_view name)
bool isArchitecturePathSlug(const std::string_view name)
std::shared_ptr< const std::string > binaryContentsSnapshot(const DocumentStore &documents, const std::string_view slug)