6#ifndef MIMICPP_PRINTING_TYPE_NAME_LEXER_HPP
7#define MIMICPP_PRINTING_TYPE_NAME_LEXER_HPP
15#ifndef MIMICPP_DETAIL_IS_MODULE
28 inline auto constexpr is_space = [](
char const c)
noexcept {
29 return static_cast<bool>(
30 std::isspace(
static_cast<unsigned char>(c)));
34 inline auto constexpr is_digit = [](
char const c)
noexcept {
35 return static_cast<bool>(
36 std::isdigit(
static_cast<unsigned char>(c)));
42 inline std::array
constexpr visibilityKeywords = std::to_array<StringViewT>({
"public",
"protected",
"private"});
43 inline std::array
constexpr specKeywords = std::to_array<StringViewT>({
"const",
"constexpr",
"volatile",
"noexcept",
"static"});
44 inline std::array
constexpr contextKeywords = std::to_array<StringViewT>({
"operator",
"struct",
"class",
"enum"});
45 inline std::array
constexpr typeKeywords = std::to_array<StringViewT>(
47 {
"auto",
"void",
"bool",
"char",
"char8_t",
"char16_t",
"char32_t",
"wchar_t",
"double",
"float",
"int",
"long",
"__int64",
"short",
"signed",
"unsigned"});
48 inline std::array
constexpr otherKeywords = std::to_array<StringViewT>({
"new",
"delete",
"co_await"});
49 inline std::array
constexpr digraphs = std::to_array<StringViewT>(
50 {
"and",
"or",
"xor",
"not",
"bitand",
"bitor",
"compl",
"and_eq",
"or_eq",
"xor_eq",
"not_eq"});
52 inline std::array
constexpr braceLikes = std::to_array<StringViewT>({
"{",
"}",
"[",
"]",
"(",
")",
"`",
"'"});
53 inline std::array
constexpr comparison = std::to_array<StringViewT>({
"==",
"!=",
"<",
"<=",
">",
">=",
"<=>"});
54 inline std::array
constexpr assignment = std::to_array<StringViewT>({
"=",
"+=",
"-=",
"*=",
"/=",
"%=",
"&=",
"|=",
"^=",
"<<=",
">>="});
55 inline std::array
constexpr incOrDec = std::to_array<StringViewT>({
"++",
"--"});
56 inline std::array
constexpr arithmetic = std::to_array<StringViewT>({
"+",
"-",
"*",
"/",
"%"});
57 inline std::array
constexpr bitArithmetic = std::to_array<StringViewT>({
"~",
"&",
"|",
"^",
"<<",
">>"});
58 inline std::array
constexpr logical = std::to_array<StringViewT>({
"!",
"&&",
"||"});
59 inline std::array
constexpr access = std::to_array<StringViewT>({
".",
".*",
"->",
"->*"});
60 inline std::array
constexpr specialAngles = std::to_array<StringViewT>({
"<:",
":>",
"<%",
"%>"});
61 inline std::array
constexpr rest = std::to_array<StringViewT>({
"::",
";",
",",
":",
"...",
"?"});
75 std::ranges::sort(collection);
76 MIMICPP_ASSERT(collection.cend() == std::ranges::unique(collection).begin(),
"Fix your input!");
96 std::ranges::sort(collection);
97 MIMICPP_ASSERT(collection.cend() == std::ranges::unique(collection).begin(),
"Fix your input!");
116 std::ranges::distance(
123 explicit constexpr keyword(std::ptrdiff_t
const keywordIndex) noexcept
124 : m_KeywordIndex{keywordIndex}
139 std::ptrdiff_t m_KeywordIndex;
150 std::ranges::distance(
158 : m_TextIndex{textIndex}
173 std::ptrdiff_t m_TextIndex;
208 : m_Text{std::move(text)},
216 return std::exchange(m_Next, find_next());
230 constexpr token find_next() noexcept
235 .content = {m_Text.cend(), m_Text.cend()},
236 .classification = end{}
251 .classification = space{}};
259 m_Text.substr(0u, 1u)))
261 return next_as_op_or_punctuator(options);
276 .classification = identifier{.content = content}};
282 auto const end = std::ranges::find_if_not(m_Text.cbegin() + 1, m_Text.cend(),
is_space);
294 constexpr token next_as_op_or_punctuator(std::span<StringViewT const> options)
noexcept
296 MIMICPP_ASSERT(m_Text.substr(0u, 1u) == options.front(),
"Assumption does not hold.");
298 auto const try_advance = [&,
this](std::size_t
const n) {
299 if (n <= m_Text.size())
303 StringViewT{m_Text.cbegin(), m_Text.cbegin() + n});
306 return std::ranges::subrange{options.end(), options.end()};
309 std::size_t length{1u};
311 while (
auto const nextOptions = try_advance(length + 1))
314 options = {nextOptions.begin(), nextOptions.end()};
317 if (
auto const& front = options.front();
318 length == front.size())
328 StringViewT const content{m_Text.substr(0u, lastMatch->size())};
329 m_Text.remove_prefix(lastMatch->size());
333 .classification = operator_or_punctuator{index}};
347 constexpr StringViewT next_as_identifier() noexcept
349 auto const last = std::ranges::find_if_not(
354 && !std::ranges::binary_search(operator_or_punctuator::textCollection, StringViewT{&c, 1u});
358 m_Text = {last, m_Text.cend()};
#define MIMICPP_ASSERT(condition, msg)
Definition Config.hpp:51
constexpr token next() noexcept
Definition NameLexer.hpp:214
constexpr token const & peek() const noexcept
Definition NameLexer.hpp:220
constexpr NameLexer(StringViewT text) noexcept
Definition NameLexer.hpp:207
Definition NameLexer.hpp:40
std::array constexpr incOrDec
Definition NameLexer.hpp:55
std::array constexpr assignment
Definition NameLexer.hpp:54
std::array constexpr contextKeywords
Definition NameLexer.hpp:44
std::array constexpr typeKeywords
Definition NameLexer.hpp:45
std::array constexpr rest
Definition NameLexer.hpp:61
std::array constexpr logical
Definition NameLexer.hpp:58
std::array constexpr otherKeywords
Definition NameLexer.hpp:48
std::array constexpr digraphs
Definition NameLexer.hpp:49
std::array constexpr specialAngles
Definition NameLexer.hpp:60
std::array constexpr arithmetic
Definition NameLexer.hpp:56
std::array constexpr comparison
Definition NameLexer.hpp:53
std::array constexpr braceLikes
Definition NameLexer.hpp:52
std::array constexpr specKeywords
Definition NameLexer.hpp:43
std::array constexpr visibilityKeywords
Definition NameLexer.hpp:42
std::array constexpr access
Definition NameLexer.hpp:59
std::array constexpr bitArithmetic
Definition NameLexer.hpp:57
Definition NameLexer.hpp:26
consteval auto make_operator_or_punctuator_collection() noexcept
Definition NameLexer.hpp:82
std::variant< end, space, keyword, operator_or_punctuator, identifier > token_class
Definition NameLexer.hpp:190
auto constexpr is_space
Definition NameLexer.hpp:28
auto constexpr is_digit
Definition NameLexer.hpp:34
consteval auto make_keyword_collection() noexcept
Definition NameLexer.hpp:65
constexpr std::ranges::borrowed_subrange_t< Range > prefix_range(Range &&range, Prefix &&prefix)
Returns a view containing all elements, which start with the given prefix.
Definition Algorithm.hpp:192
constexpr std::array< T, firstN+secondN > concat_arrays(std::array< T, firstN > const &first, std::array< T, secondN > const &second)
Concatenates the given arrays by copying all elements into a new array.
Definition Algorithm.hpp:218
constexpr detail::binary_find_fn binary_find
Finds the specified value within the container and returns an iterator pointing to it....
Definition Algorithm.hpp:361
std::basic_string_view< CharT, CharTraitsT > StringViewT
Definition Fwd.hpp:392
Definition NameLexer.hpp:185
bool operator==(end const &) const =default
Definition NameLexer.hpp:177
bool operator==(identifier const &) const =default
StringViewT content
Definition NameLexer.hpp:178
Definition NameLexer.hpp:109
constexpr keyword(StringViewT const &text) noexcept
Definition NameLexer.hpp:114
constexpr keyword(std::ptrdiff_t const keywordIndex) noexcept
Definition NameLexer.hpp:123
constexpr StringViewT text() const noexcept
Definition NameLexer.hpp:130
bool operator==(keyword const &) const =default
static constexpr std::array textCollection
Definition NameLexer.hpp:111
Definition NameLexer.hpp:143
bool operator==(operator_or_punctuator const &) const =default
constexpr StringViewT text() const noexcept
Definition NameLexer.hpp:164
static constexpr std::array textCollection
Definition NameLexer.hpp:145
constexpr operator_or_punctuator(std::ptrdiff_t const textIndex) noexcept
Definition NameLexer.hpp:157
constexpr operator_or_punctuator(StringViewT const &text) noexcept
Definition NameLexer.hpp:148
Definition NameLexer.hpp:103
bool operator==(space const &) const =default
Definition NameLexer.hpp:198
token_class classification
Definition NameLexer.hpp:200
StringViewT content
Definition NameLexer.hpp:199