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