From 90350e8584b56e75677921a4b3e4b3c2de97e64c Mon Sep 17 00:00:00 2001 From: Brett Date: Sat, 29 Jul 2023 13:38:19 -0400 Subject: [PATCH] parse --- CMakeLists.txt | 8 +++ include/blt/nbt/nbt.h | 25 +++++-- include/blt/parse/argparse.h | 135 +++++++++++++++++++++++++++++------ src/blt/parse/argparse.cpp | 11 ++- 4 files changed, 153 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eb69df3..a1b11a6 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,10 @@ else() set(PARSE_FILES "") endif() +if(EXISTS libraries/parallel-hashmap) + include_directories(libraries/parallel-hashmap) +endif() + #include zlib if the user has it. find_package(ZLIB QUIET) @@ -69,6 +73,10 @@ target_include_directories(BLT PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/config/) if(${ZLIB_FOUND}) target_link_libraries(BLT PUBLIC ZLIB::ZLIB) endif() +if(EXISTS ${CMAKE_SOURCE_DIR}/libraries/parallel-hashmap) + message("Including phmap") + target_include_directories(BLT PUBLIC libraries/parallel-hashmap) +endif() if(MSVC) #target_compile_options(BLT PRIVATE /W4) diff --git a/include/blt/nbt/nbt.h b/include/blt/nbt/nbt.h index 5c8a4d5..234b7ed 100755 --- a/include/blt/nbt/nbt.h +++ b/include/blt/nbt/nbt.h @@ -17,12 +17,29 @@ #include "blt/std/filesystem.h" #include "blt/std/logging.h" -namespace blt::nbt { #ifndef HASHMAP - #define HASHMAP HASHMAP - template - using HASHMAP = std::unordered_map; + #if defined __has_include && __has_include() + #define HASHMAP HASHMAP + #include + #include + template + using HASHMAP = phmap::flat_hash_map(); + template + using HASHSET = phmap::flat_hash_set(); + #else + #define HASHMAP HASHMAP + #include + #include + + template + using HASHMAP = std::unordered_map(); + + template + using HASHSET = std::unordered_set(); + #endif #endif + +namespace blt::nbt { void writeUTF8String(blt::fs::block_writer& stream, const std::string& str); diff --git a/include/blt/parse/argparse.h b/include/blt/parse/argparse.h index 8669099..a384a4e 100644 --- a/include/blt/parse/argparse.h +++ b/include/blt/parse/argparse.h @@ -11,27 +11,44 @@ #include #include #include -#include - -namespace blt::parser { #ifndef HASHMAP - #define HASHMAP HASHMAP - template - using HASHMAP = std::unordered_map(); + #if defined __has_include && __has_include() + #define HASHMAP HASHMAP + + #include + #include + +template +using HASHMAP = phmap::flat_hash_map(); +template +using HASHSET = phmap::flat_hash_set(); + #else + #define HASHMAP HASHMAP + #include + #include + +template +using HASHMAP = std::unordered_map(); + +template +using HASHSET = std::unordered_set(); + #endif #endif +namespace blt::parser { + enum class arg_action { - STORE, - STORE_CONST, - STORE_TRUE, - STORE_FALSE, - APPEND, - APPEND_CONST, - COUNT, - HELP, - VERSION, - EXTEND + STORE, + STORE_CONST, + STORE_TRUE, + STORE_FALSE, + APPEND, + APPEND_CONST, + COUNT, + HELP, + VERSION, + EXTEND }; class arg_t { @@ -47,22 +64,31 @@ namespace blt::parser { std::vector flags; void insertAndSort(const std::string& str); + public: arg_vector() = default; - arg_vector(std::vector args); + + arg_vector(const std::vector& args); + arg_vector(std::initializer_list args); + arg_vector(const std::string& arg); + arg_vector(const char* arg); arg_vector& operator=(const std::string& arg); + arg_vector& operator=(const char* arg); + arg_vector& operator=(std::initializer_list& args); + arg_vector& operator=(std::vector& args); - [[nodiscard]] inline std::vector& getNames(){ + [[nodiscard]] inline std::vector& getNames() { return names; } - [[nodiscard]] inline std::vector& getFlags(){ + + [[nodiscard]] inline std::vector& getFlags() { return flags; } }; @@ -74,20 +100,30 @@ namespace blt::parser { static constexpr int ALL_REQUIRED = 0x4; int args = 0; int flags = 0; + void decode(char c); + public: arg_nargs() = default; + arg_nargs(int args): args(args) {} + arg_nargs(char c); + arg_nargs(std::string s); + arg_nargs(const char* s); + arg_nargs& operator=(const std::string& s); + arg_nargs& operator=(const char* s); + arg_nargs& operator=(char c); + arg_nargs& operator=(int args); }; - struct args_properties { + struct arg_properties { private: public: arg_vector a_flags; @@ -101,12 +137,69 @@ namespace blt::parser { bool a_required = false; }; - class argparse { + class arg_tokenizer { + private: + static constexpr char FLAG = '-'; + std::vector args; + size_t nextIndex = 0; + + inline const std::string& get(size_t i){ + return args[i]; + } + inline const std::string& next(size_t& i){ + return args[i++]; + } + inline bool hasNext(size_t i){ + return (size_t)i < args.size(); + } + inline bool isFlag(size_t i){ + return get(i).starts_with(FLAG); + } + public: + arg_tokenizer() = default; + + arg_tokenizer(const char** argv, size_t argc); + + inline void forward() { + nextIndex++; + } + + inline const std::string& get() { + return get(nextIndex); + } + + inline const std::string& next() { + return next(nextIndex); + } + + inline bool hasNext() { + return hasNext(nextIndex); + } + + inline bool isFlag() { + return isFlag(nextIndex); + } + + inline bool isValue() { + return isValue(nextIndex); + } + }; + + struct arg_results { private: public: + + }; + + class argparse { + private: + arg_tokenizer tokenizer; + public: argparse() = default; + void addArgument(const arg_properties& args); + }; } diff --git a/src/blt/parse/argparse.cpp b/src/blt/parse/argparse.cpp index b535e6d..95b7c44 100644 --- a/src/blt/parse/argparse.cpp +++ b/src/blt/parse/argparse.cpp @@ -7,7 +7,7 @@ namespace blt::parser { - arg_vector::arg_vector(std::vector args) { + arg_vector::arg_vector(const std::vector& args) { for (auto& arg : args) insertAndSort(arg); } @@ -94,4 +94,13 @@ namespace blt::parser { if (c == '*') flags |= ALL; } + + arg_tokenizer::arg_tokenizer(const char** argv, size_t argc) { + for (size_t i = 0; i < argc; i++) + args.emplace_back(argv[i]); + } + + void argparse::addArgument(const arg_properties& args) { + + } } \ No newline at end of file