perl 4.0.00: (no release announcement available)
[p5sagit/p5-mst-13.2.git] / regcomp.h
1 /* $Header: regcomp.h,v 4.0 91/03/20 01:39:09 lwall Locked $
2  *
3  * $Log:        regcomp.h,v $
4  * Revision 4.0  91/03/20  01:39:09  lwall
5  * 4.0 baseline.
6  * 
7  */
8
9 /*
10  * The "internal use only" fields in regexp.h are present to pass info from
11  * compile to execute that permits the execute phase to run lots faster on
12  * simple cases.  They are:
13  *
14  * regstart     str that must begin a match; Nullch if none obvious
15  * reganch      is the match anchored (at beginning-of-line only)?
16  * regmust      string (pointer into program) that match must include, or NULL
17  *  [regmust changed to STR* for bminstr()--law]
18  * regmlen      length of regmust string
19  *  [regmlen not used currently]
20  *
21  * Regstart and reganch permit very fast decisions on suitable starting points
22  * for a match, cutting down the work a lot.  Regmust permits fast rejection
23  * of lines that cannot possibly match.  The regmust tests are costly enough
24  * that regcomp() supplies a regmust only if the r.e. contains something
25  * potentially expensive (at present, the only such thing detected is * or +
26  * at the start of the r.e., which can involve a lot of backup).  Regmlen is
27  * supplied because the test in regexec() needs it and regcomp() is computing
28  * it anyway.
29  * [regmust is now supplied always.  The tests that use regmust have a
30  * heuristic that disables the test if it usually matches.]
31  *
32  * [In fact, we now use regmust in many cases to locate where the search
33  * starts in the string, so if regback is >= 0, the regmust search is never
34  * wasted effort.  The regback variable says how many characters back from
35  * where regmust matched is the earliest possible start of the match.
36  * For instance, /[a-z].foo/ has a regmust of 'foo' and a regback of 2.]
37  */
38
39 /*
40  * Structure for regexp "program".  This is essentially a linear encoding
41  * of a nondeterministic finite-state machine (aka syntax charts or
42  * "railroad normal form" in parsing technology).  Each node is an opcode
43  * plus a "next" pointer, possibly plus an operand.  "Next" pointers of
44  * all nodes except BRANCH implement concatenation; a "next" pointer with
45  * a BRANCH on both ends of it is connecting two alternatives.  (Here we
46  * have one of the subtle syntax dependencies:  an individual BRANCH (as
47  * opposed to a collection of them) is never concatenated with anything
48  * because of operator precedence.)  The operand of some types of node is
49  * a literal string; for others, it is a node leading into a sub-FSM.  In
50  * particular, the operand of a BRANCH node is the first node of the branch.
51  * (NB this is *not* a tree structure:  the tail of the branch connects
52  * to the thing following the set of BRANCHes.)  The opcodes are:
53  */
54
55 /* definition   number  opnd?   meaning */
56 #define END     0       /* no   End of program. */
57 #define BOL     1       /* no   Match "" at beginning of line. */
58 #define EOL     2       /* no   Match "" at end of line. */
59 #define ANY     3       /* no   Match any one character. */
60 #define ANYOF   4       /* str  Match character in (or not in) this class. */
61 #define CURLY   5       /* str  Match this simple thing {n,m} times. */
62 #define BRANCH  6       /* node Match this alternative, or the next... */
63 #define BACK    7       /* no   Match "", "next" ptr points backward. */
64 #define EXACTLY 8       /* str  Match this string (preceded by length). */
65 #define NOTHING 9       /* no   Match empty string. */
66 #define STAR    10      /* node Match this (simple) thing 0 or more times. */
67 #define PLUS    11      /* node Match this (simple) thing 1 or more times. */
68 #define ALNUM   12      /* no   Match any alphanumeric character */
69 #define NALNUM  13      /* no   Match any non-alphanumeric character */
70 #define BOUND   14      /* no   Match "" at any word boundary */
71 #define NBOUND  15      /* no   Match "" at any word non-boundary */
72 #define SPACE   16      /* no   Match any whitespace character */
73 #define NSPACE  17      /* no   Match any non-whitespace character */
74 #define DIGIT   18      /* no   Match any numeric character */
75 #define NDIGIT  19      /* no   Match any non-numeric character */
76 #define REF     20      /* num  Match some already matched string */
77 #define OPEN    21      /* num  Mark this point in input as start of #n. */
78 #define CLOSE   22      /* num  Analogous to OPEN. */
79
80 /*
81  * Opcode notes:
82  *
83  * BRANCH       The set of branches constituting a single choice are hooked
84  *              together with their "next" pointers, since precedence prevents
85  *              anything being concatenated to any individual branch.  The
86  *              "next" pointer of the last BRANCH in a choice points to the
87  *              thing following the whole choice.  This is also where the
88  *              final "next" pointer of each individual branch points; each
89  *              branch starts with the operand node of a BRANCH node.
90  *
91  * BACK         Normal "next" pointers all implicitly point forward; BACK
92  *              exists to make loop structures possible.
93  *
94  * STAR,PLUS    '?', and complex '*' and '+', are implemented as circular
95  *              BRANCH structures using BACK.  Simple cases (one character
96  *              per match) are implemented with STAR and PLUS for speed
97  *              and to minimize recursive plunges.
98  *
99  * OPEN,CLOSE   ...are numbered at compile time.
100  */
101
102 #ifndef DOINIT
103 extern char regarglen[];
104 #else
105 char regarglen[] = {0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2};
106 #endif
107
108 /* The following have no fixed length. */
109 #ifndef DOINIT
110 extern char varies[];
111 #else
112 char varies[] = {BRANCH,BACK,STAR,PLUS,CURLY,REF,0};
113 #endif
114
115 /* The following always have a length of 1. */
116 #ifndef DOINIT
117 extern char simple[];
118 #else
119 char simple[] = {ANY,ANYOF,ALNUM,NALNUM,SPACE,NSPACE,DIGIT,NDIGIT,0};
120 #endif
121
122 EXT char regdummy;
123
124 /*
125  * A node is one char of opcode followed by two chars of "next" pointer.
126  * "Next" pointers are stored as two 8-bit pieces, high order first.  The
127  * value is a positive offset from the opcode of the node containing it.
128  * An operand, if any, simply follows the node.  (Note that much of the
129  * code generation knows about this implicit relationship.)
130  *
131  * Using two bytes for the "next" pointer is vast overkill for most things,
132  * but allows patterns to get big without disasters.
133  *
134  * [If REGALIGN is defined, the "next" pointer is always aligned on an even
135  * boundary, and reads the offset directly as a short.  Also, there is no
136  * special test to reverse the sign of BACK pointers since the offset is
137  * stored negative.]
138  */
139
140 #ifndef gould
141 #ifndef cray
142 #ifndef eta10
143 #define REGALIGN
144 #endif
145 #endif
146 #endif
147
148 #define OP(p)   (*(p))
149
150 #ifndef lint
151 #ifdef REGALIGN
152 #define NEXT(p) (*(short*)(p+1))
153 #define ARG1(p) (*(unsigned short*)(p+3))
154 #define ARG2(p) (*(unsigned short*)(p+5))
155 #else
156 #define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
157 #define ARG1(p) (((*((p)+3)&0377)<<8) + (*((p)+4)&0377))
158 #define ARG2(p) (((*((p)+5)&0377)<<8) + (*((p)+6)&0377))
159 #endif
160 #else /* lint */
161 #define NEXT(p) 0
162 #endif /* lint */
163
164 #define OPERAND(p)      ((p) + 3)
165
166 #ifdef REGALIGN
167 #define NEXTOPER(p)     ((p) + 4)
168 #else
169 #define NEXTOPER(p)     ((p) + 3)
170 #endif
171
172 #define MAGIC 0234
173
174 /*
175  * Utility definitions.
176  */
177 #ifndef lint
178 #ifndef CHARBITS
179 #define UCHARAT(p)      ((int)*(unsigned char *)(p))
180 #else
181 #define UCHARAT(p)      ((int)*(p)&CHARBITS)
182 #endif
183 #else /* lint */
184 #define UCHARAT(p)      regdummy
185 #endif /* lint */
186
187 #define FAIL(m) fatal("/%s/: %s",regprecomp,m)
188
189 char *regnext();
190 #ifdef DEBUGGING
191 void regdump();
192 char *regprop();
193 #endif
194