-
Notifications
You must be signed in to change notification settings - Fork 5
/
mpw-regex.cpp
269 lines (229 loc) · 5.15 KB
/
mpw-regex.cpp
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "mpw-regex.h"
#include "environment.h"
typedef std::string::const_iterator iterator;
namespace {
bool ecma_special(unsigned char c) {
//
switch(c) {
case '|':
case '{':
case '}':
case '(':
case ')':
case '[':
case ']':
case '*':
case '+':
case '^':
case '$':
case '.':
case '\\':
return true;
default:
return false;
}
}
}
mpw_regex::mpw_regex(const std::string &s, bool slash) {
convert_re(s, slash);
}
bool mpw_regex::is_glob(const std::string &s) {
bool esc = false;
for (unsigned char c : s) {
if (esc) {
esc = false;
continue;
}
switch(c) {
case 0xb6:
esc = true;
break;
case '[':
case '?':
case '*':
case '+':
case 0xc7:
case 0xc5:
return true;
default:
break;
}
}
return false;
}
bool mpw_regex::match(const std::string &s, Environment &e) {
std::smatch m;
bool ok = std::regex_match(s, m, re);
if (!ok) return false;
for (int i = 0; i < 10; ++i) {
int index = capture_map[i];
if (index && index < m.size() && m[index].matched) {
std::string v(m[index].first, m[index].second);
std::string k("\xa8");
k += (i + '0');
e.set(k, std::move(v));
}
}
return true;
}
bool mpw_regex::match(const std::string &s) {
return std::regex_match(s, re);
}
// convert a mpw-flavor regex to std::regex flavor regex.
void mpw_regex::convert_re(const std::string &s, bool slash) {
std::string accumulator;
auto iter = s.begin();
auto end = s.end();
if (slash) {
if (iter == end || *iter++ != '/')
throw std::regex_error(std::regex_constants::error_space);
}
iter = convert_re(iter, end, accumulator, slash ? '/' : 0);
if (iter != end) throw std::regex_error(std::regex_constants::error_space);
re = std::regex(accumulator);
if (slash) key = s;
else key = "/" + s + "/";
}
iterator mpw_regex::convert_re(iterator iter, iterator end, std::string &accumulator, unsigned char term) {
while (iter != end) {
unsigned char c = *iter++;
if (c == 0xb6) {
// escape
if (iter == end) throw std::regex_error(std::regex_constants::error_escape);
c = *iter++;
if (ecma_special(c))
accumulator += '\\';
accumulator += c;
continue;
}
if (term && c == term) {
return iter;
}
if (c == '?') {
// match any char
accumulator += '.';
continue;
}
if (c == 0xc5) {
// match any string
accumulator += ".*";
continue;
}
if (c == '[') {
// begin a set
iter = convert_re_set(iter, end, accumulator);
continue;
}
if (c == '(') {
// begin a capture
iter = convert_re_capture(iter, end, accumulator);
continue;
}
if (c == 0xc7) {
// repeat
iter = convert_re_repeat(iter, end, accumulator);
continue;
}
if (c == '+' || c == '*') {
// same meaning
accumulator += c;
continue;
}
if (ecma_special(c)) {
accumulator += '\\';
}
accumulator += c;
}
if (term) throw std::regex_error(std::regex_constants::error_paren);
return iter;
}
iterator mpw_regex::convert_re_repeat(iterator iter, iterator end, std::string &accumulator) {
int min = -1;
int max = -1;
accumulator += "{";
while (iter != end) {
unsigned char c = *iter++;
if (c == 0xc8) {
accumulator += "}";
return iter;
}
if (c != ',' && !isdigit(c)) break;
accumulator += c;
}
throw std::regex_error(std::regex_constants::error_brace);
}
iterator mpw_regex::convert_re_set(iterator iter, iterator end, std::string &accumulator) {
// need extra logic to block character classes.
unsigned char c;
accumulator += "[";
if (iter != end && static_cast<unsigned char>(*iter) == 0xc2) {
accumulator += "^";
++iter;
} else if (iter != end && *iter == '^') {
// leading ^ needs to be escaped.
accumulator += "\\^";
++iter;
}
while (iter != end) {
c = *iter++;
if (c == 0xb6) {
// escape
if (iter == end) throw std::regex_error(std::regex_constants::error_escape);
c = *iter++;
accumulator += '\\';
accumulator += c;
continue;
}
if (c == ']') {
accumulator += "]";
return iter;
}
if (c == '\\') {
accumulator += "\\\\";
continue;
}
accumulator += c;
}
throw std::regex_error(std::regex_constants::error_brack);
}
iterator mpw_regex::convert_re_capture(iterator iter, iterator end, std::string &accumulator) {
/*
* consider: (abc(abc)®1(xyz))®2
* m[1] = (abcabcxyz)
* m[2] = (abc)
* BUT we don't know if it's captured until the ® is parsed.
*/
std::string scratch;
bool capture = false;
int n = -1;
int ecma_index = ++num_captures;
if (iter != end && *iter == '?') {
// leading ? needs to be escaped.
scratch += "\\?";
++iter;
}
iter = convert_re(iter, end, scratch, ')');
// check for capture?
if (iter != end && static_cast<unsigned char>(*iter) == 0xa8) {
++iter;
if (iter == end || !isdigit(*iter))
throw std::regex_error(std::regex_constants::error_badbrace); // eh
n = *iter++ - '0';
capture = true;
}
accumulator += '(';
if (capture) {
/// ummm capture within a capture? backwards?
capture_map[n] = ecma_index;
} else {
accumulator += "?:";
// re-number all sub-captures.
--num_captures;
for (int &index : capture_map) {
if (index >= ecma_index) --index;
}
}
accumulator += scratch;
accumulator += ')';
return iter;
}