3 * Copyright (C) 1993, 1994, 1996, 1997, 1999, 2000, 2001, 2003,
4 * 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * Definitions etc. for regexp(3) routines.
14 * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
15 * not the System V one.
17 #ifndef PLUGGABLE_RE_EXTENSION
18 /* we don't want to include this stuff if we are inside of
19 an external regex engine based on the core one - like re 'debug'*/
23 typedef SSize_t regnode_offset;
38 typedef struct regnode regnode;
40 struct reg_substr_data;
47 struct reg_substr_datum {
48 SSize_t min_offset; /* min pos (in chars) that substr must appear */
49 SSize_t max_offset; /* max pos (in chars) that substr must appear */
50 SV *substr; /* non-utf8 variant */
51 SV *utf8_substr; /* utf8 variant */
52 SSize_t end_shift; /* how many fixed chars must end the string */
54 struct reg_substr_data {
55 U8 check_ix; /* index into data[] of check substr */
56 struct reg_substr_datum data[3]; /* Actual array */
60 # define SV_SAVED_COPY SV *saved_copy; /* If non-NULL, SV which is COW from original */
62 # define SV_SAVED_COPY
65 /* offsets within a string of a particular /(.)/ capture */
67 typedef struct regexp_paren_pair {
70 /* 'start_tmp' records a new opening position before the matching end
71 * has been found, so that the old start and end values are still
73 * "abc" =~ /(.(?{print "[$1]"}))+/
75 * This field is not part of the API. */
79 # if defined(PERL_IN_REGCOMP_ANY) || defined(PERL_IN_UTF8_C)
80 # define _invlist_union(a, b, output) _invlist_union_maybe_complement_2nd(a, b, FALSE, output)
81 # define _invlist_intersection(a, b, output) _invlist_intersection_maybe_complement_2nd(a, b, FALSE, output)
83 /* Subtracting b from a leaves in a everything that was there that isn't in b,
84 * that is the intersection of a with b's complement */
85 # define _invlist_subtract(a, b, output) _invlist_intersection_maybe_complement_2nd(a, b, TRUE, output)
88 /* record the position of a (?{...}) within a pattern */
90 struct reg_code_block {
97 /* array of reg_code_block's plus header info */
99 struct reg_code_blocks {
100 int refcnt; /* we may be pointed to from a regex and from the savestack */
101 int count; /* how many code blocks */
102 struct reg_code_block *cb; /* array of reg_code_block's */
107 = for apidoc AyT||regexp
108 The regexp/REGEXP struct, see L<perlreapi> for further documentation
109 on the individual fields. The struct is ordered so that the most
110 commonly used fields are placed at the start.
112 Any patch that adds items to this struct will need to include
113 changes to F<sv.c> (C<Perl_re_dup()>) and F<regcomp.c>
114 (C<pregfree()>). This involves freeing or cloning items in the
115 regexp's data array based on the data item's type.
118 typedef struct regexp {
120 const struct regexp_engine* engine; /* what engine created this regexp? */
121 REGEXP *mother_re; /* what re is this a lightweight copy of? */
122 HV *paren_names; /* Optional hash of paren names */
124 /*----------------------------------------------------------------------
125 * Information about the match that the perl core uses to manage things
128 /* see comment in regcomp_internal.h about branch reset to understand
129 the distinction between physical and logical capture buffers */
130 U32 nparens; /* physical number of capture buffers */
131 U32 logical_nparens; /* logical_number of capture buffers */
132 I32 *logical_to_parno; /* map logical parno to first physcial */
133 I32 *parno_to_logical; /* map every physical parno to logical */
134 I32 *parno_to_logical_next; /* map every physical parno to the next
135 physical with the same logical id */
137 U32 extflags; /* Flags used both externally and internally */
138 SSize_t minlen; /* minimum possible number of chars in string to match */
139 SSize_t minlenret; /* minimum possible number of chars in $& */
140 STRLEN gofs; /* chars left of pos that we search from */
141 /* substring data about strings that must appear in
142 * the final match, used for optimisations */
144 struct reg_substr_data *substrs;
146 /* private engine specific data */
148 void *pprivate; /* Data private to the regex engine which
149 * created this object. */
150 U32 intflags; /* Engine Specific Internal flags */
152 /*----------------------------------------------------------------------
153 * Data about the last/current match. These are modified during matching
156 U32 lastparen; /* highest close paren matched ($+) */
157 regexp_paren_pair *offs; /* Array of offsets for (@-) and (@+) */
158 char **recurse_locinput; /* used to detect infinite recursion, XXX: move to internal */
159 U32 lastcloseparen; /* last close paren matched ($^N) */
162 /*---------------------------------------------------------------------- */
164 /* offset from wrapped to the start of precomp */
165 PERL_BITFIELD32 pre_prefix:4;
167 /* original flags used to compile the pattern, may differ from
168 * extflags in various ways */
169 PERL_BITFIELD32 compflags:9;
171 /*---------------------------------------------------------------------- */
173 char *subbeg; /* saved or original string so \digit works forever. */
174 SV_SAVED_COPY /* If non-NULL, SV which is COW from original */
175 SSize_t sublen; /* Length of string pointed by subbeg */
176 SSize_t suboffset; /* byte offset of subbeg from logical start of str */
177 SSize_t subcoffset; /* suboffset equiv, but in chars (for @-/@+) */
178 SSize_t maxlen; /* minimum possible number of chars in string to match */
180 /*---------------------------------------------------------------------- */
183 CV *qr_anoncv; /* the anon sub wrapped round qr/(?{..})/ */
187 #define RXp_PAREN_NAMES(rx) ((rx)->paren_names)
189 #define RXp_OFFS_START(rx,n) ((rx)->offs[(n)].start)
191 #define RXp_OFFS_END(rx,n) ((rx)->offs[(n)].end)
193 #define RXp_OFFS_VALID(rx,n) \
194 ( (rx)->offs[(n)].end != -1 && (rx)->offs[(n)].start != -1 )
196 #define RX_OFFS_START(rx_sv,n) RXp_OFFS_START(ReANY(rx_sv),n)
197 #define RX_OFFS_END(rx_sv,n) RXp_OFFS_END(ReANY(rx_sv),n)
198 #define RX_OFFS_VALID(rx_sv,n) RXp_OFFS_VALID(ReANY(rx_sv),n)
200 /* used for high speed searches */
201 typedef struct re_scream_pos_data_s
203 char **scream_olds; /* match pos */
204 SSize_t *scream_pos; /* Internal iterator of scream. */
205 } re_scream_pos_data;
207 /* regexp_engine structure. This is the dispatch table for regexes.
208 * Any regex engine implementation must be able to build one of these.
210 typedef struct regexp_engine {
211 REGEXP* (*comp) (pTHX_ SV * const pattern, U32 flags);
212 I32 (*exec) (pTHX_ REGEXP * const rx, char* stringarg, char* strend,
213 char* strbeg, SSize_t minend, SV* sv,
214 void* data, U32 flags);
215 char* (*intuit) (pTHX_
218 const char * const strbeg,
222 re_scream_pos_data *data);
223 SV* (*checkstr) (pTHX_ REGEXP * const rx);
224 void (*rxfree) (pTHX_ REGEXP * const rx);
225 void (*numbered_buff_FETCH) (pTHX_ REGEXP * const rx, const I32 paren,
227 void (*numbered_buff_STORE) (pTHX_ REGEXP * const rx, const I32 paren,
228 SV const * const value);
229 I32 (*numbered_buff_LENGTH) (pTHX_ REGEXP * const rx, const SV * const sv,
231 SV* (*named_buff) (pTHX_ REGEXP * const rx, SV * const key,
232 SV * const value, const U32 flags);
233 SV* (*named_buff_iter) (pTHX_ REGEXP * const rx, const SV * const lastkey,
235 SV* (*qr_package)(pTHX_ REGEXP * const rx);
237 void* (*dupe) (pTHX_ REGEXP * const rx, CLONE_PARAMS *param);
239 REGEXP* (*op_comp) (pTHX_ SV ** const patternp, int pat_count,
240 OP *expr, const struct regexp_engine* eng,
242 bool *is_bare_re, U32 orig_rx_flags, U32 pm_flags);
246 These are passed to the numbered capture variable callbacks as the
247 paren name. >= 1 is reserved for actual numbered captures, i.e. $1,
250 # define RX_BUFF_IDX_CARET_PREMATCH -5 /* ${^PREMATCH} */
251 # define RX_BUFF_IDX_CARET_POSTMATCH -4 /* ${^POSTMATCH} */
252 # define RX_BUFF_IDX_CARET_FULLMATCH -3 /* ${^MATCH} */
253 # define RX_BUFF_IDX_PREMATCH -2 /* $` */
254 # define RX_BUFF_IDX_POSTMATCH -1 /* $' */
255 # define RX_BUFF_IDX_FULLMATCH 0 /* $& */
258 Flags that are passed to the named_buff and named_buff_iter
259 callbacks above. Those routines are called from universal.c via the
260 Tie::Hash::NamedCapture interface for %+ and %- and the re::
261 functions in the same file.
264 /* The Tie::Hash::NamedCapture operation this is part of, if any */
265 # define RXapif_FETCH 0x0001
266 # define RXapif_STORE 0x0002
267 # define RXapif_DELETE 0x0004
268 # define RXapif_CLEAR 0x0008
269 # define RXapif_EXISTS 0x0010
270 # define RXapif_SCALAR 0x0020
271 # define RXapif_FIRSTKEY 0x0040
272 # define RXapif_NEXTKEY 0x0080
274 /* Whether %+ or %- is being operated on */
275 # define RXapif_ONE 0x0100 /* %+ */
276 # define RXapif_ALL 0x0200 /* %- */
278 /* Whether this is being called from a re:: function */
279 # define RXapif_REGNAME 0x0400
280 # define RXapif_REGNAMES 0x0800
281 # define RXapif_REGNAMES_COUNT 0x1000
284 =for apidoc Am|REGEXP *|SvRX|SV *sv
286 Convenience macro to get the REGEXP from a SV. This is approximately
287 equivalent to the following snippet:
292 sv = MUTABLE_SV(SvRV(sv));
293 if (SvTYPE(sv) == SVt_REGEXP)
296 C<NULL> will be returned if a REGEXP* is not found.
298 =for apidoc Am|bool|SvRXOK|SV* sv
300 Returns a boolean indicating whether the SV (or the one it references)
303 If you want to do something with the REGEXP* later use SvRX instead
309 # define SvRX(sv) (Perl_get_re_arg(aTHX_ sv))
310 # define SvRXOK(sv) cBOOL(Perl_get_re_arg(aTHX_ sv))
313 /* Flags stored in regexp->extflags
314 * These are used by code external to the regexp engine
316 * Note that the flags whose names start with RXf_PMf_ are defined in
317 * op_reg_common.h, being copied from the parallel flags of op_pmflags
319 * NOTE: if you modify any RXf flags you should run regen.pl or
320 * regen/regcomp.pl so that regnodes.h is updated with the changes.
324 # include "op_reg_common.h"
326 # define RXf_PMf_STD_PMMOD (RXf_PMf_MULTILINE|RXf_PMf_SINGLELINE|RXf_PMf_FOLD|RXf_PMf_EXTENDED|RXf_PMf_EXTENDED_MORE|RXf_PMf_NOCAPTURE)
328 # define CASE_STD_PMMOD_FLAGS_PARSE_SET(pmfl, x_count) \
329 case IGNORE_PAT_MOD: *(pmfl) |= RXf_PMf_FOLD; break; \
330 case MULTILINE_PAT_MOD: *(pmfl) |= RXf_PMf_MULTILINE; break; \
331 case SINGLE_PAT_MOD: *(pmfl) |= RXf_PMf_SINGLELINE; break; \
332 case XTENDED_PAT_MOD: if (x_count == 0) { \
333 *(pmfl) |= RXf_PMf_EXTENDED; \
334 *(pmfl) &= ~RXf_PMf_EXTENDED_MORE; \
337 *(pmfl) |= RXf_PMf_EXTENDED \
338 |RXf_PMf_EXTENDED_MORE; \
340 (x_count)++; break; \
341 case NOCAPTURE_PAT_MOD: *(pmfl) |= RXf_PMf_NOCAPTURE; break;
343 /* Note, includes charset ones, assumes 0 is the default for them */
344 # define STD_PMMOD_FLAGS_CLEAR(pmfl) \
345 *(pmfl) &= ~(RXf_PMf_FOLD|RXf_PMf_MULTILINE|RXf_PMf_SINGLELINE|RXf_PMf_EXTENDED|RXf_PMf_EXTENDED_MORE|RXf_PMf_CHARSET|RXf_PMf_NOCAPTURE)
347 /* chars and strings used as regex pattern modifiers
348 * Singular is a 'c'har, plural is a "string"
350 * NOTE, KEEPCOPY was originally 'k', but was changed to 'p' for preserve
351 * for compatibility reasons with Regexp::Common which highjacked (?k:...)
352 * for its own uses. So 'k' is out as well.
354 # define DEFAULT_PAT_MOD '^' /* Short for all the default modifiers */
355 # define EXEC_PAT_MOD 'e'
356 # define KEEPCOPY_PAT_MOD 'p'
357 # define NOCAPTURE_PAT_MOD 'n'
358 # define ONCE_PAT_MOD 'o'
359 # define GLOBAL_PAT_MOD 'g'
360 # define CONTINUE_PAT_MOD 'c'
361 # define MULTILINE_PAT_MOD 'm'
362 # define SINGLE_PAT_MOD 's'
363 # define IGNORE_PAT_MOD 'i'
364 # define XTENDED_PAT_MOD 'x'
365 # define NONDESTRUCT_PAT_MOD 'r'
366 # define LOCALE_PAT_MOD 'l'
367 # define UNICODE_PAT_MOD 'u'
368 # define DEPENDS_PAT_MOD 'd'
369 # define ASCII_RESTRICT_PAT_MOD 'a'
371 # define ONCE_PAT_MODS "o"
372 # define KEEPCOPY_PAT_MODS "p"
373 # define NOCAPTURE_PAT_MODS "n"
374 # define EXEC_PAT_MODS "e"
375 # define LOOP_PAT_MODS "gc"
376 # define NONDESTRUCT_PAT_MODS "r"
377 # define LOCALE_PAT_MODS "l"
378 # define UNICODE_PAT_MODS "u"
379 # define DEPENDS_PAT_MODS "d"
380 # define ASCII_RESTRICT_PAT_MODS "a"
381 # define ASCII_MORE_RESTRICT_PAT_MODS "aa"
383 /* This string is expected by regcomp.c to be ordered so that the first
384 * character is the flag in bit RXf_PMf_STD_PMMOD_SHIFT of extflags; the next
385 * character is bit +1, etc. */
386 # define STD_PAT_MODS "msixxn"
388 # define CHARSET_PAT_MODS ASCII_RESTRICT_PAT_MODS DEPENDS_PAT_MODS LOCALE_PAT_MODS UNICODE_PAT_MODS
390 /* This string is expected by XS_re_regexp_pattern() in universal.c to be ordered
391 * so that the first character is the flag in bit RXf_PMf_STD_PMMOD_SHIFT of
392 * extflags; the next character is in bit +1, etc. */
393 # define INT_PAT_MODS STD_PAT_MODS KEEPCOPY_PAT_MODS
395 # define EXT_PAT_MODS ONCE_PAT_MODS KEEPCOPY_PAT_MODS NOCAPTURE_PAT_MODS
396 # define QR_PAT_MODS STD_PAT_MODS EXT_PAT_MODS CHARSET_PAT_MODS
397 # define M_PAT_MODS QR_PAT_MODS LOOP_PAT_MODS
398 # define S_PAT_MODS M_PAT_MODS EXEC_PAT_MODS NONDESTRUCT_PAT_MODS
401 * NOTE: if you modify any RXf flags you should run regen.pl or
402 * regen/regcomp.pl so that regnodes.h is updated with the changes.
407 Set in Perl_pmruntime for a split. Will be used by regex engines to
408 check whether they should set RXf_SKIPWHITE
410 # define RXf_SPLIT RXf_PMf_SPLIT
412 /* Currently the regex flags occupy a single 32-bit word. Not all bits are
413 * currently used. The lower bits are shared with their corresponding PMf flag
414 * bits, up to but not including _RXf_PMf_SHIFT_NEXT. The unused bits
415 * immediately follow; finally the used RXf-only (unshared) bits, so that the
416 * highest bit in the word is used. This gathers all the unused bits as a pool
417 * in the middle, like so: 11111111111111110000001111111111
418 * where the '1's represent used bits, and the '0's unused. This design allows
419 * us to allocate off one end of the pool if we need to add a shared bit, and
420 * off the other end if we need a non-shared bit, without disturbing the other
421 * bits. This maximizes the likelihood of being able to change things without
422 * breaking binary compatibility.
424 * To add shared bits, do so in op_reg_common.h. This should change
425 * _RXf_PMf_SHIFT_NEXT so that things won't compile. Then come to regexp.h and
426 * op.h and adjust the constant adders in the definitions of RXf_BASE_SHIFT and
427 * Pmf_BASE_SHIFT down by the number of shared bits you added. That's it.
428 * Things should be binary compatible. But if either of these gets to having
429 * to subtract rather than add, leave at 0 and instead adjust all the entries
430 * that are in terms of it. But if the first one of those is already
431 * RXf_BASE_SHIFT+0, there are no bits left, and a redesign is in order.
433 * To remove unshared bits, just delete its entry. If you're where breaking
434 * binary compatibility is ok to do, you might want to adjust things to move
435 * the newly opened space so that it gets absorbed into the common pool.
437 * To add unshared bits, first use up any gaps in the middle. Otherwise,
438 * allocate off the low end until you get to RXf_BASE_SHIFT+0. If that isn't
439 * enough, move RXf_BASE_SHIFT down (if possible) and add the new bit at the
440 * other end instead; this preserves binary compatibility.
442 * For the regexp bits, PL_reg_extflags_name[] in regnodes.h has a comment
443 * giving which bits are used/unused */
445 # define RXf_BASE_SHIFT (_RXf_PMf_SHIFT_NEXT + 2)
447 /* What we have seen */
448 # define RXf_NO_INPLACE_SUBST (1U<<(RXf_BASE_SHIFT+2))
449 # define RXf_EVAL_SEEN (1U<<(RXf_BASE_SHIFT+3))
452 # define RXf_UNBOUNDED_QUANTIFIER_SEEN (1U<<(RXf_BASE_SHIFT+4))
453 # define RXf_CHECK_ALL (1U<<(RXf_BASE_SHIFT+5))
456 # define RXf_MATCH_UTF8 (1U<<(RXf_BASE_SHIFT+6)) /* $1 etc are utf8 */
459 # define RXf_USE_INTUIT_NOML (1U<<(RXf_BASE_SHIFT+7))
460 # define RXf_USE_INTUIT_ML (1U<<(RXf_BASE_SHIFT+8))
461 # define RXf_INTUIT_TAIL (1U<<(RXf_BASE_SHIFT+9))
462 # define RXf_USE_INTUIT (RXf_USE_INTUIT_NOML|RXf_USE_INTUIT_ML)
464 /* Do we have some sort of anchor? */
465 # define RXf_IS_ANCHORED (1U<<(RXf_BASE_SHIFT+10))
467 /* Copy and tainted info */
468 # define RXf_COPY_DONE (1U<<(RXf_BASE_SHIFT+11))
470 /* post-execution: $1 et al are tainted */
471 # define RXf_TAINTED_SEEN (1U<<(RXf_BASE_SHIFT+12))
472 /* this pattern was tainted during compilation */
473 # define RXf_TAINTED (1U<<(RXf_BASE_SHIFT+13))
475 /* Flags indicating special patterns */
476 # define RXf_START_ONLY (1U<<(RXf_BASE_SHIFT+14)) /* Pattern is /^/ */
477 # define RXf_SKIPWHITE (1U<<(RXf_BASE_SHIFT+15)) /* Pattern is for a */
479 # define RXf_WHITE (1U<<(RXf_BASE_SHIFT+16)) /* Pattern is /\s+/ */
480 # define RXf_NULL (1U<<(RXf_BASE_SHIFT+17)) /* Pattern is // */
482 /* See comments at the beginning of these defines about adding bits. The
483 * highest bit position should be used, so that if RXf_BASE_SHIFT gets
484 * increased, the #error below will be triggered so that you will be reminded
485 * to adjust things at the other end to keep the bit positions unchanged */
486 # if RXf_BASE_SHIFT+17 > 31
487 # error Too many RXf_PMf bits used. See comments at beginning of these for what to do
491 * NOTE: if you modify any RXf flags you should run regen.pl or
492 * regen/regcomp.pl so that regnodes.h is updated with the changes.
496 # ifdef NO_TAINT_SUPPORT
497 # define RX_ISTAINTED(rx_sv) 0
498 # define RXp_ISTAINTED(prog) 0
499 # define RX_TAINT_on(rx_sv) NOOP
500 # define RXp_MATCH_TAINTED(prog) 0
501 # define RX_MATCH_TAINTED(rx_sv) 0
502 # define RXp_MATCH_TAINTED_on(prog) NOOP
503 # define RX_MATCH_TAINTED_on(rx_sv) NOOP
504 # define RXp_MATCH_TAINTED_off(prog) NOOP
505 # define RX_MATCH_TAINTED_off(rx_sv) NOOP
507 # define RX_ISTAINTED(rx_sv) (RX_EXTFLAGS(rx_sv) & RXf_TAINTED)
508 # define RXp_ISTAINTED(prog) (RXp_EXTFLAGS(prog) & RXf_TAINTED)
509 # define RX_TAINT_on(rx_sv) (RX_EXTFLAGS(rx_sv) |= RXf_TAINTED)
510 # define RXp_MATCH_TAINTED(prog) (RXp_EXTFLAGS(prog) & RXf_TAINTED_SEEN)
511 # define RX_MATCH_TAINTED(rx_sv) (RX_EXTFLAGS(rx_sv) & RXf_TAINTED_SEEN)
512 # define RXp_MATCH_TAINTED_on(prog) (RXp_EXTFLAGS(prog) |= RXf_TAINTED_SEEN)
513 # define RX_MATCH_TAINTED_on(rx_sv) (RX_EXTFLAGS(rx_sv) |= RXf_TAINTED_SEEN)
514 # define RXp_MATCH_TAINTED_off(prog) (RXp_EXTFLAGS(prog) &= ~RXf_TAINTED_SEEN)
515 # define RX_MATCH_TAINTED_off(rx_sv) (RX_EXTFLAGS(rx_sv) &= ~RXf_TAINTED_SEEN)
518 # define RXp_HAS_CUTGROUP(prog) ((prog)->intflags & PREGf_CUTGROUP_SEEN)
520 # define RX_MATCH_TAINTED_set(rx_sv, t) ((t) \
521 ? RX_MATCH_TAINTED_on(rx_sv) \
522 : RX_MATCH_TAINTED_off(rx_sv))
524 # define RXp_MATCH_COPIED(prog) (RXp_EXTFLAGS(prog) & RXf_COPY_DONE)
525 # define RX_MATCH_COPIED(rx_sv) (RX_EXTFLAGS(rx_sv) & RXf_COPY_DONE)
526 # define RXp_MATCH_COPIED_on(prog) (RXp_EXTFLAGS(prog) |= RXf_COPY_DONE)
527 # define RX_MATCH_COPIED_on(rx_sv) (RX_EXTFLAGS(rx_sv) |= RXf_COPY_DONE)
528 # define RXp_MATCH_COPIED_off(prog) (RXp_EXTFLAGS(prog) &= ~RXf_COPY_DONE)
529 # define RX_MATCH_COPIED_off(rx_sv) (RX_EXTFLAGS(rx_sv) &= ~RXf_COPY_DONE)
530 # define RX_MATCH_COPIED_set(rx_sv,t) ((t) \
531 ? RX_MATCH_COPIED_on(rx_sv) \
532 : RX_MATCH_COPIED_off(rx_sv))
534 # define RXp_EXTFLAGS(rx) ((rx)->extflags)
535 # define RXp_COMPFLAGS(rx) ((rx)->compflags)
537 /* For source compatibility. We used to store these explicitly. */
538 # define RX_PRECOMP(rx_sv) (RX_WRAPPED(rx_sv) \
539 + ReANY(rx_sv)->pre_prefix)
540 # define RX_PRECOMP_const(rx_sv) (RX_WRAPPED_const(rx_sv) \
541 + ReANY(rx_sv)->pre_prefix)
542 /* FIXME? Are we hardcoding too much here and constraining plugin extension
543 writers? Specifically, the value 1 assumes that the wrapped version always
544 has exactly one character at the end, a ')'. Will that always be true? */
545 # define RX_PRELEN(rx_sv) (RX_WRAPLEN(rx_sv) \
546 - ReANY(rx_sv)->pre_prefix - 1)
548 # define RX_WRAPPED(rx_sv) SvPVX(rx_sv)
549 # define RX_WRAPPED_const(rx_sv) SvPVX_const(rx_sv)
550 # define RX_WRAPLEN(rx_sv) SvCUR(rx_sv)
551 # define RX_CHECK_SUBSTR(rx_sv) (ReANY(rx_sv)->check_substr)
552 # define RX_REFCNT(rx_sv) SvREFCNT(rx_sv)
553 # define RX_EXTFLAGS(rx_sv) RXp_EXTFLAGS(ReANY(rx_sv))
554 # define RX_COMPFLAGS(rx_sv) RXp_COMPFLAGS(ReANY(rx_sv))
555 # define RXp_ENGINE(prog) ((prog)->engine)
556 # define RX_ENGINE(rx_sv) (RXp_ENGINE(ReANY(rx_sv)))
557 # define RXp_SUBBEG(prog) (prog->subbeg)
558 # define RX_SUBBEG(rx_sv) (RXp_SUBBEG(ReANY(rx_sv)))
559 # define RXp_SUBOFFSET(prog) (prog->suboffset)
560 # define RX_SUBOFFSET(rx_sv) (RXp_SUBOFFSET(ReANY(rx_sv)))
561 # define RX_SUBCOFFSET(rx_sv) (ReANY(rx_sv)->subcoffset)
562 # define RXp_OFFSp(prog) (prog->offs)
563 # define RX_OFFSp(rx_sv) (RXp_OFFSp(ReANY(rx_sv)))
564 # define RXp_LOGICAL_NPARENS(prog) (prog->logical_nparens)
565 # define RX_LOGICAL_NPARENS(rx_sv) (RXp_LOGICAL_NPARENS(ReANY(rx_sv)))
566 # define RXp_LOGICAL_TO_PARNO(prog) (prog->logical_to_parno)
567 # define RX_LOGICAL_TO_PARNO(rx_sv) (RXp_LOGICAL_TO_PARNO(ReANY(rx_sv)))
568 # define RXp_PARNO_TO_LOGICAL(prog) (prog->parno_to_logical)
569 # define RX_PARNO_TO_LOGICAL(rx_sv) (RXp_PARNO_TO_LOGICAL(ReANY(rx_sv)))
570 # define RXp_PARNO_TO_LOGICAL_NEXT(prog) (prog->parno_to_logical_next)
571 # define RX_PARNO_TO_LOGICAL_NEXT(rx_sv) (RXp_PARNO_TO_LOGICAL_NEXT(ReANY(rx_sv)))
572 # define RXp_NPARENS(prog) (prog->nparens)
573 # define RX_NPARENS(rx_sv) (RXp_NPARENS(ReANY(rx_sv)))
574 # define RX_SUBLEN(rx_sv) (ReANY(rx_sv)->sublen)
575 # define RXp_MINLEN(prog) (prog->minlen)
576 # define RX_MINLEN(rx_sv) (RXp_MINLEN(ReANY(rx_sv)))
577 # define RXp_MINLENRET(prog) (prog->minlenret)
578 # define RX_MINLENRET(rx_sv) (RXp_MINLENRET(ReANY(rx_sv)))
579 # define RXp_GOFS(prog) (prog->gofs)
580 # define RX_GOFS(rx_sv) (RXp_GOFS(ReANY(rx_sv)))
581 # define RX_LASTPAREN(rx_sv) (ReANY(rx_sv)->lastparen)
582 # define RX_LASTCLOSEPAREN(rx_sv) (ReANY(rx_sv)->lastcloseparen)
583 # define RXp_SAVED_COPY(prog) (prog->saved_copy)
584 # define RX_SAVED_COPY(rx_sv) (RXp_SAVED_COPY(ReANY(rx_sv)))
585 /* last match was zero-length */
586 # define RXp_ZERO_LEN(prog) \
587 (RXp_OFFS_START(prog,0) + (SSize_t)RXp_GOFS(prog) \
588 == RXp_OFFS_END(prog,0))
589 # define RX_ZERO_LEN(rx_sv) (RXp_ZERO_LEN(ReANY(rx_sv)))
591 #endif /* PLUGGABLE_RE_EXTENSION */
593 /* Stuff that needs to be included in the pluggable extension goes below here */
596 # define RXp_MATCH_COPY_FREE(prog) \
598 if (RXp_SAVED_COPY(prog)) { \
599 SV_CHECK_THINKFIRST_COW_DROP(RXp_SAVED_COPY(prog)); \
601 if (RXp_MATCH_COPIED(prog)) { \
602 Safefree(RXp_SUBBEG(prog)); \
603 RXp_MATCH_COPIED_off(prog); \
607 # define RXp_MATCH_COPY_FREE(prog) \
609 if (RXp_MATCH_COPIED(prog)) { \
610 Safefree(RXp_SUBBEG(prog)); \
611 RXp_MATCH_COPIED_off(prog); \
615 #define RX_MATCH_COPY_FREE(rx_sv) RXp_MATCH_COPY_FREE(ReANY(rx_sv))
617 #define RXp_MATCH_UTF8(prog) (RXp_EXTFLAGS(prog) & RXf_MATCH_UTF8)
618 #define RX_MATCH_UTF8(rx_sv) (RX_EXTFLAGS(rx_sv) & RXf_MATCH_UTF8)
619 #define RXp_MATCH_UTF8_on(prog) (RXp_EXTFLAGS(prog) |= RXf_MATCH_UTF8)
620 #define RX_MATCH_UTF8_on(rx_sv) (RXp_MATCH_UTF8_on(ReANY(rx_sv)))
621 #define RXp_MATCH_UTF8_off(prog) (RXp_EXTFLAGS(prog) &= ~RXf_MATCH_UTF8)
622 #define RX_MATCH_UTF8_off(rx_sv) (RXp_MATCH_UTF8_off(ReANY(rx_sv))
623 #define RXp_MATCH_UTF8_set(prog, t) ((t) \
624 ? RXp_MATCH_UTF8_on(prog) \
625 : RXp_MATCH_UTF8_off(prog))
626 #define RX_MATCH_UTF8_set(rx_sv, t) (RXp_MATCH_UTF8_set(ReANY(rx_sv), t))
628 /* Whether the pattern stored at RX_WRAPPED is in UTF-8 */
629 #define RX_UTF8(rx_sv) SvUTF8(rx_sv)
632 /* bits in flags arg of Perl_regexec_flags() */
634 #define REXEC_COPY_STR 0x01 /* Need to copy the string for captures. */
635 #define REXEC_CHECKED 0x02 /* re_intuit_start() already called. */
636 #define REXEC_SCREAM 0x04 /* currently unused. */
637 #define REXEC_IGNOREPOS 0x08 /* use stringarg, not pos(), for \G match */
638 #define REXEC_NOT_FIRST 0x10 /* This is another iteration of //g:
639 no need to copy string again */
641 /* under REXEC_COPY_STR, it's ok for the
642 engine (modulo PL_sawamperand etc)
643 to skip copying: ... */
644 #define REXEC_COPY_SKIP_PRE 0x20 /* ...the $` part of the string, or */
645 #define REXEC_COPY_SKIP_POST 0x40 /* ...the $' part of the string */
646 #define REXEC_FAIL_ON_UNDERFLOW 0x80 /* fail the match if $& would start before
647 the start pos (so s/.\G// would fail
648 on second iteration */
650 #if defined(PERL_USE_GCC_BRACE_GROUPS)
651 # define ReREFCNT_inc(re) \
653 /* This is here to generate a casting warning if incorrect. */ \
654 REGEXP *const _rerefcnt_inc = (re); \
655 assert(SvTYPE(_rerefcnt_inc) == SVt_REGEXP); \
656 SvREFCNT_inc(_rerefcnt_inc); \
659 # define ReREFCNT_dec(re) \
661 /* This is here to generate a casting warning if incorrect. */ \
662 REGEXP *const _rerefcnt_dec = (re); \
663 SvREFCNT_dec(_rerefcnt_dec); \
666 # define ReREFCNT_dec(re) SvREFCNT_dec(re)
667 # define ReREFCNT_inc(re) ((REGEXP *) SvREFCNT_inc(re))
669 #define ReANY(re) Perl_ReANY((const REGEXP *)(re))
671 /* FIXME for plugins. */
673 #define FBMcf_TAIL_DOLLAR 1
674 #define FBMcf_TAIL_DOLLARM 2
675 #define FBMcf_TAIL_Z 4
676 #define FBMcf_TAIL_z 8
677 #define FBMcf_TAIL (FBMcf_TAIL_DOLLAR|FBMcf_TAIL_DOLLARM|FBMcf_TAIL_Z|FBMcf_TAIL_z)
679 #define FBMrf_MULTILINE 1
681 struct regmatch_state;
682 struct regmatch_slab;
684 /* like regmatch_info_aux, but contains extra fields only needed if the
685 * pattern contains (?{}). If used, is snuck into the second slot in the
686 * regmatch_state stack at the start of execution */
690 PMOP *curpm; /* saved PL_curpm */
692 SV *saved_copy; /* saved saved_copy field from rex */
694 char *subbeg; /* saved subbeg field from rex */
695 STRLEN sublen; /* saved sublen field from rex */
696 STRLEN suboffset; /* saved suboffset field from rex */
697 STRLEN subcoffset; /* saved subcoffset field from rex */
698 SV *sv; /* $_ during (?{}) */
699 MAGIC *pos_magic; /* pos() magic attached to $_ */
700 SSize_t pos; /* the original value of pos() in pos_magic */
701 U8 pos_flags; /* flags to be restored; currently only MGf_BYTES*/
702 } regmatch_info_aux_eval;
705 /* fields that logically live in regmatch_info, but which need cleaning
706 * up on croak(), and so are instead are snuck into the first slot in
707 * the regmatch_state stack at the start of execution */
710 regmatch_info_aux_eval *info_aux_eval;
711 struct regmatch_state *old_regmatch_state; /* saved PL_regmatch_state */
712 struct regmatch_slab *old_regmatch_slab; /* saved PL_regmatch_slab */
713 char *poscache; /* S-L cache of fail positions of WHILEMs */
718 =for apidoc Ay||regmatch_info
719 Some basic information about the current match that is created by
720 Perl_regexec_flags and then passed to regtry(), regmatch() etc.
721 It is allocated as a local var on the stack, so nothing should be
722 stored in it that needs preserving or clearing up on croak().
723 For that, see the aux_info and aux_info_eval members of the
724 regmatch_state union.
730 REGEXP *prog; /* the regex being executed */
731 const char * strbeg; /* real start of string */
732 char *strend; /* one byte beyond last char of match string */
733 char *till; /* matches shorter than this fail (see minlen arg) */
734 SV *sv; /* the SV string currently being matched */
735 char *ganch; /* position of \G anchor */
736 char *cutpoint; /* (*COMMIT) position (if any) */
737 regmatch_info_aux *info_aux; /* extra fields that need cleanup */
738 regmatch_info_aux_eval *info_aux_eval; /* extra saved state for (?{}) */
739 I32 poscache_maxiter; /* how many whilems todo before S-L cache kicks in */
740 I32 poscache_iter; /* current countdown from _maxiter to zero */
741 STRLEN poscache_size; /* size of regmatch_info_aux.poscache */
742 bool intuit; /* re_intuit_start() is the top-level caller */
743 bool is_utf8_pat; /* regex is utf8 */
744 bool is_utf8_target; /* string being matched is utf8 */
745 bool warned; /* we have issued a recursion warning; no need for more */
749 /* structures for holding and saving the state maintained by regmatch() */
751 #ifndef MAX_RECURSE_EVAL_NOCHANGE_DEPTH
752 # define MAX_RECURSE_EVAL_NOCHANGE_DEPTH 10
755 /* The +1 is because everything matches itself, which isn't included in
756 * MAX_FOLD_FROMS; the +2 is based on the current Unicode standards needs, and
757 * is unlikely to change. An assertion should fail in regexec.c if it is too
758 * low. It is needed for certain edge cases involving multi-character folds
759 * when the first component also participates in a fold individually. */
760 #define MAX_MATCHES (MAX_FOLD_FROMS + 1 + 2)
762 struct next_matchable_info {
767 PERL_INT_FAST8_T count; /* Negative means not initialized */
768 PERL_UINT_FAST8_T min_length;
769 PERL_UINT_FAST8_T max_length;
770 PERL_UINT_FAST8_T initial_definitive;
771 PERL_UINT_FAST8_T initial_exact;
772 PERL_UINT_FAST8_T lengths[MAX_MATCHES];
774 /* The size is from trial and error, and could change with new Unicode
775 * standards, in which case there is an assertion that should start
776 * failing. This size could be calculated in one of the regen scripts
777 * dealing with Unicode, but khw thinks the likelihood of it changing is
778 * low enough that it isn't worth the effort. */
782 typedef I32 CHECKPOINT;
784 typedef struct regmatch_state {
785 int resume_state; /* where to jump to on return */
786 char *locinput; /* where to backtrack in string on failure */
788 U8 *sr0; /* position of start of script run, or NULL */
792 /* the 'info_aux' and 'info_aux_eval' union members are cuckoos in
793 * the nest. They aren't saved backtrack state; rather they
794 * represent one or two extra chunks of data that need allocating
795 * at the start of a match. These fields would logically live in
796 * the regmatch_info struct, except that is allocated on the
797 * C stack, and these fields are all things that require cleanup
798 * after a croak(), when the stack is lost.
799 * As a convenience, we just use the first 1 or 2 regmatch_state
800 * slots to store this info, as we will be allocating a slab of
801 * these anyway. Otherwise we'd have to malloc and then free them,
802 * or allocate them on the save stack (where they will get
803 * realloced if the save stack grows).
804 * info_aux contains the extra fields that are always needed;
805 * info_aux_eval contains extra fields that only needed if
806 * the pattern contains code blocks
807 * We split them into two separate structs to avoid increasing
808 * the size of the union.
811 regmatch_info_aux info_aux;
813 regmatch_info_aux_eval info_aux_eval;
815 /* this is a fake union member that matches the first element
816 * of each member that needs to store positive backtrack
819 struct regmatch_state *prev_yes_state;
822 /* branchlike members */
823 /* this is a fake union member that matches the first elements
824 * of each member that needs to behave like a branch */
826 /* this first element must match u.yes */
827 struct regmatch_state *prev_yes_state;
835 /* the first elements must match u.branchlike */
836 struct regmatch_state *prev_yes_state;
841 regnode *next_branch; /* next branch node */
845 /* the first elements must match u.branchlike */
846 struct regmatch_state *prev_yes_state;
851 U32 accepted; /* how many accepting states left */
852 bool longfold;/* saw a fold with a 1->n char mapping */
853 U16 *jump; /* positive offsets from me */
854 regnode *me; /* Which node am I - needed for jump tries*/
855 U8 *firstpos;/* pos in string of first trie match */
856 U32 firstchars;/* len in chars of firstpos from start */
857 U16 nextword;/* next word to try */
858 U16 topword; /* longest accepted word */
861 /* special types - these members are used to store state for special
862 regops like eval, if/then, lookaround and the markpoint state */
864 /* this first element must match u.yes */
865 struct regmatch_state *prev_yes_state;
866 struct regmatch_state *prev_curlyx;
867 struct regmatch_state *prev_eval;
869 CHECKPOINT cp; /* remember current savestack indexes */
871 U32 close_paren; /* which close bracket is our end (+1) */
872 regnode *B; /* the node following us */
873 char *prev_recurse_locinput;
877 /* this first element must match u.yes */
878 struct regmatch_state *prev_yes_state;
880 I32 logical; /* saved copy of 'logical' var */
881 U8 count; /* number of beginning positions */
884 regnode *me; /* the IFMATCH/SUSPEND/UNLESSM node */
885 char *prev_match_end;
886 } ifmatch; /* and SUSPEND/UNLESSM */
889 /* this first element must match u.yes */
890 struct regmatch_state *prev_yes_state;
891 struct regmatch_state *prev_mark;
900 /* quantifiers - these members are used for storing state for
901 the regops used to implement quantifiers */
903 /* this first element must match u.yes */
904 struct regmatch_state *prev_yes_state;
905 struct regmatch_state *prev_curlyx; /* previous cur_curlyx */
906 regnode *me; /* the CURLYX node */
907 regnode *B; /* the B node in /A*B/ */
908 CHECKPOINT cp; /* remember current savestack index */
910 int parenfloor;/* how far back to strip paren data */
912 /* these two are modified by WHILEM */
913 int count; /* how many instances of A we've matched */
914 char *lastloc;/* where previous A matched (0-len detect) */
918 /* this first element must match u.yes */
919 struct regmatch_state *prev_yes_state;
920 struct regmatch_state *save_curlyx;
921 CHECKPOINT cp; /* remember current savestack indexes */
923 char *save_lastloc; /* previous curlyx.lastloc */
929 /* this first element must match u.yes */
930 struct regmatch_state *prev_yes_state;
934 I32 alen; /* length of first-matched A string */
937 regnode *A, *B; /* the nodes corresponding to /A*B/ */
938 regnode *me; /* the curlym node */
939 struct next_matchable_info Binfo;
947 char *maxpos; /* highest possible point in string to match */
948 char *oldloc; /* the previous locinput */
950 int min, max; /* {m,n} */
951 regnode *A, *B; /* the nodes corresponding to /A*B/ */
952 struct next_matchable_info Binfo;
953 } curly; /* and CURLYN/PLUS/STAR */
960 /* how many regmatch_state structs to allocate as a single slab.
961 * We do it in 4K blocks for efficiency. The "3" is 2 for the next/prev
962 * pointers, plus 1 for any mythical malloc overhead. */
964 #define PERL_REGMATCH_SLAB_SLOTS \
965 ((4096 - 3 * sizeof (void*)) / sizeof(regmatch_state))
967 typedef struct regmatch_slab {
968 regmatch_state states[PERL_REGMATCH_SLAB_SLOTS];
969 struct regmatch_slab *prev, *next;
973 #define REG_FETCH_ABSOLUTE 1
976 * ex: set ts=8 sts=4 sw=4 et: