6#ifndef MIMICPP_PRINTING_TYPE_NAME_PARSER_REDUCTIONS_HPP
7#define MIMICPP_PRINTING_TYPE_NAME_PARSER_REDUCTIONS_HPP
14#ifndef MIMICPP_DETAIL_IS_MODULE
25 template <
typename Last,
typename... Others>
27 constexpr bool is_suffix_of(
28 [[maybe_unused]] util::type_list<Last, Others...>
const types,
29 std::span<Token const>
const tokenStack)
noexcept
31 if (tokenStack.empty()
32 || !std::holds_alternative<Last>(tokenStack.back()))
37 if constexpr (0u <
sizeof...(Others))
40 util::type_list<Others...>{},
41 tokenStack.first(tokenStack.size() - 1));
51 constexpr bool is_suffix_of(std::span<Token const>
const tokenStack)
noexcept
55 return 1u +
sizeof...(Others) <= tokenStack.
size()
59 template <token_type Leading, token_type... Others>
61 constexpr auto match_suffix(std::span<Token>
const tokenStack)
noexcept
63 if constexpr (0u ==
sizeof...(Others))
68 result = &std::get<Leading>(tokenStack.back());
75 std::optional<std::tuple<Leading&, Others&...>> result{};
78 auto const suffix = tokenStack.last(1u +
sizeof...(Others));
81 [&]<std::size_t... indices>([[maybe_unused]] std::index_sequence<indices...>
const)
noexcept {
83 std::get<Leading>(suffix[0u]),
84 std::get<Others>(suffix[1u + indices])...);
86 std::index_sequence_for<Others...>{});
93 constexpr void remove_suffix(std::span<Token>& tokenStack, std::size_t
const count)
noexcept
95 MIMICPP_ASSERT(count <= tokenStack.size(),
"Count exceeds stack size.");
96 tokenStack = tokenStack.first(tokenStack.size() - count);
111 &&
id->is_reserved())
123 std::span pendingTokens{tokenStack};
133 scope = std::move(*identifier);
137 scope = std::move(*funIdentifier);
145 tokenStack.resize(pendingTokens.size());
149 sequence->scopes.emplace_back(std::move(scope));
153 tokenStack.emplace_back(
155 .scopes = {std::move(scope)}});
163 std::span pendingTokens{tokenStack};
168 auto& [seq, sep,
type] = *suffix;
170 seq.types.emplace_back(std::move(
type));
171 tokenStack.resize(pendingTokens.size());
181 seq.
types.emplace_back(std::move(*
type));
182 tokenStack.resize(pendingTokens.size());
183 tokenStack.emplace_back(std::move(seq));
193 std::span pendingTokens{tokenStack};
214 || id->is_template())
221 id->templateArgs = std::move(*args);
225 id->templateArgs.emplace();
227 tokenStack.resize(pendingTokens.size());
234 std::span pendingTokens{tokenStack};
264 if (1u != args->types.size()
265 || !args->types.front().is_void())
267 funCtx.args = std::move(*args);
271 tokenStack.resize(pendingTokens.size());
272 tokenStack.emplace_back(std::move(funCtx));
279 std::span pendingStack{tokenStack};
291 auto& [identifier, funCtx] = *suffix;
293 .identifier = std::move(identifier),
294 .context = std::move(funCtx)};
296 tokenStack.resize(pendingStack.size() + 1u);
297 tokenStack.back() = std::move(funIdentifier);
308 return tokenStack.empty()
319 template <token_type Opening, token_type Closing>
323 std::span pendingTokens{tokenStack.begin(), tokenStack.end() - 1};
325 auto const openingIter = std::ranges::find_if(
326 pendingTokens.rbegin(),
327 pendingTokens.rend(),
328 [](
Token const&
token)
noexcept { return std::holds_alternative<Opening>(token); });
329 if (openingIter == pendingTokens.rend()
336 auto const& opening = std::get<Opening>(*std::ranges::prev(openingIter.base(), 1));
337 auto const& closing = std::get<Closing>(tokenStack.back());
338 auto const contentLength = (closing.content.data() - opening.content.data()) + closing.content.size();
339 StringViewT const content{opening.content.data(), contentLength};
341 pendingTokens = std::span{pendingTokens.begin(), openingIter.base() - 1};
346 tokenStack.resize(pendingTokens.size() + 1u);
347 tokenStack.back() =
Identifier{.content = content};
354 std::span pendingTokens{tokenStack};
373 || !
id->is_reserved())
391 .returnType = std::make_shared<Type>(std::move(*returnType)),
392 .context = std::move(*ctx)};
395 std::exchange(pendingTokens, {}).size() + 1u);
396 tokenStack.back().emplace<
Type>(std::move(funType));
403 std::span pendingTokens{tokenStack};
429 if (specs && specs->has_ptr())
442 if (
auto const*
const regular = std::get_if<RegularType>(&
type->state);
444 && regular->identifier.is_reserved())
455 regular = std::get_if<RegularType>(&
type->state);
463 || !regular->identifier.is_reserved()
464 || !regular->specs.has_ptr())
469 specs = ®ular->
specs;
482 funPtr.scopes = std::move(*scopeSeq);
488 .ctx = std::move(*nestedFunCtx)};
492 nested.ptr = std::make_shared<FunctionPtr>(std::move(*nestedFunPtr));
495 funPtr.nested = std::move(nested);
498 tokenStack.resize(pendingTokens.size());
499 tokenStack.emplace_back(std::move(funPtr));
506 void handled_nested_function_ptr(
TokenStack& tokenStack, FunctionPtr::NestedInfo info);
511 std::span pendingTokens{tokenStack};
520 auto& [returnType, space, ptr, ctx] = *suffix;
522 std::optional nestedInfo = std::move(ptr.nested);
524 .returnType = std::make_shared<Type>(std::move(returnType)),
525 .scopes = std::move(ptr.scopes),
526 .specs = std::move(ptr.specs),
527 .context = std::move(ctx)};
530 std::exchange(pendingTokens, {}).size() + 1u);
531 tokenStack.back().emplace<
Type>(std::move(ptrType));
538 detail::handled_nested_function_ptr(tokenStack, *std::move(nestedInfo));
549 inline void handled_nested_function_ptr(
TokenStack& tokenStack, FunctionPtr::NestedInfo info)
551 auto& [ptr, ctx] = info;
554 tokenStack.emplace_back(Space{});
556 bool const isFunPtr{ptr};
559 tokenStack.emplace_back(std::move(*ptr));
562 tokenStack.emplace_back(std::move(ctx));
577 std::span pendingTokens{tokenStack};
587 if (identifier->is_reserved()
590 tokenStack.pop_back();
605 if (
auto const& layers = prefixSpecs->layers;
607 || prefixSpecs->isNoexcept
608 || 1u != layers.size())
623 RegularType newType{.identifier = std::move(*identifier)};
626 newType.specs = std::move(*prefixSpecs);
631 newType.scopes = std::move(*scopes);
640 tokenStack.resize(pendingTokens.size());
641 tokenStack.emplace_back(
642 std::in_place_type<Type>,
657 std::span pendingTokens{tokenStack};
661 .identifier = std::move(*funIdentifier)};
666 function.scopes = std::move(*scopes);
675 function.returnType = std::make_shared<Type>(std::move(*returnType));
680 std::exchange(pendingTokens, {}).size());
681 tokenStack.emplace_back(std::move(function));
693 std::optional<FunctionContext> funCtx{};
696 funCtx = std::move(*ctx);
697 tokenStack.pop_back();
702 auto targetType = std::make_shared<Type>(
703 std::get<Type>(std::move(tokenStack.back())));
704 tokenStack.pop_back();
712 tokenStack.emplace_back(*std::move(funCtx));
725 return std::get<Type>(tokenStack.back()).specs();
730 MIMICPP_ASSERT(std::get<Identifier>(tokenStack.back()).is_reserved(),
"Unexpected token.");
731 tokenStack.pop_back();
736 return type->specs();
750 return std::get<Specs>(tokenStack.emplace_back(
Specs{}));
#define MIMICPP_DETAIL_CONSTEXPR_VECTOR
Definition Config.hpp:69
#define MIMICPP_ASSERT(condition, msg)
Definition Config.hpp:51
Definition NameParserTokens.hpp:235
std::vector< Type > types
Definition NameParserTokens.hpp:237
Definition NameParserTokens.hpp:350
Definition NameParserTokens.hpp:615
Definition NameParserTokens.hpp:360
Definition NameParserTokens.hpp:466
Definition NameParserTokens.hpp:481
Definition NameParserTokens.hpp:441
Definition NameParserTokens.hpp:254
Definition NameParserTokens.hpp:415
Specs specs
Definition NameParserTokens.hpp:419
Definition NameParserTokens.hpp:376
std::variant< Identifier, FunctionIdentifier > Scope
Definition NameParserTokens.hpp:378
Definition NameParserTokens.hpp:156
@ none
Definition NameParserTokens.hpp:184
Definition NameParserTokens.hpp:517
Definition NameParserTokens.hpp:676
Definition NameParserReductions.hpp:118
constexpr bool try_reduce_as_placeholder_identifier_wrapped(TokenStack &tokenStack)
Definition NameParserReductions.hpp:320
bool try_reduce_as_function(TokenStack &tokenStack)
Definition NameParserReductions.hpp:655
bool try_reduce_as_type(TokenStack &tokenStack)
Definition NameParserReductions.hpp:648
bool try_reduce_as_regular_type(TokenStack &tokenStack)
Definition NameParserReductions.hpp:575
constexpr bool try_reduce_as_template_identifier(TokenStack &tokenStack)
Definition NameParserReductions.hpp:191
MIMICPP_DETAIL_CONSTEXPR_VECTOR bool try_reduce_as_arg_sequence(TokenStack &tokenStack)
Definition NameParserReductions.hpp:161
void reduce_as_conversion_operator_function_identifier(TokenStack &tokenStack)
Definition NameParserReductions.hpp:689
bool try_reduce_as_scope_sequence(TokenStack &tokenStack)
Definition NameParserReductions.hpp:121
bool try_reduce_as_function_identifier(TokenStack &tokenStack)
Definition NameParserReductions.hpp:277
MIMICPP_DETAIL_CONSTEXPR_VECTOR bool try_reduce_as_function_context(TokenStack &tokenStack)
Definition NameParserReductions.hpp:232
bool try_reduce_as_function_ptr(TokenStack &tokenStack)
Definition NameParserReductions.hpp:401
bool try_reduce_as_function_type(TokenStack &tokenStack)
Definition NameParserReductions.hpp:352
constexpr bool is_identifier_prefix(std::span< Token const > const tokenStack) noexcept
Definition NameParserReductions.hpp:306
constexpr Specs & get_or_emplace_specs(TokenStack &tokenStack)
Definition NameParserReductions.hpp:718
bool try_reduce_as_function_ptr_type(TokenStack &tokenStack)
Definition NameParserReductions.hpp:509
Definition NameParser.hpp:27
constexpr void ignore_reserved_identifier(std::span< Token > &tokenStack) noexcept
Definition NameParserReductions.hpp:107
constexpr void remove_suffix(std::span< Token > &tokenStack, std::size_t const count) noexcept
Definition NameParserReductions.hpp:93
std::variant< token::Space, token::OperatorKeyword, token::ScopeResolution, token::ArgSeparator, token::OpeningAngle, token::ClosingAngle, token::OpeningParens, token::ClosingParens, token::OpeningCurly, token::ClosingCurly, token::OpeningBacktick, token::ClosingSingleQuote, token::TypeContext, token::Identifier, token::FunctionIdentifier, token::ScopeSequence, token::ArgSequence, token::FunctionContext, token::FunctionPtr, token::Specs, token::Type, token::Function > Token
Definition NameParserTokens.hpp:649
constexpr bool is_suffix_of(std::span< Token const > const tokenStack) noexcept
Definition NameParserReductions.hpp:51
constexpr auto match_suffix(std::span< Token > const tokenStack) noexcept
Definition NameParserReductions.hpp:61
constexpr void ignore_space(std::span< Token > &tokenStack) noexcept
Definition NameParserReductions.hpp:99
std::vector< Token > TokenStack
Definition NameParserTokens.hpp:673
typename type_list_reverse< TypeList >::type type_list_reverse_t
Definition TypeList.hpp:58
std::basic_string_view< CharT, CharTraitsT > StringViewT
Definition Fwd.hpp:392
Definition NameParserTokens.hpp:472
Definition NameParserTokens.hpp:259
A very basic type-list template.
Definition TypeList.hpp:29
static constexpr std::size_t size
Definition TypeList.hpp:30