utils functions, expect, ensure, error handling refactoring (string -> string_view), std::source_location

This commit is contained in:
ProgramSnail 2024-03-11 00:33:53 +03:00
parent 5afbaf06ae
commit d6a3ce1946
7 changed files with 149 additions and 29 deletions

View file

@ -1,10 +1,13 @@
#pragma once
#include "utils.hpp"
#include <optional>
#include <ranges>
#include <string>
#include <utility>
#include <variant>
#include <vector>
namespace nodes {
@ -222,6 +225,8 @@ private:
class Identifier : public Node {
public:
static constexpr char NAME_DELIMITER = '.';
enum IdentifierType {
SIMPLE_NAME,
SIMPLE_TYPE,
@ -251,18 +256,51 @@ public:
//
void append_before(const std::string &name) { value_ = name + "." + value_; }
void append_before(const std::string &name) {
value_ = name + NAME_DELIMITER + value_;
}
void append_before(
const Identifier &identifier,
std::source_location location = std::source_location::current()) {
error_handling::expect(identifier.type_ == type_,
"different Identifier types on append_before",
location);
value_ = *identifier.get() + NAME_DELIMITER + value_;
}
void append_after(const std::string &name) {
value_ += "." + name;
value_ += NAME_DELIMITER + name;
}
void append_after(
const Identifier &identifier,
std::source_location location = std::source_location::current()) {
error_handling::expect(identifier.type_ == type_,
"different Identifier types on append_after",
location);
value_ += NAME_DELIMITER + *identifier.get();
}
std::vector<Identifier> git_fragments() const {
std::vector<Identifier> fragments;
for (auto &&fragment_name :
std::ranges::views::split(value_, NAME_DELIMITER)) {
fragments.emplace_back(
*this, type_,
std::string(fragment_name.begin(), fragment_name.end()));
}
return fragments;
}
std::pair<Identifier, Identifier> split_first() {
const auto pos = value_.find('.');
const auto pos = value_.find(NAME_DELIMITER);
if (pos == std::string::npos) {
return {Identifier(*this, type_, ""), *this};
}
return {Identifier(*this, type_, value_.substr(0, pos)), Identifier(*this, type_, value_.substr(pos + 1))}; // '.' is leaved out
return {
Identifier(*this, type_, value_.substr(0, pos)),
Identifier(*this, type_, value_.substr(pos + 1))}; // '.' is leaved out
}
//