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