-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathre_vm.cpp
318 lines (281 loc) · 9.44 KB
/
re_vm.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#ifndef MINI_REGEXP_VM_CPP_
#define MINI_REGEXP_VM_CPP_
#include "re_vm.hpp"
using namespace mini_regexp_vm;
RE_VM::RE_VM() {}
bool RE_VM::vm(const std::string& target, std::vector<ByteCode>& Code, RE_Config& config)
{
vm_init(target, Code);
vm_result_init();
return vm_main(target, Code, config);
}
inline void RE_VM::vm_init(const std::string& target, std::vector<ByteCode>& Code)
{
_code_ip = 0;
_code_len = Code.size();
_target_start_pos = 0;
_target_len = target.length();
_matched_index = 0;
_matched_len = 0;
_sub_matched_start = 0;
_sub_matched_len = 0;
}
bool RE_VM::vm_main(const std::string& target,
std::vector<ByteCode>& Code, RE_Config& config)
{
bool is_accept = false;
while (_target_start_pos < _target_len)
{
vm_stack_init();
_code_ip = 0;
_matched_index = _target_start_pos;
_matched_len = 0;
_sub_matched_start = _sub_matched_len = 0;
is_accept = false;
__repeat:;
while (!is_accept && _code_ip < _code_len)
{
switch (Code[_code_ip].op)
{
case BYTE_CODE::MATCH:
{
if (!vm_match(target, Code, config))
goto __next_loop;
break;
}
case BYTE_CODE::SPLIT: { vm_split(Code); break; }
case BYTE_CODE::REPEAT: { vm_repeat(Code); break; }
case BYTE_CODE::REPEND:
{
if (!Repeat_stack.empty())
{
repeat_stack_t& rs = Repeat_stack.top();
if (--rs.n > 0)
{
_code_ip = rs.ip + 1;
goto __repeat;
}
Repeat_stack.pop();
}
_code_ip++;
break;
}
case BYTE_CODE::ENTER: { vm_enter(); break; }
case BYTE_CODE::LEAVE: { vm_leave(target); break; }
case BYTE_CODE::JMP: { vm_jmp(Code); break; }
case BYTE_CODE::RANGE:
{
if (!vm_range(target, Code))
goto __next_loop;
break;
}
/*--- 零宽断言 start ---*/
case BYTE_CODE::ZERO_WIDTH_ASSERT_ENTER:
ZeroWidthAssert_stack.push(
zero_width_assert_stack_t(
(TOKEN)reinterpret_cast<std::ptrdiff_t>(Code[_code_ip].exp1),
_matched_index, _matched_len));
vm_switch_eval_stack();
_code_ip++;
break;
case BYTE_CODE::ZERO_WIDTH_ASSERT_LEAVE:
{
vm_zero_width_assert(target);
vm_switch_eval_stack();
break;
}
/*--- 零宽断言 end ---*/
case BYTE_CODE::ACCEPT:
_code_ip++;
is_accept = true;
break;
case BYTE_CODE::HALT:
goto __next_loop;
break;
default:
_code_ip++;
break;
}
}
if (is_accept)
{
/* match success */
if (_matched_len > 0)
{
regex_result.count++;
regex_result.matched.push_back(target.substr(_target_start_pos, _matched_len));
}
else
_matched_len = 1;
_target_start_pos += _matched_len;
}
else
{
/* match error */
__next_loop:;
_target_start_pos++;
}
}
return true;
}
inline void RE_VM::vm_stack_init()
{
while (!Split_stack.empty()) Split_stack.pop();
while (!Repeat_stack.empty()) Repeat_stack.pop();
while (!ZeroWidthAssert_stack.empty()) ZeroWidthAssert_stack.pop();
while (!Sub_Split_stack.empty()) Sub_Split_stack.pop();
}
inline void RE_VM::vm_result_init()
{
regex_result.count = 0;
regex_result.sub_matched.clear();
regex_result.matched.clear();
}
inline void RE_VM::vm_switch_eval_stack()
{
/* 清理子栈环境 */
while (!Sub_Split_stack.empty()) Sub_Split_stack.pop();
/* 恢复栈 */
std::swap(Split_stack, Sub_Split_stack);
}
inline void RE_VM::vm_split(std::vector<ByteCode>& Code)
{
auto exp1 = reinterpret_cast<std::ptrdiff_t>(Code[_code_ip].exp1),
exp2 = reinterpret_cast<std::ptrdiff_t>(Code[_code_ip].exp2);
// if (exp1 == _code_ip + exp1 && !Split_stack.empty() && exp2 == Split_stack.top().ip) /* 死循环 */
// {
// std::cout << " infinite loop." << std::endl;
// goto __next_loop;
// }
/* 默认选exp1分支 执行失败则进入exp2分支 */
Split_stack.push(split_stack_t(_code_ip + exp2,
_target_start_pos,
_matched_index, _matched_len,
_sub_matched_start, _sub_matched_len));
_code_ip += exp1;
}
inline bool RE_VM::vm_match(const std::string& target,
std::vector<ByteCode>& Code,
RE_Config& config)
{
/* 匹配超出目标串长度 */
if (_matched_index >= _target_len)
return _vm_backtrack();
auto exp_t = reinterpret_cast<std::ptrdiff_t>(Code[_code_ip].exp1);
/* ANY */
if (exp_t == TOKEN::ANY)
{
if (is_ANY(target[_matched_index], config.DOTALL))
return _vm_match_ok();
else
return _vm_backtrack();
}
/* GROUP */
else if (exp_t == TOKEN::GROUP)
{
auto group_number = reinterpret_cast<std::ptrdiff_t>(Code[_code_ip].exp2);
/* Group */
if (regex_result.sub_matched.size() > group_number - 1)
{
std::string& tmp = regex_result.sub_matched[group_number - 1];
if (target.compare(_matched_index, tmp.length(), tmp) == 0)
return _vm_match_ok(tmp.length());
return _vm_backtrack();
}
/* 八进制转义字符 */
else
{
/* 不打算做了 真的烦 哼(ノ=Д=)ノ┻━┻ */
/* \n \nm \nml */
auto n = std::to_string(group_number);
std::cout << "Not implement!" << std::endl;
}
}
/* normal string */
else
{
std::string s = reinterpret_cast<const char*>(exp_t);
// std::cout << "match: " << target.substr(_matched_index, s.length())
// << " " << s << std::endl;
if (target.compare(_matched_index, s.length(), s) == 0)
return _vm_match_ok(s.length());
return _vm_backtrack();
}
return true;
}
inline bool RE_VM::_vm_match_ok(std::ptrdiff_t n)
{
_matched_index += n;
_matched_len += n;
_code_ip++;
return true;
}
inline bool RE_VM::_vm_backtrack()
{
if (!Split_stack.empty())
{
_code_ip = Split_stack.top().ip;
_target_start_pos = Split_stack.top().target_start_pos;
_matched_index = Split_stack.top().match_index;
_matched_len = Split_stack.top().match_len;
_sub_matched_start = Split_stack.top().sub_match_start;
_sub_matched_len = Split_stack.top().sub_match_len;
Split_stack.pop();
return true;
}
return false;
}
inline void RE_VM::vm_enter()
{
vm_switch_eval_stack();
_sub_matched_start = _matched_index;
_code_ip++;
}
inline void RE_VM::vm_leave(const std::string& target)
{
vm_switch_eval_stack();
_sub_matched_len = _matched_index - _sub_matched_start;
regex_result.sub_matched.push_back(target.substr(_sub_matched_start, _sub_matched_len));
_sub_matched_start = _sub_matched_len = 0;
_code_ip++;
}
inline void RE_VM::vm_jmp(std::vector<ByteCode>& Code)
{
_code_ip += reinterpret_cast<std::ptrdiff_t>(Code[_code_ip].exp1);
}
inline void RE_VM::vm_repeat(std::vector<ByteCode>& Code)
{
Repeat_stack.push(repeat_stack_t(_code_ip, reinterpret_cast<std::ptrdiff_t>(Code[_code_ip].exp1)));
_code_ip++;
}
inline bool RE_VM::vm_range(const std::string& target, std::vector<ByteCode>& Code)
{
auto a = reinterpret_cast<std::ptrdiff_t>(Code[_code_ip].exp1),
b = reinterpret_cast<std::ptrdiff_t>(Code[_code_ip].exp2);
// std::cout << "range: " << target[_matched_index] << std::endl;
if (is_range_in(target[_matched_index], a, b))
return _vm_match_ok();
else
return _vm_backtrack();
}
inline void RE_VM::vm_zero_width_assert(const std::string& target)
{
switch (ZeroWidthAssert_stack.top().tk)
{
case TOKEN::FORWARD_PRE_MATCH: case TOKEN::FORWARD_PRE_MATCH_NOT:
_matched_index = ZeroWidthAssert_stack.top()._matched_index;
_matched_len = ZeroWidthAssert_stack.top()._matched_len;
break;
case TOKEN::BACKWORD_PRE_MATCH: case TOKEN::BACKWORD_PRE_MATCH_NOT:
_target_start_pos += _matched_len - ZeroWidthAssert_stack.top()._matched_len;
_matched_len -= _matched_len - ZeroWidthAssert_stack.top()._matched_len;
break;
case TOKEN::NORMAL_PRE_MATCH: break;
default:
/* ERROR 不应该出现其他类型 */
break;
}
ZeroWidthAssert_stack.pop();
_code_ip++;
}
#endif