G:/ScriptBasic/source/extensions/re/regex2.h

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
00003  * Copyright (c) 1992, 1993, 1994
00004  *      The Regents of the University of California.  All rights reserved.
00005  *
00006  * This code is derived from software contributed to Berkeley by
00007  * Henry Spencer.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer.
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the
00016  *    documentation and/or other materials provided with the distribution.
00017  * 3. All advertising materials mentioning features or use of this software
00018  *    must display the following acknowledgement:
00019  *      This product includes software developed by the University of
00020  *      California, Berkeley and its contributors.
00021  * 4. Neither the name of the University nor the names of its contributors
00022  *    may be used to endorse or promote products derived from this software
00023  *    without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00026  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00031  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00032  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00034  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00035  * SUCH DAMAGE.
00036  *
00037  *      @(#)regex2.h    8.4 (Berkeley) 3/20/94
00038  */
00039 
00040 /*
00041  * First, the stuff that ends up in the outside-world include file
00042  = typedef off_t regoff_t;
00043  = typedef struct {
00044  =      int re_magic;
00045  =      size_t re_nsub;         // number of parenthesized subexpressions
00046  =      const char *re_endp;    // end pointer for REG_PEND
00047  =      struct re_guts *re_g;   // none of your business :-)
00048  = } regex_t;
00049  = typedef struct {
00050  =      regoff_t rm_so;         // start of match
00051  =      regoff_t rm_eo;         // end of match
00052  = } regmatch_t;
00053  */
00054 /*
00055  * internals of regex_t
00056  */
00057 #define MAGIC1  ((('r'^0200)<<8) | 'e')
00058 
00059 /*
00060  * The internal representation is a *strip*, a sequence of
00061  * operators ending with an endmarker.  (Some terminology etc. is a
00062  * historical relic of earlier versions which used multiple strips.)
00063  * Certain oddities in the representation are there to permit running
00064  * the machinery backwards; in particular, any deviation from sequential
00065  * flow must be marked at both its source and its destination.  Some
00066  * fine points:
00067  *
00068  * - OPLUS_ and O_PLUS are *inside* the loop they create.
00069  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
00070  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
00071  *   OOR1 and OOR2 are respectively the end and the beginning of one of
00072  *   the branches.  Note that there is an implicit OOR2 following OCH_
00073  *   and an implicit OOR1 preceding O_CH.
00074  *
00075  * In state representations, an operator's bit is on to signify a state
00076  * immediately *preceding* "execution" of that operator.
00077  */
00078 typedef unsigned long sop;      /* strip operator */
00079 typedef long sopno;
00080 #define OPRMASK 0xf8000000
00081 #define OPDMASK 0x07ffffff
00082 #define OPSHIFT ((unsigned)27)
00083 #define OP(n)   ((n)&OPRMASK)
00084 #define OPND(n) ((n)&OPDMASK)
00085 #define SOP(op, opnd)   ((op)|(opnd))
00086 /* operators                       meaning      operand                 */
00087 /*                                              (back, fwd are offsets) */
00088 #define OEND    (1<<OPSHIFT)    /* endmarker    -                       */
00089 #define OCHAR   (2<<OPSHIFT)    /* character    unsigned char           */
00090 #define OBOL    (3<<OPSHIFT)    /* left anchor  -                       */
00091 #define OEOL    (4<<OPSHIFT)    /* right anchor -                       */
00092 #define OANY    (5<<OPSHIFT)    /* .            -                       */
00093 #define OANYOF  (6<<OPSHIFT)    /* [...]        set number              */
00094 #define OBACK_  (7<<OPSHIFT)    /* begin \d     paren number            */
00095 #define O_BACK  (8<<OPSHIFT)    /* end \d       paren number            */
00096 #define OPLUS_  (9<<OPSHIFT)    /* + prefix     fwd to suffix           */
00097 #define O_PLUS  (10<<OPSHIFT)   /* + suffix     back to prefix          */
00098 #define OQUEST_ (11<<OPSHIFT)   /* ? prefix     fwd to suffix           */
00099 #define O_QUEST (12<<OPSHIFT)   /* ? suffix     back to prefix          */
00100 #define OLPAREN (13<<OPSHIFT)   /* (            fwd to )                */
00101 #define ORPAREN (14<<OPSHIFT)   /* )            back to (               */
00102 #define OCH_    (15<<OPSHIFT)   /* begin choice fwd to OOR2             */
00103 #define OOR1    (16<<OPSHIFT)   /* | pt. 1      back to OOR1 or OCH_    */
00104 #define OOR2    (17<<OPSHIFT)   /* | pt. 2      fwd to OOR2 or O_CH     */
00105 #define O_CH    (18<<OPSHIFT)   /* end choice   back to OOR1            */
00106 #define OBOW    (19<<OPSHIFT)   /* begin word   -                       */
00107 #define OEOW    (20<<OPSHIFT)   /* end word     -                       */
00108 
00109 /*
00110  * Structure for [] character-set representation.  Character sets are
00111  * done as bit vectors, grouped 8 to a byte vector for compactness.
00112  * The individual set therefore has both a pointer to the byte vector
00113  * and a mask to pick out the relevant bit of each byte.  A hash code
00114  * simplifies testing whether two sets could be identical.
00115  *
00116  * This will get trickier for multicharacter collating elements.  As
00117  * preliminary hooks for dealing with such things, we also carry along
00118  * a string of multi-character elements, and decide the size of the
00119  * vectors at run time.
00120  */
00121 typedef struct {
00122         uch *ptr;               /* -> uch [csetsize] */
00123         uch mask;               /* bit within array */
00124         uch hash;               /* hash code */
00125         size_t smultis;
00126         char *multis;           /* -> char[smulti]  ab\0cd\0ef\0\0 */
00127 } cset;
00128 /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
00129 #define CHadd(cs, c)    ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c))
00130 #define CHsub(cs, c)    ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c))
00131 #define CHIN(cs, c)     ((cs)->ptr[(uch)(c)] & (cs)->mask)
00132 #define MCadd(p, cs, cp)        mcadd(p, cs, cp)        /* regcomp() internal fns */
00133 #define MCsub(p, cs, cp)        mcsub(p, cs, cp)
00134 #define MCin(p, cs, cp) mcin(p, cs, cp)
00135 
00136 /* stuff for character categories */
00137 typedef unsigned char cat_t;
00138 
00139 /*
00140  * main compiled-expression structure
00141  */
00142 struct re_guts {
00143         int magic;
00144 #               define  MAGIC2  ((('R'^0200)<<8)|'E')
00145         sop *strip;             /* malloced area for strip */
00146         int csetsize;           /* number of bits in a cset vector */
00147         int ncsets;             /* number of csets in use */
00148         cset *sets;             /* -> cset [ncsets] */
00149         uch *setbits;           /* -> uch[csetsize][ncsets/CHAR_BIT] */
00150         int cflags;             /* copy of regcomp() cflags argument */
00151         sopno nstates;          /* = number of sops */
00152         sopno firststate;       /* the initial OEND (normally 0) */
00153         sopno laststate;        /* the final OEND */
00154         int iflags;             /* internal flags */
00155 #               define  USEBOL  01      /* used ^ */
00156 #               define  USEEOL  02      /* used $ */
00157 #               define  BAD     04      /* something wrong */
00158         int nbol;               /* number of ^ used */
00159         int neol;               /* number of $ used */
00160         int ncategories;        /* how many character categories */
00161         cat_t *categories;      /* ->catspace[-CHAR_MIN] */
00162         char *must;             /* match must contain this string */
00163         int mlen;               /* length of must */
00164         size_t nsub;            /* copy of re_nsub */
00165         int backrefs;           /* does it use back references? */
00166         sopno nplus;            /* how deep does it nest +s? */
00167         /* catspace must be last */
00168         cat_t catspace[1];      /* actually [NC] */
00169 };
00170 
00171 /* misc utilities */
00172 #define OUT     (CHAR_MAX+1)    /* a non-character value */
00173 #define ISWORD(c)       (isalnum(c) || (c) == '_')

Generated on Sun Mar 12 23:56:30 2006 for ScriptBasic by  doxygen 1.4.6-NO