Initial version of Tokenizer and Token Generator template for coroutines (used in tokenizer) Utilities like string related functions, TrieMap, and error handling TODO: Add tests for Tokenizer TODO: Add tests for Generator
68 lines
1.4 KiB
C++
68 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#include <map>
|
|
#include <optional>
|
|
#include <string_view>
|
|
|
|
namespace arti::lang {
|
|
|
|
template <typename T>
|
|
struct TrieMap {
|
|
struct Node {
|
|
bool isLeaf;
|
|
std::optional<T> value;
|
|
std::map<char, Node> childs;
|
|
|
|
Node() noexcept
|
|
: isLeaf(false)
|
|
, value(std::nullopt)
|
|
, childs() { }
|
|
|
|
~Node() noexcept = default;
|
|
|
|
Node(Node &&) noexcept = default;
|
|
Node &operator=(Node &&) noexcept = default;
|
|
|
|
Node(const Node &) noexcept = default;
|
|
Node &operator=(const Node &) noexcept = default;
|
|
};
|
|
|
|
Node root;
|
|
|
|
TrieMap() noexcept = default;
|
|
~TrieMap() noexcept = default;
|
|
|
|
TrieMap(TrieMap &&) noexcept = default;
|
|
TrieMap &operator=(TrieMap &&) noexcept = default;
|
|
|
|
TrieMap(const TrieMap &) noexcept = default;
|
|
TrieMap &operator=(const TrieMap &) noexcept = default;
|
|
|
|
void insert(std::string_view str, T &&value) noexcept {
|
|
auto ptrNode = &root;
|
|
|
|
for (auto c : str) {
|
|
ptrNode = &(ptrNode->childs[c]);
|
|
}
|
|
|
|
ptrNode->isLeaf = true;
|
|
ptrNode->value = std::forward<T>(value);
|
|
}
|
|
|
|
T *find(std::string_view str) const noexcept {
|
|
auto ptrNode = &root;
|
|
|
|
for (auto c : str) {
|
|
if (not ptrNode->childs.contains(c)) {
|
|
return nullptr;
|
|
}
|
|
|
|
ptrNode = &(ptrNode->childs.at(c));
|
|
}
|
|
|
|
return ptrNode;
|
|
}
|
|
};
|
|
|
|
} // namespace arti::lang
|