25#include <unordered_set>
30#include <nlohmann/json.hpp>
35 [[nodiscard]]
bool containsControlCharacter(
const std::string_view value)
37 return std::ranges::any_of(value, [](
const unsigned char character) {
38 return character < 0x20 || character == 0x7f;
42 [[nodiscard]]
bool isValidDocumentPath(
const std::string_view path,
45 if (!path.starts_with(info.root) || !path.ends_with(info.suffix))
48 if (path.size() < info.root.size() + info.suffix.size())
51 const auto slug = path.substr(
52 info.root.size(), path.size() - info.root.size() - info.suffix.size());
58 [[nodiscard]] std::string_view leafFileName(
const std::string_view path)
60 const auto separator = path.find_last_of(
"/\\");
61 return separator == std::string_view::npos ? path : path.substr(separator + 1);
64 [[nodiscard]] std::string lowerCaseExtension(
const std::string_view fileName)
66 const auto dot = fileName.find_last_of(
'.');
67 if (dot == std::string_view::npos || dot == 0)
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));
77 [[nodiscard]] std::string fileStem(
const std::string_view fileName)
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));
85 [[nodiscard]]
bool isCircuitContents(
const std::string_view contents)
87 const auto json = nlohmann::json::parse(contents,
nullptr,
false);
88 if (!json.is_object())
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());
97 [[nodiscard]]
bool isTextContents(
const std::string_view contents)
99 for (std::size_t index = 0; index < contents.size();) {
100 const auto byte =
static_cast<unsigned char>(contents[index]);
102 if ((
byte < 0x20 &&
byte !=
'\t' &&
byte !=
'\n' &&
byte !=
'\r') ||
byte == 0x7f)
109 std::uint32_t codePoint;
110 std::uint32_t minimumCodePoint;
111 if (
byte >= 0xc2 &&
byte <= 0xdf) {
113 codePoint =
byte & 0x1f;
114 minimumCodePoint = 0x80;
115 }
else if (
byte >= 0xe0 &&
byte <= 0xef) {
117 codePoint =
byte & 0x0f;
118 minimumCodePoint = 0x800;
119 }
else if (
byte >= 0xf0 &&
byte <= 0xf4) {
121 codePoint =
byte & 0x07;
122 minimumCodePoint = 0x10000;
127 if (index + length > contents.size())
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)
133 codePoint = (codePoint << 6) | (continuation & 0x3f);
136 if (codePoint < minimumCodePoint
137 || (length == 3 && codePoint >= 0xd800 && codePoint <= 0xdfff)
138 || (length == 4 && codePoint > 0x10ffff))
145 [[nodiscard]] std::optional<DocumentType>
146 documentTypeForFile(
const std::string_view fileName,
const std::string_view contents)
148 const auto extension = lowerCaseExtension(fileName);
149 if (extension ==
".v")
151 if (extension ==
".sisl")
153 if (extension ==
".bin")
156 if (isCircuitContents(contents))
158 if (!contents.empty() && !isTextContents(contents))
169 return isValidDocumentPath(path, info);
182 path.substr(info.root.size(), path.size() - info.root.size() - info.suffix.size()));
187 return !slug.empty() && slug !=
"." && slug !=
".." && !slug.contains(
'/')
188 && !slug.contains(
'\\') && !containsControlCharacter(slug);
197 throw std::invalid_argument(
"Invalid document slug");
199 return std::format(
"{}{}{}", info.root, slug, info.suffix);
207 throw std::invalid_argument(
"Document path is invalid");
227 this->contents = std::move(
contents);
232 const auto fileName = leafFileName(sourcePath);
233 if (fileName.empty() || fileName ==
"." || fileName ==
"..")
234 throw std::invalid_argument(
"Document file name is invalid");
236 const auto type = documentTypeForFile(fileName, contents);
238 throw std::invalid_argument(std::format(
"Unsupported document file: {}", fileName));
244 throw std::invalid_argument(
"SISL file must begin with a valid arch declaration");
248 : fileStem(fileName);
251 throw std::invalid_argument(
"Document file name is invalid");
258 return std::string(leafFileName(document.
getPath()));
269 return find(documentPath) !=
nullptr;
274 return std::ranges::any_of(
283std::optional<std::size_t>
289 return static_cast<std::size_t
>(std::distance(
documents.begin(), it));
294 return listeners.add(std::move(listener));
310 const std::string_view slug)
315 const auto* document =
320 return std::make_shared<const std::string>(document->getContents());
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)