-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex_core1.v
277 lines (241 loc) · 5.2 KB
/
regex_core1.v
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
module regex
// get_char get a char from position i and return an u32 with the unicode code
[direct_array_access; inline]
fn get_char(in_txt string, i int) (u32, int) {
ini := unsafe { in_txt.str[i] }
// ascii 8 bit
if /*(re.flag & regex.f_bin) != 0 ||*/ ini & 0x80 == 0 {
return u32(ini), 1
}
// unicode char
char_len := utf8util_char_len(ini)
mut tmp := 0
mut ch := u32(0)
for tmp < char_len {
ch = (ch << 8) | unsafe { in_txt.str[i + tmp] }
tmp++
}
return ch, char_len
}
const (
// max 65355 groups
group_start = 0x0001_0000
group_end = 0x0002_0000
concatenation = 0x0004_0000
postfix_or = 0x0008_0000
)
struct Paren{
mut:
nalt i32
natom i32
}
struct PostfixToken {
mut:
ch rune
fcode u32
}
pub
fn (re REcontext) re2post(in_re_str string) []PostfixToken {
mut buf := []PostfixToken
mut c := 0
mut nalt := 0
mut natom := 0
mut paren := []Paren{len:in_re_str.len}
mut p_index := 0
mut group_count := u32(0)
for c < in_re_str.len {
ch, ch_len := get_char(in_re_str, c)
match u8(ch) {
`|` {
if natom == 0 {
return []PostfixToken
}
natom--
for natom > 0 {
buf << PostfixToken{ch:`.`, fcode: concatenation}
natom--
}
nalt++
}
`(` {
group_count++
if natom > 1 {
natom--
buf << PostfixToken{ch:`.`, fcode: concatenation }
}
buf << PostfixToken{ch:`(`, fcode: group_start | group_count}
paren[p_index].nalt = nalt
paren[p_index].natom = natom
p_index++
nalt = 0
natom = 0
}
`)` {
if p_index == 0 {
return []PostfixToken
}
if natom == 0 {
return []PostfixToken
}
natom--
for natom > 0 {
buf << PostfixToken{ch:`.`, fcode: concatenation | group_end | group_count}
natom--
}
for nalt > 0 {
buf << PostfixToken{ch:`|`, fcode: postfix_or | group_end | group_count}
nalt--
}
buf << PostfixToken{ch:`)`, fcode: group_end | group_count}
p_index--
nalt = paren[p_index].nalt
natom = paren[p_index].natom
natom++
group_count--
}
`+`,`*`,`?` {
if natom == 0 {
return []PostfixToken
}
buf << PostfixToken{ch:ch}
}
// standard rune
else {
// println("ch: ${ch:c}")
if natom > 1 {
natom--
buf << PostfixToken{ch:`.`, fcode: concatenation}
}
buf << PostfixToken{ch:ch}
natom++
}
}
c += ch_len
}
natom--
for natom > 0 {
buf << PostfixToken{ch:`.`, fcode: concatenation}
natom--
}
println("nalt: ${nalt}")
for nalt > 0 {
buf << PostfixToken{ch:`|`, fcode: postfix_or }
nalt--
}
return buf
}
pub fn (re REcontext) print_runes(in_rune_list []PostfixToken) {
for x in in_rune_list {
print(utf8_str(x.ch))
/*
if x.fcode & 0xFFFF_0000 > 0 {
print("[${x.fcode >> 16:04X}|${x.fcode&0xFFF:02}]")
}
*/
}
println("")
}
/*
*
* State
*
*/
const (
re_match = 0x0001
re_split = 0x0002
)
[heap]
struct MatchState {
mut:
ch rune
fcode u32
out0 int = -1
out1 int = -1
last_list int
list_index int = -1
}
pub
struct REcontext {
mut:
nstate int
frag_stack [10]int
frag_stack_index int
state_list []MatchState
}
fn (mut re REcontext) patch(index_e1 int, index_e2 int) {
mut i := index_e1
for re.state_list[i].out0 != -1 {
i = re.state_list[i].out0
}
re.state_list[i].out0 = index_e2
}
fn (mut re REcontext) append(index_e1 int, index_e2 int) {
mut i := index_e1
for re.state_list[i].out0 != -1 {
i = re.state_list[i].out0
}
}
//
// Convert postfix regular expression to NFA.
// Return start state.
//
pub
fn (mut re REcontext) post_to_nfa(ps_list []PostfixToken) {
unsafe {
mut s := &MatchState(nil)
for p in ps_list {
// . concatenation
if (p.fcode & concatenation) > 0 {
re.frag_stack_index--
ind_e2 := re.frag_stack[re.frag_stack_index]
re.frag_stack_index--
ind_e1 := re.frag_stack[re.frag_stack_index]
re.patch(ind_e1, ind_e2)
re.frag_stack[re.frag_stack_index] = re.state_list[ind_e2].list_index
re.frag_stack_index++
println("list concat => ${re.frag_stack} index: ${re.frag_stack_index}")
} else
// | operator
if (p.fcode & postfix_or) > 0 {
re.frag_stack_index--
ind_e2 := re.frag_stack[re.frag_stack_index]
re.frag_stack_index--
ind_e1 := re.frag_stack[re.frag_stack_index]
re.nstate++
re.state_list << MatchState{
fcode: re_split,
list_index: re.state_list.len
out0: ind_e1,
out1: ind_e2
}
re.frag_stack[re.frag_stack_index] = re.state_list.len - 1
re.frag_stack_index++
println("list OR => ${re.frag_stack} index: ${re.frag_stack_index}")
} else
if p.fcode == 0 {
re.nstate++
re.state_list << MatchState{
ch: p.ch,
list_index: re.state_list.len
}
re.frag_stack[re.frag_stack_index] = re.state_list.len - 1
re.frag_stack_index++
println("list ch => ${re.frag_stack} index: ${re.frag_stack_index}")
}
}
// Add the end match state
re.nstate++
re.state_list << MatchState{
fcode: re_match,
list_index: re.state_list.len
}
re.frag_stack[re.frag_stack_index] = re.state_list.len - 1
re.frag_stack_index++
re.frag_stack_index--
ind_e2 := re.frag_stack[re.frag_stack_index]
re.frag_stack_index--
ind_e1 := re.frag_stack[re.frag_stack_index]
re.patch(ind_e1, ind_e2)
println(re.state_list)
}
}