brintos

brintos / llvm-project-archived public Read only

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