xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/Support/regex2.h (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1*7330f729Sjoerg /*-
2*7330f729Sjoerg  * This code is derived from OpenBSD's libc/regex, original license follows:
3*7330f729Sjoerg  *
4*7330f729Sjoerg  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5*7330f729Sjoerg  * Copyright (c) 1992, 1993, 1994
6*7330f729Sjoerg  *	The Regents of the University of California.  All rights reserved.
7*7330f729Sjoerg  *
8*7330f729Sjoerg  * This code is derived from software contributed to Berkeley by
9*7330f729Sjoerg  * Henry Spencer.
10*7330f729Sjoerg  *
11*7330f729Sjoerg  * Redistribution and use in source and binary forms, with or without
12*7330f729Sjoerg  * modification, are permitted provided that the following conditions
13*7330f729Sjoerg  * are met:
14*7330f729Sjoerg  * 1. Redistributions of source code must retain the above copyright
15*7330f729Sjoerg  *    notice, this list of conditions and the following disclaimer.
16*7330f729Sjoerg  * 2. Redistributions in binary form must reproduce the above copyright
17*7330f729Sjoerg  *    notice, this list of conditions and the following disclaimer in the
18*7330f729Sjoerg  *    documentation and/or other materials provided with the distribution.
19*7330f729Sjoerg  * 3. Neither the name of the University nor the names of its contributors
20*7330f729Sjoerg  *    may be used to endorse or promote products derived from this software
21*7330f729Sjoerg  *    without specific prior written permission.
22*7330f729Sjoerg  *
23*7330f729Sjoerg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*7330f729Sjoerg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*7330f729Sjoerg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*7330f729Sjoerg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*7330f729Sjoerg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*7330f729Sjoerg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*7330f729Sjoerg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*7330f729Sjoerg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*7330f729Sjoerg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*7330f729Sjoerg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*7330f729Sjoerg  * SUCH DAMAGE.
34*7330f729Sjoerg  *
35*7330f729Sjoerg  *	@(#)regex2.h	8.4 (Berkeley) 3/20/94
36*7330f729Sjoerg  */
37*7330f729Sjoerg 
38*7330f729Sjoerg #ifndef LLVM_SUPPORT_REGEX2_H
39*7330f729Sjoerg #define LLVM_SUPPORT_REGEX2_H
40*7330f729Sjoerg 
41*7330f729Sjoerg #include "regutils.h"
42*7330f729Sjoerg #include <stddef.h>
43*7330f729Sjoerg 
44*7330f729Sjoerg /*
45*7330f729Sjoerg  * internals of regex_t
46*7330f729Sjoerg  */
47*7330f729Sjoerg #define	MAGIC1	((('r'^0200)<<8) | 'e')
48*7330f729Sjoerg 
49*7330f729Sjoerg /*
50*7330f729Sjoerg  * The internal representation is a *strip*, a sequence of
51*7330f729Sjoerg  * operators ending with an endmarker.  (Some terminology etc. is a
52*7330f729Sjoerg  * historical relic of earlier versions which used multiple strips.)
53*7330f729Sjoerg  * Certain oddities in the representation are there to permit running
54*7330f729Sjoerg  * the machinery backwards; in particular, any deviation from sequential
55*7330f729Sjoerg  * flow must be marked at both its source and its destination.  Some
56*7330f729Sjoerg  * fine points:
57*7330f729Sjoerg  *
58*7330f729Sjoerg  * - OPLUS_ and O_PLUS are *inside* the loop they create.
59*7330f729Sjoerg  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
60*7330f729Sjoerg  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
61*7330f729Sjoerg  *   OOR1 and OOR2 are respectively the end and the beginning of one of
62*7330f729Sjoerg  *   the branches.  Note that there is an implicit OOR2 following OCH_
63*7330f729Sjoerg  *   and an implicit OOR1 preceding O_CH.
64*7330f729Sjoerg  *
65*7330f729Sjoerg  * In state representations, an operator's bit is on to signify a state
66*7330f729Sjoerg  * immediately *preceding* "execution" of that operator.
67*7330f729Sjoerg  */
68*7330f729Sjoerg typedef unsigned long sop;	/* strip operator */
69*7330f729Sjoerg typedef long sopno;
70*7330f729Sjoerg #define	OPRMASK	0xf8000000LU
71*7330f729Sjoerg #define	OPDMASK	0x07ffffffLU
72*7330f729Sjoerg #define	OPSHIFT	((unsigned)27)
73*7330f729Sjoerg #define	OP(n)	((n)&OPRMASK)
74*7330f729Sjoerg #define	OPND(n)	((n)&OPDMASK)
75*7330f729Sjoerg #define	SOP(op, opnd)	((op)|(opnd))
76*7330f729Sjoerg /* operators			   meaning	operand			*/
77*7330f729Sjoerg /*						(back, fwd are offsets)	*/
78*7330f729Sjoerg #define	OEND	(1LU<<OPSHIFT)	/* endmarker	-			*/
79*7330f729Sjoerg #define	OCHAR	(2LU<<OPSHIFT)	/* character	unsigned char		*/
80*7330f729Sjoerg #define	OBOL	(3LU<<OPSHIFT)	/* left anchor	-			*/
81*7330f729Sjoerg #define	OEOL	(4LU<<OPSHIFT)	/* right anchor	-			*/
82*7330f729Sjoerg #define	OANY	(5LU<<OPSHIFT)	/* .		-			*/
83*7330f729Sjoerg #define	OANYOF	(6LU<<OPSHIFT)	/* [...]	set number		*/
84*7330f729Sjoerg #define	OBACK_	(7LU<<OPSHIFT)	/* begin \d	paren number		*/
85*7330f729Sjoerg #define	O_BACK	(8LU<<OPSHIFT)	/* end \d	paren number		*/
86*7330f729Sjoerg #define	OPLUS_	(9LU<<OPSHIFT)	/* + prefix	fwd to suffix		*/
87*7330f729Sjoerg #define	O_PLUS	(10LU<<OPSHIFT)	/* + suffix	back to prefix		*/
88*7330f729Sjoerg #define	OQUEST_	(11LU<<OPSHIFT)	/* ? prefix	fwd to suffix		*/
89*7330f729Sjoerg #define	O_QUEST	(12LU<<OPSHIFT)	/* ? suffix	back to prefix		*/
90*7330f729Sjoerg #define	OLPAREN	(13LU<<OPSHIFT)	/* (		fwd to )		*/
91*7330f729Sjoerg #define	ORPAREN	(14LU<<OPSHIFT)	/* )		back to (		*/
92*7330f729Sjoerg #define	OCH_	(15LU<<OPSHIFT)	/* begin choice	fwd to OOR2		*/
93*7330f729Sjoerg #define	OOR1	(16LU<<OPSHIFT)	/* | pt. 1	back to OOR1 or OCH_	*/
94*7330f729Sjoerg #define	OOR2	(17LU<<OPSHIFT)	/* | pt. 2	fwd to OOR2 or O_CH	*/
95*7330f729Sjoerg #define	O_CH	(18LU<<OPSHIFT)	/* end choice	back to OOR1		*/
96*7330f729Sjoerg #define	OBOW	(19LU<<OPSHIFT)	/* begin word	-			*/
97*7330f729Sjoerg #define	OEOW	(20LU<<OPSHIFT)	/* end word	-			*/
98*7330f729Sjoerg 
99*7330f729Sjoerg /*
100*7330f729Sjoerg  * Structure for [] character-set representation.  Character sets are
101*7330f729Sjoerg  * done as bit vectors, grouped 8 to a byte vector for compactness.
102*7330f729Sjoerg  * The individual set therefore has both a pointer to the byte vector
103*7330f729Sjoerg  * and a mask to pick out the relevant bit of each byte.  A hash code
104*7330f729Sjoerg  * simplifies testing whether two sets could be identical.
105*7330f729Sjoerg  *
106*7330f729Sjoerg  * This will get trickier for multicharacter collating elements.  As
107*7330f729Sjoerg  * preliminary hooks for dealing with such things, we also carry along
108*7330f729Sjoerg  * a string of multi-character elements, and decide the size of the
109*7330f729Sjoerg  * vectors at run time.
110*7330f729Sjoerg  */
111*7330f729Sjoerg typedef struct {
112*7330f729Sjoerg 	uch *ptr;		/* -> uch [csetsize] */
113*7330f729Sjoerg 	uch mask;		/* bit within array */
114*7330f729Sjoerg 	uch hash;		/* hash code */
115*7330f729Sjoerg 	size_t smultis;
116*7330f729Sjoerg 	char *multis;		/* -> char[smulti]  ab\0cd\0ef\0\0 */
117*7330f729Sjoerg } cset;
118*7330f729Sjoerg /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
119*7330f729Sjoerg #define	CHadd(cs, c)	((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c))
120*7330f729Sjoerg #define	CHsub(cs, c)	((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c))
121*7330f729Sjoerg #define	CHIN(cs, c)	((cs)->ptr[(uch)(c)] & (cs)->mask)
122*7330f729Sjoerg #define	MCadd(p, cs, cp)	mcadd(p, cs, cp)	/* llvm_regcomp() internal fns */
123*7330f729Sjoerg #define	MCsub(p, cs, cp)	mcsub(p, cs, cp)
124*7330f729Sjoerg #define	MCin(p, cs, cp)	mcin(p, cs, cp)
125*7330f729Sjoerg 
126*7330f729Sjoerg /* stuff for character categories */
127*7330f729Sjoerg typedef unsigned char cat_t;
128*7330f729Sjoerg 
129*7330f729Sjoerg /*
130*7330f729Sjoerg  * main compiled-expression structure
131*7330f729Sjoerg  */
132*7330f729Sjoerg struct re_guts {
133*7330f729Sjoerg 	int magic;
134*7330f729Sjoerg #		define	MAGIC2	((('R'^0200)<<8)|'E')
135*7330f729Sjoerg 	sop *strip;		/* malloced area for strip */
136*7330f729Sjoerg 	int csetsize;		/* number of bits in a cset vector */
137*7330f729Sjoerg 	int ncsets;		/* number of csets in use */
138*7330f729Sjoerg 	cset *sets;		/* -> cset [ncsets] */
139*7330f729Sjoerg 	uch *setbits;		/* -> uch[csetsize][ncsets/CHAR_BIT] */
140*7330f729Sjoerg 	int cflags;		/* copy of llvm_regcomp() cflags argument */
141*7330f729Sjoerg 	sopno nstates;		/* = number of sops */
142*7330f729Sjoerg 	sopno firststate;	/* the initial OEND (normally 0) */
143*7330f729Sjoerg 	sopno laststate;	/* the final OEND */
144*7330f729Sjoerg 	int iflags;		/* internal flags */
145*7330f729Sjoerg #		define	USEBOL	01	/* used ^ */
146*7330f729Sjoerg #		define	USEEOL	02	/* used $ */
147*7330f729Sjoerg #		define	REGEX_BAD	04	/* something wrong */
148*7330f729Sjoerg 	int nbol;		/* number of ^ used */
149*7330f729Sjoerg 	int neol;		/* number of $ used */
150*7330f729Sjoerg 	int ncategories;	/* how many character categories */
151*7330f729Sjoerg 	cat_t *categories;	/* ->catspace[-CHAR_MIN] */
152*7330f729Sjoerg 	char *must;		/* match must contain this string */
153*7330f729Sjoerg 	int mlen;		/* length of must */
154*7330f729Sjoerg 	size_t nsub;		/* copy of re_nsub */
155*7330f729Sjoerg 	int backrefs;		/* does it use back references? */
156*7330f729Sjoerg 	sopno nplus;		/* how deep does it nest +s? */
157*7330f729Sjoerg 	/* catspace must be last */
158*7330f729Sjoerg 	cat_t catspace[1];	/* actually [NC] */
159*7330f729Sjoerg };
160*7330f729Sjoerg 
161*7330f729Sjoerg /* misc utilities */
162*7330f729Sjoerg #define	OUT	(CHAR_MAX+1)	/* a non-character value */
163*7330f729Sjoerg #define	ISWORD(c)	(isalnum(c&0xff) || (c) == '_')
164*7330f729Sjoerg 
165*7330f729Sjoerg #endif
166