|
| 1 | +create_result: (resultExpr: std::string, r) -> std::string = { |
| 2 | + result: std::string = ""; |
| 3 | + |
| 4 | + get_next := :(iter) -> _ = { |
| 5 | + start := std::distance(resultExpr&$*.cbegin(), iter); |
| 6 | + firstDollar := resultExpr&$*.find("$", start); |
| 7 | + firstAt := resultExpr&$*.find("@", start); |
| 8 | + |
| 9 | + end := std::min(firstDollar, firstAt); |
| 10 | + if end != std::string::npos { |
| 11 | + return resultExpr&$*.cbegin() + end; |
| 12 | + } |
| 13 | + else { |
| 14 | + return resultExpr&$*.cend(); |
| 15 | + } |
| 16 | + }; |
| 17 | + extract_group_and_advance := :(inout iter) -> _ = { |
| 18 | + start := iter; |
| 19 | + |
| 20 | + while std::isdigit(iter*) next iter++ {} |
| 21 | + |
| 22 | + return std::stoi(std::string(start, iter)); |
| 23 | + }; |
| 24 | + extract_until := :(inout iter, to: char) -> _ = { |
| 25 | + start := iter; |
| 26 | + |
| 27 | + while (to != iter*) next iter++ {} // TODO: Without bracket: error: postfix unary * (dereference) cannot be immediately followed by a (, identifier, or literal - add whitespace before * here if you meant binary * (multiplication) |
| 28 | + |
| 29 | + return std::string(start, iter); |
| 30 | + }; |
| 31 | + |
| 32 | + iter := resultExpr.begin(); |
| 33 | + |
| 34 | + while iter != resultExpr.end() { |
| 35 | + next := get_next(iter); |
| 36 | + |
| 37 | + if next != iter { |
| 38 | + result += std::string(iter, next); |
| 39 | + } |
| 40 | + if next != resultExpr.end() { |
| 41 | + if next* == '$' { |
| 42 | + next++; |
| 43 | + |
| 44 | + if next* == '&' { |
| 45 | + next++; |
| 46 | + result += r.group(0); |
| 47 | + } |
| 48 | + else if next* == '-' || next* == '+' { |
| 49 | + is_start := next* == '-'; |
| 50 | + next++; |
| 51 | + if next* == '{' { |
| 52 | + next++; // Skip { |
| 53 | + group := extract_until(next, '}'); |
| 54 | + next++; // Skip } |
| 55 | + result += r.group(group); |
| 56 | + } |
| 57 | + else if next* == '[' { |
| 58 | + next++; // Skip [ |
| 59 | + group := extract_group_and_advance(next); |
| 60 | + next++; // Skip ] |
| 61 | + |
| 62 | + if is_start { |
| 63 | + result += std::to_string(r.group_start(group)); |
| 64 | + } |
| 65 | + else { |
| 66 | + result += std::to_string(r.group_end(group)); |
| 67 | + } |
| 68 | + } |
| 69 | + else { |
| 70 | + // Return max group |
| 71 | + result += r.group(r.group_number() - 1); |
| 72 | + } |
| 73 | + } |
| 74 | + else if std::isdigit(next*) { |
| 75 | + group := extract_group_and_advance(next); |
| 76 | + result += r.group(group); |
| 77 | + } |
| 78 | + else { |
| 79 | + std::cerr << "Not implemented"; |
| 80 | + } |
| 81 | + } |
| 82 | + else if next* == '@' { |
| 83 | + next++; |
| 84 | + |
| 85 | + if next* == '-' || next* == '+' { |
| 86 | + i := 0; |
| 87 | + while i < cpp2::unsafe_narrow<int>(r.group_number()) next i++ { |
| 88 | + pos := 0; |
| 89 | + if next* == '-' { |
| 90 | + pos = r.group_start(i); |
| 91 | + } |
| 92 | + else { |
| 93 | + pos = r.group_end(i); |
| 94 | + } |
| 95 | + result += std::to_string(pos); |
| 96 | + } |
| 97 | + } |
| 98 | + else { |
| 99 | + std::cerr << "Not implemented"; |
| 100 | + } |
| 101 | + } |
| 102 | + else { |
| 103 | + std::cerr << "Not implemented."; |
| 104 | + } |
| 105 | + } |
| 106 | + iter = next; |
| 107 | + } |
| 108 | + |
| 109 | + return result; |
| 110 | +} |
| 111 | + |
| 112 | +test: <M> (regex: M, id: std::string, regex_str: std::string, str: std::string, kind: std::string, resultExpr: std::string, |
| 113 | + resultExpected: std::string) = { |
| 114 | + |
| 115 | + warning: std::string = ""; |
| 116 | + if regex.to_string() != regex_str { |
| 117 | + warning = "Warning: Parsed regex does not match."; |
| 118 | + } |
| 119 | + |
| 120 | + status: std::string = "OK"; |
| 121 | + |
| 122 | + r := regex.search(str); |
| 123 | + |
| 124 | + if "y" == kind || "yM" == kind || "yS" == kind || "yB" == kind { |
| 125 | + if !r.matched { |
| 126 | + status = "Failure: Regex should apply."; |
| 127 | + } |
| 128 | + else { |
| 129 | + // Have a match check the result |
| 130 | + |
| 131 | + result := create_result(resultExpr, r); |
| 132 | + |
| 133 | + if result != resultExpected { |
| 134 | + status = "Failure: Result is wrong. (is: (result)$)"; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + else if "n" == kind { |
| 139 | + if r.matched { |
| 140 | + status = "Failure: Regex should not apply. Result is '(r.group(0))$'"; |
| 141 | + } |
| 142 | + } else { |
| 143 | + status = "Unknown kind '(kind)$'"; |
| 144 | + } |
| 145 | + |
| 146 | + if !warning.empty() { |
| 147 | + warning += " "; |
| 148 | + } |
| 149 | + std::cout << "(id)$_(kind)$: (status)$ (warning)$regex: (regex_str)$ parsed_regex: (regex.to_string())$ str: (str)$ result_expr: (resultExpr)$ expected_results (resultExpected)$" << std::endl; |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | +test_tests_01_char_matcher: @regex type = { |
| 154 | + regex_01 := R"(abc)"; |
| 155 | + regex_02 := R"(abc)"; |
| 156 | + regex_03 := R"(abc)"; |
| 157 | + regex_04 := R"(abc)"; |
| 158 | + regex_05 := R"(abc)"; |
| 159 | + regex_06 := R"(abc)"; |
| 160 | + regex_07 := R"(abc)"; |
| 161 | + regex_08 := R"(abc)"; |
| 162 | + regex_09 := R"(abc)"; |
| 163 | + regex_10 := R"(abc)"; |
| 164 | + regex_11 := R"(abc)"; |
| 165 | + regex_12 := R"(abc)"; |
| 166 | + run: (this) = { |
| 167 | + std::cout << "Running tests_01_char_matcher:"<< std::endl; |
| 168 | + test(regex_01, "01", R"(abc)", "abc", "y", R"($&)", "abc"); |
| 169 | + test(regex_02, "02", R"(abc)", "abc", "y", R"($-[0])", "0"); |
| 170 | + test(regex_03, "03", R"(abc)", "abc", "y", R"($+[0])", "3"); |
| 171 | + test(regex_04, "04", R"(abc)", "xbc", "n", R"(-)", "-"); |
| 172 | + test(regex_05, "05", R"(abc)", "axc", "n", R"(-)", "-"); |
| 173 | + test(regex_06, "06", R"(abc)", "abx", "n", R"(-)", "-"); |
| 174 | + test(regex_07, "07", R"(abc)", "xabcy", "y", R"($&)", "abc"); |
| 175 | + test(regex_08, "08", R"(abc)", "xabcy", "y", R"($-[0])", "1"); |
| 176 | + test(regex_09, "09", R"(abc)", "xabcy", "y", R"($+[0])", "4"); |
| 177 | + test(regex_10, "10", R"(abc)", "ababc", "y", R"($&)", "abc"); |
| 178 | + test(regex_11, "11", R"(abc)", "ababc", "y", R"($-[0])", "2"); |
| 179 | + test(regex_12, "12", R"(abc)", "ababc", "y", R"($+[0])", "5"); |
| 180 | + std::cout << std::endl; |
| 181 | + } |
| 182 | +} |
| 183 | +main: () = { |
| 184 | + test_tests_01_char_matcher().run(); |
| 185 | +} |
0 commit comments