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