CPP-TOOLBOX
Loading...
Searching...
No Matches
regex_utils.hpp
Go to the documentation of this file.
1#ifndef REGEX_UTILS_HPP
2#define REGEX_UTILS_HPP
3
4#include <regex>
5#include <unordered_map>
6
8
9namespace regex_utils {
10
11inline std::string surround_with(const std::string &s, const std::string &w) { return w + s + w; }
12inline std::string capture(const std::string &s) { return "(" + s + ")"; }
13inline std::string non_capture_optional_group(const std::string &s) { return "(:?" + s + ")?"; }
14inline std::vector<std::string> capture(const std::vector<std::string> &s) {
15 std::vector<std::string> result;
16 result.reserve(s.size());
17 for (const auto &elem : s) {
18 result.push_back(capture(elem));
19 }
20 return result;
21}
22inline std::string character_class(const std::vector<std::string> &chars) {
23 std::string result = "[";
24 for (const auto &ch : chars) {
25 result += ch;
26 }
27 result += "]";
28 return result;
29}
30
31inline std::string negated_character_class(const std::vector<std::string> &chars) {
32 std::string result = "[^";
33 for (const auto &ch : chars) {
34 result += ch;
35 }
36 result += "]";
37 return result;
38}
39
40inline std::string one_or_more(const std::string &character_class) { return character_class + "+"; }
41inline std::string zero_or_more(const std::string &character_class) { return character_class + "*"; }
42
43inline const std::string start_of_line = R"(^)";
44inline const std::string end_of_line = R"($)";
45inline const std::string any_char = R"(.)";
46inline const std::string any_char_greedy = R"(.*)";
47inline const std::string any_char_nongreedy = R"(.*?)";
48
49inline const std::string right_parenthesis = R"(\))";
50inline const std::string left_parenthesis = R"(\‍()";
51
52inline std::string wrap_parentheses(const std::string &s) { return left_parenthesis + s + right_parenthesis; }
53
54inline const std::string ws_char = R"(\s)";
55inline const std::string optional_ws = R"(\s*)";
56inline const std::string one_or_more_ws = R"(\s+)";
57inline const std::string digit = R"(\d)";
58inline const std::string nonzero_digit = R"([1-9])";
59inline const std::string binary_digit = R"([0-1])";
60inline const std::string lowercase_letter = R"([a-z])";
61inline const std::string uppercase_letter = R"([A-Z])";
62inline const std::string letter = R"([A-Za-z])";
63inline const std::string word_char = R"(\w)";
64inline const std::string word = R"(\w+)";
65inline const std::string identifier = R"([A-Za-z_]\w*)";
66
67inline const std::string string_literal(R"("(?:[^"\\]|\\.)*")");
68inline const std::string char_literal(R"('(?:[^'\\]|\\.)')");
69
70inline const std::string optional_ws_comma = surround_with(",", optional_ws);
71
72inline std::string tuple_of(const std::vector<std::string> &regexes) {
75}
76
77inline const std::string int_regex = R"(-?\d+)";
78inline const std::string unsigned_int_regex = R"(\d+)";
79inline const std::string float_regex = R"(-?\d+(?:\.\d+)?)";
80inline const std::string captured_float_regex = capture(float_regex);
81
82inline const std::string float_tuple = surround_with(
85
86inline const std::string captured_float_tuple =
90
91inline const std::string float_triplet =
95
96inline const std::string captured_float_triplet = surround_with(
101
102inline const std::string type_char_class = character_class({word_char, ws_char, ":", "<", ">", "(", ")"});
103inline const std::string type =
105inline std::regex function_signature_re(R"(^\s*([\w:<>()]+(?:\s*[*&])?)\s+(\w+)\s*\‍((.*)\)\s*$)");
111inline const std::string simple_template_type =
113
114// std::string return_type = match[1];
115// std::string func_name = match[2];
116// std::string args_str = match[3];
117
118}; // namespace regex_utils
119
120#endif // REGEX_UTILS_HPP
@ s
Definition input_state.hpp:45
@ w
Definition input_state.hpp:49
Definition regex_utils.hpp:9
const std::string one_or_more_ws
Definition regex_utils.hpp:56
const std::string word_char
Definition regex_utils.hpp:63
const std::string unsigned_int_regex
Definition regex_utils.hpp:78
std::string non_capture_optional_group(const std::string &s)
Definition regex_utils.hpp:13
const std::string float_tuple
Definition regex_utils.hpp:82
const std::string ws_char
Definition regex_utils.hpp:54
const std::string digit
Definition regex_utils.hpp:57
std::string surround_with(const std::string &s, const std::string &w)
Definition regex_utils.hpp:11
const std::string float_regex
Definition regex_utils.hpp:79
std::string tuple_of(const std::vector< std::string > &regexes)
Definition regex_utils.hpp:72
const std::string captured_float_tuple
Definition regex_utils.hpp:86
const std::string left_parenthesis
Definition regex_utils.hpp:50
const std::string lowercase_letter
Definition regex_utils.hpp:60
const std::string start_of_line
Definition regex_utils.hpp:43
const std::string any_char_greedy
Definition regex_utils.hpp:46
const std::string type_char_class
Definition regex_utils.hpp:102
const std::string any_char_nongreedy
Definition regex_utils.hpp:47
const std::string binary_digit
Definition regex_utils.hpp:59
const std::string type
Definition regex_utils.hpp:103
std::string one_or_more(const std::string &character_class)
Definition regex_utils.hpp:40
std::string negated_character_class(const std::vector< std::string > &chars)
Definition regex_utils.hpp:31
const std::string right_parenthesis
Definition regex_utils.hpp:49
const std::string nonzero_digit
Definition regex_utils.hpp:58
std::regex function_signature_re(R"(^\s*([\w:<>()]+(?:\s*[*&])?)\s+(\w+)\s*\‍((.*)\‍)\s*$)")
const std::string letter
Definition regex_utils.hpp:62
std::string function_signature_ree
Definition regex_utils.hpp:106
const std::string end_of_line
Definition regex_utils.hpp:44
const std::string string_literal(R"("(?:[^"\\]|\\.)*")")
const std::string any_char
Definition regex_utils.hpp:45
const std::string char_literal(R"('(?:[^'\\]|\\.)')")
const std::string word
Definition regex_utils.hpp:64
const std::string captured_float_triplet
Definition regex_utils.hpp:96
std::string character_class(const std::vector< std::string > &chars)
Definition regex_utils.hpp:22
const std::string optional_ws
Definition regex_utils.hpp:55
const std::string int_regex
Definition regex_utils.hpp:77
std::string wrap_parentheses(const std::string &s)
Definition regex_utils.hpp:52
const std::string float_triplet
Definition regex_utils.hpp:91
const std::string optional_ws_comma
Definition regex_utils.hpp:70
const std::string uppercase_letter
Definition regex_utils.hpp:61
const std::string identifier
Definition regex_utils.hpp:65
std::string zero_or_more(const std::string &character_class)
Definition regex_utils.hpp:41
const std::string simple_template_type
Definition regex_utils.hpp:111
std::string capture(const std::string &s)
Definition regex_utils.hpp:12
const std::string captured_float_regex
Definition regex_utils.hpp:80
std::string constructor_signature_re
Definition regex_utils.hpp:109
std::string join(const std::vector< std::string > &elements, const std::string &separator)
Join elements into a single string with a separator.
Definition text_utils.cpp:115