5 * The "internal use only" fields in regexp.h are present to pass info from
6 * compile to execute that permits the execute phase to run lots faster on
7 * simple cases. They are:
9 * regstart sv that must begin a match; Nullch if none obvious
10 * reganch is the match anchored (at beginning-of-line only)?
11 * regmust string (pointer into program) that match must include, or NULL
12 * [regmust changed to SV* for bminstr()--law]
13 * regmlen length of regmust string
14 * [regmlen not used currently]
16 * Regstart and reganch permit very fast decisions on suitable starting points
17 * for a match, cutting down the work a lot. Regmust permits fast rejection
18 * of lines that cannot possibly match. The regmust tests are costly enough
19 * that pregcomp() supplies a regmust only if the r.e. contains something
20 * potentially expensive (at present, the only such thing detected is * or +
21 * at the start of the r.e., which can involve a lot of backup). Regmlen is
22 * supplied because the test in pregexec() needs it and pregcomp() is computing
24 * [regmust is now supplied always. The tests that use regmust have a
25 * heuristic that disables the test if it usually matches.]
27 * [In fact, we now use regmust in many cases to locate where the search
28 * starts in the string, so if regback is >= 0, the regmust search is never
29 * wasted effort. The regback variable says how many characters back from
30 * where regmust matched is the earliest possible start of the match.
31 * For instance, /[a-z].foo/ has a regmust of 'foo' and a regback of 2.]
35 * Structure for regexp "program". This is essentially a linear encoding
36 * of a nondeterministic finite-state machine (aka syntax charts or
37 * "railroad normal form" in parsing technology). Each node is an opcode
38 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
39 * all nodes except BRANCH implement concatenation; a "next" pointer with
40 * a BRANCH on both ends of it is connecting two alternatives. (Here we
41 * have one of the subtle syntax dependencies: an individual BRANCH (as
42 * opposed to a collection of them) is never concatenated with anything
43 * because of operator precedence.) The operand of some types of node is
44 * a literal string; for others, it is a node leading into a sub-FSM. In
45 * particular, the operand of a BRANCH node is the first node of the branch.
46 * (NB this is *not* a tree structure: the tail of the branch connects
47 * to the thing following the set of BRANCHes.) The opcodes are:
50 /* definition number opnd? meaning */
51 #define END 0 /* no End of program. */
52 #define BOL 1 /* no Match "" at beginning of line. */
53 #define MBOL 2 /* no Same, assuming multiline. */
54 #define SBOL 3 /* no Same, assuming singleline. */
55 #define EOL 4 /* no Match "" at end of line. */
56 #define MEOL 5 /* no Same, assuming multiline. */
57 #define SEOL 6 /* no Same, assuming singleline. */
58 #define ANY 7 /* no Match any one character (except newline). */
59 #define SANY 8 /* no Match any one character. */
60 #define ANYOF 9 /* sv Match character in (or not in) this class. */
61 #define CURLY 10 /* sv Match this simple thing {n,m} times. */
62 #define CURLYX 11 /* sv Match this complex thing {n,m} times. */
63 #define BRANCH 12 /* node Match this alternative, or the next... */
64 #define BACK 13 /* no Match "", "next" ptr points backward. */
65 #define EXACT 14 /* sv Match this string (preceded by length). */
66 #define EXACTF 15 /* sv Match this string, folded (prec. by length). */
67 #define EXACTFL 16 /* sv Match this string, folded in locale (w/len). */
68 #define NOTHING 17 /* no Match empty string. */
69 #define STAR 18 /* node Match this (simple) thing 0 or more times. */
70 #define PLUS 19 /* node Match this (simple) thing 1 or more times. */
71 #define BOUND 20 /* no Match "" at any word boundary */
72 #define BOUNDL 21 /* no Match "" at any word boundary */
73 #define NBOUND 22 /* no Match "" at any word non-boundary */
74 #define NBOUNDL 23 /* no Match "" at any word non-boundary */
75 #define REF 24 /* num Match some already matched string */
76 #define OPEN 25 /* num Mark this point in input as start of #n. */
77 #define CLOSE 26 /* num Analogous to OPEN. */
78 #define MINMOD 27 /* no Next operator is not greedy. */
79 #define GBOL 28 /* no Matches where last m//g left off. */
80 #define IFMATCH 29 /* no Succeeds if the following matches. */
81 #define UNLESSM 30 /* no Fails if the following matches. */
82 #define SUCCEED 31 /* no Return from a subroutine, basically. */
83 #define WHILEM 32 /* no Do curly processing and see if rest matches. */
84 #define ALNUM 33 /* no Match any alphanumeric character */
85 #define ALNUML 34 /* no Match any alphanumeric char in locale */
86 #define NALNUM 35 /* no Match any non-alphanumeric character */
87 #define NALNUML 36 /* no Match any non-alphanumeric char in locale */
88 #define SPACE 37 /* no Match any whitespace character */
89 #define SPACEL 38 /* no Match any whitespace char in locale */
90 #define NSPACE 39 /* no Match any non-whitespace character */
91 #define NSPACEL 40 /* no Match any non-whitespace char in locale */
92 #define DIGIT 41 /* no Match any numeric character */
93 #define NDIGIT 42 /* no Match any non-numeric character */
98 * BRANCH The set of branches constituting a single choice are hooked
99 * together with their "next" pointers, since precedence prevents
100 * anything being concatenated to any individual branch. The
101 * "next" pointer of the last BRANCH in a choice points to the
102 * thing following the whole choice. This is also where the
103 * final "next" pointer of each individual branch points; each
104 * branch starts with the operand node of a BRANCH node.
106 * BACK Normal "next" pointers all implicitly point forward; BACK
107 * exists to make loop structures possible.
109 * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
110 * BRANCH structures using BACK. Simple cases (one character
111 * per match) are implemented with STAR and PLUS for speed
112 * and to minimize recursive plunges.
114 * OPEN,CLOSE ...are numbered at compile time.
118 EXT char regarglen[];
120 EXT char regarglen[] = {
121 0,0,0,0,0,0,0,0,0,0,
122 /*CURLY*/ 4, /*CURLYX*/ 4,
123 0,0,0,0,0,0,0,0,0,0,0,0,
124 /*REF*/ 2, /*OPEN*/ 2, /*CLOSE*/ 2,
125 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
132 EXT char regkind[] = {
179 /* The following have no fixed length. */
183 EXT char varies[] = {
184 BRANCH, BACK, STAR, PLUS, CURLY, CURLYX, REF, WHILEM, 0
188 /* The following always have a length of 1. */
192 EXT char simple[] = {
194 ALNUM, ALNUML, NALNUM, NALNUML,
195 SPACE, SPACEL, NSPACE, NSPACEL,
203 * A node is one char of opcode followed by two chars of "next" pointer.
204 * "Next" pointers are stored as two 8-bit pieces, high order first. The
205 * value is a positive offset from the opcode of the node containing it.
206 * An operand, if any, simply follows the node. (Note that much of the
207 * code generation knows about this implicit relationship.)
209 * Using two bytes for the "next" pointer is vast overkill for most things,
210 * but allows patterns to get big without disasters.
212 * [If REGALIGN is defined, the "next" pointer is always aligned on an even
213 * boundary, and reads the offset directly as a short. Also, there is no
214 * special test to reverse the sign of BACK pointers since the offset is
230 #define NEXT(p) (*(short*)(p+1))
231 #define ARG1(p) (*(unsigned short*)(p+3))
232 #define ARG2(p) (*(unsigned short*)(p+5))
234 #define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
235 #define ARG1(p) (((*((p)+3)&0377)<<8) + (*((p)+4)&0377))
236 #define ARG2(p) (((*((p)+5)&0377)<<8) + (*((p)+6)&0377))
242 #define OPERAND(p) ((p) + 3)
245 #define NEXTOPER(p) ((p) + 4)
246 #define PREVOPER(p) ((p) - 4)
248 #define NEXTOPER(p) ((p) + 3)
249 #define PREVOPER(p) ((p) - 3)
254 /* Flags for first parameter byte of ANYOF */
255 #define ANYOF_INVERT 0x40
256 #define ANYOF_FOLD 0x20
257 #define ANYOF_LOCALE 0x10
258 #define ANYOF_ISA 0x0F
259 #define ANYOF_ALNUML 0x08
260 #define ANYOF_NALNUML 0x04
261 #define ANYOF_SPACEL 0x02
262 #define ANYOF_NSPACEL 0x01
265 * Utility definitions.
269 #define UCHARAT(p) ((int)*(unsigned char *)(p))
271 #define UCHARAT(p) ((int)*(p)&CHARMASK)
274 #define UCHARAT(p) regdummy
277 #define FAIL(m) croak("/%.127s/: %s",regprecomp,m)