lang/include/nodes/doc_nodes.hpp
2024-01-04 19:29:29 +03:00

102 lines
2.6 KiB
C++

#pragma once
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
namespace nodes {
class SymbolDocs {
public:
SymbolDocs() {}
SymbolDocs(std::string &&description)
: description_(std::move(description)) {}
SymbolDocs(const std::string &description) : description_(description) {}
bool add_annotation_info(const std::string &annotation, std::string &&info) {
if (annotations_info_ids_.count(annotation) == 0) {
annotations_info_ids_[annotation] = annotations_info_.size();
annotations_info_.emplace_back(annotation, std::move(info));
return true;
}
return false;
}
bool add_annotation_info(const std::string &annotation,
const std::string &info) {
if (annotations_info_ids_.count(annotation) == 0) {
annotations_info_ids_[annotation] = annotations_info_.size();
annotations_info_.emplace_back(annotation, std::move(info));
return true;
}
return false;
}
//
std::optional<std::string *> get_description() {
if (description_.has_value()) {
return &description_.value();
}
return std::nullopt;
}
std::optional<const std::string *> get_description() const {
if (description_.has_value()) {
return &description_.value();
}
return std::nullopt;
}
//
std::optional<std::string *>
get_annotation_info(const std::string &annotation) {
auto info_iterator = annotations_info_ids_.find(annotation);
if (info_iterator != annotations_info_ids_.end()) {
return &annotations_info_[info_iterator->second].second;
}
return std::nullopt;
}
std::optional<const std::string *>
get_annotation_info(const std::string &annotation) const {
auto info_iterator = annotations_info_ids_.find(annotation);
if (info_iterator != annotations_info_ids_.end()) {
return &annotations_info_[info_iterator->second].second;
}
return std::nullopt;
}
//
size_t annotations_info_size() const { return annotations_info_.size(); }
std::string *get_annotation(size_t id) {
return &annotations_info_[id].first;
}
const std::string *get_annotation(size_t id) const {
return &annotations_info_[id].first;
}
std::string *get_annotation_info(size_t id) {
return &annotations_info_[id].second;
}
const std::string *get_annotation_info(size_t id) const {
return &annotations_info_[id].second;
}
//
private:
std::optional<std::string> description_;
std::vector<std::pair<std::string, std::string>> annotations_info_;
std::unordered_map<std::string, size_t> annotations_info_ids_;
};
} // namespace nodes