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;
32 /* this ensures that on alignment sensitive platforms
33 * this struct is aligned on 32 bit boundaries */
48 union regnode_head head;
51 typedef struct regnode regnode;
53 struct reg_substr_data;
60 struct reg_substr_datum {
61 SSize_t min_offset; /* min pos (in chars) that substr must appear */
62 SSize_t max_offset; /* max pos (in chars) that substr must appear */
63 SV *substr; /* non-utf8 variant */
64 SV *utf8_substr; /* utf8 variant */
65 SSize_t end_shift; /* how many fixed chars must end the string */
67 struct reg_substr_data {
68 U8 check_ix; /* index into data[] of check substr */
69 struct reg_substr_datum data[3]; /* Actual array */
73 # define SV_SAVED_COPY SV *saved_copy; /* If non-NULL, SV which is COW from original */
75 # define SV_SAVED_COPY
78 /* offsets within a string of a particular /(.)/ capture
79 * if you change this by adding new non-temporary fields
80 * then be sure to update Perl_rxres_save() in pp_ctl.c */
81 typedef struct regexp_paren_pair {
85 /* 'start_tmp' records a new opening position before the matching end
86 * has been found, so that the old start and end values are still
88 * "abc" =~ /(.(?{print "[$1]"}))+/
90 * This field is not part of the API. */
94 # if defined(PERL_IN_REGCOMP_ANY) || defined(PERL_IN_UTF8_C)
95 # define _invlist_union(a, b, output) _invlist_union_maybe_complement_2nd(a, b, FALSE, output)
96 # define _invlist_intersection(a, b, output) _invlist_intersection_maybe_complement_2nd(a, b, FALSE, output)
98 /* Subtracting b from a leaves in a everything that was there that isn't in b,
99 * that is the intersection of a with b's complement */
100 # define _invlist_subtract(a, b, output) _invlist_intersection_maybe_complement_2nd(a, b, TRUE, output)
103 /* record the position of a (?{...}) within a pattern */
105 struct reg_code_block {
112 /* array of reg_code_block's plus header info */
114 struct reg_code_blocks {
115 int refcnt; /* we may be pointed to from a regex and from the savestack */
116 int count; /* how many code blocks */
117 struct reg_code_block *cb; /* array of reg_code_block's */
122 = for apidoc AyT||regexp
123 The regexp/REGEXP struct, see L<perlreapi> for further documentation
124 on the individual fields. The struct is ordered so that the most
125 commonly used fields are placed at the start.
127 Any patch that adds items to this struct will need to include
128 changes to F<sv.c> (C<Perl_re_dup()>) and F<regcomp.c>
129 (C<pregfree()>). This involves freeing or cloning items in the
130 regexp's data array based on the data item's type.
133 /* NOTE: There is a copy of this struct in the documentation in perlreapi.pod
134 * if you make ANY changes please make sure they are ALSO made there.
136 typedef struct regexp {
137 /*----------------------------------------------------------------------
138 * Fields required for compatibility with SV types
142 /*----------------------------------------------------------------------
146 const struct regexp_engine* engine; /* what engine created this regexp? */
147 REGEXP *mother_re; /* what re is this a lightweight copy of? */
148 HV *paren_names; /* Optional hash of paren names */
150 /*----------------------------------------------------------------------
151 * Information about the match that the perl core uses to manage things
154 /* see comment in regcomp_internal.h about branch reset to understand
155 the distinction between physical and logical capture buffers */
156 U32 nparens; /* physical number of capture buffers */
157 U32 logical_nparens; /* logical_number of capture buffers */
158 I32 *logical_to_parno; /* map logical parno to first physcial */
159 I32 *parno_to_logical; /* map every physical parno to logical */
160 I32 *parno_to_logical_next; /* map every physical parno to the next
161 physical with the same logical id */
163 SSize_t maxlen; /* maximum possible number of chars in string to match */
164 SSize_t minlen; /* minimum possible number of chars in string to match */
165 SSize_t minlenret; /* minimum possible number of chars in $& */
166 STRLEN gofs; /* chars left of pos that we search from */
167 /* substring data about strings that must appear in
168 * the final match, used for optimisations */
170 struct reg_substr_data *substrs;
172 /* private engine specific data */
174 void *pprivate; /* Data private to the regex engine which
175 * created this object. */
177 U32 extflags; /* Flags used both externally and internally */
178 U32 intflags; /* Engine Specific Internal flags */
180 /*----------------------------------------------------------------------
181 * Data about the last/current match. These are modified during matching
184 U32 lastparen; /* highest close paren matched ($+) */
185 U32 lastcloseparen; /* last close paren matched ($^N) */
186 regexp_paren_pair *offs; /* Array of offsets for (@-) and (@+) */
187 char **recurse_locinput; /* used to detect infinite recursion, XXX: move to internal */
189 /*---------------------------------------------------------------------- */
191 /* offset from wrapped to the start of precomp */
192 PERL_BITFIELD32 pre_prefix:4;
194 /* original flags used to compile the pattern, may differ from
195 * extflags in various ways */
196 PERL_BITFIELD32 compflags:9;
198 /*---------------------------------------------------------------------- */
200 char *subbeg; /* saved or original string so \digit works forever. */
201 SV_SAVED_COPY /* If non-NULL, SV which is COW from original */
202 SSize_t sublen; /* Length of string pointed by subbeg */
203 SSize_t suboffset; /* byte offset of subbeg from logical start of str */
204 SSize_t subcoffset; /* suboffset equiv, but in chars (for @-/@+) */
206 /*----------------------------------------------------------------------
207 * More Operational fields
210 CV *qr_anoncv; /* the anon sub wrapped round qr/(?{..})/ */
214 #define RXp_PAREN_NAMES(rx) ((rx)->paren_names)
216 #define RXp_OFFS_START(rx,n) \
217 RXp_OFFSp(rx)[(n)].start
219 #define RXp_OFFS_END(rx,n) \
220 RXp_OFFSp(rx)[(n)].end
222 #define RXp_OFFS_VALID(rx,n) \
223 (RXp_OFFSp(rx)[(n)].end != -1 && RXp_OFFSp(rx)[(n)].start != -1 )
226 #define RX_OFFS_START(rx_sv,n) RXp_OFFS_START(ReANY(rx_sv),n)
227 #define RX_OFFS_END(rx_sv,n) RXp_OFFS_END(ReANY(rx_sv),n)
228 #define RX_OFFS_VALID(rx_sv,n) RXp_OFFS_VALID(ReANY(rx_sv),n)
232 =for apidoc mh||RX_OFFSp|const REGEXP * rx_sv
233 Returns the RX_OFFS structure as a pointer. This should NOT be used
234 directly, and the structure it returns may change in future versions of
235 perl. Use one of the other RX_OFFS_xxx macros instead, which should be
236 stable as Perl evolves. The 'p' suffix on the macro is meant to indicate
237 both that the macro is "private" to the internals, and that it returns
240 =for apidoc Amh||RX_OFFS_START|const REGEXP * rx_sv|U32 n
241 Returns the start offset of the C<n>'th capture buffer, when
242 C<n> is 0 it refers to the overall match as a whole. A value of
243 -1 is returned if the C<n>'th capture buffer did not match.
245 =for apidoc Amh||RXp_OFFS_START|const regexp * rx|U32 n
246 Same as RX_OFFS_START() but operates on a C<struct regexp *> instead of
249 =for apidoc Amh||RX_OFFS_END|const REGEXP * rx_sv|U32 n
250 Returns the end offset of the C<n>'th capture buffer, when C<n> is 0 it
251 refers to the overall match as a whole. A value of
252 -1 is returned if the C<n>'th capture buffer did not match.
254 =for apidoc Amh||RXp_OFFS_END|const regexp * rx|U32 n
255 Same as RX_OFFS_END() but operates on a C<struct regexp *> instead of
258 =for apidoc Amh||RX_OFFS_VALID|const REGEXP * rx_sv|U32 n
259 Returns true if the C<n>'th offset buffer matched, false otherwise, when
260 C<n> is 0 it refers to the overall match as a whole which should always
263 =for apidoc Amh||RXp_OFFS_VALID|const regexp * rx|U32 n
264 Same as RX_OFFS_VLAID() but operates on a C<struct regexp *> instead of
271 /* used for high speed searches */
272 typedef struct re_scream_pos_data_s
274 char **scream_olds; /* match pos */
275 SSize_t *scream_pos; /* Internal iterator of scream. */
276 } re_scream_pos_data;
278 /* regexp_engine structure. This is the dispatch table for regexes.
279 * Any regex engine implementation must be able to build one of these.
281 typedef struct regexp_engine {
282 REGEXP* (*comp) (pTHX_ SV * const pattern, U32 flags);
283 I32 (*exec) (pTHX_ REGEXP * const rx, char* stringarg, char* strend,
284 char* strbeg, SSize_t minend, SV* sv,
285 void* data, U32 flags);
286 char* (*intuit) (pTHX_
289 const char * const strbeg,
293 re_scream_pos_data *data);
294 SV* (*checkstr) (pTHX_ REGEXP * const rx);
295 void (*rxfree) (pTHX_ REGEXP * const rx);
296 void (*numbered_buff_FETCH) (pTHX_ REGEXP * const rx, const I32 paren,
298 void (*numbered_buff_STORE) (pTHX_ REGEXP * const rx, const I32 paren,
299 SV const * const value);
300 I32 (*numbered_buff_LENGTH) (pTHX_ REGEXP * const rx, const SV * const sv,
302 SV* (*named_buff) (pTHX_ REGEXP * const rx, SV * const key,
303 SV * const value, const U32 flags);
304 SV* (*named_buff_iter) (pTHX_ REGEXP * const rx, const SV * const lastkey,
306 SV* (*qr_package)(pTHX_ REGEXP * const rx);
308 void* (*dupe) (pTHX_ REGEXP * const rx, CLONE_PARAMS *param);
310 REGEXP* (*op_comp) (pTHX_ SV ** const patternp, int pat_count,
311 OP *expr, const struct regexp_engine* eng,
313 bool *is_bare_re, U32 orig_rx_flags, U32 pm_flags);
317 These are passed to the numbered capture variable callbacks as the
318 paren name. >= 1 is reserved for actual numbered captures, i.e. $1,
321 # define RX_BUFF_IDX_CARET_PREMATCH -5 /* ${^PREMATCH} */
322 # define RX_BUFF_IDX_CARET_POSTMATCH -4 /* ${^POSTMATCH} */
323 # define RX_BUFF_IDX_CARET_FULLMATCH -3 /* ${^MATCH} */
324 # define RX_BUFF_IDX_PREMATCH -2 /* $` */
325 # define RX_BUFF_IDX_POSTMATCH -1 /* $' */
326 # define RX_BUFF_IDX_FULLMATCH 0 /* $& */
329 Flags that are passed to the named_buff and named_buff_iter
330 callbacks above. Those routines are called from universal.c via the
331 Tie::Hash::NamedCapture interface for %+ and %- and the re::
332 functions in the same file.
335 /* The Tie::Hash::NamedCapture operation this is part of, if any */
336 # define RXapif_FETCH 0x0001
337 # define RXapif_STORE 0x0002
338 # define RXapif_DELETE 0x0004
339 # define RXapif_CLEAR 0x0008
340 # define RXapif_EXISTS 0x0010
341 # define RXapif_SCALAR 0x0020
342 # define RXapif_FIRSTKEY 0x0040
343 # define RXapif_NEXTKEY 0x0080
345 /* Whether %+ or %- is being operated on */
346 # define RXapif_ONE 0x0100 /* %+ */
347 # define RXapif_ALL 0x0200 /* %- */
349 /* Whether this is being called from a re:: function */
350 # define RXapif_REGNAME 0x0400
351 # define RXapif_REGNAMES 0x0800
352 # define RXapif_REGNAMES_COUNT 0x1000
355 =for apidoc Am|REGEXP *|SvRX|SV *sv
357 Convenience macro to get the REGEXP from a SV. This is approximately
358 equivalent to the following snippet:
363 sv = MUTABLE_SV(SvRV(sv));
364 if (SvTYPE(sv) == SVt_REGEXP)
367 C<NULL> will be returned if a REGEXP* is not found.
369 =for apidoc Am|bool|SvRXOK|SV* sv
371 Returns a boolean indicating whether the SV (or the one it references)
374 If you want to do something with the REGEXP* later use SvRX instead
380 # define SvRX(sv) (Perl_get_re_arg(aTHX_ sv))
381 # define SvRXOK(sv) cBOOL(Perl_get_re_arg(aTHX_ sv))
384 /* Flags stored in regexp->extflags
385 * These are used by code external to the regexp engine
387 * Note that the flags whose names start with RXf_PMf_ are defined in
388 * op_reg_common.h, being copied from the parallel flags of op_pmflags
390 * NOTE: if you modify any RXf flags you should run regen.pl or
391 * regen/regcomp.pl so that regnodes.h is updated with the changes.
395 # include "op_reg_common.h"
397 # define RXf_PMf_STD_PMMOD (RXf_PMf_MULTILINE|RXf_PMf_SINGLELINE|RXf_PMf_FOLD|RXf_PMf_EXTENDED|RXf_PMf_EXTENDED_MORE|RXf_PMf_NOCAPTURE)
399 # define CASE_STD_PMMOD_FLAGS_PARSE_SET(pmfl, x_count) \
400 case IGNORE_PAT_MOD: *(pmfl) |= RXf_PMf_FOLD; break; \
401 case MULTILINE_PAT_MOD: *(pmfl) |= RXf_PMf_MULTILINE; break; \
402 case SINGLE_PAT_MOD: *(pmfl) |= RXf_PMf_SINGLELINE; break; \
403 case XTENDED_PAT_MOD: if (x_count == 0) { \
404 *(pmfl) |= RXf_PMf_EXTENDED; \
405 *(pmfl) &= ~RXf_PMf_EXTENDED_MORE; \
408 *(pmfl) |= RXf_PMf_EXTENDED \
409 |RXf_PMf_EXTENDED_MORE; \
411 (x_count)++; break; \
412 case NOCAPTURE_PAT_MOD: *(pmfl) |= RXf_PMf_NOCAPTURE; break;
414 /* Note, includes charset ones, assumes 0 is the default for them */
415 # define STD_PMMOD_FLAGS_CLEAR(pmfl) \
416 *(pmfl) &= ~(RXf_PMf_FOLD|RXf_PMf_MULTILINE|RXf_PMf_SINGLELINE|RXf_PMf_EXTENDED|RXf_PMf_EXTENDED_MORE|RXf_PMf_CHARSET|RXf_PMf_NOCAPTURE)
418 /* chars and strings used as regex pattern modifiers
419 * Singular is a 'c'har, plural is a "string"
421 * NOTE, KEEPCOPY was originally 'k', but was changed to 'p' for preserve
422 * for compatibility reasons with Regexp::Common which highjacked (?k:...)
423 * for its own uses. So 'k' is out as well.
425 # define DEFAULT_PAT_MOD '^' /* Short for all the default modifiers */
426 # define EXEC_PAT_MOD 'e'
427 # define KEEPCOPY_PAT_MOD 'p'
428 # define NOCAPTURE_PAT_MOD 'n'
429 # define ONCE_PAT_MOD 'o'
430 # define GLOBAL_PAT_MOD 'g'
431 # define CONTINUE_PAT_MOD 'c'
432 # define MULTILINE_PAT_MOD 'm'
433 # define SINGLE_PAT_MOD 's'
434 # define IGNORE_PAT_MOD 'i'
435 # define XTENDED_PAT_MOD 'x'
436 # define NONDESTRUCT_PAT_MOD 'r'
437 # define LOCALE_PAT_MOD 'l'
438 # define UNICODE_PAT_MOD 'u'
439 # define DEPENDS_PAT_MOD 'd'
440 # define ASCII_RESTRICT_PAT_MOD 'a'
442 # define ONCE_PAT_MODS "o"
443 # define KEEPCOPY_PAT_MODS "p"
444 # define NOCAPTURE_PAT_MODS "n"
445 # define EXEC_PAT_MODS "e"
446 # define LOOP_PAT_MODS "gc"
447 # define NONDESTRUCT_PAT_MODS "r"
448 # define LOCALE_PAT_MODS "l"
449 # define UNICODE_PAT_MODS "u"
450 # define DEPENDS_PAT_MODS "d"
451 # define ASCII_RESTRICT_PAT_MODS "a"
452 # define ASCII_MORE_RESTRICT_PAT_MODS "aa"
454 /* This string is expected by regcomp.c to be ordered so that the first
455 * character is the flag in bit RXf_PMf_STD_PMMOD_SHIFT of extflags; the next
456 * character is bit +1, etc. */
457 # define STD_PAT_MODS "msixxn"
459 # define CHARSET_PAT_MODS ASCII_RESTRICT_PAT_MODS DEPENDS_PAT_MODS LOCALE_PAT_MODS UNICODE_PAT_MODS
461 /* This string is expected by XS_re_regexp_pattern() in universal.c to be ordered
462 * so that the first character is the flag in bit RXf_PMf_STD_PMMOD_SHIFT of
463 * extflags; the next character is in bit +1, etc. */
464 # define INT_PAT_MODS STD_PAT_MODS KEEPCOPY_PAT_MODS
466 # define EXT_PAT_MODS ONCE_PAT_MODS KEEPCOPY_PAT_MODS NOCAPTURE_PAT_MODS
467 # define QR_PAT_MODS STD_PAT_MODS EXT_PAT_MODS CHARSET_PAT_MODS
468 # define M_PAT_MODS QR_PAT_MODS LOOP_PAT_MODS
469 # define S_PAT_MODS M_PAT_MODS EXEC_PAT_MODS NONDESTRUCT_PAT_MODS
472 * NOTE: if you modify any RXf flags you should run regen.pl or
473 * regen/regcomp.pl so that regnodes.h is updated with the changes.
478 Set in Perl_pmruntime for a split. Will be used by regex engines to
479 check whether they should set RXf_SKIPWHITE
481 # define RXf_SPLIT RXf_PMf_SPLIT
483 /* Currently the regex flags occupy a single 32-bit word. Not all bits are
484 * currently used. The lower bits are shared with their corresponding PMf flag
485 * bits, up to but not including _RXf_PMf_SHIFT_NEXT. The unused bits
486 * immediately follow; finally the used RXf-only (unshared) bits, so that the
487 * highest bit in the word is used. This gathers all the unused bits as a pool
488 * in the middle, like so: 11111111111111110000001111111111
489 * where the '1's represent used bits, and the '0's unused. This design allows
490 * us to allocate off one end of the pool if we need to add a shared bit, and
491 * off the other end if we need a non-shared bit, without disturbing the other
492 * bits. This maximizes the likelihood of being able to change things without
493 * breaking binary compatibility.
495 * To add shared bits, do so in op_reg_common.h. This should change
496 * _RXf_PMf_SHIFT_NEXT so that things won't compile. Then come to regexp.h and
497 * op.h and adjust the constant adders in the definitions of RXf_BASE_SHIFT and
498 * Pmf_BASE_SHIFT down by the number of shared bits you added. That's it.
499 * Things should be binary compatible. But if either of these gets to having
500 * to subtract rather than add, leave at 0 and instead adjust all the entries
501 * that are in terms of it. But if the first one of those is already
502 * RXf_BASE_SHIFT+0, there are no bits left, and a redesign is in order.
504 * To remove unshared bits, just delete its entry. If you're where breaking
505 * binary compatibility is ok to do, you might want to adjust things to move
506 * the newly opened space so that it gets absorbed into the common pool.
508 * To add unshared bits, first use up any gaps in the middle. Otherwise,
509 * allocate off the low end until you get to RXf_BASE_SHIFT+0. If that isn't
510 * enough, move RXf_BASE_SHIFT down (if possible) and add the new bit at the
511 * other end instead; this preserves binary compatibility.
513 * For the regexp bits, PL_reg_extflags_name[] in regnodes.h has a comment
514 * giving which bits are used/unused */
516 # define RXf_BASE_SHIFT (_RXf_PMf_SHIFT_NEXT + 2)
518 /* What we have seen */
519 # define RXf_NO_INPLACE_SUBST (1U<<(RXf_BASE_SHIFT+2))
520 # define RXf_EVAL_SEEN (1U<<(RXf_BASE_SHIFT+3))
523 # define RXf_UNBOUNDED_QUANTIFIER_SEEN (1U<<(RXf_BASE_SHIFT+4))
524 # define RXf_CHECK_ALL (1U<<(RXf_BASE_SHIFT+5))
527 # define RXf_MATCH_UTF8 (1U<<(RXf_BASE_SHIFT+6)) /* $1 etc are utf8 */
530 # define RXf_USE_INTUIT_NOML (1U<<(RXf_BASE_SHIFT+7))
531 # define RXf_USE_INTUIT_ML (1U<<(RXf_BASE_SHIFT+8))
532 # define RXf_INTUIT_TAIL (1U<<(RXf_BASE_SHIFT+9))
533 # define RXf_USE_INTUIT (RXf_USE_INTUIT_NOML|RXf_USE_INTUIT_ML)
535 /* Do we have some sort of anchor? */
536 # define RXf_IS_ANCHORED (1U<<(RXf_BASE_SHIFT+10))
538 /* Copy and tainted info */
539 # define RXf_COPY_DONE (1U<<(RXf_BASE_SHIFT+11))
541 /* post-execution: $1 et al are tainted */
542 # define RXf_TAINTED_SEEN (1U<<(RXf_BASE_SHIFT+12))
543 /* this pattern was tainted during compilation */
544 # define RXf_TAINTED (1U<<(RXf_BASE_SHIFT+13))
546 /* Flags indicating special patterns */
547 # define RXf_START_ONLY (1U<<(RXf_BASE_SHIFT+14)) /* Pattern is /^/ */
548 # define RXf_SKIPWHITE (1U<<(RXf_BASE_SHIFT+15)) /* Pattern is for a */
550 # define RXf_WHITE (1U<<(RXf_BASE_SHIFT+16)) /* Pattern is /\s+/ */
551 # define RXf_NULL (1U<<(RXf_BASE_SHIFT+17)) /* Pattern is // */
553 /* See comments at the beginning of these defines about adding bits. The
554 * highest bit position should be used, so that if RXf_BASE_SHIFT gets
555 * increased, the #error below will be triggered so that you will be reminded
556 * to adjust things at the other end to keep the bit positions unchanged */
557 # if RXf_BASE_SHIFT+17 > 31
558 # error Too many RXf_PMf bits used. See comments at beginning of these for what to do
562 * NOTE: if you modify any RXf flags you should run regen.pl or
563 * regen/regcomp.pl so that regnodes.h is updated with the changes.
567 # ifdef NO_TAINT_SUPPORT
568 # define RX_ISTAINTED(rx_sv) 0
569 # define RXp_ISTAINTED(prog) 0
570 # define RX_TAINT_on(rx_sv) NOOP
571 # define RXp_MATCH_TAINTED(prog) 0
572 # define RX_MATCH_TAINTED(rx_sv) 0
573 # define RXp_MATCH_TAINTED_on(prog) NOOP
574 # define RX_MATCH_TAINTED_on(rx_sv) NOOP
575 # define RXp_MATCH_TAINTED_off(prog) NOOP
576 # define RX_MATCH_TAINTED_off(rx_sv) NOOP
578 # define RX_ISTAINTED(rx_sv) (RX_EXTFLAGS(rx_sv) & RXf_TAINTED)
579 # define RXp_ISTAINTED(prog) (RXp_EXTFLAGS(prog) & RXf_TAINTED)
580 # define RX_TAINT_on(rx_sv) (RX_EXTFLAGS(rx_sv) |= RXf_TAINTED)
581 # define RXp_MATCH_TAINTED(prog) (RXp_EXTFLAGS(prog) & RXf_TAINTED_SEEN)
582 # define RX_MATCH_TAINTED(rx_sv) (RX_EXTFLAGS(rx_sv) & RXf_TAINTED_SEEN)
583 # define RXp_MATCH_TAINTED_on(prog) (RXp_EXTFLAGS(prog) |= RXf_TAINTED_SEEN)
584 # define RX_MATCH_TAINTED_on(rx_sv) (RX_EXTFLAGS(rx_sv) |= RXf_TAINTED_SEEN)
585 # define RXp_MATCH_TAINTED_off(prog) (RXp_EXTFLAGS(prog) &= ~RXf_TAINTED_SEEN)
586 # define RX_MATCH_TAINTED_off(rx_sv) (RX_EXTFLAGS(rx_sv) &= ~RXf_TAINTED_SEEN)
589 # define RXp_HAS_CUTGROUP(prog) ((prog)->intflags & PREGf_CUTGROUP_SEEN)
591 # define RX_MATCH_TAINTED_set(rx_sv, t) ((t) \
592 ? RX_MATCH_TAINTED_on(rx_sv) \
593 : RX_MATCH_TAINTED_off(rx_sv))
595 # define RXp_MATCH_COPIED(prog) (RXp_EXTFLAGS(prog) & RXf_COPY_DONE)
596 # define RX_MATCH_COPIED(rx_sv) (RX_EXTFLAGS(rx_sv) & RXf_COPY_DONE)
597 # define RXp_MATCH_COPIED_on(prog) (RXp_EXTFLAGS(prog) |= RXf_COPY_DONE)
598 # define RX_MATCH_COPIED_on(rx_sv) (RX_EXTFLAGS(rx_sv) |= RXf_COPY_DONE)
599 # define RXp_MATCH_COPIED_off(prog) (RXp_EXTFLAGS(prog) &= ~RXf_COPY_DONE)
600 # define RX_MATCH_COPIED_off(rx_sv) (RX_EXTFLAGS(rx_sv) &= ~RXf_COPY_DONE)
601 # define RX_MATCH_COPIED_set(rx_sv,t) ((t) \
602 ? RX_MATCH_COPIED_on(rx_sv) \
603 : RX_MATCH_COPIED_off(rx_sv))
605 # define RXp_EXTFLAGS(rx) ((rx)->extflags)
606 # define RXp_COMPFLAGS(rx) ((rx)->compflags)
608 /* For source compatibility. We used to store these explicitly. */
609 # define RX_PRECOMP(rx_sv) (RX_WRAPPED(rx_sv) \
610 + ReANY(rx_sv)->pre_prefix)
611 # define RX_PRECOMP_const(rx_sv) (RX_WRAPPED_const(rx_sv) \
612 + ReANY(rx_sv)->pre_prefix)
613 /* FIXME? Are we hardcoding too much here and constraining plugin extension
614 writers? Specifically, the value 1 assumes that the wrapped version always
615 has exactly one character at the end, a ')'. Will that always be true? */
616 # define RX_PRELEN(rx_sv) (RX_WRAPLEN(rx_sv) \
617 - ReANY(rx_sv)->pre_prefix - 1)
619 # define RX_WRAPPED(rx_sv) SvPVX(rx_sv)
620 # define RX_WRAPPED_const(rx_sv) SvPVX_const(rx_sv)
621 # define RX_WRAPLEN(rx_sv) SvCUR(rx_sv)
622 # define RX_CHECK_SUBSTR(rx_sv) (ReANY(rx_sv)->check_substr)
623 # define RX_REFCNT(rx_sv) SvREFCNT(rx_sv)
624 # define RX_EXTFLAGS(rx_sv) RXp_EXTFLAGS(ReANY(rx_sv))
625 # define RX_COMPFLAGS(rx_sv) RXp_COMPFLAGS(ReANY(rx_sv))
626 # define RXp_ENGINE(prog) ((prog)->engine)
627 # define RX_ENGINE(rx_sv) (RXp_ENGINE(ReANY(rx_sv)))
628 # define RXp_SUBBEG(prog) ((prog)->subbeg)
629 # define RX_SUBBEG(rx_sv) (RXp_SUBBEG(ReANY(rx_sv)))
630 # define RXp_SUBOFFSET(prog) ((prog)->suboffset)
631 # define RX_SUBOFFSET(rx_sv) (RXp_SUBOFFSET(ReANY(rx_sv)))
632 # define RXp_SUBCOFFSET(prog) ((prog)->subcoffset)
633 # define RX_SUBCOFFSET(rx_sv) (RXp_SUBCOFFSET(ReANY(rx_sv)))
634 # define RXp_OFFSp(prog) ((prog)->offs)
635 # define RX_OFFSp(rx_sv) (RXp_OFFSp(ReANY(rx_sv)))
636 # define RXp_LOGICAL_NPARENS(prog) ((prog)->logical_nparens)
637 # define RX_LOGICAL_NPARENS(rx_sv) (RXp_LOGICAL_NPARENS(ReANY(rx_sv)))
638 # define RXp_LOGICAL_TO_PARNO(prog) ((prog)->logical_to_parno)
639 # define RX_LOGICAL_TO_PARNO(rx_sv) (RXp_LOGICAL_TO_PARNO(ReANY(rx_sv)))
640 # define RXp_PARNO_TO_LOGICAL(prog) ((prog)->parno_to_logical)
641 # define RX_PARNO_TO_LOGICAL(rx_sv) (RXp_PARNO_TO_LOGICAL(ReANY(rx_sv)))
642 # define RXp_PARNO_TO_LOGICAL_NEXT(prog) ((prog)->parno_to_logical_next)
643 # define RX_PARNO_TO_LOGICAL_NEXT(rx_sv) (RXp_PARNO_TO_LOGICAL_NEXT(ReANY(rx_sv)))
644 # define RXp_NPARENS(prog) ((prog)->nparens)
645 # define RX_NPARENS(rx_sv) (RXp_NPARENS(ReANY(rx_sv)))
646 # define RXp_SUBLEN(prog) ((prog)->sublen)
647 # define RX_SUBLEN(rx_sv) (RXp_SUBLEN(ReANY(rx_sv)))
648 # define RXp_MINLEN(prog) ((prog)->minlen)
649 # define RX_MINLEN(rx_sv) (RXp_MINLEN(ReANY(rx_sv)))
650 # define RXp_MINLENRET(prog) ((prog)->minlenret)
651 # define RX_MINLENRET(rx_sv) (RXp_MINLENRET(ReANY(rx_sv)))
652 # define RXp_GOFS(prog) ((prog)->gofs)
653 # define RX_GOFS(rx_sv) (RXp_GOFS(ReANY(rx_sv)))
654 # define RXp_LASTPAREN(prog) ((prog)->lastparen)
655 # define RX_LASTPAREN(rx_sv) (RXp_LASTPAREN(ReANY(rx_sv)))
656 # define RXp_LASTCLOSEPAREN(prog) ((prog)->lastcloseparen)
657 # define RX_LASTCLOSEPAREN(rx_sv) (RXp_LASTCLOSEPAREN(ReANY(rx_sv)))
658 # define RXp_SAVED_COPY(prog) ((prog)->saved_copy)
659 # define RX_SAVED_COPY(rx_sv) (RXp_SAVED_COPY(ReANY(rx_sv)))
660 # define RXp_SUBSTRS(prog) ((prog)->substrs)
661 # define RX_SUBSTRS(rx_sv) (RXp_SUBSTRS(ReANY(rx_sv)))
662 # define RXp_PPRIVATE(prog) ((prog)->pprivate)
663 # define RX_PPRIVATE(rx_sv) (RXp_PPRIVATE(ReANY(rx_sv)))
664 # define RXp_QR_ANONCV(prog) ((prog)->qr_anoncv)
665 # define RX_QR_ANONCV(rx_sv) (RXp_QR_ANONCV(ReANY(rx_sv)))
666 # define RXp_MOTHER_RE(prog) ((prog)->mother_re)
667 # define RX_MOTHER_RE(rx_sv) (RXp_MOTHER_RE(ReANY(rx_sv)))
668 # define RXp_PRE_PREFIX(prog) ((prog)->pre_prefix)
669 # define RX_PRE_PREFIX(rx_sv) (RXp_PRE_PREFIX(ReANY(rx_sv)))
671 /* last match was zero-length */
672 # define RXp_ZERO_LEN(prog) \
673 (RXp_OFFS_START(prog,0) + (SSize_t)RXp_GOFS(prog) \
674 == RXp_OFFS_END(prog,0))
675 # define RX_ZERO_LEN(rx_sv) (RXp_ZERO_LEN(ReANY(rx_sv)))
677 #endif /* PLUGGABLE_RE_EXTENSION */
679 /* Stuff that needs to be included in the pluggable extension goes below here */
682 # define RXp_MATCH_COPY_FREE(prog) \
684 if (RXp_SAVED_COPY(prog)) { \
685 SV_CHECK_THINKFIRST_COW_DROP(RXp_SAVED_COPY(prog)); \
687 if (RXp_MATCH_COPIED(prog)) { \
688 Safefree(RXp_SUBBEG(prog)); \
689 RXp_MATCH_COPIED_off(prog); \
693 # define RXp_MATCH_COPY_FREE(prog) \
695 if (RXp_MATCH_COPIED(prog)) { \
696 Safefree(RXp_SUBBEG(prog)); \
697 RXp_MATCH_COPIED_off(prog); \
701 #define RX_MATCH_COPY_FREE(rx_sv) RXp_MATCH_COPY_FREE(ReANY(rx_sv))
703 #define RXp_MATCH_UTF8(prog) (RXp_EXTFLAGS(prog) & RXf_MATCH_UTF8)
704 #define RX_MATCH_UTF8(rx_sv) (RX_EXTFLAGS(rx_sv) & RXf_MATCH_UTF8)
705 #define RXp_MATCH_UTF8_on(prog) (RXp_EXTFLAGS(prog) |= RXf_MATCH_UTF8)
706 #define RX_MATCH_UTF8_on(rx_sv) (RXp_MATCH_UTF8_on(ReANY(rx_sv)))
707 #define RXp_MATCH_UTF8_off(prog) (RXp_EXTFLAGS(prog) &= ~RXf_MATCH_UTF8)
708 #define RX_MATCH_UTF8_off(rx_sv) (RXp_MATCH_UTF8_off(ReANY(rx_sv))
709 #define RXp_MATCH_UTF8_set(prog, t) ((t) \
710 ? RXp_MATCH_UTF8_on(prog) \
711 : RXp_MATCH_UTF8_off(prog))
712 #define RX_MATCH_UTF8_set(rx_sv, t) (RXp_MATCH_UTF8_set(ReANY(rx_sv), t))
714 /* Whether the pattern stored at RX_WRAPPED is in UTF-8 */
715 #define RX_UTF8(rx_sv) SvUTF8(rx_sv)
718 /* bits in flags arg of Perl_regexec_flags() */
720 #define REXEC_COPY_STR 0x01 /* Need to copy the string for captures. */
721 #define REXEC_CHECKED 0x02 /* re_intuit_start() already called. */
722 #define REXEC_SCREAM 0x04 /* currently unused. */
723 #define REXEC_IGNOREPOS 0x08 /* use stringarg, not pos(), for \G match */
724 #define REXEC_NOT_FIRST 0x10 /* This is another iteration of //g:
725 no need to copy string again */
727 /* under REXEC_COPY_STR, it's ok for the
728 engine (modulo PL_sawamperand etc)
729 to skip copying: ... */
730 #define REXEC_COPY_SKIP_PRE 0x20 /* ...the $` part of the string, or */
731 #define REXEC_COPY_SKIP_POST 0x40 /* ...the $' part of the string */
732 #define REXEC_FAIL_ON_UNDERFLOW 0x80 /* fail the match if $& would start before
733 the start pos (so s/.\G// would fail
734 on second iteration */
736 #if defined(PERL_USE_GCC_BRACE_GROUPS)
737 # define ReREFCNT_inc(re) \
739 /* This is here to generate a casting warning if incorrect. */ \
740 REGEXP *const _rerefcnt_inc = (re); \
741 assert(SvTYPE(_rerefcnt_inc) == SVt_REGEXP); \
742 SvREFCNT_inc(_rerefcnt_inc); \
745 # define ReREFCNT_dec(re) \
747 /* This is here to generate a casting warning if incorrect. */ \
748 REGEXP *const _rerefcnt_dec = (re); \
749 SvREFCNT_dec(_rerefcnt_dec); \
752 # define ReREFCNT_dec(re) SvREFCNT_dec(re)
753 # define ReREFCNT_inc(re) ((REGEXP *) SvREFCNT_inc(re))
755 #define ReANY(re) Perl_ReANY((const REGEXP *)(re))
757 /* FIXME for plugins. */
759 #define FBMcf_TAIL_DOLLAR 1
760 #define FBMcf_TAIL_DOLLARM 2
761 #define FBMcf_TAIL_Z 4
762 #define FBMcf_TAIL_z 8
763 #define FBMcf_TAIL (FBMcf_TAIL_DOLLAR|FBMcf_TAIL_DOLLARM|FBMcf_TAIL_Z|FBMcf_TAIL_z)
765 #define FBMrf_MULTILINE 1
767 struct regmatch_state;
768 struct regmatch_slab;
770 /* like regmatch_info_aux, but contains extra fields only needed if the
771 * pattern contains (?{}). If used, is snuck into the second slot in the
772 * regmatch_state stack at the start of execution */
776 PMOP *curpm; /* saved PL_curpm */
778 SV *saved_copy; /* saved saved_copy field from rex */
780 char *subbeg; /* saved subbeg field from rex */
781 STRLEN sublen; /* saved sublen field from rex */
782 STRLEN suboffset; /* saved suboffset field from rex */
783 STRLEN subcoffset; /* saved subcoffset field from rex */
784 SV *sv; /* $_ during (?{}) */
785 MAGIC *pos_magic; /* pos() magic attached to $_ */
786 SSize_t pos; /* the original value of pos() in pos_magic */
787 U8 pos_flags; /* flags to be restored; currently only MGf_BYTES*/
788 } regmatch_info_aux_eval;
791 /* fields that logically live in regmatch_info, but which need cleaning
792 * up on croak(), and so are instead are snuck into the first slot in
793 * the regmatch_state stack at the start of execution */
796 regmatch_info_aux_eval *info_aux_eval;
797 struct regmatch_state *old_regmatch_state; /* saved PL_regmatch_state */
798 struct regmatch_slab *old_regmatch_slab; /* saved PL_regmatch_slab */
799 char *poscache; /* S-L cache of fail positions of WHILEMs */
804 =for apidoc Ay||regmatch_info
805 Some basic information about the current match that is created by
806 Perl_regexec_flags and then passed to regtry(), regmatch() etc.
807 It is allocated as a local var on the stack, so nothing should be
808 stored in it that needs preserving or clearing up on croak().
809 For that, see the aux_info and aux_info_eval members of the
810 regmatch_state union.
816 REGEXP *prog; /* the regex being executed */
817 const char * strbeg; /* real start of string */
818 char *strend; /* one byte beyond last char of match string */
819 char *till; /* matches shorter than this fail (see minlen arg) */
820 SV *sv; /* the SV string currently being matched */
821 char *ganch; /* position of \G anchor */
822 char *cutpoint; /* (*COMMIT) position (if any) */
823 regmatch_info_aux *info_aux; /* extra fields that need cleanup */
824 regmatch_info_aux_eval *info_aux_eval; /* extra saved state for (?{}) */
825 I32 poscache_maxiter; /* how many whilems todo before S-L cache kicks in */
826 I32 poscache_iter; /* current countdown from _maxiter to zero */
827 STRLEN poscache_size; /* size of regmatch_info_aux.poscache */
828 bool intuit; /* re_intuit_start() is the top-level caller */
829 bool is_utf8_pat; /* regex is utf8 */
830 bool is_utf8_target; /* string being matched is utf8 */
831 bool warned; /* we have issued a recursion warning; no need for more */
835 /* structures for holding and saving the state maintained by regmatch() */
837 #ifndef MAX_RECURSE_EVAL_NOCHANGE_DEPTH
838 # define MAX_RECURSE_EVAL_NOCHANGE_DEPTH 10
841 /* The +1 is because everything matches itself, which isn't included in
842 * MAX_FOLD_FROMS; the +2 is based on the current Unicode standards needs, and
843 * is unlikely to change. An assertion should fail in regexec.c if it is too
844 * low. It is needed for certain edge cases involving multi-character folds
845 * when the first component also participates in a fold individually. */
846 #define MAX_MATCHES (MAX_FOLD_FROMS + 1 + 2)
848 struct next_matchable_info {
853 PERL_INT_FAST8_T count; /* Negative means not initialized */
854 PERL_UINT_FAST8_T min_length;
855 PERL_UINT_FAST8_T max_length;
856 PERL_UINT_FAST8_T initial_definitive;
857 PERL_UINT_FAST8_T initial_exact;
858 PERL_UINT_FAST8_T lengths[MAX_MATCHES];
860 /* The size is from trial and error, and could change with new Unicode
861 * standards, in which case there is an assertion that should start
862 * failing. This size could be calculated in one of the regen scripts
863 * dealing with Unicode, but khw thinks the likelihood of it changing is
864 * low enough that it isn't worth the effort. */
868 typedef I32 CHECKPOINT;
870 typedef struct regmatch_state {
871 int resume_state; /* where to jump to on return */
872 char *locinput; /* where to backtrack in string on failure */
874 U8 *sr0; /* position of start of script run, or NULL */
878 /* the 'info_aux' and 'info_aux_eval' union members are cuckoos in
879 * the nest. They aren't saved backtrack state; rather they
880 * represent one or two extra chunks of data that need allocating
881 * at the start of a match. These fields would logically live in
882 * the regmatch_info struct, except that is allocated on the
883 * C stack, and these fields are all things that require cleanup
884 * after a croak(), when the stack is lost.
885 * As a convenience, we just use the first 1 or 2 regmatch_state
886 * slots to store this info, as we will be allocating a slab of
887 * these anyway. Otherwise we'd have to malloc and then free them,
888 * or allocate them on the save stack (where they will get
889 * realloced if the save stack grows).
890 * info_aux contains the extra fields that are always needed;
891 * info_aux_eval contains extra fields that only needed if
892 * the pattern contains code blocks
893 * We split them into two separate structs to avoid increasing
894 * the size of the union.
897 regmatch_info_aux info_aux;
899 regmatch_info_aux_eval info_aux_eval;
901 /* this is a fake union member that matches the first element
902 * of each member that needs to store positive backtrack
905 struct regmatch_state *prev_yes_state;
909 /* NOTE: Regarding 'cp' and 'lastcp' in the following structs...
911 * In the majority of cases we use 'cp' for the "normal"
912 * checkpoint for paren saves, and 'lastcp' for the addtional
913 * paren saves that are done only under RE_PESSIMISTIC_PARENS.
915 * There may be a few cases where both are used always.
916 * Regardless they tend be used something like this:
918 * ST.cp = regcppush(rex, 0, maxopenparen);
919 * REGCP_SET(ST.lastcp);
921 * thus ST.cp holds the checkpoint from before we push parens,
922 * and ST.lastcp holds the checkpoint from afterwards.
925 /* branchlike members */
926 /* this is a fake union member that matches the first elements
927 * of each member that needs to behave like a branch */
929 /* this first element must match u.yes */
930 struct regmatch_state *prev_yes_state;
933 CHECKPOINT cp; /* see note above "struct branchlike" */
934 CHECKPOINT lastcp; /* see note above "struct branchlike" */
941 /* the first elements must match u.branchlike */
942 struct regmatch_state *prev_yes_state;
945 CHECKPOINT cp; /* see note above "struct branchlike" */
946 CHECKPOINT lastcp; /* see note above "struct branchlike" */
950 regnode *next_branch; /* next branch node */
954 /* the first elements must match u.branchlike */
955 struct regmatch_state *prev_yes_state;
958 CHECKPOINT cp; /* see note above "struct branchlike" */
959 CHECKPOINT lastcp; /* see note above "struct branchlike" */
963 U32 accepted; /* how many accepting states left */
964 bool longfold; /* saw a fold with a 1->n char mapping */
965 U16 *jump; /* positive offsets from me */
968 regnode *me; /* Which node am I - needed for jump tries*/
969 U8 *firstpos; /* pos in string of first trie match */
970 U32 firstchars; /* len in chars of firstpos from start */
971 U16 nextword; /* next word to try */
972 U16 topword; /* longest accepted word */
975 /* special types - these members are used to store state for special
976 regops like eval, if/then, lookaround and the markpoint state */
978 /* this first element must match u.yes */
979 struct regmatch_state *prev_yes_state;
980 struct regmatch_state *prev_curlyx;
981 struct regmatch_state *prev_eval;
983 CHECKPOINT cp; /* see note above "struct branchlike" */
984 CHECKPOINT lastcp; /* see note above "struct branchlike" */
985 U32 close_paren; /* which close bracket is our end (+1) */
986 regnode *B; /* the node following us */
987 char *prev_recurse_locinput;
991 /* this first element must match u.yes */
992 struct regmatch_state *prev_yes_state;
994 I32 logical; /* saved copy of 'logical' var */
995 U8 count; /* number of beginning positions */
998 regnode *me; /* the IFMATCH/SUSPEND/UNLESSM node */
999 char *prev_match_end;
1000 } ifmatch; /* and SUSPEND/UNLESSM */
1003 /* this first element must match u.yes */
1004 struct regmatch_state *prev_yes_state;
1005 struct regmatch_state *prev_mark;
1014 /* quantifiers - these members are used for storing state for
1015 the regops used to implement quantifiers */
1017 /* this first element must match u.yes */
1018 struct regmatch_state *prev_yes_state;
1019 struct regmatch_state *prev_curlyx; /* previous cur_curlyx */
1020 regnode *me; /* the CURLYX node */
1021 regnode *B; /* the B node in /A*B/ */
1022 CHECKPOINT cp; /* see note above "struct branchlike" */
1023 CHECKPOINT lastcp; /* see note above "struct branchlike" */
1025 int parenfloor; /* how far back to strip paren data */
1027 /* these two are modified by WHILEM */
1028 int count; /* how many instances of A we've matched */
1029 char *lastloc; /* where previous A matched (0-len detect) */
1033 /* this first element must match u.yes */
1034 struct regmatch_state *prev_yes_state;
1035 struct regmatch_state *save_curlyx;
1036 CHECKPOINT cp; /* see note above "struct branchlike" */
1037 CHECKPOINT lastcp; /* see note above "struct branchlike" */
1038 char *save_lastloc; /* previous curlyx.lastloc */
1044 /* this first element must match u.yes */
1045 struct regmatch_state *prev_yes_state;
1048 CHECKPOINT cp; /* see note above "struct branchlike" */
1049 CHECKPOINT lastcp; /* see note above "struct branchlike" */
1050 I32 alen; /* length of first-matched A string */
1053 regnode *A, *B; /* the nodes corresponding to /A*B/ */
1054 regnode *me; /* the curlym node */
1055 struct next_matchable_info Binfo;
1062 CHECKPOINT cp; /* see note above "struct branchlike" */
1063 CHECKPOINT lastcp; /* see note above "struct branchlike" */
1064 char *maxpos; /* highest possible point in string to match */
1065 char *oldloc; /* the previous locinput */
1067 int min, max; /* {m,n} */
1068 regnode *A, *B; /* the nodes corresponding to /A*B/ */
1069 struct next_matchable_info Binfo;
1070 } curly; /* and CURLYN/PLUS/STAR */
1075 } backref; /* REF and friends */
1081 /* how many regmatch_state structs to allocate as a single slab.
1082 * We do it in 4K blocks for efficiency. The "3" is 2 for the next/prev
1083 * pointers, plus 1 for any mythical malloc overhead. */
1085 #define PERL_REGMATCH_SLAB_SLOTS \
1086 ((4096 - 3 * sizeof (void*)) / sizeof(regmatch_state))
1088 typedef struct regmatch_slab {
1089 regmatch_state states[PERL_REGMATCH_SLAB_SLOTS];
1090 struct regmatch_slab *prev, *next;
1094 #define REG_FETCH_ABSOLUTE 1
1097 * ex: set ts=8 sts=4 sw=4 et: