Created
April 7, 2025 06:35
-
-
Save sefyan0hack/e448868789403c68b9566eda0db521c6 to your computer and use it in GitHub Desktop.
compile time regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| cmake_minimum_required(VERSION 3.30) | |
| project(reGex LANGUAGES C CXX) | |
| set(CMAKE_CXX_STANDARD 23) | |
| add_subdirectory(ctre) | |
| set(SOURCES | |
| main.cpp | |
| ) | |
| add_executable(${PROJECT_NAME} ${SOURCES}) | |
| target_link_libraries(${PROJECT_NAME} | |
| PRIVATE | |
| ctre # https://github.com/hanickadot/compile-time-regular-expressions.git | |
| ) | |
| target_compile_options(${PROJECT_NAME} PUBLIC "$<$<C_COMPILER_ID:MSVC>:/utf-8>") | |
| target_compile_options(${PROJECT_NAME} PUBLIC "$<$<C_COMPILER_ID:GNU>:-fconstexpr-ops-limit=1000000>") | |
| */ | |
| #include <ctre.hpp> | |
| #include <iostream> | |
| #include <array> | |
| #include <locale> | |
| #include <codecvt> | |
| struct Enum | |
| { | |
| std::basic_string_view<char32_t> code{}, name{}, vs{}; | |
| friend std::ostream& operator << (std::ostream& o, const Enum& a){ | |
| std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter; | |
| std::string codeu = converter.to_bytes(a.code.data(), a.code.data() + a.code.size()); | |
| std::string nameu = converter.to_bytes(a.name.data(), a.name.data() + a.name.size()); | |
| std::string vsu = converter.to_bytes(a.vs.data(), a.vs.data() + a.vs.size()); | |
| return o | |
| << "code: " << codeu << "\n" | |
| << "name: " << nameu << "\n" | |
| << "vs: " << vsu << "\n"; | |
| } | |
| }; | |
| template <ctll::fixed_string Input> | |
| consteval auto parse_enums() { | |
| constexpr auto pattern = ctll::fixed_string{R"(\benum\s+(?:class\s+)?(\w+)\s*(?::\s*\w+)?\s*\{\s*([^}]+?)\s*\}\s*;)"}; | |
| constexpr auto matches = ctre::search_all<pattern>(Input); | |
| constexpr size_t count = [&]() constexpr { | |
| size_t n = 0; | |
| for (auto match : matches) ++n; | |
| return n; | |
| }(); | |
| std::array<Enum, count> arr{}; | |
| size_t i = 0; | |
| for (auto match : matches) { | |
| arr[i++] = Enum{ | |
| .code = match.template get<0>().to_view(), | |
| .name = match.template get<1>().to_view(), | |
| .vs = match.template get<2>().to_view() | |
| }; | |
| } | |
| return arr; | |
| } | |
| constexpr auto input = ctll::fixed_string(R"( | |
| enum Color { Red, Green, Blue }; | |
| enum class Fruit : int { Apple, Banana }; | |
| enum class TrafficLight : int { Red, Yellow = 1, Green }; | |
| enum class Status { Ok, Error = 0x12ff00 }; | |
| enum class GATE : bool { OPEN = true, CLOSE = false }; | |
| )"); | |
| constinit auto r = parse_enums<input>(); | |
| int main() { | |
| for (const auto& e : r) { | |
| std::cout << e << "\n"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment