ppc-lang/include/lang/common.hh

88 lines
3.0 KiB
C++
Raw Normal View History

2022-09-19 07:34:19 +00:00
#pragma once
2022-12-05 18:31:31 +00:00
#include <vector>
2023-01-19 09:29:04 +00:00
#include <string>
2022-12-05 18:31:31 +00:00
namespace ppc::lang {
2023-01-26 08:06:39 +00:00
struct nmsp_t: public std::vector<std::string> {
2022-12-05 18:31:31 +00:00
using base = std::vector<std::string>;
2023-01-26 08:06:39 +00:00
int compare(const nmsp_t &other) const;
2022-12-05 18:31:31 +00:00
2023-01-26 08:06:39 +00:00
bool operator==(const nmsp_t &other) const { return compare(other) == 0; }
bool operator!=(const nmsp_t &other) const { return compare(other) != 0; }
bool operator<(const nmsp_t &other) const { return compare(other) < 0; }
bool operator<=(const nmsp_t &other) const { return compare(other) <= 0; }
bool operator>(const nmsp_t &other) const { return compare(other) > 0; }
bool operator>=(const nmsp_t &other) const { return compare(other) >= 0; }
2022-12-05 18:31:31 +00:00
operator std::string() const { return to_string(); }
std::string to_string() const;
2023-01-26 08:06:39 +00:00
nmsp_t() { }
nmsp_t(std::initializer_list<std::string> segments): base(segments.begin(), segments.end()) { }
2022-12-05 18:31:31 +00:00
};
}
template <>
2023-01-26 08:06:39 +00:00
struct std::hash<ppc::lang::nmsp_t> {
std::size_t operator()(const ppc::lang::nmsp_t& k) const {
2022-12-05 18:31:31 +00:00
size_t res = 0;
for (auto &el : k) {
res ^= std::hash<std::string>()(el);
}
return res;
}
};
2022-09-19 07:34:19 +00:00
#include "utils/message.hh"
#include "utils/location.hh"
namespace ppc::lang {
2023-01-26 08:06:39 +00:00
template <class ParserT>
struct located_t: ParserT {
2022-10-04 13:00:18 +00:00
location_t location;
2023-01-26 08:06:39 +00:00
located_t(location_t loc, const ParserT &val): ParserT(val), location(loc) { }
located_t(const ParserT &val): ParserT(val), location(location_t::NONE) { }
2022-10-04 20:45:08 +00:00
located_t() { }
2022-10-04 13:00:18 +00:00
};
2022-10-19 11:21:05 +00:00
2023-01-26 08:06:39 +00:00
struct loc_nmsp_t: public std::vector<located_t<std::string>> {
2022-10-09 11:18:38 +00:00
using base = std::vector<located_t<std::string>>;
2023-01-26 08:06:39 +00:00
int compare(const loc_nmsp_t &other) const;
2023-01-26 08:06:39 +00:00
bool operator==(const loc_nmsp_t &other) const { return compare(other) == 0; }
bool operator!=(const loc_nmsp_t &other) const { return compare(other) != 0; }
bool operator<(const loc_nmsp_t &other) const { return compare(other) < 0; }
bool operator<=(const loc_nmsp_t &other) const { return compare(other) <= 0; }
bool operator>(const loc_nmsp_t &other) const { return compare(other) > 0; }
bool operator>=(const loc_nmsp_t &other) const { return compare(other) >= 0; }
2022-10-09 11:18:38 +00:00
2023-01-26 08:06:39 +00:00
nmsp_t strip_location() const;
2023-01-26 16:47:20 +00:00
std::string to_string() const;
2022-10-09 11:18:38 +00:00
2023-01-27 16:40:44 +00:00
operator nmsp_t() const { return strip_location(); }
2022-10-09 11:18:38 +00:00
operator std::string() const { return to_string(); }
2023-01-26 08:06:39 +00:00
loc_nmsp_t() { }
loc_nmsp_t(std::initializer_list<located_t<std::string>> segments): base(segments.begin(), segments.end()) { }
2022-10-09 11:18:38 +00:00
};
2023-01-26 08:06:39 +00:00
template <class SetT>
bool resolve_name(const SetT &defs, const nmsp_t &src, const nmsp_t &target) {
if (src == target) return true;
2023-01-27 16:40:44 +00:00
for (auto it : defs) {
nmsp_t val = (nmsp_t)(it);
2023-01-26 08:06:39 +00:00
val.insert(val.end(), src.begin(), src.end());
if (val == target) return true;
}
2023-01-26 16:47:20 +00:00
return false;
2022-09-19 07:34:19 +00:00
}
2022-12-05 18:31:31 +00:00
}