4 typedef OP OP_4tree; /* Will be redefined later. */
7 * The "internal use only" fields in regexp.h are present to pass info from
8 * compile to execute that permits the execute phase to run lots faster on
9 * simple cases. They are:
11 * regstart sv that must begin a match; Nullch if none obvious
12 * reganch is the match anchored (at beginning-of-line only)?
13 * regmust string (pointer into program) that match must include, or NULL
14 * [regmust changed to SV* for bminstr()--law]
15 * regmlen length of regmust string
16 * [regmlen not used currently]
18 * Regstart and reganch permit very fast decisions on suitable starting points
19 * for a match, cutting down the work a lot. Regmust permits fast rejection
20 * of lines that cannot possibly match. The regmust tests are costly enough
21 * that pregcomp() supplies a regmust only if the r.e. contains something
22 * potentially expensive (at present, the only such thing detected is * or +
23 * at the start of the r.e., which can involve a lot of backup). Regmlen is
24 * supplied because the test in pregexec() needs it and pregcomp() is computing
26 * [regmust is now supplied always. The tests that use regmust have a
27 * heuristic that disables the test if it usually matches.]
29 * [In fact, we now use regmust in many cases to locate where the search
30 * starts in the string, so if regback is >= 0, the regmust search is never
31 * wasted effort. The regback variable says how many characters back from
32 * where regmust matched is the earliest possible start of the match.
33 * For instance, /[a-z].foo/ has a regmust of 'foo' and a regback of 2.]
45 # define REGALIGN_STRUCT
49 * Structure for regexp "program". This is essentially a linear encoding
50 * of a nondeterministic finite-state machine (aka syntax charts or
51 * "railroad normal form" in parsing technology). Each node is an opcode
52 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
53 * all nodes except BRANCH implement concatenation; a "next" pointer with
54 * a BRANCH on both ends of it is connecting two alternatives. (Here we
55 * have one of the subtle syntax dependencies: an individual BRANCH (as
56 * opposed to a collection of them) is never concatenated with anything
57 * because of operator precedence.) The operand of some types of node is
58 * a literal string; for others, it is a node leading into a sub-FSM. In
59 * particular, the operand of a BRANCH node is the first node of the branch.
60 * (NB this is *not* a tree structure: the tail of the branch connects
61 * to the thing following the set of BRANCHes.) The opcodes are:
64 /* definition number opnd? meaning */
65 #define END 0 /* no End of program. */
66 #define BOL 1 /* no Match "" at beginning of line. */
67 #define MBOL 2 /* no Same, assuming multiline. */
68 #define SBOL 3 /* no Same, assuming singleline. */
69 #define EOL 4 /* no Match "" at end of line. */
70 #define MEOL 5 /* no Same, assuming multiline. */
71 #define SEOL 6 /* no Same, assuming singleline. */
72 #define ANY 7 /* no Match any one character (except newline). */
73 #define SANY 8 /* no Match any one character. */
74 #define ANYOF 9 /* sv Match character in (or not in) this class. */
75 #define CURLY 10 /* sv Match this simple thing {n,m} times. */
76 #define CURLYX 11 /* sv Match this complex thing {n,m} times. */
77 #define BRANCH 12 /* node Match this alternative, or the next... */
78 #define BACK 13 /* no Match "", "next" ptr points backward. */
79 #define EXACT 14 /* sv Match this string (preceded by length). */
80 #define EXACTF 15 /* sv Match this string, folded (prec. by length). */
81 #define EXACTFL 16 /* sv Match this string, folded in locale (w/len). */
82 #define NOTHING 17 /* no Match empty string. */
83 #define STAR 18 /* node Match this (simple) thing 0 or more times. */
84 #define PLUS 19 /* node Match this (simple) thing 1 or more times. */
85 #define BOUND 20 /* no Match "" at any word boundary */
86 #define BOUNDL 21 /* no Match "" at any word boundary */
87 #define NBOUND 22 /* no Match "" at any word non-boundary */
88 #define NBOUNDL 23 /* no Match "" at any word non-boundary */
89 #define REF 24 /* num Match some already matched string */
90 #define OPEN 25 /* num Mark this point in input as start of #n. */
91 #define CLOSE 26 /* num Analogous to OPEN. */
92 #define MINMOD 27 /* no Next operator is not greedy. */
93 #define GPOS 28 /* no Matches where last m//g left off. */
94 #define IFMATCH 29 /* off Succeeds if the following matches. */
95 #define UNLESSM 30 /* off Fails if the following matches. */
96 #define SUCCEED 31 /* no Return from a subroutine, basically. */
97 #define WHILEM 32 /* no Do curly processing and see if rest matches. */
98 #define ALNUM 33 /* no Match any alphanumeric character */
99 #define ALNUML 34 /* no Match any alphanumeric char in locale */
100 #define NALNUM 35 /* no Match any non-alphanumeric character */
101 #define NALNUML 36 /* no Match any non-alphanumeric char in locale */
102 #define SPACE 37 /* no Match any whitespace character */
103 #define SPACEL 38 /* no Match any whitespace char in locale */
104 #define NSPACE 39 /* no Match any non-whitespace character */
105 #define NSPACEL 40 /* no Match any non-whitespace char in locale */
106 #define DIGIT 41 /* no Match any numeric character */
107 #define NDIGIT 42 /* no Match any non-numeric character */
108 #define CURLYM 43 /* no Match this medium-complex thing {n,m} times. */
109 #define CURLYN 44 /* no Match next-after-this simple thing
110 {n,m} times, set parenths. */
111 #define TAIL 45 /* no Match empty string. Can jump here from outside. */
112 #define REFF 46 /* num Match already matched string, folded */
113 #define REFFL 47 /* num Match already matched string, folded in loc. */
114 #define EVAL 48 /* evl Execute some Perl code. */
115 #define LONGJMP 49 /* off Jump far away, requires REGALIGN_STRUCT. */
116 #define BRANCHJ 50 /* off BRANCH with long offset, requires REGALIGN_STRUCT. */
117 #define IFTHEN 51 /* off Switch, should be preceeded by switcher . */
118 #define GROUPP 52 /* num Whether the group matched. */
119 #define LOGICAL 53 /* no Next opcode should set the flag only. */
120 #define SUSPEND 54 /* off "Independent" sub-RE. */
121 #define RENUM 55 /* off Group with independently numbered parens. */
122 #define OPTIMIZED 56 /* off Placeholder for dump. */
127 * BRANCH The set of branches constituting a single choice are hooked
128 * together with their "next" pointers, since precedence prevents
129 * anything being concatenated to any individual branch. The
130 * "next" pointer of the last BRANCH in a choice points to the
131 * thing following the whole choice. This is also where the
132 * final "next" pointer of each individual branch points; each
133 * branch starts with the operand node of a BRANCH node.
135 * BACK Normal "next" pointers all implicitly point forward; BACK
136 * exists to make loop structures possible.
138 * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
139 * BRANCH structures using BACK. Simple cases (one character
140 * per match) are implemented with STAR and PLUS for speed
141 * and to minimize recursive plunges.
143 * OPEN,CLOSE,GROUPP ...are numbered at compile time.
147 EXTCONST U8 regkind[];
149 EXTCONST U8 regkind[] = {
210 /* The following have no fixed length. char* since we do strchr on it. */
212 EXTCONST char varies[];
214 EXTCONST char varies[] = {
215 BRANCH, BACK, STAR, PLUS, CURLY, CURLYX, REF, REFF, REFFL,
216 WHILEM, CURLYM, CURLYN, BRANCHJ, IFTHEN, SUSPEND, 0
220 /* The following always have a length of 1. char* since we do strchr on it. */
222 EXTCONST char simple[];
224 EXTCONST char simple[] = {
226 ALNUM, ALNUML, NALNUM, NALNUML,
227 SPACE, SPACEL, NSPACE, NSPACEL,
233 * A node is one char of opcode followed by two chars of "next" pointer.
234 * "Next" pointers are stored as two 8-bit pieces, high order first. The
235 * value is a positive offset from the opcode of the node containing it.
236 * An operand, if any, simply follows the node. (Note that much of the
237 * code generation knows about this implicit relationship.)
239 * Using two bytes for the "next" pointer is vast overkill for most things,
240 * but allows patterns to get big without disasters.
242 * [If REGALIGN is defined, the "next" pointer is always aligned on an even
243 * boundary, and reads the offset directly as a short. Also, there is no
244 * special test to reverse the sign of BACK pointers since the offset is
248 #ifdef REGALIGN_STRUCT
250 struct regnode_string {
274 #define REG_INFTY I16_MAX
277 # define ARG_VALUE(arg) (arg)
278 # define ARG__SET(arg,val) ((arg) = (val))
280 # define ARG_VALUE(arg) (((*((char*)&arg)&0377)<<8) + (*(((char*)&arg)+1)&0377))
281 # define ARG__SET(arg,val) (((char*)&arg)[0] = (val) >> 8; ((char*)&arg)[1] = (val) & 0377;)
284 #define ARG(p) ARG_VALUE(ARG_LOC(p))
285 #define ARG1(p) ARG_VALUE(ARG1_LOC(p))
286 #define ARG2(p) ARG_VALUE(ARG2_LOC(p))
287 #define ARG_SET(p, val) ARG__SET(ARG_LOC(p), (val))
288 #define ARG1_SET(p, val) ARG__SET(ARG1_LOC(p), (val))
289 #define ARG2_SET(p, val) ARG__SET(ARG2_LOC(p), (val))
293 # ifdef REGALIGN_STRUCT
294 # define NEXT_OFF(p) ((p)->next_off)
295 # define NODE_ALIGN(node)
296 # define NODE_ALIGN_FILL(node) ((node)->flags = 0xde) /* deadbeef */
298 # define NEXT_OFF(p) (*(short*)(p+1))
299 # define NODE_ALIGN(node) ((!((long)node & 1)) ? node++ : 0)
300 # define NODE_ALIGN_FILL(node) ((!((long)node & 1)) ? *node++ = 127 : 0)
303 # define NEXT_OFF(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
304 # define NODE_ALIGN(node)
305 # define NODE_ALIGN_FILL(node)
308 # define NEXT_OFF(p) 0
309 # define NODE_ALIGN(node)
310 # define NODE_ALIGN_FILL(node)
313 #define SIZE_ALIGN NODE_ALIGN
315 #ifdef REGALIGN_STRUCT
316 # define OP(p) ((p)->type)
317 # define OPERAND(p) (((struct regnode_string *)p)->string)
318 # define NODE_ALIGN(node)
319 # define ARG_LOC(p) (((struct regnode_1 *)p)->arg1)
320 # define ARG1_LOC(p) (((struct regnode_2 *)p)->arg1)
321 # define ARG2_LOC(p) (((struct regnode_2 *)p)->arg2)
322 # define NODE_STEP_REGNODE 1 /* sizeof(regnode)/sizeof(regnode) */
323 # define EXTRA_STEP_2ARGS EXTRA_SIZE(struct regnode_2)
325 # define OP(p) (*(p))
326 # define OPERAND(p) ((p) + 3)
327 # define ARG_LOC(p) (*(unsigned short*)(p+3))
328 # define ARG1_LOC(p) (*(unsigned short*)(p+3))
329 # define ARG2_LOC(p) (*(unsigned short*)(p+5))
330 typedef char* regnode;
331 # define NODE_STEP_REGNODE NODE_STEP_B
332 # define EXTRA_STEP_2ARGS 4
336 # define NODE_STEP_B 4
338 # define NODE_STEP_B 3
341 #define NEXTOPER(p) ((p) + NODE_STEP_REGNODE)
342 #define PREVOPER(p) ((p) - NODE_STEP_REGNODE)
344 #ifdef REGALIGN_STRUCT
345 # define FILL_ADVANCE_NODE(ptr, op) STMT_START { \
346 (ptr)->type = op; (ptr)->next_off = 0; (ptr)++; } STMT_END
347 # define FILL_ADVANCE_NODE_ARG(ptr, op, arg) STMT_START { \
348 ARG_SET(ptr, arg); FILL_ADVANCE_NODE(ptr, op); (ptr) += 1; } STMT_END
350 # define FILL_ADVANCE_NODE(ptr, op) STMT_START { \
351 *(ptr)++ = op; *(ptr)++ = '\0'; *(ptr)++ = '\0'; } STMT_END
352 # define FILL_ADVANCE_NODE_ARG(ptr, op, arg) STMT_START { \
353 ARG_SET(ptr, arg); FILL_ADVANCE_NODE(ptr, op); (ptr) += 2; } STMT_END
358 #define SIZE_ONLY (regcode == ®dummy)
360 /* Flags for first parameter byte of ANYOF */
361 #define ANYOF_INVERT 0x40
362 #define ANYOF_FOLD 0x20
363 #define ANYOF_LOCALE 0x10
364 #define ANYOF_ISA 0x0F
365 #define ANYOF_ALNUML 0x08
366 #define ANYOF_NALNUML 0x04
367 #define ANYOF_SPACEL 0x02
368 #define ANYOF_NSPACEL 0x01
370 #ifdef REGALIGN_STRUCT
371 #define ANY_SKIP ((33 - 1)/sizeof(regnode) + 1)
373 #define ANY_SKIP 32 /* overwrite the first byte of
378 * Utility definitions.
382 #define UCHARAT(p) ((int)*(unsigned char *)(p))
384 #define UCHARAT(p) ((int)*(p)&CHARMASK)
387 #define UCHARAT(p) regdummy
390 #define FAIL(m) croak ("/%.127s/: %s", regprecomp,m)
391 #define FAIL2(pat,m) re_croak2("/%.127s/: ",pat,regprecomp,m)
393 #define EXTRA_SIZE(guy) ((sizeof(guy)-1)/sizeof(struct regnode))
396 const static U8 regarglen[] = {
397 # ifdef REGALIGN_STRUCT
398 0,0,0,0,0,0,0,0,0,0,
399 /*CURLY*/ EXTRA_SIZE(struct regnode_2),
400 /*CURLYX*/ EXTRA_SIZE(struct regnode_2),
401 0,0,0,0,0,0,0,0,0,0,0,0,
402 /*REF*/ EXTRA_SIZE(struct regnode_1),
403 /*OPEN*/ EXTRA_SIZE(struct regnode_1),
404 /*CLOSE*/ EXTRA_SIZE(struct regnode_1),
406 /*IFMATCH*/ EXTRA_SIZE(struct regnode_1),
407 /*UNLESSM*/ EXTRA_SIZE(struct regnode_1),
408 0,0,0,0,0,0,0,0,0,0,0,0,
409 /*CURLYM*/ EXTRA_SIZE(struct regnode_2),
410 /*CURLYN*/ EXTRA_SIZE(struct regnode_2),
412 /*REFF*/ EXTRA_SIZE(struct regnode_1),
413 /*REFFL*/ EXTRA_SIZE(struct regnode_1),
414 /*EVAL*/ EXTRA_SIZE(struct regnode_1),
415 /*LONGJMP*/ EXTRA_SIZE(struct regnode_1),
416 /*BRANCHJ*/ EXTRA_SIZE(struct regnode_1),
417 /*IFTHEN*/ EXTRA_SIZE(struct regnode_1),
418 /*GROUPP*/ EXTRA_SIZE(struct regnode_1),
420 /*SUSPEND*/ EXTRA_SIZE(struct regnode_1),
421 /*RENUM*/ EXTRA_SIZE(struct regnode_1), 0,
423 0,0,0,0,0,0,0,0,0,0,
424 /*CURLY*/ 4, /*CURLYX*/ 4,
425 0,0,0,0,0,0,0,0,0,0,0,0,
426 /*REF*/ 2, /*OPEN*/ 2, /*CLOSE*/ 2,
427 0,0, /*IFMATCH*/ 2, /*UNLESSM*/ 2,
428 0,0,0,0,0,0,0,0,0,0,0,0,/*CURLYM*/ 4,/*CURLYN*/ 4,
429 0, /*REFF*/ 2, /*REFFL*/ 2, /*EVAL*/ 2, /*LONGJMP*/ 2, /*BRANCHJ*/ 2,
430 /*IFTHEN*/ 2, /*GROUPP*/ 2, /*LOGICAL*/ 0, /*RENUM*/ 2, /*RENUM*/ 2, 0,
434 const static char reg_off_by_arg[] = {
435 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0 .. 15 */
436 0,0,0,0,0,0,0,0,0,0,0,0,0, /*IFMATCH*/ 2, /*UNLESSM*/ 2, 0,
437 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 32 .. 47 */
438 0, /*LONGJMP*/ 1, /*BRANCHJ*/ 1, /*IFTHEN*/ 1, 0, 0,
439 /*RENUM*/ 1, /*RENUM*/ 1,0,
443 #define REG_SEEN_ZERO_LEN 1
444 #define REG_SEEN_LOOKBEHIND 2
445 #define REG_SEEN_GPOS 4
448 extern char *colors[4];
451 void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn));