summaryrefslogtreecommitdiff
path: root/lib/regex/interp.myr
blob: fc179c008d1a50c59f2199b3806f9ca09145aa80 (plain)
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
use std

use "types.use"

pkg regex =
	const exec	: (re : regex#, str : byte[:] -> std.option(byte[:][:]))
	/*
	FIXME: implement. This should scan for a possible start char in the
		regex and use that to optimize.
	const search	: (re : regex#, str : byte[:] -> std.option(byte[:][:]))
	*/
;;

/* Ugly: for performance. std.option() should be used instead when unions get faster. */
const Zthr = 0 castto(rethread#)

const exec = {re, str
	var thr
	var m

	re.str = str
	re.strp = 0
	thr = run(re)
	if thr != Zthr
		m = getmatches(re, thr)
		cleanup(re)
		-> `std.Some m
	else
		cleanup(re)
		->  `std.None
	;;
}

const cleanup = {re
	var thr, next

	for thr = re.runq; thr != Zthr; thr = next
		next = thr.next
		thrfree(re, thr)
	;;
	for thr = re.expired; thr != Zthr; thr = next
		next = thr.next
		thrfree(re, thr)
	;;
}

const getmatches = {re, thr
	var ret
	var i

	ret = std.slalloc(re.nmatch)
	for i = 0; i < re.nmatch; i++
		if thr.mstart[i] != -1 && thr.mend[i] != -1
			ret[i] = re.str[thr.mstart[i]:thr.mend[i]]
		else
			ret[i] = [][:]
		;;
	;;
	-> ret
}


/* returns a matching thread, or Zthr if no threads matched */
const run = {re
	var i, ip
	var consumed
	var thr
	var states

	states = std.mkbs()
	re.runq = mkthread(re, 0)
	re.runq.mstart = std.slalloc(re.nmatch)
	re.runq.mend = std.slalloc(re.nmatch)
	for i = 0; i < re.nmatch; i++
		re.runq.mstart[i] = -1
		re.runq.mend[i] = -1
	;;
	while re.nthr > 0
		while re.runq != Zthr
			/* set up the next thread */
			thr = re.runq
			re.runq = thr.next

			trace(re, thr, "\nrunning tid={}, ip={}, s[{}]={}\n", thr.tid, thr.ip, re.strp, std.decode(re.str[re.strp:]))
			ip = thr.ip
			consumed = step(re, thr, -1)
			while !consumed
				consumed = step(re, thr, ip)
			;;

			if std.bshas(states, thr.ip)
				die(re, thr, "there can be only one")
			;;

			if thr.dead
				thrfree(re, thr)
			elif thr.matched && re.strp == re.str.len
				-> thr
			elif !thr.matched
				std.bsput(states, thr.ip)
				if re.expired == Zthr
					re.expired = thr
				;;
				if re.expiredtail != Zthr
					re.expiredtail.next = thr
				;;
				re.expiredtail = thr
				thr.next = Zthr

			;;
		;;
		std.bsclear(states)
		trace(re, thr, "switch\n")
		re.runq = re.expired
		re.expired = Zthr
		re.expiredtail = Zthr
		re.strp++
	;;
	-> Zthr
}

/* 
 Steps forward one instruction. Returns true if a byte of input was
 consumed, false otherwise.
*/
const step = {re, thr, curip
	var str
	var mstart
	var mend

	str = re.str
	match re.prog[thr.ip]
	/* Char matching. Consume exactly one byte from the string. */
	| `Ibyte b:
		trace(re, thr, "\t{}:\tByte {} ({})\n", thr.ip, b, b castto(char))
		if !within(re, str)
			die(re, thr, "end of string")
		elif b != str[re.strp]
			die(re, thr, "not right char")
		else
			thr.ip++
			trace(re, thr, "\t\tmatched {} with {}\n", b, str[re.strp])
		;;
	| `Irange (start, end):
		trace(re, thr, "\t{}:\tRange ({}, {}) /* {} - {} */\n", thr.ip, start, end, start castto(char), end castto(char))
		if !within(re, str) || start > str[re.strp] || end < str[re.strp]
			die(re, thr, "bad range")
		else
			thr.ip++
		;;
	/*
	  Non-consuming. All of these return false, and expect step to be
	  called again until exactly one byte is consumed from the string.
	 */
	| `Ibol:
		trace(re, thr, "\t{}:\tBol\n", thr.ip)
		if re.strp == 0 || str[re.strp - 1] == '\n' castto(byte)
			thr.ip++
			-> false
		else
			die(re, thr, "not beginning of line")
		;;
	| `Ieol:
		trace(re, thr, "\t{}:\tEol\n", thr.ip)
		if re.strp == str.len || str[re.strp] == '\n' castto(byte)
			thr.ip++
			-> false
		else
			die(re, thr, "not end of line")
		;;
	/* check for word characters */
	| `Ibow:
		trace(re, thr, "\t{}:\tBow\n", thr.ip)
		if iswordchar(str[re.strp:]) && (re.strp == 0 || !iswordchar(prevchar(str, re.strp)))
			thr.ip++
			-> false
		else
			die(re, thr, "not beginning of word")
		;;
	| `Ieow:
		trace(re, thr, "\t{}:\tEow\n", thr.ip)
		if re.strp == str.len && iswordchar(prevchar(str, re.strp))
			thr.ip++
			-> false
		elif re.strp > 0 && !iswordchar(str[re.strp:]) && iswordchar(prevchar(str, re.strp))
			thr.ip++
			-> false
		else
			die(re, thr, "not end of word")
		;;
	| `Ilbra m:
		trace(re, thr, "\t{}:\tLbra {}\n", thr.ip, m)
		trace(re, thr, "\t\tmatch start = {}\n", re.strp)
		thr.mstart[m] = re.strp
		thr.ip++
		-> false
	| `Irbra m:
		trace(re, thr, "\t{}:\tRbra {}\n", thr.ip, m)
		thr.mend[m] = re.strp
		thr.ip++
		-> false
	| `Ifork (lip, rip):
		trace(re, thr, "\t{}:\tFork ({}, {})\n", thr.ip, lip, rip)
		mstart = std.sldup(thr.mstart)
		mend = std.sldup(thr.mend)
		fork(re, thr, rip, curip, mstart, mend)
		thr.ip = lip
		-> false
	| `Ijmp ip:
		trace(re, thr, "\t{}:\tJmp {}\n", thr.ip, ip)
		thr.ip = ip
		-> false
	| `Imatch id:
		trace(re, thr, "\t{}:\tMatch\n", thr.ip)
		finish(re, thr)
		-> true
	;;
	-> true
}

const fork = {re, thr, ip, curip, mstart, mend
	var thr

	if ip == curip /* loop detection */
		->
	;;
	thr = mkthread(re, ip)
	thr.next = re.runq
	thr.mstart = mstart
	thr.mend = mend
	re.runq = thr
}

const die = {re, thr, msg
        /*
 	  we can have die called on a thread
	  multiple times, eg, if it has a bad
	  range *and* end in a state that another
	  thread is in. We should only decrement
	  the number of threads for that once.
	 */
	trace(re, thr, "\t\tdie {}: {}\n", thr.tid, msg)
        if !thr.dead
		re.nthr--
        ;;
	thr.dead = true
}

const finish = {re, thr
	trace(re, thr, "finish {}\n", thr.tid)
	thr.matched = true
	re.nthr--
}

var nexttid = 0
const mkthread = {re, ip
	var thr : rethread#

	thr = std.alloc()

	thr.next = Zthr

	thr.ip = ip
	thr.tid = nexttid++
	thr.dead = false
	thr.matched = false

	thr.mstart = [][:]
	thr.mend = [][:]

	re.nthr++

	-> thr
}

const thrfree = {re, thr
	trace(re, thr, "\t\tcleanup {}\n", thr.tid)
	std.slfree(thr.mstart)
	std.slfree(thr.mend)
	std.free(thr)
}

const within = {re, str 
	-> re.strp < str.len
}

const trace : (re : regex#, thr : rethread#, msg : byte[:], args : ... -> void) = {re, thr, msg, args
	var ap

	if re.debug
		ap = std.vastart(&args)
		std.putv(msg, &ap)
	;;
}

/* must be called with i >= 1 */
const prevchar = {s, i
	std.assert(i != 0, "prevchar must be called with i >= 1\n")
	i--
	while i != 0 && s[i] >= 0x80
		i--
	;;
	-> s[i:]
}

const iswordchar = {s
	var c

	c = std.decode(s)
	-> std.isalpha(c) || std.isdigit(c) || c == '_'
}