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