forked from hsutter/cppfront
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpure2-regex_05_classes.cpp2
More file actions
211 lines (186 loc) · 6.28 KB
/
pure2-regex_05_classes.cpp2
File metadata and controls
211 lines (186 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
create_result: (resultExpr: std::string, r) -> std::string = {
result: std::string = "";
get_next := :(iter) -> _ = {
start := std::distance(resultExpr&$*.cbegin(), iter);
firstDollar := resultExpr&$*.find("$", start);
firstAt := resultExpr&$*.find("@", start);
end := std::min(firstDollar, firstAt);
if end != std::string::npos {
return resultExpr&$*.cbegin() + end;
}
else {
return resultExpr&$*.cend();
}
};
extract_group_and_advance := :(inout iter) -> _ = {
start := iter;
while std::isdigit(iter*) next iter++ {}
return std::stoi(std::string(start, iter));
};
extract_until := :(inout iter, to: char) -> _ = {
start := iter;
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)
return std::string(start, iter);
};
iter := resultExpr.begin();
while iter != resultExpr.end() {
next := get_next(iter);
if next != iter {
result += std::string(iter, next);
}
if next != resultExpr.end() {
if next* == '$' {
next++;
if next* == '&' {
next++;
result += r.group(0);
}
else if next* == '-' || next* == '+' {
is_start := next* == '-';
next++;
if next* == '{' {
next++; // Skip {
group := extract_until(next, '}');
next++; // Skip }
result += r.group(group);
}
else if next* == '[' {
next++; // Skip [
group := extract_group_and_advance(next);
next++; // Skip ]
if is_start {
result += std::to_string(r.group_start(group));
}
else {
result += std::to_string(r.group_end(group));
}
}
else {
// Return max group
result += r.group(r.group_number() - 1);
}
}
else if std::isdigit(next*) {
group := extract_group_and_advance(next);
result += r.group(group);
}
else {
std::cerr << "Not implemented";
}
}
else if next* == '@' {
next++;
if next* == '-' || next* == '+' {
i := 0;
while i < cpp2::unsafe_narrow<int>(r.group_number()) next i++ {
pos := 0;
if next* == '-' {
pos = r.group_start(i);
}
else {
pos = r.group_end(i);
}
result += std::to_string(pos);
}
}
else {
std::cerr << "Not implemented";
}
}
else {
std::cerr << "Not implemented.";
}
}
iter = next;
}
return result;
}
sanitize: (copy str: std::string) -> std::string =
{
str = cpp2::string_util::replace_all(str, "\a", "\\a");
str = cpp2::string_util::replace_all(str, "\f", "\\f");
str = cpp2::string_util::replace_all(str, "\x1b", "\\e");
str = cpp2::string_util::replace_all(str, "\n", "\\n");
str = cpp2::string_util::replace_all(str, "\r", "\\r");
str = cpp2::string_util::replace_all(str, "\t", "\\t");
return str;
}
test: <M> (regex: M, id: std::string, regex_str: std::string, str: std::string, kind: std::string, resultExpr: std::string,
resultExpected: std::string) = {
warning: std::string = "";
if regex.to_string() != regex_str {
warning = "Warning: Parsed regex does not match.";
}
status: std::string = "OK";
r := regex.search(str);
if "y" == kind || "yM" == kind || "yS" == kind || "yB" == kind {
if !r.matched {
status = "Failure: Regex should apply.";
}
else {
// Have a match check the result
result := create_result(resultExpr, r);
if result != resultExpected {
status = "Failure: Result is wrong. (is: (sanitize(result))$)";
}
}
}
else if "n" == kind {
if r.matched {
status = "Failure: Regex should not apply. Result is '(r.group(0))$'";
}
} else {
status = "Unknown kind '(kind)$'";
}
if !warning.empty() {
warning += " ";
}
std::cout << "(id)$_(kind)$: (status)$ (warning)$regex: (regex_str)$ parsed_regex: (regex.to_string())$ str: (sanitize(str))$ result_expr: (resultExpr)$ expected_results (sanitize(resultExpected))$" << std::endl;
}
test_tests_05_classes: @regex type = {
regex_01 := R"(a[bc]d)";
regex_02 := R"(a[bc]d)";
regex_03 := R"(a[b]d)";
regex_04 := R"([a][b][d])";
regex_05 := R"(.[b].)";
regex_06 := R"(.[b].)";
regex_07 := R"(a[b-d]e)";
regex_08 := R"(a[b-d]e)";
regex_09 := R"(a[b-d])";
regex_10 := R"(a[-b])";
regex_11 := R"(a[b-])";
regex_12 := R"(a])";
regex_13 := R"(a[]]b)";
regex_14 := R"(a[^bc]d)";
regex_15 := R"(a[^bc]d)";
regex_16 := R"(a[^-b]c)";
regex_17 := R"(a[^-b]c)";
regex_18 := R"(a[^]b]c)";
regex_19 := R"(a[^]b]c)";
run: (this) = {
std::cout << "Running tests_05_classes:"<< std::endl;
test(regex_01, "01", R"(a[bc]d)", "abc", "n", R"(-)", "-");
test(regex_02, "02", R"(a[bc]d)", "abd", "y", R"($&)", "abd");
test(regex_03, "03", R"(a[b]d)", "abd", "y", R"($&)", "abd");
test(regex_04, "04", R"([a][b][d])", "abd", "y", R"($&)", "abd");
test(regex_05, "05", R"(.[b].)", "abd", "y", R"($&)", "abd");
test(regex_06, "06", R"(.[b].)", "aBd", "n", R"(-)", "-");
test(regex_07, "07", R"(a[b-d]e)", "abd", "n", R"(-)", "-");
test(regex_08, "08", R"(a[b-d]e)", "ace", "y", R"($&)", "ace");
test(regex_09, "09", R"(a[b-d])", "aac", "y", R"($&)", "ac");
test(regex_10, "10", R"(a[-b])", "a-", "y", R"($&)", "a-");
test(regex_11, "11", R"(a[b-])", "a-", "y", R"($&)", "a-");
test(regex_12, "12", R"(a])", "a]", "y", R"($&)", "a]");
test(regex_13, "13", R"(a[]]b)", "a]b", "y", R"($&)", "a]b");
test(regex_14, "14", R"(a[^bc]d)", "aed", "y", R"($&)", "aed");
test(regex_15, "15", R"(a[^bc]d)", "abd", "n", R"(-)", "-");
test(regex_16, "16", R"(a[^-b]c)", "adc", "y", R"($&)", "adc");
test(regex_17, "17", R"(a[^-b]c)", "a-c", "n", R"(-)", "-");
test(regex_18, "18", R"(a[^]b]c)", "a]c", "n", R"(-)", "-");
test(regex_19, "19", R"(a[^]b]c)", "adc", "y", R"($&)", "adc");
std::cout << std::endl;
}
}
main: () = {
test_tests_05_classes().run();
}