Commit | Line | Data |
a0d0e21e |
1 | /* regcomp.c |
2 | */ |
3 | |
4 | /* |
5 | * "A fair jaw-cracker dwarf-language must be." --Samwise Gamgee |
6 | */ |
7 | |
a687059c |
8 | /* NOTE: this is derived from Henry Spencer's regexp code, and should not |
9 | * confused with the original package (see point 3 below). Thanks, Henry! |
10 | */ |
11 | |
12 | /* Additional note: this code is very heavily munged from Henry's version |
13 | * in places. In some spots I've traded clarity for efficiency, so don't |
14 | * blame Henry for some of the lack of readability. |
15 | */ |
16 | |
e50aee73 |
17 | /* The names of the functions have been changed from regcomp and |
18 | * regexec to pregcomp and pregexec in order to avoid conflicts |
19 | * with the POSIX routines of the same names. |
20 | */ |
21 | |
f0fcb552 |
22 | /*SUPPRESS 112*/ |
a687059c |
23 | /* |
e50aee73 |
24 | * pregcomp and pregexec -- regsub and regerror are not used in perl |
a687059c |
25 | * |
26 | * Copyright (c) 1986 by University of Toronto. |
27 | * Written by Henry Spencer. Not derived from licensed software. |
28 | * |
29 | * Permission is granted to anyone to use this software for any |
30 | * purpose on any computer system, and to redistribute it freely, |
31 | * subject to the following restrictions: |
32 | * |
33 | * 1. The author is not responsible for the consequences of use of |
34 | * this software, no matter how awful, even if they arise |
35 | * from defects in it. |
36 | * |
37 | * 2. The origin of this software must not be misrepresented, either |
38 | * by explicit claim or by omission. |
39 | * |
40 | * 3. Altered versions must be plainly marked as such, and must not |
41 | * be misrepresented as being the original software. |
42 | * |
43 | * |
44 | **** Alterations to Henry's code are... |
45 | **** |
9607fc9c |
46 | **** Copyright (c) 1991-1997, Larry Wall |
a687059c |
47 | **** |
9ef589d8 |
48 | **** You may distribute under the terms of either the GNU General Public |
49 | **** License or the Artistic License, as specified in the README file. |
50 | |
a687059c |
51 | * |
52 | * Beware that some of this code is subtly aware of the way operator |
53 | * precedence is structured in regular expressions. Serious changes in |
54 | * regular-expression syntax might require a total rethink. |
55 | */ |
56 | #include "EXTERN.h" |
57 | #include "perl.h" |
58 | #include "INTERN.h" |
c277df42 |
59 | |
60 | #define REG_COMP_C |
a687059c |
61 | #include "regcomp.h" |
62 | |
d4cce5f1 |
63 | #ifdef op |
11343788 |
64 | #undef op |
d4cce5f1 |
65 | #endif /* op */ |
11343788 |
66 | |
fe14fcc3 |
67 | #ifdef MSDOS |
68 | # if defined(BUGGY_MSC6) |
69 | /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */ |
70 | # pragma optimize("a",off) |
71 | /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/ |
72 | # pragma optimize("w",on ) |
73 | # endif /* BUGGY_MSC6 */ |
74 | #endif /* MSDOS */ |
75 | |
a687059c |
76 | #ifndef STATIC |
77 | #define STATIC static |
78 | #endif |
79 | |
80 | #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?') |
81 | #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \ |
82 | ((*s) == '{' && regcurly(s))) |
2b69d0c2 |
83 | #ifdef atarist |
84 | #define PERL_META "^$.[()|?+*\\" |
85 | #else |
a687059c |
86 | #define META "^$.[()|?+*\\" |
2b69d0c2 |
87 | #endif |
a687059c |
88 | |
35c8bce7 |
89 | #ifdef SPSTART |
90 | #undef SPSTART /* dratted cpp namespace... */ |
91 | #endif |
a687059c |
92 | /* |
93 | * Flags to be passed up and down. |
94 | */ |
a687059c |
95 | #define WORST 0 /* Worst case. */ |
a0d0e21e |
96 | #define HASWIDTH 0x1 /* Known never to match null string. */ |
97 | #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */ |
98 | #define SPSTART 0x4 /* Starts with * or +. */ |
99 | #define TRYAGAIN 0x8 /* Weeded out a declaration. */ |
a687059c |
100 | |
101 | /* |
e50aee73 |
102 | * Forward declarations for pregcomp()'s friends. |
a687059c |
103 | */ |
a0d0e21e |
104 | |
76e3520e |
105 | #ifndef PERL_OBJECT |
c277df42 |
106 | static regnode *reg _((I32, I32 *)); |
107 | static regnode *reganode _((U8, U32)); |
108 | static regnode *regatom _((I32 *)); |
109 | static regnode *regbranch _((I32 *, I32)); |
110 | static void regc _((U8, char *)); |
111 | static regnode *regclass _((void)); |
a0d0e21e |
112 | STATIC I32 regcurly _((char *)); |
c277df42 |
113 | static regnode *reg_node _((U8)); |
114 | static regnode *regpiece _((I32 *)); |
115 | static void reginsert _((U8, regnode *)); |
116 | static void regoptail _((regnode *, regnode *)); |
c277df42 |
117 | static void regtail _((regnode *, regnode *)); |
873ef191 |
118 | static char* regwhite _((char *, char *)); |
a0d0e21e |
119 | static char* nextchar _((void)); |
3bd495df |
120 | static void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn)); |
76e3520e |
121 | #endif |
a687059c |
122 | |
c277df42 |
123 | /* Length of a variant. */ |
124 | |
76e3520e |
125 | #ifndef PERL_OBJECT |
c277df42 |
126 | typedef struct { |
127 | I32 len_min; |
128 | I32 len_delta; |
129 | I32 pos_min; |
130 | I32 pos_delta; |
131 | SV *last_found; |
132 | I32 last_end; /* min value, <0 unless valid. */ |
133 | I32 last_start_min; |
134 | I32 last_start_max; |
135 | SV **longest; /* Either &l_fixed, or &l_float. */ |
136 | SV *longest_fixed; |
137 | I32 offset_fixed; |
138 | SV *longest_float; |
139 | I32 offset_float_min; |
140 | I32 offset_float_max; |
141 | I32 flags; |
142 | } scan_data_t; |
76e3520e |
143 | #endif |
c277df42 |
144 | |
145 | static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
146 | |
147 | #define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL) |
148 | #define SF_BEFORE_SEOL 0x1 |
149 | #define SF_BEFORE_MEOL 0x2 |
150 | #define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL) |
151 | #define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL) |
152 | |
09b7f37c |
153 | #ifdef NO_UNARY_PLUS |
154 | # define SF_FIX_SHIFT_EOL (0+2) |
155 | # define SF_FL_SHIFT_EOL (0+4) |
156 | #else |
157 | # define SF_FIX_SHIFT_EOL (+2) |
158 | # define SF_FL_SHIFT_EOL (+4) |
159 | #endif |
c277df42 |
160 | |
161 | #define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL) |
162 | #define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL) |
163 | |
164 | #define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL) |
165 | #define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */ |
166 | #define SF_IS_INF 0x40 |
167 | #define SF_HAS_PAR 0x80 |
168 | #define SF_IN_PAR 0x100 |
169 | #define SF_HAS_EVAL 0x200 |
4bfe0158 |
170 | #define SCF_DO_SUBSTR 0x400 |
c277df42 |
171 | |
76e3520e |
172 | STATIC void |
c277df42 |
173 | scan_commit(scan_data_t *data) |
174 | { |
175 | STRLEN l = SvCUR(data->last_found); |
176 | STRLEN old_l = SvCUR(*data->longest); |
177 | |
178 | if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) { |
179 | sv_setsv(*data->longest, data->last_found); |
180 | if (*data->longest == data->longest_fixed) { |
181 | data->offset_fixed = l ? data->last_start_min : data->pos_min; |
182 | if (data->flags & SF_BEFORE_EOL) |
183 | data->flags |
184 | |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL); |
185 | else |
186 | data->flags &= ~SF_FIX_BEFORE_EOL; |
187 | } else { |
188 | data->offset_float_min = l ? data->last_start_min : data->pos_min; |
189 | data->offset_float_max = (l |
190 | ? data->last_start_max |
191 | : data->pos_min + data->pos_delta); |
192 | if (data->flags & SF_BEFORE_EOL) |
193 | data->flags |
194 | |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL); |
195 | else |
196 | data->flags &= ~SF_FL_BEFORE_EOL; |
197 | } |
198 | } |
199 | SvCUR_set(data->last_found, 0); |
200 | data->last_end = -1; |
201 | data->flags &= ~SF_BEFORE_EOL; |
202 | } |
203 | |
c277df42 |
204 | /* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set |
205 | to the position after last scanned or to NULL. */ |
206 | |
76e3520e |
207 | STATIC I32 |
c277df42 |
208 | study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags) |
209 | /* scanp: Start here (read-write). */ |
210 | /* deltap: Write maxlen-minlen here. */ |
211 | /* last: Stop before this one. */ |
212 | { |
213 | I32 min = 0, pars = 0, code; |
214 | regnode *scan = *scanp, *next; |
215 | I32 delta = 0; |
216 | int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF); |
217 | I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0; |
218 | scan_data_t data_fake; |
219 | |
220 | while (scan && OP(scan) != END && scan < last) { |
221 | /* Peephole optimizer: */ |
222 | |
223 | if (regkind[(U8)OP(scan)] == EXACT) { |
224 | regnode *n = regnext(scan); |
225 | U32 stringok = 1; |
226 | #ifdef DEBUGGING |
227 | regnode *stop = scan; |
228 | #endif |
229 | |
230 | next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2; |
231 | /* Skip NOTHING, merge EXACT*. */ |
232 | while (n && |
233 | ( regkind[(U8)OP(n)] == NOTHING || |
234 | (stringok && (OP(n) == OP(scan)))) |
235 | && NEXT_OFF(n) |
236 | && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) { |
237 | if (OP(n) == TAIL || n > next) |
238 | stringok = 0; |
239 | if (regkind[(U8)OP(n)] == NOTHING) { |
240 | NEXT_OFF(scan) += NEXT_OFF(n); |
241 | next = n + NODE_STEP_REGNODE; |
242 | #ifdef DEBUGGING |
243 | if (stringok) |
244 | stop = n; |
245 | #endif |
246 | n = regnext(n); |
247 | } else { |
248 | int oldl = *OPERAND(scan); |
249 | regnode *nnext = regnext(n); |
250 | |
251 | if (oldl + *OPERAND(n) > U8_MAX) |
252 | break; |
253 | NEXT_OFF(scan) += NEXT_OFF(n); |
254 | *OPERAND(scan) += *OPERAND(n); |
255 | next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2; |
256 | /* Now we can overwrite *n : */ |
257 | Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1, |
258 | *OPERAND(n) + 1, char); |
259 | #ifdef DEBUGGING |
260 | if (stringok) |
261 | stop = next - 1; |
262 | #endif |
263 | n = nnext; |
264 | } |
265 | } |
266 | #ifdef DEBUGGING |
267 | /* Allow dumping */ |
268 | n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2; |
269 | while (n <= stop) { |
ca04da08 |
270 | /* Purify reports a benign UMR here sometimes, because we |
271 | * don't initialize the OP() slot of a node when that node |
272 | * is occupied by just the trailing null of the string in |
273 | * an EXACT node */ |
c277df42 |
274 | if (regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) { |
275 | OP(n) = OPTIMIZED; |
276 | NEXT_OFF(n) = 0; |
277 | } |
278 | n++; |
279 | } |
280 | #endif |
281 | |
282 | } |
283 | if (OP(scan) != CURLYX) { |
284 | int max = (reg_off_by_arg[OP(scan)] ? I32_MAX : U16_MAX); |
285 | int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan)); |
286 | int noff; |
287 | regnode *n = scan; |
288 | |
289 | /* Skip NOTHING and LONGJMP. */ |
290 | while ((n = regnext(n)) |
291 | && ((regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n))) |
292 | || ((OP(n) == LONGJMP) && (noff = ARG(n)))) |
293 | && off + noff < max) |
294 | off += noff; |
295 | if (reg_off_by_arg[OP(scan)]) |
296 | ARG(scan) = off; |
297 | else |
298 | NEXT_OFF(scan) = off; |
299 | } |
300 | if (OP(scan) == BRANCH || OP(scan) == BRANCHJ |
301 | || OP(scan) == IFTHEN || OP(scan) == SUSPEND) { |
302 | next = regnext(scan); |
303 | code = OP(scan); |
304 | |
305 | if (OP(next) == code || code == IFTHEN || code == SUSPEND) { |
306 | I32 max1 = 0, min1 = I32_MAX, num = 0; |
307 | |
308 | if (flags & SCF_DO_SUBSTR) |
309 | scan_commit(data); |
310 | while (OP(scan) == code) { |
311 | I32 deltanext, minnext; |
312 | |
313 | num++; |
314 | data_fake.flags = 0; |
315 | next = regnext(scan); |
316 | scan = NEXTOPER(scan); |
317 | if (code != BRANCH) |
318 | scan = NEXTOPER(scan); |
319 | /* We suppose the run is continuous, last=next...*/ |
320 | minnext = study_chunk(&scan, &deltanext, next, |
321 | &data_fake, 0); |
322 | if (min1 > minnext) |
323 | min1 = minnext; |
324 | if (max1 < minnext + deltanext) |
325 | max1 = minnext + deltanext; |
326 | if (deltanext == I32_MAX) |
327 | is_inf = 1; |
328 | scan = next; |
329 | if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR)) |
330 | pars++; |
405ff068 |
331 | if (data && (data_fake.flags & SF_HAS_EVAL)) |
c277df42 |
332 | data->flags |= SF_HAS_EVAL; |
333 | if (code == SUSPEND) |
334 | break; |
335 | } |
336 | if (code == IFTHEN && num < 2) /* Empty ELSE branch */ |
337 | min1 = 0; |
338 | if (flags & SCF_DO_SUBSTR) { |
339 | data->pos_min += min1; |
340 | data->pos_delta += max1 - min1; |
341 | if (max1 != min1 || is_inf) |
342 | data->longest = &(data->longest_float); |
343 | } |
344 | min += min1; |
345 | delta += max1 - min1; |
346 | } else if (code == BRANCHJ) /* single branch is optimized. */ |
347 | scan = NEXTOPER(NEXTOPER(scan)); |
348 | else /* single branch is optimized. */ |
349 | scan = NEXTOPER(scan); |
350 | continue; |
351 | } else if (OP(scan) == EXACT) { |
352 | min += *OPERAND(scan); |
353 | if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */ |
354 | I32 l = *OPERAND(scan); |
355 | |
356 | /* The code below prefers earlier match for fixed |
357 | offset, later match for variable offset. */ |
358 | if (data->last_end == -1) { /* Update the start info. */ |
359 | data->last_start_min = data->pos_min; |
360 | data->last_start_max = is_inf |
361 | ? I32_MAX : data->pos_min + data->pos_delta; |
362 | } |
161b471a |
363 | sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), l); |
c277df42 |
364 | data->last_end = data->pos_min + l; |
365 | data->pos_min += l; /* As in the first entry. */ |
366 | data->flags &= ~SF_BEFORE_EOL; |
367 | } |
368 | } else if (regkind[(U8)OP(scan)] == EXACT) { |
369 | if (flags & SCF_DO_SUBSTR) |
370 | scan_commit(data); |
371 | min += *OPERAND(scan); |
372 | if (data && (flags & SCF_DO_SUBSTR)) |
373 | data->pos_min += *OPERAND(scan); |
374 | } else if (strchr(varies,OP(scan))) { |
375 | I32 mincount, maxcount, minnext, deltanext, pos_before, fl; |
376 | regnode *oscan = scan; |
377 | |
378 | switch (regkind[(U8)OP(scan)]) { |
379 | case WHILEM: |
380 | scan = NEXTOPER(scan); |
381 | goto finish; |
382 | case PLUS: |
383 | if (flags & SCF_DO_SUBSTR) { |
384 | next = NEXTOPER(scan); |
385 | if (OP(next) == EXACT) { |
386 | mincount = 1; |
387 | maxcount = REG_INFTY; |
388 | next = regnext(scan); |
389 | scan = NEXTOPER(scan); |
390 | goto do_curly; |
391 | } |
392 | } |
393 | if (flags & SCF_DO_SUBSTR) |
394 | data->pos_min++; |
395 | min++; |
396 | /* Fall through. */ |
397 | case STAR: |
398 | is_inf = 1; |
399 | scan = regnext(scan); |
400 | if (flags & SCF_DO_SUBSTR) { |
401 | scan_commit(data); |
402 | data->longest = &(data->longest_float); |
403 | } |
404 | goto optimize_curly_tail; |
405 | case CURLY: |
406 | mincount = ARG1(scan); |
407 | maxcount = ARG2(scan); |
408 | next = regnext(scan); |
409 | scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS; |
410 | do_curly: |
411 | if (flags & SCF_DO_SUBSTR) { |
412 | if (mincount == 0) scan_commit(data); |
413 | pos_before = data->pos_min; |
414 | } |
415 | if (data) { |
416 | fl = data->flags; |
417 | data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL); |
418 | if (is_inf) |
419 | data->flags |= SF_IS_INF; |
420 | } |
421 | /* This will finish on WHILEM, setting scan, or on NULL: */ |
422 | minnext = study_chunk(&scan, &deltanext, last, data, |
423 | mincount == 0 |
424 | ? (flags & ~SCF_DO_SUBSTR) : flags); |
425 | if (!scan) /* It was not CURLYX, but CURLY. */ |
426 | scan = next; |
427 | if (dowarn && (minnext + deltanext == 0) |
428 | && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))) |
429 | warn("Strange *+?{} on zero-length expression"); |
430 | min += minnext * mincount; |
431 | is_inf |= (maxcount == REG_INFTY && (minnext + deltanext) > 0 |
432 | || deltanext == I32_MAX); |
433 | delta += (minnext + deltanext) * maxcount - minnext * mincount; |
434 | |
435 | /* Try powerful optimization CURLYX => CURLYN. */ |
c277df42 |
436 | if ( OP(oscan) == CURLYX && data |
437 | && data->flags & SF_IN_PAR |
438 | && !(data->flags & SF_HAS_EVAL) |
439 | && !deltanext && minnext == 1 ) { |
440 | /* Try to optimize to CURLYN. */ |
441 | regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; |
442 | regnode *nxt1 = nxt, *nxt2; |
443 | |
444 | /* Skip open. */ |
445 | nxt = regnext(nxt); |
446 | if (!strchr(simple,OP(nxt)) |
447 | && !(regkind[(U8)OP(nxt)] == EXACT |
448 | && *OPERAND(nxt) == 1)) |
449 | goto nogo; |
450 | nxt2 = nxt; |
451 | nxt = regnext(nxt); |
452 | if (OP(nxt) != CLOSE) |
453 | goto nogo; |
454 | /* Now we know that nxt2 is the only contents: */ |
455 | oscan->flags = ARG(nxt); |
456 | OP(oscan) = CURLYN; |
457 | OP(nxt1) = NOTHING; /* was OPEN. */ |
458 | #ifdef DEBUGGING |
459 | OP(nxt1 + 1) = OPTIMIZED; /* was count. */ |
460 | NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */ |
461 | NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */ |
462 | OP(nxt) = OPTIMIZED; /* was CLOSE. */ |
463 | OP(nxt + 1) = OPTIMIZED; /* was count. */ |
464 | NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */ |
465 | #endif |
466 | } |
c277df42 |
467 | nogo: |
468 | |
469 | /* Try optimization CURLYX => CURLYM. */ |
470 | if ( OP(oscan) == CURLYX && data |
c277df42 |
471 | && !(data->flags & SF_HAS_PAR) |
c277df42 |
472 | && !(data->flags & SF_HAS_EVAL) |
473 | && !deltanext ) { |
474 | /* XXXX How to optimize if data == 0? */ |
475 | /* Optimize to a simpler form. */ |
476 | regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */ |
477 | regnode *nxt2; |
478 | |
479 | OP(oscan) = CURLYM; |
480 | while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/ |
481 | && (OP(nxt2) != WHILEM)) |
482 | nxt = nxt2; |
483 | OP(nxt2) = SUCCEED; /* Whas WHILEM */ |
c277df42 |
484 | /* Need to optimize away parenths. */ |
485 | if (data->flags & SF_IN_PAR) { |
486 | /* Set the parenth number. */ |
487 | regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/ |
488 | |
489 | if (OP(nxt) != CLOSE) |
490 | FAIL("panic opt close"); |
491 | oscan->flags = ARG(nxt); |
492 | OP(nxt1) = OPTIMIZED; /* was OPEN. */ |
493 | OP(nxt) = OPTIMIZED; /* was CLOSE. */ |
494 | #ifdef DEBUGGING |
495 | OP(nxt1 + 1) = OPTIMIZED; /* was count. */ |
496 | OP(nxt + 1) = OPTIMIZED; /* was count. */ |
497 | NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */ |
498 | NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */ |
499 | #endif |
500 | #if 0 |
501 | while ( nxt1 && (OP(nxt1) != WHILEM)) { |
502 | regnode *nnxt = regnext(nxt1); |
503 | |
504 | if (nnxt == nxt) { |
505 | if (reg_off_by_arg[OP(nxt1)]) |
506 | ARG_SET(nxt1, nxt2 - nxt1); |
507 | else if (nxt2 - nxt1 < U16_MAX) |
508 | NEXT_OFF(nxt1) = nxt2 - nxt1; |
509 | else |
510 | OP(nxt) = NOTHING; /* Cannot beautify */ |
511 | } |
512 | nxt1 = nnxt; |
513 | } |
514 | #endif |
515 | /* Optimize again: */ |
516 | study_chunk(&nxt1, &deltanext, nxt, NULL, 0); |
517 | } else |
518 | oscan->flags = 0; |
c277df42 |
519 | } |
520 | if (data && fl & (SF_HAS_PAR|SF_IN_PAR)) |
521 | pars++; |
522 | if (flags & SCF_DO_SUBSTR) { |
523 | SV *last_str = Nullsv; |
524 | int counted = mincount != 0; |
525 | |
526 | if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */ |
527 | I32 b = pos_before >= data->last_start_min |
528 | ? pos_before : data->last_start_min; |
529 | STRLEN l; |
530 | char *s = SvPV(data->last_found, l); |
531 | |
532 | l -= b - data->last_start_min; |
533 | /* Get the added string: */ |
534 | last_str = newSVpv(s + b - data->last_start_min, l); |
535 | if (deltanext == 0 && pos_before == b) { |
536 | /* What was added is a constant string */ |
537 | if (mincount > 1) { |
538 | SvGROW(last_str, (mincount * l) + 1); |
539 | repeatcpy(SvPVX(last_str) + l, |
540 | SvPVX(last_str), l, mincount - 1); |
541 | SvCUR(last_str) *= mincount; |
542 | /* Add additional parts. */ |
543 | SvCUR_set(data->last_found, |
544 | SvCUR(data->last_found) - l); |
545 | sv_catsv(data->last_found, last_str); |
546 | data->last_end += l * (mincount - 1); |
547 | } |
548 | } |
549 | } |
550 | /* It is counted once already... */ |
551 | data->pos_min += minnext * (mincount - counted); |
552 | data->pos_delta += - counted * deltanext + |
553 | (minnext + deltanext) * maxcount - minnext * mincount; |
554 | if (mincount != maxcount) { |
555 | scan_commit(data); |
556 | if (mincount && last_str) { |
557 | sv_setsv(data->last_found, last_str); |
558 | data->last_end = data->pos_min; |
559 | data->last_start_min = |
560 | data->pos_min - SvCUR(last_str); |
561 | data->last_start_max = is_inf |
562 | ? I32_MAX |
563 | : data->pos_min + data->pos_delta |
564 | - SvCUR(last_str); |
565 | } |
566 | data->longest = &(data->longest_float); |
567 | } |
568 | } |
405ff068 |
569 | if (data && (fl & SF_HAS_EVAL)) |
c277df42 |
570 | data->flags |= SF_HAS_EVAL; |
571 | optimize_curly_tail: |
c277df42 |
572 | if (OP(oscan) != CURLYX) { |
573 | while (regkind[(U8)OP(next = regnext(oscan))] == NOTHING |
574 | && NEXT_OFF(next)) |
575 | NEXT_OFF(oscan) += NEXT_OFF(next); |
576 | } |
c277df42 |
577 | continue; |
578 | default: /* REF only? */ |
579 | if (flags & SCF_DO_SUBSTR) { |
580 | scan_commit(data); |
581 | data->longest = &(data->longest_float); |
582 | } |
583 | is_inf = 1; |
584 | break; |
585 | } |
586 | } else if (strchr(simple,OP(scan))) { |
587 | if (flags & SCF_DO_SUBSTR) { |
588 | scan_commit(data); |
589 | data->pos_min++; |
590 | } |
591 | min++; |
592 | } else if (regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) { |
593 | data->flags |= (OP(scan) == MEOL |
594 | ? SF_BEFORE_MEOL |
595 | : SF_BEFORE_SEOL); |
596 | } else if (regkind[(U8)OP(scan)] == BRANCHJ |
597 | && (scan->flags || data) |
598 | && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) { |
599 | I32 deltanext, minnext; |
600 | regnode *nscan; |
601 | |
602 | data_fake.flags = 0; |
603 | next = regnext(scan); |
604 | nscan = NEXTOPER(NEXTOPER(scan)); |
605 | minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0); |
606 | if (scan->flags) { |
607 | if (deltanext) { |
608 | FAIL("variable length lookbehind not implemented"); |
609 | } else if (minnext > U8_MAX) { |
610 | FAIL2("lookbehind longer than %d not implemented", U8_MAX); |
611 | } |
612 | scan->flags = minnext; |
613 | } |
614 | if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR)) |
615 | pars++; |
405ff068 |
616 | if (data && (data_fake.flags & SF_HAS_EVAL)) |
c277df42 |
617 | data->flags |= SF_HAS_EVAL; |
618 | } else if (OP(scan) == OPEN) { |
619 | pars++; |
620 | } else if (OP(scan) == CLOSE && ARG(scan) == is_par) { |
c277df42 |
621 | next = regnext(scan); |
622 | |
623 | if ( next && (OP(next) != WHILEM) && next < last) |
c277df42 |
624 | is_par = 0; /* Disable optimization */ |
625 | } else if (OP(scan) == EVAL) { |
626 | if (data) |
627 | data->flags |= SF_HAS_EVAL; |
628 | } |
629 | /* Else: zero-length, ignore. */ |
630 | scan = regnext(scan); |
631 | } |
632 | |
633 | finish: |
634 | *scanp = scan; |
635 | *deltap = is_inf ? I32_MAX : delta; |
636 | if (flags & SCF_DO_SUBSTR && is_inf) |
637 | data->pos_delta = I32_MAX - data->pos_min; |
638 | if (is_par > U8_MAX) |
639 | is_par = 0; |
640 | if (is_par && pars==1 && data) { |
641 | data->flags |= SF_IN_PAR; |
642 | data->flags &= ~SF_HAS_PAR; |
643 | } else if (pars && data) { |
644 | data->flags |= SF_HAS_PAR; |
645 | data->flags &= ~SF_IN_PAR; |
646 | } |
647 | return min; |
648 | } |
649 | |
76e3520e |
650 | STATIC I32 |
c277df42 |
651 | add_data(I32 n, char *s) |
652 | { |
77d41b28 |
653 | if (regcomp_rx->data) { |
654 | Renewc(regcomp_rx->data, |
655 | sizeof(*regcomp_rx->data) + sizeof(void*) * (regcomp_rx->data->count + n - 1), |
c277df42 |
656 | char, struct reg_data); |
77d41b28 |
657 | Renew(regcomp_rx->data->what, regcomp_rx->data->count + n, U8); |
658 | regcomp_rx->data->count += n; |
c277df42 |
659 | } else { |
77d41b28 |
660 | Newc(1207, regcomp_rx->data, sizeof(*regcomp_rx->data) + sizeof(void*) * (n - 1), |
c277df42 |
661 | char, struct reg_data); |
77d41b28 |
662 | New(1208, regcomp_rx->data->what, n, U8); |
663 | regcomp_rx->data->count = n; |
c277df42 |
664 | } |
77d41b28 |
665 | Copy(s, regcomp_rx->data->what + regcomp_rx->data->count - n, n, U8); |
666 | return regcomp_rx->data->count - n; |
c277df42 |
667 | } |
668 | |
a687059c |
669 | /* |
e50aee73 |
670 | - pregcomp - compile a regular expression into internal code |
a687059c |
671 | * |
672 | * We can't allocate space until we know how big the compiled form will be, |
673 | * but we can't compile it (and thus know how big it is) until we've got a |
674 | * place to put the code. So we cheat: we compile it twice, once with code |
675 | * generation turned off and size counting turned on, and once "for real". |
676 | * This also means that we don't allocate space until we are sure that the |
677 | * thing really will compile successfully, and we never have to move the |
678 | * code and thus invalidate pointers into it. (Note that it has to be in |
679 | * one piece because free() must be able to free it all.) [NB: not true in perl] |
680 | * |
681 | * Beware that the optimization-preparation code in here knows about some |
682 | * of the structure of the compiled regexp. [I'll say.] |
683 | */ |
684 | regexp * |
8ac85365 |
685 | pregcomp(char *exp, char *xend, PMOP *pm) |
a687059c |
686 | { |
a0d0e21e |
687 | register regexp *r; |
c277df42 |
688 | regnode *scan; |
689 | SV **longest; |
690 | SV *longest_fixed; |
691 | SV *longest_float; |
692 | regnode *first; |
a0d0e21e |
693 | I32 flags; |
a0d0e21e |
694 | I32 minlen = 0; |
695 | I32 sawplus = 0; |
696 | I32 sawopen = 0; |
697 | |
698 | if (exp == NULL) |
c277df42 |
699 | FAIL("NULL regexp argument"); |
a0d0e21e |
700 | |
bbce6d69 |
701 | regprecomp = savepvn(exp, xend - exp); |
c277df42 |
702 | DEBUG_r(PerlIO_printf(Perl_debug_log, "compiling RE `%*s'\n", |
703 | xend - exp, regprecomp)); |
a0d0e21e |
704 | regflags = pm->op_pmflags; |
bbce6d69 |
705 | regsawback = 0; |
706 | |
c277df42 |
707 | regseen = 0; |
708 | seen_zerolen = *exp == '^' ? -1 : 0; |
709 | extralen = 0; |
710 | |
bbce6d69 |
711 | /* First pass: determine size, legality. */ |
77d41b28 |
712 | regcomp_parse = exp; |
a0d0e21e |
713 | regxend = xend; |
a0d0e21e |
714 | regnaughty = 0; |
a0d0e21e |
715 | regnpar = 1; |
716 | regsize = 0L; |
717 | regcode = ®dummy; |
c277df42 |
718 | regc((U8)MAGIC, (char*)regcode); |
a0d0e21e |
719 | if (reg(0, &flags) == NULL) { |
720 | Safefree(regprecomp); |
721 | regprecomp = Nullch; |
722 | return(NULL); |
723 | } |
c277df42 |
724 | DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", regsize)); |
725 | |
726 | DEBUG_r( |
727 | if (!colorset) { |
728 | int i = 0; |
76e3520e |
729 | char *s = PerlEnv_getenv("TERMCAP_COLORS"); |
c277df42 |
730 | |
731 | colorset = 1; |
732 | if (s) { |
733 | colors[0] = s = savepv(s); |
734 | while (++i < 4) { |
735 | s = strchr(s, '\t'); |
736 | if (!s) |
737 | FAIL("Not enough TABs in TERMCAP_COLORS"); |
738 | *s = '\0'; |
739 | colors[i] = ++s; |
740 | } |
741 | } else { |
742 | while (i < 4) |
743 | colors[i++] = ""; |
744 | } |
745 | /* Reset colors: */ |
746 | PerlIO_printf(Perl_debug_log, "%s%s%s%s", |
747 | colors[0],colors[1],colors[2],colors[3]); |
748 | } |
749 | ); |
a0d0e21e |
750 | |
c277df42 |
751 | /* Small enough for pointer-storage convention? |
752 | If extralen==0, this means that we will not need long jumps. */ |
c277df42 |
753 | if (regsize >= 0x10000L && extralen) |
754 | regsize += extralen; |
755 | else |
756 | extralen = 0; |
a0d0e21e |
757 | |
bbce6d69 |
758 | /* Allocate space and initialize. */ |
c277df42 |
759 | Newc(1001, r, sizeof(regexp) + (unsigned)regsize * sizeof(regnode), |
760 | char, regexp); |
a0d0e21e |
761 | if (r == NULL) |
762 | FAIL("regexp out of space"); |
c277df42 |
763 | r->refcnt = 1; |
bbce6d69 |
764 | r->prelen = xend - exp; |
a0d0e21e |
765 | r->precomp = regprecomp; |
766 | r->subbeg = r->subbase = NULL; |
77d41b28 |
767 | regcomp_rx = r; |
bbce6d69 |
768 | |
769 | /* Second pass: emit code. */ |
77d41b28 |
770 | regcomp_parse = exp; |
bbce6d69 |
771 | regxend = xend; |
772 | regnaughty = 0; |
a0d0e21e |
773 | regnpar = 1; |
774 | regcode = r->program; |
c277df42 |
775 | regc((U8)MAGIC, (char*) regcode++); |
776 | r->data = 0; |
a0d0e21e |
777 | if (reg(0, &flags) == NULL) |
778 | return(NULL); |
779 | |
780 | /* Dig out information for optimizations. */ |
781 | pm->op_pmflags = regflags; |
a0d0e21e |
782 | r->reganch = 0; |
c277df42 |
783 | r->regstclass = NULL; |
a0d0e21e |
784 | r->naughty = regnaughty >= 10; /* Probably an expensive pattern. */ |
c277df42 |
785 | scan = r->program + 1; /* First BRANCH. */ |
2779dcf1 |
786 | |
787 | /* XXXX To minimize changes to RE engine we always allocate |
788 | 3-units-long substrs field. */ |
789 | Newz(1004, r->substrs, 1, struct reg_substr_data); |
790 | |
c277df42 |
791 | if (OP(scan) != BRANCH) { /* Only one top-level choice. */ |
792 | scan_data_t data; |
793 | I32 fake; |
c5254dd6 |
794 | STRLEN longest_float_length, longest_fixed_length; |
a0d0e21e |
795 | |
c277df42 |
796 | StructCopy(&zero_scan_data, &data, scan_data_t); |
a0d0e21e |
797 | first = scan; |
c277df42 |
798 | /* Skip introductions and multiplicators >= 1. */ |
a0d0e21e |
799 | while ((OP(first) == OPEN && (sawopen = 1)) || |
800 | (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) || |
801 | (OP(first) == PLUS) || |
802 | (OP(first) == MINMOD) || |
803 | (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) { |
804 | if (OP(first) == PLUS) |
805 | sawplus = 1; |
806 | else |
807 | first += regarglen[(U8)OP(first)]; |
808 | first = NEXTOPER(first); |
a687059c |
809 | } |
810 | |
a0d0e21e |
811 | /* Starting-point info. */ |
812 | again: |
c277df42 |
813 | if (OP(first) == EXACT); /* Empty, get anchored substr later. */ |
a0d0e21e |
814 | else if (strchr(simple+2,OP(first))) |
815 | r->regstclass = first; |
bbce6d69 |
816 | else if (regkind[(U8)OP(first)] == BOUND || |
817 | regkind[(U8)OP(first)] == NBOUND) |
a0d0e21e |
818 | r->regstclass = first; |
819 | else if (regkind[(U8)OP(first)] == BOL) { |
c277df42 |
820 | r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL); |
a0d0e21e |
821 | first = NEXTOPER(first); |
774d564b |
822 | goto again; |
823 | } |
824 | else if (OP(first) == GPOS) { |
825 | r->reganch |= ROPT_ANCH_GPOS; |
826 | first = NEXTOPER(first); |
827 | goto again; |
a0d0e21e |
828 | } |
829 | else if ((OP(first) == STAR && |
830 | regkind[(U8)OP(NEXTOPER(first))] == ANY) && |
831 | !(r->reganch & ROPT_ANCH) ) |
832 | { |
833 | /* turn .* into ^.* with an implied $*=1 */ |
774d564b |
834 | r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT; |
a0d0e21e |
835 | first = NEXTOPER(first); |
774d564b |
836 | goto again; |
a0d0e21e |
837 | } |
838 | if (sawplus && (!sawopen || !regsawback)) |
839 | r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */ |
840 | |
c277df42 |
841 | /* Scan is after the zeroth branch, first is atomic matcher. */ |
842 | DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n", |
843 | first - scan + 1)); |
a0d0e21e |
844 | /* |
845 | * If there's something expensive in the r.e., find the |
846 | * longest literal string that must appear and make it the |
847 | * regmust. Resolve ties in favor of later strings, since |
848 | * the regstart check works with the beginning of the r.e. |
849 | * and avoiding duplication strengthens checking. Not a |
850 | * strong reason, but sufficient in the absence of others. |
851 | * [Now we resolve ties in favor of the earlier string if |
c277df42 |
852 | * it happens that c_offset_min has been invalidated, since the |
a0d0e21e |
853 | * earlier string may buy us something the later one won't.] |
854 | */ |
a0d0e21e |
855 | minlen = 0; |
a687059c |
856 | |
c277df42 |
857 | data.longest_fixed = newSVpv("",0); |
858 | data.longest_float = newSVpv("",0); |
859 | data.last_found = newSVpv("",0); |
860 | data.longest = &(data.longest_fixed); |
861 | first = scan; |
862 | |
863 | minlen = study_chunk(&first, &fake, scan + regsize, /* Up to end */ |
864 | &data, SCF_DO_SUBSTR); |
865 | if ( regnpar == 1 && data.longest == &(data.longest_fixed) |
866 | && data.last_start_min == 0 && data.last_end > 0 |
867 | && !seen_zerolen |
868 | && (!(regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS))) |
869 | r->reganch |= ROPT_CHECK_ALL; |
870 | scan_commit(&data); |
871 | SvREFCNT_dec(data.last_found); |
872 | |
c5254dd6 |
873 | longest_float_length = SvCUR(data.longest_float); |
874 | if (longest_float_length |
c277df42 |
875 | || (data.flags & SF_FL_BEFORE_EOL |
876 | && (!(data.flags & SF_FL_BEFORE_MEOL) |
877 | || (regflags & PMf_MULTILINE)))) { |
878 | if (SvCUR(data.longest_fixed) |
879 | && data.offset_fixed == data.offset_float_min) |
880 | goto remove; /* Like in (a)+. */ |
881 | |
882 | r->float_substr = data.longest_float; |
883 | r->float_min_offset = data.offset_float_min; |
884 | r->float_max_offset = data.offset_float_max; |
2779dcf1 |
885 | fbm_compile(r->float_substr, 0); |
c277df42 |
886 | BmUSEFUL(r->float_substr) = 100; |
887 | if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */ |
888 | && (!(data.flags & SF_FL_BEFORE_MEOL) |
889 | || (regflags & PMf_MULTILINE))) |
890 | SvTAIL_on(r->float_substr); |
891 | } else { |
892 | remove: |
893 | r->float_substr = Nullsv; |
894 | SvREFCNT_dec(data.longest_float); |
c5254dd6 |
895 | longest_float_length = 0; |
a0d0e21e |
896 | } |
c277df42 |
897 | |
c5254dd6 |
898 | longest_fixed_length = SvCUR(data.longest_fixed); |
899 | if (longest_fixed_length |
c277df42 |
900 | || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */ |
901 | && (!(data.flags & SF_FIX_BEFORE_MEOL) |
902 | || (regflags & PMf_MULTILINE)))) { |
903 | r->anchored_substr = data.longest_fixed; |
904 | r->anchored_offset = data.offset_fixed; |
2779dcf1 |
905 | fbm_compile(r->anchored_substr, 0); |
c277df42 |
906 | BmUSEFUL(r->anchored_substr) = 100; |
907 | if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */ |
908 | && (!(data.flags & SF_FIX_BEFORE_MEOL) |
909 | || (regflags & PMf_MULTILINE))) |
910 | SvTAIL_on(r->anchored_substr); |
911 | } else { |
912 | r->anchored_substr = Nullsv; |
913 | SvREFCNT_dec(data.longest_fixed); |
c5254dd6 |
914 | longest_fixed_length = 0; |
a0d0e21e |
915 | } |
c277df42 |
916 | |
917 | /* A temporary algorithm prefers floated substr to fixed one to dig more info. */ |
c5254dd6 |
918 | if (longest_fixed_length > longest_float_length) { |
c277df42 |
919 | r->check_substr = r->anchored_substr; |
920 | r->check_offset_min = r->check_offset_max = r->anchored_offset; |
921 | if (r->reganch & ROPT_ANCH_SINGLE) |
922 | r->reganch |= ROPT_NOSCAN; |
923 | } else { |
924 | r->check_substr = r->float_substr; |
925 | r->check_offset_min = data.offset_float_min; |
926 | r->check_offset_max = data.offset_float_max; |
a0d0e21e |
927 | } |
c277df42 |
928 | } else { |
929 | /* Several toplevels. Best we can is to set minlen. */ |
930 | I32 fake; |
931 | |
932 | DEBUG_r(PerlIO_printf(Perl_debug_log, "\n")); |
933 | scan = r->program + 1; |
934 | minlen = study_chunk(&scan, &fake, scan + regsize, NULL, 0); |
935 | r->check_substr = r->anchored_substr = r->float_substr = Nullsv; |
a0d0e21e |
936 | } |
937 | |
a0d0e21e |
938 | r->nparens = regnpar - 1; |
939 | r->minlen = minlen; |
c277df42 |
940 | if (regseen & REG_SEEN_GPOS) |
941 | r->reganch |= ROPT_GPOS_SEEN; |
942 | if (regseen & REG_SEEN_LOOKBEHIND) |
943 | r->reganch |= ROPT_LOOKBEHIND_SEEN; |
ce862d02 |
944 | if (regseen & REG_SEEN_EVAL) |
945 | r->reganch |= ROPT_EVAL_SEEN; |
a0d0e21e |
946 | Newz(1002, r->startp, regnpar, char*); |
947 | Newz(1002, r->endp, regnpar, char*); |
948 | DEBUG_r(regdump(r)); |
949 | return(r); |
a687059c |
950 | } |
951 | |
952 | /* |
953 | - reg - regular expression, i.e. main body or parenthesized thing |
954 | * |
955 | * Caller must absorb opening parenthesis. |
956 | * |
957 | * Combining parenthesis handling with the base level of regular expression |
958 | * is a trifle forced, but the need to tie the tails of the branches to what |
959 | * follows makes it hard to avoid. |
960 | */ |
76e3520e |
961 | STATIC regnode * |
8ac85365 |
962 | reg(I32 paren, I32 *flagp) |
c277df42 |
963 | /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */ |
a687059c |
964 | { |
c277df42 |
965 | register regnode *ret; /* Will be the head of the group. */ |
966 | register regnode *br; |
967 | register regnode *lastbr; |
968 | register regnode *ender = 0; |
a0d0e21e |
969 | register I32 parno = 0; |
c277df42 |
970 | I32 flags, oregflags = regflags, have_branch = 0, open = 0; |
971 | char c; |
a0d0e21e |
972 | |
973 | *flagp = HASWIDTH; /* Tentatively. */ |
974 | |
975 | /* Make an OPEN node, if parenthesized. */ |
976 | if (paren) { |
77d41b28 |
977 | if (*regcomp_parse == '?') { |
978 | regcomp_parse++; |
979 | paren = *regcomp_parse++; |
c277df42 |
980 | ret = NULL; /* For look-ahead/behind. */ |
a0d0e21e |
981 | switch (paren) { |
c277df42 |
982 | case '<': |
c277df42 |
983 | regseen |= REG_SEEN_LOOKBEHIND; |
77d41b28 |
984 | if (*regcomp_parse == '!') |
c277df42 |
985 | paren = ','; |
77d41b28 |
986 | if (*regcomp_parse != '=' && *regcomp_parse != '!') |
c277df42 |
987 | goto unknown; |
77d41b28 |
988 | regcomp_parse++; |
a0d0e21e |
989 | case '=': |
990 | case '!': |
c277df42 |
991 | seen_zerolen++; |
992 | case ':': |
993 | case '>': |
a0d0e21e |
994 | break; |
995 | case '$': |
996 | case '@': |
c277df42 |
997 | FAIL2("Sequence (?%c...) not implemented", (int)paren); |
a0d0e21e |
998 | break; |
999 | case '#': |
77d41b28 |
1000 | while (*regcomp_parse && *regcomp_parse != ')') |
1001 | regcomp_parse++; |
1002 | if (*regcomp_parse != ')') |
c277df42 |
1003 | FAIL("Sequence (?#... not terminated"); |
a0d0e21e |
1004 | nextchar(); |
1005 | *flagp = TRYAGAIN; |
1006 | return NULL; |
c277df42 |
1007 | case '{': |
1008 | { |
1009 | dTHR; |
1010 | I32 count = 1, n = 0; |
1011 | char c; |
77d41b28 |
1012 | char *s = regcomp_parse; |
c277df42 |
1013 | SV *sv; |
1014 | OP_4tree *sop, *rop; |
1015 | |
1016 | seen_zerolen++; |
ce862d02 |
1017 | regseen |= REG_SEEN_EVAL; |
77d41b28 |
1018 | while (count && (c = *regcomp_parse)) { |
1019 | if (c == '\\' && regcomp_parse[1]) |
1020 | regcomp_parse++; |
c277df42 |
1021 | else if (c == '{') |
1022 | count++; |
1023 | else if (c == '}') |
1024 | count--; |
77d41b28 |
1025 | regcomp_parse++; |
c277df42 |
1026 | } |
77d41b28 |
1027 | if (*regcomp_parse != ')') |
c277df42 |
1028 | FAIL("Sequence (?{...}) not terminated or not {}-balanced"); |
1029 | if (!SIZE_ONLY) { |
1030 | AV *av; |
1031 | |
77d41b28 |
1032 | if (regcomp_parse - 1 - s) |
1033 | sv = newSVpv(s, regcomp_parse - 1 - s); |
c277df42 |
1034 | else |
1035 | sv = newSVpv("", 0); |
1036 | |
1037 | rop = sv_compile_2op(sv, &sop, "re", &av); |
1038 | |
1039 | n = add_data(3, "nso"); |
77d41b28 |
1040 | regcomp_rx->data->data[n] = (void*)rop; |
1041 | regcomp_rx->data->data[n+1] = (void*)av; |
1042 | regcomp_rx->data->data[n+2] = (void*)sop; |
c277df42 |
1043 | SvREFCNT_dec(sv); |
cc6b7395 |
1044 | } else { /* First pass */ |
1045 | if (tainted) |
1046 | FAIL("Eval-group in insecure regular expression"); |
c277df42 |
1047 | } |
1048 | |
1049 | nextchar(); |
c277df42 |
1050 | return reganode(EVAL, n); |
1051 | } |
1052 | case '(': |
1053 | { |
77d41b28 |
1054 | if (regcomp_parse[0] == '?') { |
1055 | if (regcomp_parse[1] == '=' || regcomp_parse[1] == '!' |
1056 | || regcomp_parse[1] == '<' |
1057 | || regcomp_parse[1] == '{') { /* Lookahead or eval. */ |
c277df42 |
1058 | I32 flag; |
1059 | |
1060 | ret = reg_node(LOGICAL); |
1061 | regtail(ret, reg(1, &flag)); |
1062 | goto insert_if; |
1063 | } |
77d41b28 |
1064 | } else if (regcomp_parse[0] >= '1' && regcomp_parse[0] <= '9' ) { |
1065 | parno = atoi(regcomp_parse++); |
c277df42 |
1066 | |
77d41b28 |
1067 | while (isDIGIT(*regcomp_parse)) |
1068 | regcomp_parse++; |
c277df42 |
1069 | ret = reganode(GROUPP, parno); |
1070 | if ((c = *nextchar()) != ')') |
1071 | FAIL2("Switch (?(number%c not recognized", c); |
1072 | insert_if: |
1073 | regtail(ret, reganode(IFTHEN, 0)); |
1074 | br = regbranch(&flags, 1); |
1075 | if (br == NULL) |
1076 | br = reganode(LONGJMP, 0); |
1077 | else |
1078 | regtail(br, reganode(LONGJMP, 0)); |
1079 | c = *nextchar(); |
1080 | if (c == '|') { |
1081 | lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */ |
1082 | regbranch(&flags, 1); |
1083 | regtail(ret, lastbr); |
1084 | c = *nextchar(); |
1085 | } else |
1086 | lastbr = NULL; |
1087 | if (c != ')') |
1088 | FAIL("Switch (?(condition)... contains too many branches"); |
1089 | ender = reg_node(TAIL); |
1090 | regtail(br, ender); |
1091 | if (lastbr) { |
1092 | regtail(lastbr, ender); |
1093 | regtail(NEXTOPER(NEXTOPER(lastbr)), ender); |
1094 | } else |
1095 | regtail(ret, ender); |
1096 | return ret; |
1097 | } else { |
77d41b28 |
1098 | FAIL2("Unknown condition for (?(%.2s", regcomp_parse); |
c277df42 |
1099 | } |
1100 | } |
1b1626e4 |
1101 | case 0: |
c277df42 |
1102 | FAIL("Sequence (? incomplete"); |
1b1626e4 |
1103 | break; |
a0d0e21e |
1104 | default: |
77d41b28 |
1105 | --regcomp_parse; |
1106 | while (*regcomp_parse && strchr("iogcmsx", *regcomp_parse)) { |
1107 | if (*regcomp_parse != 'o') |
1108 | pmflag(®flags, *regcomp_parse); |
1109 | ++regcomp_parse; |
48c036b1 |
1110 | } |
c277df42 |
1111 | unknown: |
77d41b28 |
1112 | if (*regcomp_parse != ')') |
1113 | FAIL2("Sequence (?%c...) not recognized", *regcomp_parse); |
a0d0e21e |
1114 | nextchar(); |
1115 | *flagp = TRYAGAIN; |
1116 | return NULL; |
1117 | } |
1118 | } |
1119 | else { |
1120 | parno = regnpar; |
1121 | regnpar++; |
1122 | ret = reganode(OPEN, parno); |
c277df42 |
1123 | open = 1; |
a0d0e21e |
1124 | } |
1125 | } else |
1126 | ret = NULL; |
1127 | |
1128 | /* Pick up the branches, linking them together. */ |
c277df42 |
1129 | br = regbranch(&flags, 1); |
a0d0e21e |
1130 | if (br == NULL) |
1131 | return(NULL); |
77d41b28 |
1132 | if (*regcomp_parse == '|') { |
c277df42 |
1133 | if (!SIZE_ONLY && extralen) { |
1134 | reginsert(BRANCHJ, br); |
1135 | } else |
1136 | reginsert(BRANCH, br); |
1137 | have_branch = 1; |
1138 | if (SIZE_ONLY) |
1139 | extralen += 1; /* For BRANCHJ-BRANCH. */ |
1140 | } else if (paren == ':') { |
1141 | *flagp |= flags&SIMPLE; |
1142 | } |
1143 | if (open) { /* Starts with OPEN. */ |
1144 | regtail(ret, br); /* OPEN -> first. */ |
1145 | } else if (paren != '?') /* Not Conditional */ |
a0d0e21e |
1146 | ret = br; |
1147 | if (!(flags&HASWIDTH)) |
1148 | *flagp &= ~HASWIDTH; |
1149 | *flagp |= flags&SPSTART; |
c277df42 |
1150 | lastbr = br; |
77d41b28 |
1151 | while (*regcomp_parse == '|') { |
c277df42 |
1152 | if (!SIZE_ONLY && extralen) { |
1153 | ender = reganode(LONGJMP,0); |
1154 | regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */ |
1155 | } |
1156 | if (SIZE_ONLY) |
1157 | extralen += 2; /* Account for LONGJMP. */ |
a0d0e21e |
1158 | nextchar(); |
c277df42 |
1159 | br = regbranch(&flags, 0); |
a687059c |
1160 | if (br == NULL) |
a0d0e21e |
1161 | return(NULL); |
c277df42 |
1162 | regtail(lastbr, br); /* BRANCH -> BRANCH. */ |
1163 | lastbr = br; |
a687059c |
1164 | if (!(flags&HASWIDTH)) |
a0d0e21e |
1165 | *flagp &= ~HASWIDTH; |
a687059c |
1166 | *flagp |= flags&SPSTART; |
a0d0e21e |
1167 | } |
1168 | |
c277df42 |
1169 | if (have_branch || paren != ':') { |
1170 | /* Make a closing node, and hook it on the end. */ |
1171 | switch (paren) { |
1172 | case ':': |
1173 | ender = reg_node(TAIL); |
1174 | break; |
1175 | case 1: |
1176 | ender = reganode(CLOSE, parno); |
1177 | break; |
1178 | case '<': |
1179 | case '>': |
1180 | case ',': |
1181 | case '=': |
1182 | case '!': |
1183 | ender = reg_node(SUCCEED); |
1184 | *flagp &= ~HASWIDTH; |
1185 | break; |
1186 | case 0: |
1187 | ender = reg_node(END); |
1188 | break; |
1189 | } |
1190 | regtail(lastbr, ender); |
a0d0e21e |
1191 | |
c277df42 |
1192 | if (have_branch) { |
1193 | /* Hook the tails of the branches to the closing node. */ |
1194 | for (br = ret; br != NULL; br = regnext(br)) { |
1195 | regoptail(br, ender); |
1196 | } |
1197 | } |
a0d0e21e |
1198 | } |
c277df42 |
1199 | |
1200 | { |
1201 | char *p; |
1202 | static char parens[] = "=!<,>"; |
1203 | |
1204 | if (paren && (p = strchr(parens, paren))) { |
1205 | int node = ((p - parens) % 2) ? UNLESSM : IFMATCH; |
1206 | int flag = (p - parens) > 1; |
1207 | |
1208 | if (paren == '>') |
1209 | node = SUSPEND, flag = 0; |
1210 | reginsert(node,ret); |
c277df42 |
1211 | ret->flags = flag; |
c277df42 |
1212 | regtail(ret, reg_node(TAIL)); |
1213 | } |
a0d0e21e |
1214 | } |
1215 | |
1216 | /* Check for proper termination. */ |
77d41b28 |
1217 | if (paren && (regcomp_parse >= regxend || *nextchar() != ')')) { |
a0d0e21e |
1218 | FAIL("unmatched () in regexp"); |
77d41b28 |
1219 | } else if (!paren && regcomp_parse < regxend) { |
1220 | if (*regcomp_parse == ')') { |
a0d0e21e |
1221 | FAIL("unmatched () in regexp"); |
1222 | } else |
1223 | FAIL("junk on end of regexp"); /* "Can't happen". */ |
1224 | /* NOTREACHED */ |
1225 | } |
c277df42 |
1226 | if (paren != 0) { |
1227 | regflags = oregflags; |
1228 | } |
a687059c |
1229 | |
a0d0e21e |
1230 | return(ret); |
a687059c |
1231 | } |
1232 | |
1233 | /* |
1234 | - regbranch - one alternative of an | operator |
1235 | * |
1236 | * Implements the concatenation operator. |
1237 | */ |
76e3520e |
1238 | STATIC regnode * |
c277df42 |
1239 | regbranch(I32 *flagp, I32 first) |
a687059c |
1240 | { |
c277df42 |
1241 | register regnode *ret; |
1242 | register regnode *chain = NULL; |
1243 | register regnode *latest; |
1244 | I32 flags = 0, c = 0; |
a0d0e21e |
1245 | |
c277df42 |
1246 | if (first) |
1247 | ret = NULL; |
1248 | else { |
1249 | if (!SIZE_ONLY && extralen) |
1250 | ret = reganode(BRANCHJ,0); |
1251 | else |
1252 | ret = reg_node(BRANCH); |
1253 | } |
1254 | |
1255 | if (!first && SIZE_ONLY) |
1256 | extralen += 1; /* BRANCHJ */ |
1257 | |
1258 | *flagp = WORST; /* Tentatively. */ |
a0d0e21e |
1259 | |
77d41b28 |
1260 | regcomp_parse--; |
a0d0e21e |
1261 | nextchar(); |
77d41b28 |
1262 | while (regcomp_parse < regxend && *regcomp_parse != '|' && *regcomp_parse != ')') { |
a0d0e21e |
1263 | flags &= ~TRYAGAIN; |
1264 | latest = regpiece(&flags); |
1265 | if (latest == NULL) { |
1266 | if (flags & TRYAGAIN) |
1267 | continue; |
1268 | return(NULL); |
c277df42 |
1269 | } else if (ret == NULL) |
1270 | ret = latest; |
a0d0e21e |
1271 | *flagp |= flags&HASWIDTH; |
c277df42 |
1272 | if (chain == NULL) /* First piece. */ |
a0d0e21e |
1273 | *flagp |= flags&SPSTART; |
1274 | else { |
1275 | regnaughty++; |
1276 | regtail(chain, latest); |
a687059c |
1277 | } |
a0d0e21e |
1278 | chain = latest; |
c277df42 |
1279 | c++; |
1280 | } |
1281 | if (chain == NULL) { /* Loop ran zero times. */ |
1282 | chain = reg_node(NOTHING); |
1283 | if (ret == NULL) |
1284 | ret = chain; |
1285 | } |
1286 | if (c == 1) { |
1287 | *flagp |= flags&SIMPLE; |
a0d0e21e |
1288 | } |
a687059c |
1289 | |
a0d0e21e |
1290 | return(ret); |
a687059c |
1291 | } |
1292 | |
1293 | /* |
1294 | - regpiece - something followed by possible [*+?] |
1295 | * |
1296 | * Note that the branching code sequences used for ? and the general cases |
1297 | * of * and + are somewhat optimized: they use the same NOTHING node as |
1298 | * both the endmarker for their branch list and the body of the last branch. |
1299 | * It might seem that this node could be dispensed with entirely, but the |
1300 | * endmarker role is not redundant. |
1301 | */ |
76e3520e |
1302 | STATIC regnode * |
8ac85365 |
1303 | regpiece(I32 *flagp) |
a687059c |
1304 | { |
c277df42 |
1305 | register regnode *ret; |
a0d0e21e |
1306 | register char op; |
1307 | register char *next; |
1308 | I32 flags; |
77d41b28 |
1309 | char *origparse = regcomp_parse; |
a0d0e21e |
1310 | char *maxpos; |
1311 | I32 min; |
c277df42 |
1312 | I32 max = REG_INFTY; |
a0d0e21e |
1313 | |
1314 | ret = regatom(&flags); |
1315 | if (ret == NULL) { |
1316 | if (flags & TRYAGAIN) |
1317 | *flagp |= TRYAGAIN; |
1318 | return(NULL); |
1319 | } |
1320 | |
77d41b28 |
1321 | op = *regcomp_parse; |
a0d0e21e |
1322 | |
77d41b28 |
1323 | if (op == '{' && regcurly(regcomp_parse)) { |
1324 | next = regcomp_parse + 1; |
a0d0e21e |
1325 | maxpos = Nullch; |
1326 | while (isDIGIT(*next) || *next == ',') { |
1327 | if (*next == ',') { |
1328 | if (maxpos) |
1329 | break; |
1330 | else |
1331 | maxpos = next; |
a687059c |
1332 | } |
a0d0e21e |
1333 | next++; |
1334 | } |
1335 | if (*next == '}') { /* got one */ |
1336 | if (!maxpos) |
1337 | maxpos = next; |
77d41b28 |
1338 | regcomp_parse++; |
1339 | min = atoi(regcomp_parse); |
a0d0e21e |
1340 | if (*maxpos == ',') |
1341 | maxpos++; |
1342 | else |
77d41b28 |
1343 | maxpos = regcomp_parse; |
a0d0e21e |
1344 | max = atoi(maxpos); |
1345 | if (!max && *maxpos != '0') |
c277df42 |
1346 | max = REG_INFTY; /* meaning "infinity" */ |
1347 | else if (max >= REG_INFTY) |
1348 | FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1); |
77d41b28 |
1349 | regcomp_parse = next; |
a0d0e21e |
1350 | nextchar(); |
1351 | |
1352 | do_curly: |
1353 | if ((flags&SIMPLE)) { |
1354 | regnaughty += 2 + regnaughty / 2; |
1355 | reginsert(CURLY, ret); |
1356 | } |
1357 | else { |
1358 | regnaughty += 4 + regnaughty; /* compound interest */ |
c277df42 |
1359 | regtail(ret, reg_node(WHILEM)); |
1360 | if (!SIZE_ONLY && extralen) { |
1361 | reginsert(LONGJMP,ret); |
1362 | reginsert(NOTHING,ret); |
1363 | NEXT_OFF(ret) = 3; /* Go over LONGJMP. */ |
1364 | } |
a0d0e21e |
1365 | reginsert(CURLYX,ret); |
c277df42 |
1366 | if (!SIZE_ONLY && extralen) |
1367 | NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */ |
1368 | regtail(ret, reg_node(NOTHING)); |
1369 | if (SIZE_ONLY) |
1370 | extralen += 3; |
a0d0e21e |
1371 | } |
c277df42 |
1372 | ret->flags = 0; |
a0d0e21e |
1373 | |
1374 | if (min > 0) |
1375 | *flagp = (WORST|HASWIDTH); |
1376 | if (max && max < min) |
c277df42 |
1377 | FAIL("Can't do {n,m} with n > m"); |
1378 | if (!SIZE_ONLY) { |
1379 | ARG1_SET(ret, min); |
1380 | ARG2_SET(ret, max); |
a687059c |
1381 | } |
a687059c |
1382 | |
a0d0e21e |
1383 | goto nest_check; |
a687059c |
1384 | } |
a0d0e21e |
1385 | } |
a687059c |
1386 | |
a0d0e21e |
1387 | if (!ISMULT1(op)) { |
1388 | *flagp = flags; |
a687059c |
1389 | return(ret); |
a0d0e21e |
1390 | } |
bb20fd44 |
1391 | |
c277df42 |
1392 | #if 0 /* Now runtime fix should be reliable. */ |
bb20fd44 |
1393 | if (!(flags&HASWIDTH) && op != '?') |
c277df42 |
1394 | FAIL("regexp *+ operand could be empty"); |
1395 | #endif |
bb20fd44 |
1396 | |
a0d0e21e |
1397 | nextchar(); |
1398 | |
1399 | *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH); |
1400 | |
1401 | if (op == '*' && (flags&SIMPLE)) { |
1402 | reginsert(STAR, ret); |
c277df42 |
1403 | ret->flags = 0; |
a0d0e21e |
1404 | regnaughty += 4; |
1405 | } |
1406 | else if (op == '*') { |
1407 | min = 0; |
1408 | goto do_curly; |
1409 | } else if (op == '+' && (flags&SIMPLE)) { |
1410 | reginsert(PLUS, ret); |
c277df42 |
1411 | ret->flags = 0; |
a0d0e21e |
1412 | regnaughty += 3; |
1413 | } |
1414 | else if (op == '+') { |
1415 | min = 1; |
1416 | goto do_curly; |
1417 | } else if (op == '?') { |
1418 | min = 0; max = 1; |
1419 | goto do_curly; |
1420 | } |
1421 | nest_check: |
c277df42 |
1422 | if (dowarn && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) { |
a0d0e21e |
1423 | warn("%.*s matches null string many times", |
77d41b28 |
1424 | regcomp_parse - origparse, origparse); |
a0d0e21e |
1425 | } |
1426 | |
77d41b28 |
1427 | if (*regcomp_parse == '?') { |
a0d0e21e |
1428 | nextchar(); |
1429 | reginsert(MINMOD, ret); |
c277df42 |
1430 | regtail(ret, ret + NODE_STEP_REGNODE); |
a0d0e21e |
1431 | } |
77d41b28 |
1432 | if (ISMULT2(regcomp_parse)) |
a0d0e21e |
1433 | FAIL("nested *?+ in regexp"); |
1434 | |
1435 | return(ret); |
a687059c |
1436 | } |
1437 | |
1438 | /* |
1439 | - regatom - the lowest level |
1440 | * |
1441 | * Optimization: gobbles an entire sequence of ordinary characters so that |
1442 | * it can turn them into a single node, which is smaller to store and |
1443 | * faster to run. Backslashed characters are exceptions, each becoming a |
1444 | * separate node; the code is simpler that way and it's not worth fixing. |
1445 | * |
1446 | * [Yes, it is worth fixing, some scripts can run twice the speed.] |
1447 | */ |
76e3520e |
1448 | STATIC regnode * |
8ac85365 |
1449 | regatom(I32 *flagp) |
a687059c |
1450 | { |
c277df42 |
1451 | register regnode *ret = 0; |
a0d0e21e |
1452 | I32 flags; |
1453 | |
1454 | *flagp = WORST; /* Tentatively. */ |
1455 | |
1456 | tryagain: |
77d41b28 |
1457 | switch (*regcomp_parse) { |
a0d0e21e |
1458 | case '^': |
c277df42 |
1459 | seen_zerolen++; |
a0d0e21e |
1460 | nextchar(); |
1461 | if (regflags & PMf_MULTILINE) |
c277df42 |
1462 | ret = reg_node(MBOL); |
a0d0e21e |
1463 | else if (regflags & PMf_SINGLELINE) |
c277df42 |
1464 | ret = reg_node(SBOL); |
a0d0e21e |
1465 | else |
c277df42 |
1466 | ret = reg_node(BOL); |
a0d0e21e |
1467 | break; |
1468 | case '$': |
77d41b28 |
1469 | if (regcomp_parse[1]) |
c277df42 |
1470 | seen_zerolen++; |
a0d0e21e |
1471 | nextchar(); |
1472 | if (regflags & PMf_MULTILINE) |
c277df42 |
1473 | ret = reg_node(MEOL); |
a0d0e21e |
1474 | else if (regflags & PMf_SINGLELINE) |
c277df42 |
1475 | ret = reg_node(SEOL); |
a0d0e21e |
1476 | else |
c277df42 |
1477 | ret = reg_node(EOL); |
a0d0e21e |
1478 | break; |
1479 | case '.': |
1480 | nextchar(); |
1481 | if (regflags & PMf_SINGLELINE) |
c277df42 |
1482 | ret = reg_node(SANY); |
a0d0e21e |
1483 | else |
c277df42 |
1484 | ret = reg_node(ANY); |
a0d0e21e |
1485 | regnaughty++; |
1486 | *flagp |= HASWIDTH|SIMPLE; |
1487 | break; |
1488 | case '[': |
77d41b28 |
1489 | regcomp_parse++; |
a0d0e21e |
1490 | ret = regclass(); |
1491 | *flagp |= HASWIDTH|SIMPLE; |
1492 | break; |
1493 | case '(': |
1494 | nextchar(); |
1495 | ret = reg(1, &flags); |
1496 | if (ret == NULL) { |
1497 | if (flags & TRYAGAIN) |
1498 | goto tryagain; |
1499 | return(NULL); |
1500 | } |
c277df42 |
1501 | *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE); |
a0d0e21e |
1502 | break; |
1503 | case '|': |
1504 | case ')': |
1505 | if (flags & TRYAGAIN) { |
1506 | *flagp |= TRYAGAIN; |
1507 | return NULL; |
1508 | } |
77d41b28 |
1509 | FAIL2("internal urp in regexp at /%s/", regcomp_parse); |
a0d0e21e |
1510 | /* Supposed to be caught earlier. */ |
1511 | break; |
85afd4ae |
1512 | case '{': |
77d41b28 |
1513 | if (!regcurly(regcomp_parse)) { |
1514 | regcomp_parse++; |
85afd4ae |
1515 | goto defchar; |
1516 | } |
1517 | /* FALL THROUGH */ |
a0d0e21e |
1518 | case '?': |
1519 | case '+': |
1520 | case '*': |
3115e423 |
1521 | FAIL("?+*{} follows nothing in regexp"); |
a0d0e21e |
1522 | break; |
1523 | case '\\': |
77d41b28 |
1524 | switch (*++regcomp_parse) { |
a0d0e21e |
1525 | case 'A': |
c277df42 |
1526 | seen_zerolen++; |
1527 | ret = reg_node(SBOL); |
a0d0e21e |
1528 | *flagp |= SIMPLE; |
1529 | nextchar(); |
1530 | break; |
1531 | case 'G': |
c277df42 |
1532 | ret = reg_node(GPOS); |
1533 | regseen |= REG_SEEN_GPOS; |
a0d0e21e |
1534 | *flagp |= SIMPLE; |
1535 | nextchar(); |
1536 | break; |
1537 | case 'Z': |
c277df42 |
1538 | ret = reg_node(SEOL); |
a0d0e21e |
1539 | *flagp |= SIMPLE; |
1540 | nextchar(); |
1541 | break; |
1542 | case 'w': |
c277df42 |
1543 | ret = reg_node((regflags & PMf_LOCALE) ? ALNUML : ALNUM); |
a0d0e21e |
1544 | *flagp |= HASWIDTH|SIMPLE; |
1545 | nextchar(); |
1546 | break; |
1547 | case 'W': |
c277df42 |
1548 | ret = reg_node((regflags & PMf_LOCALE) ? NALNUML : NALNUM); |
a0d0e21e |
1549 | *flagp |= HASWIDTH|SIMPLE; |
1550 | nextchar(); |
1551 | break; |
1552 | case 'b': |
c277df42 |
1553 | seen_zerolen++; |
1554 | ret = reg_node((regflags & PMf_LOCALE) ? BOUNDL : BOUND); |
a0d0e21e |
1555 | *flagp |= SIMPLE; |
1556 | nextchar(); |
1557 | break; |
1558 | case 'B': |
c277df42 |
1559 | seen_zerolen++; |
1560 | ret = reg_node((regflags & PMf_LOCALE) ? NBOUNDL : NBOUND); |
a0d0e21e |
1561 | *flagp |= SIMPLE; |
1562 | nextchar(); |
1563 | break; |
1564 | case 's': |
c277df42 |
1565 | ret = reg_node((regflags & PMf_LOCALE) ? SPACEL : SPACE); |
a0d0e21e |
1566 | *flagp |= HASWIDTH|SIMPLE; |
1567 | nextchar(); |
1568 | break; |
1569 | case 'S': |
c277df42 |
1570 | ret = reg_node((regflags & PMf_LOCALE) ? NSPACEL : NSPACE); |
a0d0e21e |
1571 | *flagp |= HASWIDTH|SIMPLE; |
1572 | nextchar(); |
1573 | break; |
1574 | case 'd': |
c277df42 |
1575 | ret = reg_node(DIGIT); |
a0d0e21e |
1576 | *flagp |= HASWIDTH|SIMPLE; |
1577 | nextchar(); |
1578 | break; |
1579 | case 'D': |
c277df42 |
1580 | ret = reg_node(NDIGIT); |
a0d0e21e |
1581 | *flagp |= HASWIDTH|SIMPLE; |
1582 | nextchar(); |
1583 | break; |
1584 | case 'n': |
1585 | case 'r': |
1586 | case 't': |
1587 | case 'f': |
1588 | case 'e': |
1589 | case 'a': |
1590 | case 'x': |
1591 | case 'c': |
1592 | case '0': |
1593 | goto defchar; |
1594 | case '1': case '2': case '3': case '4': |
1595 | case '5': case '6': case '7': case '8': case '9': |
1596 | { |
77d41b28 |
1597 | I32 num = atoi(regcomp_parse); |
a0d0e21e |
1598 | |
1599 | if (num > 9 && num >= regnpar) |
1600 | goto defchar; |
1601 | else { |
1602 | regsawback = 1; |
c8756f30 |
1603 | ret = reganode((regflags & PMf_FOLD) |
1604 | ? ((regflags & PMf_LOCALE) ? REFFL : REFF) |
1605 | : REF, num); |
a0d0e21e |
1606 | *flagp |= HASWIDTH; |
77d41b28 |
1607 | while (isDIGIT(*regcomp_parse)) |
1608 | regcomp_parse++; |
1609 | regcomp_parse--; |
a0d0e21e |
1610 | nextchar(); |
1611 | } |
1612 | } |
1613 | break; |
1614 | case '\0': |
77d41b28 |
1615 | if (regcomp_parse >= regxend) |
a0d0e21e |
1616 | FAIL("trailing \\ in regexp"); |
1617 | /* FALL THROUGH */ |
1618 | default: |
1619 | goto defchar; |
1620 | } |
1621 | break; |
4633a7c4 |
1622 | |
1623 | case '#': |
1624 | if (regflags & PMf_EXTENDED) { |
77d41b28 |
1625 | while (regcomp_parse < regxend && *regcomp_parse != '\n') regcomp_parse++; |
1626 | if (regcomp_parse < regxend) |
4633a7c4 |
1627 | goto tryagain; |
1628 | } |
1629 | /* FALL THROUGH */ |
1630 | |
a0d0e21e |
1631 | default: { |
1632 | register I32 len; |
c277df42 |
1633 | register U8 ender; |
a0d0e21e |
1634 | register char *p; |
c277df42 |
1635 | char *oldp, *s; |
a0d0e21e |
1636 | I32 numlen; |
1637 | |
77d41b28 |
1638 | regcomp_parse++; |
a0d0e21e |
1639 | |
1640 | defchar: |
c277df42 |
1641 | ret = reg_node((regflags & PMf_FOLD) |
bbce6d69 |
1642 | ? ((regflags & PMf_LOCALE) ? EXACTFL : EXACTF) |
1643 | : EXACT); |
161b471a |
1644 | s = (char *) OPERAND(ret); |
c277df42 |
1645 | regc(0, s++); /* save spot for len */ |
77d41b28 |
1646 | for (len = 0, p = regcomp_parse - 1; |
a0d0e21e |
1647 | len < 127 && p < regxend; |
1648 | len++) |
1649 | { |
1650 | oldp = p; |
5b5a24f7 |
1651 | |
1652 | if (regflags & PMf_EXTENDED) |
1653 | p = regwhite(p, regxend); |
a0d0e21e |
1654 | switch (*p) { |
1655 | case '^': |
1656 | case '$': |
1657 | case '.': |
1658 | case '[': |
1659 | case '(': |
1660 | case ')': |
1661 | case '|': |
1662 | goto loopdone; |
1663 | case '\\': |
1664 | switch (*++p) { |
1665 | case 'A': |
1666 | case 'G': |
1667 | case 'Z': |
1668 | case 'w': |
1669 | case 'W': |
1670 | case 'b': |
1671 | case 'B': |
1672 | case 's': |
1673 | case 'S': |
1674 | case 'd': |
1675 | case 'D': |
1676 | --p; |
1677 | goto loopdone; |
1678 | case 'n': |
1679 | ender = '\n'; |
1680 | p++; |
a687059c |
1681 | break; |
a0d0e21e |
1682 | case 'r': |
1683 | ender = '\r'; |
1684 | p++; |
a687059c |
1685 | break; |
a0d0e21e |
1686 | case 't': |
1687 | ender = '\t'; |
1688 | p++; |
a687059c |
1689 | break; |
a0d0e21e |
1690 | case 'f': |
1691 | ender = '\f'; |
1692 | p++; |
a687059c |
1693 | break; |
a0d0e21e |
1694 | case 'e': |
1695 | ender = '\033'; |
1696 | p++; |
a687059c |
1697 | break; |
a0d0e21e |
1698 | case 'a': |
1699 | ender = '\007'; |
1700 | p++; |
a687059c |
1701 | break; |
a0d0e21e |
1702 | case 'x': |
1703 | ender = scan_hex(++p, 2, &numlen); |
1704 | p += numlen; |
a687059c |
1705 | break; |
a0d0e21e |
1706 | case 'c': |
1707 | p++; |
bbce6d69 |
1708 | ender = UCHARAT(p++); |
1709 | ender = toCTRL(ender); |
a687059c |
1710 | break; |
a0d0e21e |
1711 | case '0': case '1': case '2': case '3':case '4': |
1712 | case '5': case '6': case '7': case '8':case '9': |
1713 | if (*p == '0' || |
1714 | (isDIGIT(p[1]) && atoi(p) >= regnpar) ) { |
1715 | ender = scan_oct(p, 3, &numlen); |
1716 | p += numlen; |
1717 | } |
1718 | else { |
1719 | --p; |
1720 | goto loopdone; |
a687059c |
1721 | } |
1722 | break; |
a0d0e21e |
1723 | case '\0': |
1724 | if (p >= regxend) |
a687059c |
1725 | FAIL("trailing \\ in regexp"); |
1726 | /* FALL THROUGH */ |
a0d0e21e |
1727 | default: |
1728 | ender = *p++; |
1729 | break; |
1730 | } |
1731 | break; |
a687059c |
1732 | default: |
a0d0e21e |
1733 | ender = *p++; |
1734 | break; |
a687059c |
1735 | } |
5b5a24f7 |
1736 | if (regflags & PMf_EXTENDED) |
1737 | p = regwhite(p, regxend); |
a0d0e21e |
1738 | if (ISMULT2(p)) { /* Back off on ?+*. */ |
1739 | if (len) |
1740 | p = oldp; |
1741 | else { |
1742 | len++; |
c277df42 |
1743 | regc(ender, s++); |
a0d0e21e |
1744 | } |
1745 | break; |
a687059c |
1746 | } |
c277df42 |
1747 | regc(ender, s++); |
a0d0e21e |
1748 | } |
1749 | loopdone: |
77d41b28 |
1750 | regcomp_parse = p - 1; |
a0d0e21e |
1751 | nextchar(); |
1752 | if (len < 0) |
1753 | FAIL("internal disaster in regexp"); |
1754 | if (len > 0) |
1755 | *flagp |= HASWIDTH; |
1756 | if (len == 1) |
1757 | *flagp |= SIMPLE; |
c277df42 |
1758 | if (!SIZE_ONLY) |
a0d0e21e |
1759 | *OPERAND(ret) = len; |
c277df42 |
1760 | regc('\0', s++); |
1761 | if (SIZE_ONLY) { |
c277df42 |
1762 | regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode); |
c277df42 |
1763 | } else { |
1764 | regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode); |
1765 | } |
a687059c |
1766 | } |
a0d0e21e |
1767 | break; |
1768 | } |
a687059c |
1769 | |
a0d0e21e |
1770 | return(ret); |
a687059c |
1771 | } |
1772 | |
873ef191 |
1773 | STATIC char * |
8ac85365 |
1774 | regwhite(char *p, char *e) |
5b5a24f7 |
1775 | { |
1776 | while (p < e) { |
1777 | if (isSPACE(*p)) |
1778 | ++p; |
1779 | else if (*p == '#') { |
1780 | do { |
1781 | p++; |
1782 | } while (p < e && *p != '\n'); |
1783 | } |
1784 | else |
1785 | break; |
1786 | } |
1787 | return p; |
1788 | } |
1789 | |
76e3520e |
1790 | STATIC regnode * |
8ac85365 |
1791 | regclass(void) |
a687059c |
1792 | { |
c277df42 |
1793 | register char *opnd, *s; |
8ac85365 |
1794 | register I32 Class; |
a0d0e21e |
1795 | register I32 lastclass = 1234; |
1796 | register I32 range = 0; |
c277df42 |
1797 | register regnode *ret; |
a0d0e21e |
1798 | register I32 def; |
1799 | I32 numlen; |
1800 | |
161b471a |
1801 | s = opnd = (char *) OPERAND(regcode); |
c277df42 |
1802 | ret = reg_node(ANYOF); |
8ac85365 |
1803 | for (Class = 0; Class < 33; Class++) |
c277df42 |
1804 | regc(0, s++); |
77d41b28 |
1805 | if (*regcomp_parse == '^') { /* Complement of range. */ |
a0d0e21e |
1806 | regnaughty++; |
77d41b28 |
1807 | regcomp_parse++; |
c277df42 |
1808 | if (!SIZE_ONLY) |
bbce6d69 |
1809 | *opnd |= ANYOF_INVERT; |
1810 | } |
c277df42 |
1811 | if (!SIZE_ONLY) { |
1812 | regcode += ANY_SKIP; |
bbce6d69 |
1813 | if (regflags & PMf_FOLD) |
1814 | *opnd |= ANYOF_FOLD; |
1815 | if (regflags & PMf_LOCALE) |
1816 | *opnd |= ANYOF_LOCALE; |
c277df42 |
1817 | } else { |
1818 | regsize += ANY_SKIP; |
a0d0e21e |
1819 | } |
77d41b28 |
1820 | if (*regcomp_parse == ']' || *regcomp_parse == '-') |
a0d0e21e |
1821 | goto skipcond; /* allow 1st char to be ] or - */ |
77d41b28 |
1822 | while (regcomp_parse < regxend && *regcomp_parse != ']') { |
a0d0e21e |
1823 | skipcond: |
77d41b28 |
1824 | Class = UCHARAT(regcomp_parse++); |
1825 | if (Class == '[' && regcomp_parse + 1 < regxend && |
4599a1de |
1826 | /* I smell either [: or [= or [. -- POSIX has been here, right? */ |
77d41b28 |
1827 | (*regcomp_parse == ':' || *regcomp_parse == '=' || *regcomp_parse == '.')) { |
1828 | char posixccc = *regcomp_parse; |
1829 | char* posixccs = regcomp_parse++; |
4599a1de |
1830 | |
77d41b28 |
1831 | while (regcomp_parse < regxend && *regcomp_parse != posixccc) |
1832 | regcomp_parse++; |
1833 | if (regcomp_parse == regxend) |
4599a1de |
1834 | /* Grandfather lone [:, [=, [. */ |
77d41b28 |
1835 | regcomp_parse = posixccs; |
4599a1de |
1836 | else { |
77d41b28 |
1837 | regcomp_parse++; /* skip over the posixccc */ |
1838 | if (*regcomp_parse == ']') { |
4599a1de |
1839 | /* Not Implemented Yet. |
1840 | * (POSIX Extended Character Classes, that is) |
1841 | * The text between e.g. [: and :] would start |
77d41b28 |
1842 | * at posixccs + 1 and stop at regcomp_parse - 2. */ |
4599a1de |
1843 | if (dowarn && !SIZE_ONLY) |
1844 | warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc); |
77d41b28 |
1845 | regcomp_parse++; /* skip over the ending ] */ |
4599a1de |
1846 | } |
1847 | } |
1848 | } |
8ac85365 |
1849 | if (Class == '\\') { |
77d41b28 |
1850 | Class = UCHARAT(regcomp_parse++); |
8ac85365 |
1851 | switch (Class) { |
a0d0e21e |
1852 | case 'w': |
ae5c130c |
1853 | if (!SIZE_ONLY) { |
1854 | if (regflags & PMf_LOCALE) |
bbce6d69 |
1855 | *opnd |= ANYOF_ALNUML; |
ae5c130c |
1856 | else { |
1857 | for (Class = 0; Class < 256; Class++) |
1858 | if (isALNUM(Class)) |
1859 | ANYOF_SET(opnd, Class); |
1860 | } |
bbce6d69 |
1861 | } |
a0d0e21e |
1862 | lastclass = 1234; |
1863 | continue; |
1864 | case 'W': |
ae5c130c |
1865 | if (!SIZE_ONLY) { |
1866 | if (regflags & PMf_LOCALE) |
bbce6d69 |
1867 | *opnd |= ANYOF_NALNUML; |
ae5c130c |
1868 | else { |
1869 | for (Class = 0; Class < 256; Class++) |
1870 | if (!isALNUM(Class)) |
1871 | ANYOF_SET(opnd, Class); |
1872 | } |
bbce6d69 |
1873 | } |
a0d0e21e |
1874 | lastclass = 1234; |
1875 | continue; |
1876 | case 's': |
ae5c130c |
1877 | if (!SIZE_ONLY) { |
1878 | if (regflags & PMf_LOCALE) |
bbce6d69 |
1879 | *opnd |= ANYOF_SPACEL; |
ae5c130c |
1880 | else { |
1881 | for (Class = 0; Class < 256; Class++) |
1882 | if (isSPACE(Class)) |
1883 | ANYOF_SET(opnd, Class); |
1884 | } |
bbce6d69 |
1885 | } |
a0d0e21e |
1886 | lastclass = 1234; |
1887 | continue; |
1888 | case 'S': |
ae5c130c |
1889 | if (!SIZE_ONLY) { |
1890 | if (regflags & PMf_LOCALE) |
bbce6d69 |
1891 | *opnd |= ANYOF_NSPACEL; |
ae5c130c |
1892 | else { |
1893 | for (Class = 0; Class < 256; Class++) |
1894 | if (!isSPACE(Class)) |
1895 | ANYOF_SET(opnd, Class); |
1896 | } |
bbce6d69 |
1897 | } |
a0d0e21e |
1898 | lastclass = 1234; |
1899 | continue; |
1900 | case 'd': |
ae5c130c |
1901 | if (!SIZE_ONLY) { |
1902 | for (Class = '0'; Class <= '9'; Class++) |
1903 | ANYOF_SET(opnd, Class); |
1904 | } |
a0d0e21e |
1905 | lastclass = 1234; |
1906 | continue; |
1907 | case 'D': |
ae5c130c |
1908 | if (!SIZE_ONLY) { |
1909 | for (Class = 0; Class < '0'; Class++) |
1910 | ANYOF_SET(opnd, Class); |
1911 | for (Class = '9' + 1; Class < 256; Class++) |
1912 | ANYOF_SET(opnd, Class); |
1913 | } |
a0d0e21e |
1914 | lastclass = 1234; |
1915 | continue; |
1916 | case 'n': |
8ac85365 |
1917 | Class = '\n'; |
a0d0e21e |
1918 | break; |
1919 | case 'r': |
8ac85365 |
1920 | Class = '\r'; |
a0d0e21e |
1921 | break; |
1922 | case 't': |
8ac85365 |
1923 | Class = '\t'; |
a0d0e21e |
1924 | break; |
1925 | case 'f': |
8ac85365 |
1926 | Class = '\f'; |
a0d0e21e |
1927 | break; |
1928 | case 'b': |
8ac85365 |
1929 | Class = '\b'; |
a0d0e21e |
1930 | break; |
1931 | case 'e': |
8ac85365 |
1932 | Class = '\033'; |
a0d0e21e |
1933 | break; |
1934 | case 'a': |
8ac85365 |
1935 | Class = '\007'; |
a0d0e21e |
1936 | break; |
1937 | case 'x': |
77d41b28 |
1938 | Class = scan_hex(regcomp_parse, 2, &numlen); |
1939 | regcomp_parse += numlen; |
a0d0e21e |
1940 | break; |
1941 | case 'c': |
77d41b28 |
1942 | Class = UCHARAT(regcomp_parse++); |
8ac85365 |
1943 | Class = toCTRL(Class); |
a0d0e21e |
1944 | break; |
1945 | case '0': case '1': case '2': case '3': case '4': |
1946 | case '5': case '6': case '7': case '8': case '9': |
77d41b28 |
1947 | Class = scan_oct(--regcomp_parse, 3, &numlen); |
1948 | regcomp_parse += numlen; |
a0d0e21e |
1949 | break; |
1950 | } |
1951 | } |
1952 | if (range) { |
8ac85365 |
1953 | if (lastclass > Class) |
a0d0e21e |
1954 | FAIL("invalid [] range in regexp"); |
1955 | range = 0; |
1956 | } |
1957 | else { |
8ac85365 |
1958 | lastclass = Class; |
77d41b28 |
1959 | if (*regcomp_parse == '-' && regcomp_parse+1 < regxend && |
1960 | regcomp_parse[1] != ']') { |
1961 | regcomp_parse++; |
a0d0e21e |
1962 | range = 1; |
1963 | continue; /* do it next time */ |
1964 | } |
a687059c |
1965 | } |
ae5c130c |
1966 | if (!SIZE_ONLY) { |
1967 | for ( ; lastclass <= Class; lastclass++) |
1968 | ANYOF_SET(opnd, lastclass); |
1969 | } |
8ac85365 |
1970 | lastclass = Class; |
a0d0e21e |
1971 | } |
77d41b28 |
1972 | if (*regcomp_parse != ']') |
a0d0e21e |
1973 | FAIL("unmatched [] in regexp"); |
1974 | nextchar(); |
ae5c130c |
1975 | /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */ |
1976 | if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) { |
1977 | for (Class = 0; Class < 256; ++Class) { |
1978 | if (ANYOF_TEST(opnd, Class)) { |
1979 | I32 cf = fold[Class]; |
1980 | ANYOF_SET(opnd, cf); |
1981 | } |
1982 | } |
1983 | *opnd &= ~ANYOF_FOLD; |
1984 | } |
1985 | /* optimize inverted simple patterns (e.g. [^a-z]) */ |
1986 | if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) { |
1987 | for (Class = 0; Class < 32; ++Class) |
1988 | opnd[1 + Class] ^= 0xFF; |
1989 | *opnd = 0; |
1990 | } |
a0d0e21e |
1991 | return ret; |
1992 | } |
1993 | |
76e3520e |
1994 | STATIC char* |
8ac85365 |
1995 | nextchar(void) |
a0d0e21e |
1996 | { |
77d41b28 |
1997 | char* retval = regcomp_parse++; |
a0d0e21e |
1998 | |
4633a7c4 |
1999 | for (;;) { |
77d41b28 |
2000 | if (*regcomp_parse == '(' && regcomp_parse[1] == '?' && |
2001 | regcomp_parse[2] == '#') { |
2002 | while (*regcomp_parse && *regcomp_parse != ')') |
2003 | regcomp_parse++; |
2004 | regcomp_parse++; |
4633a7c4 |
2005 | continue; |
2006 | } |
2007 | if (regflags & PMf_EXTENDED) { |
77d41b28 |
2008 | if (isSPACE(*regcomp_parse)) { |
2009 | regcomp_parse++; |
748a9306 |
2010 | continue; |
2011 | } |
77d41b28 |
2012 | else if (*regcomp_parse == '#') { |
2013 | while (*regcomp_parse && *regcomp_parse != '\n') |
2014 | regcomp_parse++; |
2015 | regcomp_parse++; |
748a9306 |
2016 | continue; |
2017 | } |
748a9306 |
2018 | } |
4633a7c4 |
2019 | return retval; |
a0d0e21e |
2020 | } |
a687059c |
2021 | } |
2022 | |
2023 | /* |
c277df42 |
2024 | - reg_node - emit a node |
a0d0e21e |
2025 | */ |
76e3520e |
2026 | STATIC regnode * /* Location. */ |
c277df42 |
2027 | reg_node(U8 op) |
a687059c |
2028 | { |
c277df42 |
2029 | register regnode *ret; |
2030 | register regnode *ptr; |
a687059c |
2031 | |
a0d0e21e |
2032 | ret = regcode; |
c277df42 |
2033 | if (SIZE_ONLY) { |
2034 | SIZE_ALIGN(regsize); |
c277df42 |
2035 | regsize += 1; |
a0d0e21e |
2036 | return(ret); |
2037 | } |
a687059c |
2038 | |
c277df42 |
2039 | NODE_ALIGN_FILL(ret); |
a0d0e21e |
2040 | ptr = ret; |
c277df42 |
2041 | FILL_ADVANCE_NODE(ptr, op); |
a0d0e21e |
2042 | regcode = ptr; |
a687059c |
2043 | |
a0d0e21e |
2044 | return(ret); |
a687059c |
2045 | } |
2046 | |
2047 | /* |
a0d0e21e |
2048 | - reganode - emit a node with an argument |
2049 | */ |
76e3520e |
2050 | STATIC regnode * /* Location. */ |
c277df42 |
2051 | reganode(U8 op, U32 arg) |
fe14fcc3 |
2052 | { |
c277df42 |
2053 | register regnode *ret; |
2054 | register regnode *ptr; |
fe14fcc3 |
2055 | |
a0d0e21e |
2056 | ret = regcode; |
c277df42 |
2057 | if (SIZE_ONLY) { |
2058 | SIZE_ALIGN(regsize); |
c277df42 |
2059 | regsize += 2; |
a0d0e21e |
2060 | return(ret); |
2061 | } |
fe14fcc3 |
2062 | |
c277df42 |
2063 | NODE_ALIGN_FILL(ret); |
a0d0e21e |
2064 | ptr = ret; |
c277df42 |
2065 | FILL_ADVANCE_NODE_ARG(ptr, op, arg); |
a0d0e21e |
2066 | regcode = ptr; |
fe14fcc3 |
2067 | |
a0d0e21e |
2068 | return(ret); |
fe14fcc3 |
2069 | } |
2070 | |
2071 | /* |
a0d0e21e |
2072 | - regc - emit (if appropriate) a byte of code |
2073 | */ |
76e3520e |
2074 | STATIC void |
c277df42 |
2075 | regc(U8 b, char* s) |
a687059c |
2076 | { |
c277df42 |
2077 | if (!SIZE_ONLY) |
2078 | *s = b; |
a687059c |
2079 | } |
2080 | |
2081 | /* |
a0d0e21e |
2082 | - reginsert - insert an operator in front of already-emitted operand |
2083 | * |
2084 | * Means relocating the operand. |
2085 | */ |
76e3520e |
2086 | STATIC void |
c277df42 |
2087 | reginsert(U8 op, regnode *opnd) |
a687059c |
2088 | { |
c277df42 |
2089 | register regnode *src; |
2090 | register regnode *dst; |
2091 | register regnode *place; |
2092 | register int offset = regarglen[(U8)op]; |
2093 | |
2094 | /* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */ |
2095 | |
2096 | if (SIZE_ONLY) { |
2097 | regsize += NODE_STEP_REGNODE + offset; |
a0d0e21e |
2098 | return; |
2099 | } |
a687059c |
2100 | |
a0d0e21e |
2101 | src = regcode; |
c277df42 |
2102 | regcode += NODE_STEP_REGNODE + offset; |
a0d0e21e |
2103 | dst = regcode; |
2104 | while (src > opnd) |
c277df42 |
2105 | StructCopy(--src, --dst, regnode); |
a0d0e21e |
2106 | |
2107 | place = opnd; /* Op node, where operand used to be. */ |
c277df42 |
2108 | src = NEXTOPER(place); |
2109 | FILL_ADVANCE_NODE(place, op); |
2110 | Zero(src, offset, regnode); |
a687059c |
2111 | } |
2112 | |
2113 | /* |
c277df42 |
2114 | - regtail - set the next-pointer at the end of a node chain of p to val. |
a0d0e21e |
2115 | */ |
76e3520e |
2116 | STATIC void |
c277df42 |
2117 | regtail(regnode *p, regnode *val) |
a687059c |
2118 | { |
c277df42 |
2119 | register regnode *scan; |
2120 | register regnode *temp; |
a0d0e21e |
2121 | register I32 offset; |
2122 | |
c277df42 |
2123 | if (SIZE_ONLY) |
a0d0e21e |
2124 | return; |
2125 | |
2126 | /* Find last node. */ |
2127 | scan = p; |
2128 | for (;;) { |
2129 | temp = regnext(scan); |
2130 | if (temp == NULL) |
2131 | break; |
2132 | scan = temp; |
2133 | } |
a687059c |
2134 | |
c277df42 |
2135 | if (reg_off_by_arg[OP(scan)]) { |
2136 | ARG_SET(scan, val - scan); |
2137 | } else { |
2138 | NEXT_OFF(scan) = val - scan; |
2139 | } |
a687059c |
2140 | } |
2141 | |
2142 | /* |
a0d0e21e |
2143 | - regoptail - regtail on operand of first argument; nop if operandless |
2144 | */ |
76e3520e |
2145 | STATIC void |
c277df42 |
2146 | regoptail(regnode *p, regnode *val) |
a687059c |
2147 | { |
a0d0e21e |
2148 | /* "Operandless" and "op != BRANCH" are synonymous in practice. */ |
c277df42 |
2149 | if (p == NULL || SIZE_ONLY) |
2150 | return; |
2151 | if (regkind[(U8)OP(p)] == BRANCH) { |
2152 | regtail(NEXTOPER(p), val); |
2153 | } else if ( regkind[(U8)OP(p)] == BRANCHJ) { |
2154 | regtail(NEXTOPER(NEXTOPER(p)), val); |
2155 | } else |
a0d0e21e |
2156 | return; |
a687059c |
2157 | } |
2158 | |
2159 | /* |
2160 | - regcurly - a little FSA that accepts {\d+,?\d*} |
2161 | */ |
79072805 |
2162 | STATIC I32 |
8ac85365 |
2163 | regcurly(register char *s) |
a687059c |
2164 | { |
2165 | if (*s++ != '{') |
2166 | return FALSE; |
f0fcb552 |
2167 | if (!isDIGIT(*s)) |
a687059c |
2168 | return FALSE; |
f0fcb552 |
2169 | while (isDIGIT(*s)) |
a687059c |
2170 | s++; |
2171 | if (*s == ',') |
2172 | s++; |
f0fcb552 |
2173 | while (isDIGIT(*s)) |
a687059c |
2174 | s++; |
2175 | if (*s != '}') |
2176 | return FALSE; |
2177 | return TRUE; |
2178 | } |
2179 | |
a687059c |
2180 | |
76e3520e |
2181 | STATIC regnode * |
c277df42 |
2182 | dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l) |
2183 | { |
35ff7856 |
2184 | #ifdef DEBUGGING |
c277df42 |
2185 | register char op = EXACT; /* Arbitrary non-END op. */ |
2186 | register regnode *next, *onode; |
2187 | |
2188 | while (op != END && (!last || node < last)) { |
2189 | /* While that wasn't END last time... */ |
2190 | |
2191 | NODE_ALIGN(node); |
2192 | op = OP(node); |
2193 | if (op == CLOSE) |
2194 | l--; |
2195 | next = regnext(node); |
2196 | /* Where, what. */ |
2197 | if (OP(node) == OPTIMIZED) |
2198 | goto after_print; |
2199 | regprop(sv, node); |
2200 | PerlIO_printf(Perl_debug_log, "%4d%*s%s", node - start, |
2201 | 2*l + 1, "", SvPVX(sv)); |
2202 | if (next == NULL) /* Next ptr. */ |
2203 | PerlIO_printf(Perl_debug_log, "(0)"); |
2204 | else |
2205 | PerlIO_printf(Perl_debug_log, "(%d)", next - start); |
2206 | (void)PerlIO_putc(Perl_debug_log, '\n'); |
2207 | after_print: |
2208 | if (regkind[(U8)op] == BRANCHJ) { |
2209 | register regnode *nnode = (OP(next) == LONGJMP |
2210 | ? regnext(next) |
2211 | : next); |
2212 | if (last && nnode > last) |
2213 | nnode = last; |
2214 | node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1); |
2215 | } else if (regkind[(U8)op] == BRANCH) { |
2216 | node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1); |
2217 | } else if ( op == CURLY) { /* `next' might be very big: optimizer */ |
2218 | node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS, |
2219 | NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1); |
2220 | } else if (regkind[(U8)op] == CURLY && op != CURLYX) { |
2221 | node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS, |
2222 | next, sv, l + 1); |
2223 | } else if ( op == PLUS || op == STAR) { |
2224 | node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1); |
2225 | } else if (op == ANYOF) { |
2226 | node = NEXTOPER(node); |
2227 | node += ANY_SKIP; |
2228 | } else if (regkind[(U8)op] == EXACT) { |
2229 | /* Literal string, where present. */ |
2230 | node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode); |
2231 | node = NEXTOPER(node); |
2232 | } else { |
2233 | node = NEXTOPER(node); |
2234 | node += regarglen[(U8)op]; |
2235 | } |
2236 | if (op == CURLYX || op == OPEN) |
2237 | l++; |
2238 | else if (op == WHILEM) |
2239 | l--; |
2240 | } |
17c3b450 |
2241 | #endif /* DEBUGGING */ |
c277df42 |
2242 | return node; |
2243 | } |
2244 | |
a687059c |
2245 | /* |
fd181c75 |
2246 | - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form |
a687059c |
2247 | */ |
2248 | void |
8ac85365 |
2249 | regdump(regexp *r) |
a687059c |
2250 | { |
35ff7856 |
2251 | #ifdef DEBUGGING |
46fc3d4c |
2252 | SV *sv = sv_newmortal(); |
a687059c |
2253 | |
c277df42 |
2254 | (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0); |
a0d0e21e |
2255 | |
2256 | /* Header fields of interest. */ |
c277df42 |
2257 | if (r->anchored_substr) |
2258 | PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ", |
2259 | colors[0], |
2260 | SvPVX(r->anchored_substr), |
2261 | colors[1], |
2262 | SvTAIL(r->anchored_substr) ? "$" : "", |
2263 | r->anchored_offset); |
2264 | if (r->float_substr) |
2265 | PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ", |
2266 | colors[0], |
2267 | SvPVX(r->float_substr), |
2268 | colors[1], |
2269 | SvTAIL(r->float_substr) ? "$" : "", |
2270 | r->float_min_offset, r->float_max_offset); |
2271 | if (r->check_substr) |
2272 | PerlIO_printf(Perl_debug_log, |
2273 | r->check_substr == r->float_substr |
2274 | ? "(checking floating" : "(checking anchored"); |
2275 | if (r->reganch & ROPT_NOSCAN) |
2276 | PerlIO_printf(Perl_debug_log, " noscan"); |
2277 | if (r->reganch & ROPT_CHECK_ALL) |
2278 | PerlIO_printf(Perl_debug_log, " isall"); |
2279 | if (r->check_substr) |
2280 | PerlIO_printf(Perl_debug_log, ") "); |
2281 | |
46fc3d4c |
2282 | if (r->regstclass) { |
2283 | regprop(sv, r->regstclass); |
2284 | PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv)); |
2285 | } |
774d564b |
2286 | if (r->reganch & ROPT_ANCH) { |
2287 | PerlIO_printf(Perl_debug_log, "anchored"); |
2288 | if (r->reganch & ROPT_ANCH_BOL) |
2289 | PerlIO_printf(Perl_debug_log, "(BOL)"); |
c277df42 |
2290 | if (r->reganch & ROPT_ANCH_MBOL) |
2291 | PerlIO_printf(Perl_debug_log, "(MBOL)"); |
774d564b |
2292 | if (r->reganch & ROPT_ANCH_GPOS) |
2293 | PerlIO_printf(Perl_debug_log, "(GPOS)"); |
2294 | PerlIO_putc(Perl_debug_log, ' '); |
2295 | } |
c277df42 |
2296 | if (r->reganch & ROPT_GPOS_SEEN) |
2297 | PerlIO_printf(Perl_debug_log, "GPOS "); |
a0d0e21e |
2298 | if (r->reganch & ROPT_SKIP) |
760ac839 |
2299 | PerlIO_printf(Perl_debug_log, "plus "); |
a0d0e21e |
2300 | if (r->reganch & ROPT_IMPLICIT) |
760ac839 |
2301 | PerlIO_printf(Perl_debug_log, "implicit "); |
760ac839 |
2302 | PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen); |
ce862d02 |
2303 | if (r->reganch & ROPT_EVAL_SEEN) |
2304 | PerlIO_printf(Perl_debug_log, "with eval "); |
760ac839 |
2305 | PerlIO_printf(Perl_debug_log, "\n"); |
17c3b450 |
2306 | #endif /* DEBUGGING */ |
a687059c |
2307 | } |
2308 | |
2309 | /* |
a0d0e21e |
2310 | - regprop - printable representation of opcode |
2311 | */ |
46fc3d4c |
2312 | void |
c277df42 |
2313 | regprop(SV *sv, regnode *o) |
a687059c |
2314 | { |
35ff7856 |
2315 | #ifdef DEBUGGING |
a0d0e21e |
2316 | register char *p = 0; |
2317 | |
46fc3d4c |
2318 | sv_setpv(sv, ":"); |
11343788 |
2319 | switch (OP(o)) { |
a0d0e21e |
2320 | case BOL: |
2321 | p = "BOL"; |
2322 | break; |
2323 | case MBOL: |
2324 | p = "MBOL"; |
2325 | break; |
2326 | case SBOL: |
2327 | p = "SBOL"; |
2328 | break; |
2329 | case EOL: |
2330 | p = "EOL"; |
2331 | break; |
2332 | case MEOL: |
2333 | p = "MEOL"; |
2334 | break; |
2335 | case SEOL: |
2336 | p = "SEOL"; |
2337 | break; |
2338 | case ANY: |
2339 | p = "ANY"; |
2340 | break; |
2341 | case SANY: |
2342 | p = "SANY"; |
2343 | break; |
2344 | case ANYOF: |
2345 | p = "ANYOF"; |
2346 | break; |
2347 | case BRANCH: |
2348 | p = "BRANCH"; |
2349 | break; |
bbce6d69 |
2350 | case EXACT: |
c277df42 |
2351 | sv_catpvf(sv, "EXACT <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]); |
bbce6d69 |
2352 | break; |
2353 | case EXACTF: |
c277df42 |
2354 | sv_catpvf(sv, "EXACTF <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]); |
bbce6d69 |
2355 | break; |
2356 | case EXACTFL: |
c277df42 |
2357 | sv_catpvf(sv, "EXACTFL <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]); |
a0d0e21e |
2358 | break; |
2359 | case NOTHING: |
2360 | p = "NOTHING"; |
2361 | break; |
c277df42 |
2362 | case TAIL: |
2363 | p = "TAIL"; |
2364 | break; |
a0d0e21e |
2365 | case BACK: |
2366 | p = "BACK"; |
2367 | break; |
2368 | case END: |
2369 | p = "END"; |
2370 | break; |
a0d0e21e |
2371 | case BOUND: |
2372 | p = "BOUND"; |
2373 | break; |
bbce6d69 |
2374 | case BOUNDL: |
2375 | p = "BOUNDL"; |
2376 | break; |
a0d0e21e |
2377 | case NBOUND: |
2378 | p = "NBOUND"; |
2379 | break; |
bbce6d69 |
2380 | case NBOUNDL: |
2381 | p = "NBOUNDL"; |
a0d0e21e |
2382 | break; |
2383 | case CURLY: |
5dc0d613 |
2384 | sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o)); |
a0d0e21e |
2385 | break; |
c277df42 |
2386 | case CURLYM: |
c277df42 |
2387 | sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o)); |
c277df42 |
2388 | break; |
2389 | case CURLYN: |
c277df42 |
2390 | sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o)); |
c277df42 |
2391 | break; |
a0d0e21e |
2392 | case CURLYX: |
5dc0d613 |
2393 | sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o)); |
a0d0e21e |
2394 | break; |
2395 | case REF: |
c277df42 |
2396 | sv_catpvf(sv, "REF%d", ARG(o)); |
a0d0e21e |
2397 | break; |
c8756f30 |
2398 | case REFF: |
c277df42 |
2399 | sv_catpvf(sv, "REFF%d", ARG(o)); |
c8756f30 |
2400 | break; |
2401 | case REFFL: |
c277df42 |
2402 | sv_catpvf(sv, "REFFL%d", ARG(o)); |
c8756f30 |
2403 | break; |
a0d0e21e |
2404 | case OPEN: |
c277df42 |
2405 | sv_catpvf(sv, "OPEN%d", ARG(o)); |
a0d0e21e |
2406 | break; |
2407 | case CLOSE: |
c277df42 |
2408 | sv_catpvf(sv, "CLOSE%d", ARG(o)); |
a0d0e21e |
2409 | p = NULL; |
2410 | break; |
2411 | case STAR: |
2412 | p = "STAR"; |
2413 | break; |
2414 | case PLUS: |
2415 | p = "PLUS"; |
2416 | break; |
2417 | case MINMOD: |
2418 | p = "MINMOD"; |
2419 | break; |
774d564b |
2420 | case GPOS: |
2421 | p = "GPOS"; |
a0d0e21e |
2422 | break; |
2423 | case UNLESSM: |
c277df42 |
2424 | sv_catpvf(sv, "UNLESSM[-%d]", o->flags); |
a0d0e21e |
2425 | break; |
2426 | case IFMATCH: |
c277df42 |
2427 | sv_catpvf(sv, "IFMATCH[-%d]", o->flags); |
a0d0e21e |
2428 | break; |
2429 | case SUCCEED: |
2430 | p = "SUCCEED"; |
2431 | break; |
2432 | case WHILEM: |
2433 | p = "WHILEM"; |
2434 | break; |
bbce6d69 |
2435 | case DIGIT: |
2436 | p = "DIGIT"; |
2437 | break; |
2438 | case NDIGIT: |
2439 | p = "NDIGIT"; |
2440 | break; |
2441 | case ALNUM: |
2442 | p = "ALNUM"; |
2443 | break; |
2444 | case NALNUM: |
2445 | p = "NALNUM"; |
2446 | break; |
2447 | case SPACE: |
2448 | p = "SPACE"; |
2449 | break; |
2450 | case NSPACE: |
2451 | p = "NSPACE"; |
2452 | break; |
2453 | case ALNUML: |
2454 | p = "ALNUML"; |
2455 | break; |
2456 | case NALNUML: |
2457 | p = "NALNUML"; |
2458 | break; |
2459 | case SPACEL: |
2460 | p = "SPACEL"; |
2461 | break; |
2462 | case NSPACEL: |
2463 | p = "NSPACEL"; |
2464 | break; |
c277df42 |
2465 | case EVAL: |
2466 | p = "EVAL"; |
2467 | break; |
2468 | case LONGJMP: |
2469 | p = "LONGJMP"; |
2470 | break; |
2471 | case BRANCHJ: |
2472 | p = "BRANCHJ"; |
2473 | break; |
2474 | case IFTHEN: |
2475 | p = "IFTHEN"; |
2476 | break; |
2477 | case GROUPP: |
2478 | sv_catpvf(sv, "GROUPP%d", ARG(o)); |
2479 | break; |
2480 | case LOGICAL: |
2481 | p = "LOGICAL"; |
2482 | break; |
2483 | case SUSPEND: |
2484 | p = "SUSPEND"; |
2485 | break; |
2486 | case RENUM: |
2487 | p = "RENUM"; |
2488 | break; |
2489 | case OPTIMIZED: |
2490 | p = "OPTIMIZED"; |
2491 | break; |
a0d0e21e |
2492 | default: |
2493 | FAIL("corrupted regexp opcode"); |
2494 | } |
46fc3d4c |
2495 | if (p) |
2496 | sv_catpv(sv, p); |
17c3b450 |
2497 | #endif /* DEBUGGING */ |
35ff7856 |
2498 | } |
a687059c |
2499 | |
2b69d0c2 |
2500 | void |
8ac85365 |
2501 | pregfree(struct regexp *r) |
a687059c |
2502 | { |
c277df42 |
2503 | if (!r || (--r->refcnt > 0)) |
a0d0e21e |
2504 | return; |
c277df42 |
2505 | if (r->precomp) |
a0d0e21e |
2506 | Safefree(r->precomp); |
c277df42 |
2507 | if (r->subbase) |
a0d0e21e |
2508 | Safefree(r->subbase); |
a193d654 |
2509 | if (r->substrs) { |
2510 | if (r->anchored_substr) |
2511 | SvREFCNT_dec(r->anchored_substr); |
2512 | if (r->float_substr) |
2513 | SvREFCNT_dec(r->float_substr); |
2779dcf1 |
2514 | Safefree(r->substrs); |
a193d654 |
2515 | } |
c277df42 |
2516 | if (r->data) { |
2517 | int n = r->data->count; |
2518 | while (--n >= 0) { |
2519 | switch (r->data->what[n]) { |
2520 | case 's': |
2521 | SvREFCNT_dec((SV*)r->data->data[n]); |
2522 | break; |
2523 | case 'o': |
2524 | op_free((OP_4tree*)r->data->data[n]); |
2525 | break; |
2526 | case 'n': |
2527 | break; |
2528 | default: |
2529 | FAIL2("panic: regfree data code '%c'", r->data->what[n]); |
2530 | } |
2531 | } |
2532 | Safefree(r->data->what); |
2533 | Safefree(r->data); |
a0d0e21e |
2534 | } |
2535 | Safefree(r->startp); |
2536 | Safefree(r->endp); |
2537 | Safefree(r); |
a687059c |
2538 | } |
c277df42 |
2539 | |
2540 | /* |
2541 | - regnext - dig the "next" pointer out of a node |
2542 | * |
2543 | * [Note, when REGALIGN is defined there are two places in regmatch() |
2544 | * that bypass this code for speed.] |
2545 | */ |
2546 | regnode * |
2547 | regnext(register regnode *p) |
2548 | { |
2549 | register I32 offset; |
2550 | |
2551 | if (p == ®dummy) |
2552 | return(NULL); |
2553 | |
2554 | offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p)); |
2555 | if (offset == 0) |
2556 | return(NULL); |
2557 | |
c277df42 |
2558 | return(p+offset); |
c277df42 |
2559 | } |
2560 | |
01f988be |
2561 | STATIC void |
c277df42 |
2562 | re_croak2(const char* pat1,const char* pat2,...) |
c277df42 |
2563 | { |
2564 | va_list args; |
2565 | STRLEN l1 = strlen(pat1); |
2566 | STRLEN l2 = strlen(pat2); |
2567 | char buf[512]; |
2568 | char *message; |
2569 | |
2570 | if (l1 > 510) |
2571 | l1 = 510; |
2572 | if (l1 + l2 > 510) |
2573 | l2 = 510 - l1; |
2574 | Copy(pat1, buf, l1 , char); |
2575 | Copy(pat2, buf + l1, l2 , char); |
3b818b81 |
2576 | buf[l1 + l2] = '\n'; |
2577 | buf[l1 + l2 + 1] = '\0'; |
c277df42 |
2578 | va_start(args, pat2); |
c277df42 |
2579 | message = mess(buf, &args); |
2580 | va_end(args); |
2581 | l1 = strlen(message); |
2582 | if (l1 > 512) |
2583 | l1 = 512; |
2584 | Copy(message, buf, l1 , char); |
2585 | buf[l1] = '\0'; /* Overwrite \n */ |
2586 | croak("%s", buf); |
2587 | } |