├── LICENSE ├── README.md ├── lex.hpp ├── parser.hpp ├── regex.hpp └── util.hpp /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Dinglan Peng 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # static-regex 2 | static-regex is a static regular expression engine that can convert a regular expression to the corresponding DFA at compile time, and execute the DFA at run time / compile time. It is a project to practice using template metaprogramming. 3 | 4 | Example: 5 | 6 | ```c++ 7 | #include 8 | #include "util.hpp" 9 | #include "regex.hpp" 10 | #include "parser.hpp" 11 | 12 | using namespace std; 13 | 14 | int main() { 15 | using Decimal = Regex>, 18 | Digit, 19 | Star, 20 | Option, Star>>, 21 | Option, Option>, Plus>>, 22 | End 23 | >>; 24 | using Decimal2 = ParseRegex<'^', '[', '+', '\\', '-', ']', '?', '[', '0', '-', '9', ']', '+', 25 | '(', '.', '[', '0', '-', '9', ']', '*', ')', '?', '(', '[', 'e', 26 | 'E', ']', '[', '+', '\\', '-', ']', '?', '[', '0', '-', '9', ']', 27 | '+', ')', '?', '$'>; 28 | using Decimal3 = REGEX("^[+\\-]?[0-9]+(.[0-9]*)?([eE][+\\-]?[0-9]+)?$"); 29 | // Same as Decimal and Decimal2, but it's based on a Clang and G++ extension, NOT C++ STANDARD. 30 | cout << Decimal::match("123") << endl; // 1 31 | cout << Decimal::match("abc") << endl; // 0 32 | cout << Decimal::match("1.") << endl; // 1 33 | cout << Decimal::match("-1.1") << endl; // 1 34 | cout << Decimal::match("+1.1e+2") << endl; // 1 35 | cout << Decimal::match("+1.1e-2") << endl; // 1 36 | cout << Decimal::match("+1.1E2") << endl; // 1 37 | static_assert(!Decimal::match("abc")); 38 | static_assert(Decimal::match("+1.1E2")); 39 | return 0; 40 | } 41 | ``` -------------------------------------------------------------------------------- /lex.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "regex.hpp" 3 | #include "util.hpp" 4 | #include "parser.hpp" 5 | 6 | template 7 | class Lexer; 8 | 9 | struct ConstexprToken { 10 | constexpr ConstexprToken() : start(0), length(0), type(0) {} 11 | size_t start; 12 | size_t length; 13 | int type; 14 | }; 15 | 16 | template 17 | struct StaticString { 18 | static constexpr char String[] = {C..., '\0'}; 19 | }; 20 | 21 | template 22 | StaticString operator ""_staticstring(); 23 | 24 | #define STATICSTRING(r) decltype(r##_staticstring) 25 | 26 | template