xref: /PHP-5.5/ext/ereg/regex/regex2.h (revision aa3eee1d)
1 /*
2  * First, the stuff that ends up in the outside-world include file
3  = #ifdef WIN32
4  = #define API_EXPORT(type)    __declspec(dllexport) type __stdcall
5  = #else
6  = #define API_EXPORT(type)    type
7  = #endif
8  =
9  = typedef off_t regoff_t;
10  = typedef struct {
11  = 	int re_magic;
12  = 	size_t re_nsub;		// number of parenthesized subexpressions
13  = 	const unsigned char *re_endp;	// end pointer for REG_PEND
14  = 	struct re_guts *re_g;	// none of your business :-)
15  = } regex_t;
16  = typedef struct {
17  = 	regoff_t rm_so;		// start of match
18  = 	regoff_t rm_eo;		// end of match
19  = } regmatch_t;
20  */
21 /*
22  * internals of regex_t
23  */
24 #define	MAGIC1	((('r'^0200)<<8) | 'e')
25 
26 /*
27  * The internal representation is a *strip*, a sequence of
28  * operators ending with an endmarker.  (Some terminology etc. is a
29  * historical relic of earlier versions which used multiple strips.)
30  * Certain oddities in the representation are there to permit running
31  * the machinery backwards; in particular, any deviation from sequential
32  * flow must be marked at both its source and its destination.  Some
33  * fine points:
34  *
35  * - OPLUS_ and O_PLUS are *inside* the loop they create.
36  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
37  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
38  *   OOR1 and OOR2 are respectively the end and the beginning of one of
39  *   the branches.  Note that there is an implicit OOR2 following OCH_
40  *   and an implicit OOR1 preceding O_CH.
41  *
42  * In state representations, an operator's bit is on to signify a state
43  * immediately *preceding* "execution" of that operator.
44  */
45 typedef long sop;		/* strip operator */
46 typedef long sopno;
47 #define	OPRMASK	0x7c000000
48 #define	OPDMASK	0x03ffffff
49 #define	OPSHIFT	(26)
50 #define	OP(n)	((n)&OPRMASK)
51 #define	OPND(n)	((n)&OPDMASK)
52 #define	SOP(op, opnd)	((op)|(opnd))
53 /* operators			   meaning	operand			*/
54 /*						(back, fwd are offsets)	*/
55 #define	OEND	(1<<OPSHIFT)	/* endmarker	-			*/
56 #define	OCHAR	(2<<OPSHIFT)	/* character	unsigned char		*/
57 #define	OBOL	(3<<OPSHIFT)	/* left anchor	-			*/
58 #define	OEOL	(4<<OPSHIFT)	/* right anchor	-			*/
59 #define	OANY	(5<<OPSHIFT)	/* .		-			*/
60 #define	OANYOF	(6<<OPSHIFT)	/* [...]	set number		*/
61 #define	OBACK_	(7<<OPSHIFT)	/* begin \d	paren number		*/
62 #define	O_BACK	(8<<OPSHIFT)	/* end \d	paren number		*/
63 #define	OPLUS_	(9<<OPSHIFT)	/* + prefix	fwd to suffix		*/
64 #define	O_PLUS	(10<<OPSHIFT)	/* + suffix	back to prefix		*/
65 #define	OQUEST_	(11<<OPSHIFT)	/* ? prefix	fwd to suffix		*/
66 #define	O_QUEST	(12<<OPSHIFT)	/* ? suffix	back to prefix		*/
67 #define	OLPAREN	(13<<OPSHIFT)	/* (		fwd to )		*/
68 #define	ORPAREN	(14<<OPSHIFT)	/* )		back to (		*/
69 #define	OCH_	(15<<OPSHIFT)	/* begin choice	fwd to OOR2		*/
70 #define	OOR1	(16<<OPSHIFT)	/* | pt. 1	back to OOR1 or OCH_	*/
71 #define	OOR2	(17<<OPSHIFT)	/* | pt. 2	fwd to OOR2 or O_CH	*/
72 #define	O_CH	(18<<OPSHIFT)	/* end choice	back to OOR1		*/
73 #define	OBOW	(19<<OPSHIFT)	/* begin word	-			*/
74 #define	OEOW	(20<<OPSHIFT)	/* end word	-			*/
75 
76 /*
77  * Structure for [] character-set representation.  Character sets are
78  * done as bit vectors, grouped 8 to a byte vector for compactness.
79  * The individual set therefore has both a pointer to the byte vector
80  * and a mask to pick out the relevant bit of each byte.  A hash code
81  * simplifies testing whether two sets could be identical.
82  *
83  * This will get trickier for multicharacter collating elements.  As
84  * preliminary hooks for dealing with such things, we also carry along
85  * a string of multi-character elements, and decide the size of the
86  * vectors at run time.
87  */
88 typedef struct {
89 	uch *ptr;		/* -> uch [csetsize] */
90 	uch mask;		/* bit within array */
91 	uch hash;		/* hash code */
92 	size_t smultis;
93 	unsigned char *multis;		/* -> char[smulti]  ab\0cd\0ef\0\0 */
94 } cset;
95 /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
96 #define	CHadd(cs, c)	((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c))
97 #define	CHsub(cs, c)	((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c))
98 #define	CHIN(cs, c)	((cs)->ptr[(uch)(c)] & (cs)->mask)
99 #define	MCadd(p, cs, cp)	mcadd(p, cs, cp)	/* regcomp() internal fns */
100 #define	MCsub(p, cs, cp)	mcsub(p, cs, cp)
101 #define	MCin(p, cs, cp)	mcin(p, cs, cp)
102 
103 /* stuff for character categories */
104 typedef unsigned char cat_t;
105 
106 /*
107  * main compiled-expression structure
108  */
109 struct re_guts {
110 	int magic;
111 #		define	MAGIC2	((('R'^0200)<<8)|'E')
112 	sop *strip;		/* malloced area for strip */
113 	int csetsize;		/* number of bits in a cset vector */
114 	int ncsets;		/* number of csets in use */
115 	cset *sets;		/* -> cset [ncsets] */
116 	uch *setbits;		/* -> uch[csetsize][ncsets/CHAR_BIT] */
117 	int cflags;		/* copy of regcomp() cflags argument */
118 	sopno nstates;		/* = number of sops */
119 	sopno firststate;	/* the initial OEND (normally 0) */
120 	sopno laststate;	/* the final OEND */
121 	int iflags;		/* internal flags */
122 #		define	USEBOL	01	/* used ^ */
123 #		define	USEEOL	02	/* used $ */
124 #		define	BAD	04	/* something wrong */
125 	int nbol;		/* number of ^ used */
126 	int neol;		/* number of $ used */
127 	int ncategories;	/* how many character categories */
128 	cat_t *categories;	/* ->catspace[-UCHAR_MIN] */
129 	unsigned char *must;		/* match must contain this string */
130 	int mlen;		/* length of must */
131 	size_t nsub;		/* copy of re_nsub */
132 	int backrefs;		/* does it use back references? */
133 	sopno nplus;		/* how deep does it nest +s? */
134 	/* catspace must be last */
135 	cat_t catspace[1];	/* actually [NC] */
136 };
137 
138 /* misc utilities */
139 #define	OUT	(UCHAR_MAX+1)	/* a non-character value */
140 #define	ISWORD(c)	(isalnum(c) || (c) == '_')
141