summaryrefslogtreecommitdiff
path: root/lib/regex/types.myr
blob: b283d2ab150dad0f27f74d52a2bdfceed490fb3f (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
use std

pkg regex =
	type status = union
		`Noimpl
		`Incomplete
		`Unbalanced
		`Emptyparen
		`Badrep
		`Badrange
		`Badescape
	;;

	type ast = union
		/* basic string building */
		`Alt	(ast#, ast#)
		`Cat	(ast#, ast#)

		/* repetition */
		`Star	ast#
		`Rstar  ast#
		`Plus	ast#
		`Rplus	ast#
		`Quest	ast#	

		/* end matches */
		`Chr	char
		`Ranges	char[2][:]

		/* meta */
		`Cap	(std.size, ast#) /* id, ast */
		`Bol	/* beginning of line */
		`Eol	/* end of line */
		`Bow	/* beginning of word */
		`Eow	/* end of word */
	;;


	type regex = struct
		/* compile state */
		debug	: bool
		pat	: byte[:]
		nmatch	: std.size

		/* VM state */
		runq	: rethread#
		expired	: rethread#
		expiredtail	: rethread#
		proglen	: std.size
		prog	: reinst[:]
		nthr	: std.size
		str	: byte[:]
		strp	: std.size
	;;

	type rethread = struct
		next	: rethread#	/* run queue link */

		tid	: std.size	/* just for debugging */
		ip	: std.size	/* the instruction pointer */
		dead	: bool		/* thread died */
		matched	: bool		/* thread matched */

		mstart	: std.size[:]	/* match starts */
		mend	: std.size[:]	/* match ends */
	;;

	pkglocal type reinst = union
		/* direct consumers */
		`Ibyte	byte
		`Irange	(byte, byte)

		/* groups */
		`Ilbra	std.size
		`Irbra	std.size

		/* anchors */
		`Ibol
		`Ieol
		`Ibow
		`Ieow

		/* control flow */
		`Ifork	(std.size, std.size)
		`Ijmp	std.size
		`Imatch	std.size
	;;
;;