6#ifndef MIMICPP_PRINTING_TYPE_LEXER_HPP
7#define MIMICPP_PRINTING_TYPE_LEXER_HPP
15#ifndef MIMICPP_DETAIL_IS_MODULE
29 inline auto constexpr is_space = [](
char const c)
noexcept {
32 case ' ': [[fallthrough]];
33 case '\f': [[fallthrough]];
34 case '\n': [[fallthrough]];
35 case '\r': [[fallthrough]];
36 case '\t': [[fallthrough]];
37 case '\v':
return true;
39 default:
return false;
45 inline auto constexpr is_digit = [](
char const c)
noexcept {
46 return '0' <= c && c <=
'9';
52 inline std::array
constexpr visibilityKeywords = std::to_array<StringViewT>({
"public",
"protected",
"private"});
53 inline std::array
constexpr specKeywords = std::to_array<StringViewT>({
"const",
"constexpr",
"volatile",
"noexcept",
"static",
"mutable"});
54 inline std::array
constexpr contextKeywords = std::to_array<StringViewT>({
"operator",
"struct",
"class",
"enum"});
55 inline std::array
constexpr typeKeywords = std::to_array<StringViewT>(
57 {
"auto",
"void",
"bool",
"char",
"char8_t",
"char16_t",
"char32_t",
"wchar_t",
"double",
"float",
"int",
"long",
"__int64",
"short",
"signed",
"unsigned"});
58 inline std::array
constexpr otherKeywords = std::to_array<StringViewT>({
"new",
"delete",
"co_await"});
59 inline std::array
constexpr digraphs = std::to_array<StringViewT>(
60 {
"and",
"or",
"xor",
"not",
"bitand",
"bitor",
"compl",
"and_eq",
"or_eq",
"xor_eq",
"not_eq"});
62 inline std::array
constexpr braceLikes = std::to_array<StringViewT>({
"{",
"}",
"[",
"]",
"(",
")",
"`",
"'"});
63 inline std::array
constexpr comparison = std::to_array<StringViewT>({
"==",
"!=",
"<",
"<=",
">",
">=",
"<=>"});
64 inline std::array
constexpr assignment = std::to_array<StringViewT>({
"=",
"+=",
"-=",
"*=",
"/=",
"%=",
"&=",
"|=",
"^=",
"<<=",
">>="});
65 inline std::array
constexpr incOrDec = std::to_array<StringViewT>({
"++",
"--"});
66 inline std::array
constexpr arithmetic = std::to_array<StringViewT>({
"+",
"-",
"*",
"/",
"%"});
67 inline std::array
constexpr bitArithmetic = std::to_array<StringViewT>({
"~",
"&",
"|",
"^",
"<<",
">>"});
68 inline std::array
constexpr logical = std::to_array<StringViewT>({
"!",
"&&",
"||"});
69 inline std::array
constexpr access = std::to_array<StringViewT>({
".",
".*",
"->",
"->*"});
70 inline std::array
constexpr rest = std::to_array<StringViewT>({
"::",
";",
",",
":",
"...",
"?"});
87 std::ranges::sort(collection);
88 MIMICPP_ASSERT(collection.cend() == std::ranges::unique(collection).begin(),
"Fix your input!");
107 std::ranges::sort(collection);
108 MIMICPP_ASSERT(collection.cend() == std::ranges::unique(collection).begin(),
"Fix your input!");
127 std::ranges::distance(
134 explicit constexpr keyword(std::ptrdiff_t
const keywordIndex) noexcept
135 : m_KeywordIndex{keywordIndex}
147 constexpr std::ptrdiff_t
index() const noexcept
149 return m_KeywordIndex;
156 std::ptrdiff_t m_KeywordIndex;
167 std::ranges::distance(
175 : m_TextIndex{textIndex}
187 constexpr std::ptrdiff_t
index() const noexcept
196 std::ptrdiff_t m_TextIndex;
240 : m_Text{std::move(text)},
248 return std::exchange(m_Next, find_next());
262 constexpr token find_next() noexcept
267 .content = {m_Text.cend(), m_Text.cend()},
268 .classification = end{}
283 .classification = space{}};
291 m_Text.substr(0u, 1u)))
293 return next_as_op_or_punctuator(options);
296 if (std::optional literal = try_next_as_literal())
299 .content = literal->content,
300 .classification = *std::move(literal)};
315 .classification = identifier{.content = content}};
321 auto const end = std::ranges::find_if_not(m_Text.cbegin() + 1, m_Text.cend(),
is_space);
333 constexpr token next_as_op_or_punctuator(std::span<StringViewT const> options)
noexcept
335 MIMICPP_ASSERT(m_Text.substr(0u, 1u) == options.front(),
"Assumption does not hold.");
337 auto const try_advance = [&,
this](std::size_t
const n) {
338 if (n <= m_Text.size())
342 StringViewT{m_Text.cbegin(), m_Text.cbegin() + n});
345 return std::ranges::subrange{options.end(), options.end()};
348 std::size_t length{1u};
350 while (
auto const nextOptions = try_advance(length + 1))
353 options = {nextOptions.begin(), nextOptions.end()};
356 if (
auto const& front = options.front();
357 length == front.size())
367 StringViewT const content{m_Text.substr(0u, lastMatch->size())};
368 m_Text.remove_prefix(lastMatch->size());
372 .classification = operator_or_punctuator{index}};
386 constexpr StringViewT next_as_identifier() noexcept
388 auto const last = std::ranges::find_if_not(
393 && !std::ranges::binary_search(operator_or_punctuator::textCollection, StringViewT{&c, 1u});
397 m_Text = {last, m_Text.cend()};
405 constexpr std::optional<literal> try_next_as_literal() noexcept
411 if (
'0' == m_Text.front()
412 && 2u <= m_Text.size())
421 auto const last = std::ranges::find_if_not(m_Text.cbegin() + 1, m_Text.cend(),
is_digit);
423 m_Text = {last, m_Text.cend()};
425 return literal{.content = content};
#define MIMICPP_ASSERT(condition, msg)
Definition Config.hpp:54
constexpr token next() noexcept
Definition Lexer.hpp:246
constexpr token const & peek() const noexcept
Definition Lexer.hpp:252
constexpr NameLexer(StringViewT text) noexcept
Definition Lexer.hpp:239
std::array constexpr incOrDec
Definition Lexer.hpp:65
std::array constexpr assignment
Definition Lexer.hpp:64
std::array constexpr contextKeywords
Definition Lexer.hpp:54
std::array constexpr typeKeywords
Definition Lexer.hpp:55
std::array constexpr rest
Definition Lexer.hpp:70
std::array constexpr logical
Definition Lexer.hpp:68
std::array constexpr otherKeywords
Definition Lexer.hpp:58
std::array constexpr digraphs
Definition Lexer.hpp:59
std::array constexpr arithmetic
Definition Lexer.hpp:66
std::array constexpr comparison
Definition Lexer.hpp:63
std::array constexpr braceLikes
Definition Lexer.hpp:62
std::array constexpr specKeywords
Definition Lexer.hpp:53
std::array constexpr visibilityKeywords
Definition Lexer.hpp:52
std::array constexpr access
Definition Lexer.hpp:69
std::array constexpr bitArithmetic
Definition Lexer.hpp:67
std::variant< end, space, keyword, operator_or_punctuator, identifier, literal > token_class
Definition Lexer.hpp:221
consteval auto make_operator_or_punctuator_collection() noexcept
Definition Lexer.hpp:94
auto constexpr is_space
Definition Lexer.hpp:29
auto constexpr is_digit
Definition Lexer.hpp:45
consteval auto make_keyword_collection() noexcept
Definition Lexer.hpp:77
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:398
bool operator==(end const &) const =default
bool operator==(identifier const &) const =default
StringViewT content
Definition Lexer.hpp:201
constexpr keyword(StringViewT const &text) noexcept
Definition Lexer.hpp:125
constexpr keyword(std::ptrdiff_t const keywordIndex) noexcept
Definition Lexer.hpp:134
constexpr StringViewT text() const noexcept
Definition Lexer.hpp:141
constexpr std::ptrdiff_t index() const noexcept
Definition Lexer.hpp:147
bool operator==(keyword const &) const =default
static constexpr std::array textCollection
Definition Lexer.hpp:122
bool operator==(literal const &) const =default
StringViewT content
Definition Lexer.hpp:209
constexpr std::ptrdiff_t index() const noexcept
Definition Lexer.hpp:187
bool operator==(operator_or_punctuator const &) const =default
constexpr StringViewT text() const noexcept
Definition Lexer.hpp:181
static constexpr std::array textCollection
Definition Lexer.hpp:162
constexpr operator_or_punctuator(std::ptrdiff_t const textIndex) noexcept
Definition Lexer.hpp:174
constexpr operator_or_punctuator(StringViewT const &text) noexcept
Definition Lexer.hpp:165
bool operator==(space const &) const =default
token_class classification
Definition Lexer.hpp:232
StringViewT content
Definition Lexer.hpp:231