This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
new perldelta
[perl5.git] / regexp.h
CommitLineData
a0d0e21e 1/* regexp.h
d6376244 2 *
4bb101f2 3 * Copyright (C) 1993, 1994, 1996, 1997, 1999, 2000, 2001, 2003,
1129b882 4 * 2005, 2006, 2007, 2008 by Larry Wall and others
d6376244
JH
5 *
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.
8 *
a0d0e21e
LW
9 */
10
378cc40b
LW
11/*
12 * Definitions etc. for regexp(3) routines.
13 *
14 * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
15 * not the System V one.
16 */
be8e71aa 17#ifndef PLUGGABLE_RE_EXTENSION
785a26d5
YO
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'*/
378cc40b 20
6c5474e8 21# include "utf8.h"
79a2a0e8 22
f55b7b26
KW
23typedef SSize_t regnode_offset;
24
12d173c9
YO
25struct regnode_meta {
26 U8 type;
27 U8 arg_len;
28 U8 arg_len_varies;
29 U8 off_by_arg;
30};
31
44eb4cdc
YO
32/* this ensures that on alignment sensitive platforms
33 * this struct is aligned on 32 bit boundaries */
34union regnode_head {
35 struct {
36 union {
37 U8 flags;
38 U8 str_len_u8;
39 U8 first_byte;
40 } u_8;
41 U8 type;
42 U16 next_off;
43 } data;
44 U32 data_u32;
45};
46
c277df42 47struct regnode {
44eb4cdc 48 union regnode_head head;
c277df42
IZ
49};
50
51typedef struct regnode regnode;
52
cad2e5aa 53struct reg_substr_data;
2779dcf1 54
0ee3c8d0
JH
55struct reg_data;
56
f9f4320a 57struct regexp_engine;
28d8d7f4 58struct regexp;
bbe252da 59
785a26d5 60struct reg_substr_datum {
c4828ca0 61 SSize_t min_offset; /* min pos (in chars) that substr must appear */
6459affc 62 SSize_t max_offset; /* max pos (in chars) that substr must appear */
85900e28
YO
63 SV *substr; /* non-utf8 variant */
64 SV *utf8_substr; /* utf8 variant */
c4828ca0 65 SSize_t end_shift; /* how many fixed chars must end the string */
785a26d5
YO
66};
67struct reg_substr_data {
6480a6c4 68 U8 check_ix; /* index into data[] of check substr */
85900e28 69 struct reg_substr_datum data[3]; /* Actual array */
785a26d5 70};
f9f4320a 71
6c5474e8
KW
72# ifdef PERL_ANY_COW
73# define SV_SAVED_COPY SV *saved_copy; /* If non-NULL, SV which is COW from original */
74# else
75# define SV_SAVED_COPY
76# endif
28d8d7f4 77
1ed8aa9f
YO
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 */
f0ab9afb 81typedef struct regexp_paren_pair {
99a90e59
FC
82 SSize_t start;
83 SSize_t end;
05b13cf6 84
b3fd53f3
DM
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
87 * valid, e.g.
85900e28 88 * "abc" =~ /(.(?{print "[$1]"}))+/
b3fd53f3
DM
89 *outputs [][a][b]
90 * This field is not part of the API. */
ea3daa5d 91 SSize_t start_tmp;
f0ab9afb 92} regexp_paren_pair;
28d8d7f4 93
85900e28 94# if defined(PERL_IN_REGCOMP_ANY) || defined(PERL_IN_UTF8_C)
6c5474e8
KW
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)
3f80b571
KW
97
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 */
6c5474e8
KW
100# define _invlist_subtract(a, b, output) _invlist_intersection_maybe_complement_2nd(a, b, TRUE, output)
101# endif
52ae8f7e 102
3d2bd50a
DM
103/* record the position of a (?{...}) within a pattern */
104
105struct reg_code_block {
106 STRLEN start;
107 STRLEN end;
108 OP *block;
b30fcab9 109 REGEXP *src_regex;
3d2bd50a
DM
110};
111
1acab4c5
DM
112/* array of reg_code_block's plus header info */
113
114struct reg_code_blocks {
f8def6c7 115 int refcnt; /* we may be pointed to from a regex and from the savestack */
1acab4c5
DM
116 int count; /* how many code blocks */
117 struct reg_code_block *cb; /* array of reg_code_block's */
118};
119
3d2bd50a 120
882227b7 121/*
1f6e74eb 122= for apidoc AyT||regexp
882227b7
AB
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.
126
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.
131*/
132
a6d10131
YO
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.
135 */
bbe252da 136typedef struct regexp {
a6d10131
YO
137 /*----------------------------------------------------------------------
138 * Fields required for compatibility with SV types
139 */
e1168c11 140 _XPV_HEAD;
a6d10131
YO
141
142 /*----------------------------------------------------------------------
143 * Operational fields
144 */
145
e1168c11
DM
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 */
149
150 /*----------------------------------------------------------------------
151 * Information about the match that the perl core uses to manage things
152 */
153
fe5492d9
YO
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 */
162
1ee51d7e 163 SSize_t maxlen; /* maximum possible number of chars in string to match */
e1168c11 164 SSize_t minlen; /* minimum possible number of chars in string to match */
0c6362ad 165 SSize_t minlenret; /* minimum possible number of chars in $& */
e1168c11
DM
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 */
fe5492d9 169
e1168c11 170 struct reg_substr_data *substrs;
e1168c11
DM
171
172 /* private engine specific data */
173
e1168c11
DM
174 void *pprivate; /* Data private to the regex engine which
175 * created this object. */
a6d10131
YO
176
177 U32 extflags; /* Flags used both externally and internally */
2bb68ff1 178 U32 intflags; /* Engine Specific Internal flags */
e1168c11
DM
179
180 /*----------------------------------------------------------------------
181 * Data about the last/current match. These are modified during matching
182 */
183
0b9dad94 184 U32 lastparen; /* highest close paren matched ($+) */
a6d10131 185 U32 lastcloseparen; /* last close paren matched ($^N) */
e1168c11
DM
186 regexp_paren_pair *offs; /* Array of offsets for (@-) and (@+) */
187 char **recurse_locinput; /* used to detect infinite recursion, XXX: move to internal */
fe5492d9 188
e1168c11
DM
189 /*---------------------------------------------------------------------- */
190
191 /* offset from wrapped to the start of precomp */
192 PERL_BITFIELD32 pre_prefix:4;
193
194 /* original flags used to compile the pattern, may differ from
195 * extflags in various ways */
196 PERL_BITFIELD32 compflags:9;
197
198 /*---------------------------------------------------------------------- */
199
da35f4ca 200 char *subbeg; /* saved or original string so \digit works forever. */
2bb68ff1
RL
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 @-/@+) */
2bb68ff1 205
a6d10131
YO
206 /*----------------------------------------------------------------------
207 * More Operational fields
208 */
2bb68ff1 209
e1168c11 210 CV *qr_anoncv; /* the anon sub wrapped round qr/(?{..})/ */
f8fc2ecf
YO
211} regexp;
212
e1168c11 213
05b13cf6 214#define RXp_PAREN_NAMES(rx) ((rx)->paren_names)
3f11a285 215
05b13cf6 216#define RXp_OFFS_START(rx,n) \
bf0d793b 217 RXp_OFFSp(rx)[(n)].start
3f11a285 218
05b13cf6 219#define RXp_OFFS_END(rx,n) \
bf0d793b 220 RXp_OFFSp(rx)[(n)].end
3f11a285
YO
221
222#define RXp_OFFS_VALID(rx,n) \
bf0d793b 223 (RXp_OFFSp(rx)[(n)].end != -1 && RXp_OFFSp(rx)[(n)].start != -1 )
3f11a285 224
28b45ff7 225
3f11a285
YO
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)
5daac39c 229
28b45ff7
YO
230/*
231
232=for apidoc mh||RX_OFFSp|const REGEXP * rx_sv
233Returns the RX_OFFS structure as a pointer. This should NOT be used
234directly, and the structure it returns may change in future versions of
235perl. Use one of the other RX_OFFS_xxx macros instead, which should be
236stable as Perl evolves. The 'p' suffix on the macro is meant to indicate
237both that the macro is "private" to the internals, and that it returns
238a pointer.
239
240=for apidoc Amh||RX_OFFS_START|const REGEXP * rx_sv|U32 n
241Returns the start offset of the C<n>'th capture buffer, when
242C<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.
244
245=for apidoc Amh||RXp_OFFS_START|const regexp * rx|U32 n
246Same as RX_OFFS_START() but operates on a C<struct regexp *> instead of
247an C<SV *>.
248
249=for apidoc Amh||RX_OFFS_END|const REGEXP * rx_sv|U32 n
250Returns the end offset of the C<n>'th capture buffer, when C<n> is 0 it
251refers to the overall match as a whole. A value of
252-1 is returned if the C<n>'th capture buffer did not match.
253
254=for apidoc Amh||RXp_OFFS_END|const regexp * rx|U32 n
255Same as RX_OFFS_END() but operates on a C<struct regexp *> instead of
256an C<SV *>.
257
258=for apidoc Amh||RX_OFFS_VALID|const REGEXP * rx_sv|U32 n
259Returns true if the C<n>'th offset buffer matched, false otherwise, when
260C<n> is 0 it refers to the overall match as a whole which should always
261return true.
262
263=for apidoc Amh||RXp_OFFS_VALID|const regexp * rx|U32 n
264Same as RX_OFFS_VLAID() but operates on a C<struct regexp *> instead of
265an C<SV *>.
266
dab8baec
KW
267=cut
268
28b45ff7
YO
269*/
270
785a26d5 271/* used for high speed searches */
f9f4320a
YO
272typedef struct re_scream_pos_data_s
273{
85900e28
YO
274 char **scream_olds; /* match pos */
275 SSize_t *scream_pos; /* Internal iterator of scream. */
f9f4320a
YO
276} re_scream_pos_data;
277
785a26d5
YO
278/* regexp_engine structure. This is the dispatch table for regexes.
279 * Any regex engine implementation must be able to build one of these.
280 */
f9f4320a 281typedef struct regexp_engine {
1593ad57 282 REGEXP* (*comp) (pTHX_ SV * const pattern, U32 flags);
49d7dfbc 283 I32 (*exec) (pTHX_ REGEXP * const rx, char* stringarg, char* strend,
ea3daa5d 284 char* strbeg, SSize_t minend, SV* sv,
49d7dfbc 285 void* data, U32 flags);
52a21eb3
DM
286 char* (*intuit) (pTHX_
287 REGEXP * const rx,
288 SV *sv,
289 const char * const strbeg,
290 char *strpos,
291 char *strend,
292 const U32 flags,
9f61653a 293 re_scream_pos_data *data);
49d7dfbc 294 SV* (*checkstr) (pTHX_ REGEXP * const rx);
fc6bde6f 295 void (*rxfree) (pTHX_ REGEXP * const rx);
2fdbfb4d 296 void (*numbered_buff_FETCH) (pTHX_ REGEXP * const rx, const I32 paren,
192b9cd1 297 SV * const sv);
2fdbfb4d
AB
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,
301 const I32 paren);
192b9cd1
AB
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,
305 const U32 flags);
49d7dfbc 306 SV* (*qr_package)(pTHX_ REGEXP * const rx);
6c5474e8 307# ifdef USE_ITHREADS
49d7dfbc 308 void* (*dupe) (pTHX_ REGEXP * const rx, CLONE_PARAMS *param);
6c5474e8 309# endif
3c13cae6 310 REGEXP* (*op_comp) (pTHX_ SV ** const patternp, int pat_count,
1f4fbd3b
MS
311 OP *expr, const struct regexp_engine* eng,
312 REGEXP *old_re,
313 bool *is_bare_re, U32 orig_rx_flags, U32 pm_flags);
f9f4320a
YO
314} regexp_engine;
315
192b9cd1
AB
316/*
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,
319 $2 etc.
320*/
6c5474e8
KW
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 /* $& */
192b9cd1
AB
327
328/*
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.
333*/
334
335/* The Tie::Hash::NamedCapture operation this is part of, if any */
6c5474e8
KW
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
192b9cd1
AB
344
345/* Whether %+ or %- is being operated on */
6c5474e8
KW
346# define RXapif_ONE 0x0100 /* %+ */
347# define RXapif_ALL 0x0200 /* %- */
192b9cd1
AB
348
349/* Whether this is being called from a re:: function */
6c5474e8
KW
350# define RXapif_REGNAME 0x0400
351# define RXapif_REGNAMES 0x0800
352# define RXapif_REGNAMES_COUNT 0x1000
192b9cd1 353
f7e71195 354/*
f7e71195
AB
355=for apidoc Am|REGEXP *|SvRX|SV *sv
356
72d33970 357Convenience macro to get the REGEXP from a SV. This is approximately
f7e71195
AB
358equivalent to the following snippet:
359
360 if (SvMAGICAL(sv))
361 mg_get(sv);
1af910ea
DL
362 if (SvROK(sv))
363 sv = MUTABLE_SV(SvRV(sv));
364 if (SvTYPE(sv) == SVt_REGEXP)
365 return (REGEXP*) sv;
f7e71195 366
796b6530 367C<NULL> will be returned if a REGEXP* is not found.
f7e71195
AB
368
369=for apidoc Am|bool|SvRXOK|SV* sv
370
1af910ea
DL
371Returns a boolean indicating whether the SV (or the one it references)
372is a REGEXP.
f7e71195
AB
373
374If you want to do something with the REGEXP* later use SvRX instead
375and check for NULL.
376
377=cut
378*/
379
6c5474e8
KW
380# define SvRX(sv) (Perl_get_re_arg(aTHX_ sv))
381# define SvRXOK(sv) cBOOL(Perl_get_re_arg(aTHX_ sv))
f7e71195
AB
382
383
7948fc08 384/* Flags stored in regexp->extflags
bbe252da 385 * These are used by code external to the regexp engine
e357fc67 386 *
5b126c84
KW
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
cb5027f2 389 *
eb2624c9
FC
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.
cb5027f2 392 *
bbe252da
YO
393 */
394
6c5474e8 395# include "op_reg_common.h"
5b126c84 396
85900e28 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)
cde0cee5 398
6c5474e8 399# define CASE_STD_PMMOD_FLAGS_PARSE_SET(pmfl, x_count) \
cc4d09e1
KW
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; \
77c8f263
KW
403 case XTENDED_PAT_MOD: if (x_count == 0) { \
404 *(pmfl) |= RXf_PMf_EXTENDED; \
405 *(pmfl) &= ~RXf_PMf_EXTENDED_MORE; \
406 } \
407 else { \
408 *(pmfl) |= RXf_PMf_EXTENDED \
409 |RXf_PMf_EXTENDED_MORE; \
410 } \
411 (x_count)++; break; \
41d7c59e 412 case NOCAPTURE_PAT_MOD: *(pmfl) |= RXf_PMf_NOCAPTURE; break;
cc4d09e1 413
94b03d7d 414/* Note, includes charset ones, assumes 0 is the default for them */
6c5474e8 415# define STD_PMMOD_FLAGS_CLEAR(pmfl) \
77c8f263 416 *(pmfl) &= ~(RXf_PMf_FOLD|RXf_PMf_MULTILINE|RXf_PMf_SINGLELINE|RXf_PMf_EXTENDED|RXf_PMf_EXTENDED_MORE|RXf_PMf_CHARSET|RXf_PMf_NOCAPTURE)
fb85c044 417
bcdf7404 418/* chars and strings used as regex pattern modifiers
486ec47a 419 * Singular is a 'c'har, plural is a "string"
87e95b7f
YO
420 *
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.
bcdf7404 424 */
6c5474e8
KW
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'
441
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"
bcdf7404 453
78fecfc8 454/* This string is expected by regcomp.c to be ordered so that the first
c18d5d15
KW
455 * character is the flag in bit RXf_PMf_STD_PMMOD_SHIFT of extflags; the next
456 * character is bit +1, etc. */
6c5474e8 457# define STD_PAT_MODS "msixxn"
bcdf7404 458
6c5474e8 459# define CHARSET_PAT_MODS ASCII_RESTRICT_PAT_MODS DEPENDS_PAT_MODS LOCALE_PAT_MODS UNICODE_PAT_MODS
94b03d7d 460
78fecfc8 461/* This string is expected by XS_re_regexp_pattern() in universal.c to be ordered
c18d5d15
KW
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. */
6c5474e8 464# define INT_PAT_MODS STD_PAT_MODS KEEPCOPY_PAT_MODS
bcdf7404 465
6c5474e8 466# define EXT_PAT_MODS ONCE_PAT_MODS KEEPCOPY_PAT_MODS NOCAPTURE_PAT_MODS
85900e28 467# define QR_PAT_MODS STD_PAT_MODS EXT_PAT_MODS CHARSET_PAT_MODS
6c5474e8
KW
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
bcdf7404 470
cb5027f2 471/*
eb2624c9
FC
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.
cb5027f2
YO
474 *
475 */
bcdf7404 476
dbc200c5 477/*
5012eebe
DM
478 Set in Perl_pmruntime for a split. Will be used by regex engines to
479 check whether they should set RXf_SKIPWHITE
dbc200c5 480*/
6c5474e8 481# define RXf_SPLIT RXf_PMf_SPLIT
32f62247 482
0bd51918
KW
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.
494 *
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.
503 *
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.
507 *
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.
512 *
513 * For the regexp bits, PL_reg_extflags_name[] in regnodes.h has a comment
514 * giving which bits are used/unused */
515
6c5474e8 516# define RXf_BASE_SHIFT (_RXf_PMf_SHIFT_NEXT + 2)
52d81aa8 517
bbe252da 518/* What we have seen */
6c5474e8 519# define RXf_NO_INPLACE_SUBST (1U<<(RXf_BASE_SHIFT+2))
85900e28 520# define RXf_EVAL_SEEN (1U<<(RXf_BASE_SHIFT+3))
bbe252da
YO
521
522/* Special */
6c5474e8 523# define RXf_UNBOUNDED_QUANTIFIER_SEEN (1U<<(RXf_BASE_SHIFT+4))
85900e28 524# define RXf_CHECK_ALL (1U<<(RXf_BASE_SHIFT+5))
bbe252da
YO
525
526/* UTF8 related */
85900e28 527# define RXf_MATCH_UTF8 (1U<<(RXf_BASE_SHIFT+6)) /* $1 etc are utf8 */
bbe252da
YO
528
529/* Intuit related */
85900e28
YO
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))
6c5474e8 533# define RXf_USE_INTUIT (RXf_USE_INTUIT_NOML|RXf_USE_INTUIT_ML)
bbe252da 534
a3b51d37 535/* Do we have some sort of anchor? */
6c5474e8 536# define RXf_IS_ANCHORED (1U<<(RXf_BASE_SHIFT+10))
a3b51d37 537
bbe252da 538/* Copy and tainted info */
85900e28 539# define RXf_COPY_DONE (1U<<(RXf_BASE_SHIFT+11))
ef07e810 540
1738e041 541/* post-execution: $1 et al are tainted */
85900e28 542# define RXf_TAINTED_SEEN (1U<<(RXf_BASE_SHIFT+12))
ef07e810 543/* this pattern was tainted during compilation */
85900e28 544# define RXf_TAINTED (1U<<(RXf_BASE_SHIFT+13))
52d81aa8
NC
545
546/* Flags indicating special patterns */
6c5474e8
KW
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 */
9cba692b 549 /* split " " */
85900e28
YO
550# define RXf_WHITE (1U<<(RXf_BASE_SHIFT+16)) /* Pattern is /\s+/ */
551# define RXf_NULL (1U<<(RXf_BASE_SHIFT+17)) /* Pattern is // */
0bd51918
KW
552
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 */
6c5474e8
KW
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
559# endif
c737faaf 560
cb5027f2 561/*
eb2624c9
FC
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.
cb5027f2
YO
564 *
565 */
bbe252da 566
6c5474e8
KW
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
577# else
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)
587# endif
588
589# define RXp_HAS_CUTGROUP(prog) ((prog)->intflags & PREGf_CUTGROUP_SEEN)
590
591# define RX_MATCH_TAINTED_set(rx_sv, t) ((t) \
a885a8e0
DM
592 ? RX_MATCH_TAINTED_on(rx_sv) \
593 : RX_MATCH_TAINTED_off(rx_sv))
594
6c5474e8
KW
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) \
a885a8e0
DM
602 ? RX_MATCH_COPIED_on(rx_sv) \
603 : RX_MATCH_COPIED_off(rx_sv))
604
6c5474e8
KW
605# define RXp_EXTFLAGS(rx) ((rx)->extflags)
606# define RXp_COMPFLAGS(rx) ((rx)->compflags)
866c78d1 607
07bc277f 608/* For source compatibility. We used to store these explicitly. */
6c5474e8 609# define RX_PRECOMP(rx_sv) (RX_WRAPPED(rx_sv) \
a885a8e0 610 + ReANY(rx_sv)->pre_prefix)
6c5474e8 611# define RX_PRECOMP_const(rx_sv) (RX_WRAPPED_const(rx_sv) \
a885a8e0 612 + ReANY(rx_sv)->pre_prefix)
5509d87a
NC
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? */
6c5474e8 616# define RX_PRELEN(rx_sv) (RX_WRAPLEN(rx_sv) \
a885a8e0
DM
617 - ReANY(rx_sv)->pre_prefix - 1)
618
6c5474e8
KW
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)))
d145af01 628# define RXp_SUBBEG(prog) ((prog)->subbeg)
6c5474e8 629# define RX_SUBBEG(rx_sv) (RXp_SUBBEG(ReANY(rx_sv)))
d145af01 630# define RXp_SUBOFFSET(prog) ((prog)->suboffset)
6c5474e8 631# define RX_SUBOFFSET(rx_sv) (RXp_SUBOFFSET(ReANY(rx_sv)))
248ff857
YO
632# define RXp_SUBCOFFSET(prog) ((prog)->subcoffset)
633# define RX_SUBCOFFSET(rx_sv) (RXp_SUBCOFFSET(ReANY(rx_sv)))
d145af01 634# define RXp_OFFSp(prog) ((prog)->offs)
3f11a285 635# define RX_OFFSp(rx_sv) (RXp_OFFSp(ReANY(rx_sv)))
d145af01 636# define RXp_LOGICAL_NPARENS(prog) ((prog)->logical_nparens)
fe5492d9 637# define RX_LOGICAL_NPARENS(rx_sv) (RXp_LOGICAL_NPARENS(ReANY(rx_sv)))
d145af01 638# define RXp_LOGICAL_TO_PARNO(prog) ((prog)->logical_to_parno)
fe5492d9 639# define RX_LOGICAL_TO_PARNO(rx_sv) (RXp_LOGICAL_TO_PARNO(ReANY(rx_sv)))
d145af01 640# define RXp_PARNO_TO_LOGICAL(prog) ((prog)->parno_to_logical)
fe5492d9 641# define RX_PARNO_TO_LOGICAL(rx_sv) (RXp_PARNO_TO_LOGICAL(ReANY(rx_sv)))
d145af01 642# define RXp_PARNO_TO_LOGICAL_NEXT(prog) ((prog)->parno_to_logical_next)
fe5492d9 643# define RX_PARNO_TO_LOGICAL_NEXT(rx_sv) (RXp_PARNO_TO_LOGICAL_NEXT(ReANY(rx_sv)))
d145af01 644# define RXp_NPARENS(prog) ((prog)->nparens)
6c5474e8 645# define RX_NPARENS(rx_sv) (RXp_NPARENS(ReANY(rx_sv)))
248ff857
YO
646# define RXp_SUBLEN(prog) ((prog)->sublen)
647# define RX_SUBLEN(rx_sv) (RXp_SUBLEN(ReANY(rx_sv)))
d145af01 648# define RXp_MINLEN(prog) ((prog)->minlen)
6c5474e8 649# define RX_MINLEN(rx_sv) (RXp_MINLEN(ReANY(rx_sv)))
d145af01 650# define RXp_MINLENRET(prog) ((prog)->minlenret)
6c5474e8 651# define RX_MINLENRET(rx_sv) (RXp_MINLENRET(ReANY(rx_sv)))
d145af01 652# define RXp_GOFS(prog) ((prog)->gofs)
6c5474e8 653# define RX_GOFS(rx_sv) (RXp_GOFS(ReANY(rx_sv)))
d145af01
YO
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)
6c5474e8 659# define RX_SAVED_COPY(rx_sv) (RXp_SAVED_COPY(ReANY(rx_sv)))
248ff857
YO
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)))
670
03c83e26 671/* last match was zero-length */
6c5474e8 672# define RXp_ZERO_LEN(prog) \
3f11a285
YO
673 (RXp_OFFS_START(prog,0) + (SSize_t)RXp_GOFS(prog) \
674 == RXp_OFFS_END(prog,0))
6c5474e8 675# define RX_ZERO_LEN(rx_sv) (RXp_ZERO_LEN(ReANY(rx_sv)))
220fc49f 676
be8e71aa
YO
677#endif /* PLUGGABLE_RE_EXTENSION */
678
486ec47a 679/* Stuff that needs to be included in the pluggable extension goes below here */
be8e71aa 680
db2c6cb3 681#ifdef PERL_ANY_COW
ffd0ba0c
YO
682# define RXp_MATCH_COPY_FREE(prog) \
683 STMT_START { \
684 if (RXp_SAVED_COPY(prog)) { \
685 SV_CHECK_THINKFIRST_COW_DROP(RXp_SAVED_COPY(prog)); \
686 } \
687 if (RXp_MATCH_COPIED(prog)) { \
688 Safefree(RXp_SUBBEG(prog)); \
689 RXp_MATCH_COPIED_off(prog); \
690 } \
691 } STMT_END
ed252734 692#else
ffd0ba0c
YO
693# define RXp_MATCH_COPY_FREE(prog) \
694 STMT_START { \
695 if (RXp_MATCH_COPIED(prog)) { \
696 Safefree(RXp_SUBBEG(prog)); \
697 RXp_MATCH_COPIED_off(prog); \
698 } \
699 } STMT_END
ed252734 700#endif
a8cb1947 701#define RX_MATCH_COPY_FREE(rx_sv) RXp_MATCH_COPY_FREE(ReANY(rx_sv))
ed252734 702
a885a8e0
DM
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)
196a02af 705#define RXp_MATCH_UTF8_on(prog) (RXp_EXTFLAGS(prog) |= RXf_MATCH_UTF8)
97f6857b 706#define RX_MATCH_UTF8_on(rx_sv) (RXp_MATCH_UTF8_on(ReANY(rx_sv)))
196a02af
DM
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))
efd26800
NC
713
714/* Whether the pattern stored at RX_WRAPPED is in UTF-8 */
a885a8e0 715#define RX_UTF8(rx_sv) SvUTF8(rx_sv)
7948fc08 716
a340edde
DM
717
718/* bits in flags arg of Perl_regexec_flags() */
719
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 */
726
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 */
d5e7783a
DM
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 */
c277df42 735
041c1a23 736#if defined(PERL_USE_GCC_BRACE_GROUPS)
85900e28
YO
737# define ReREFCNT_inc(re) \
738 ({ \
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); \
743 _rerefcnt_inc; \
288b8c02 744 })
85900e28
YO
745# define ReREFCNT_dec(re) \
746 ({ \
747 /* This is here to generate a casting warning if incorrect. */ \
748 REGEXP *const _rerefcnt_dec = (re); \
749 SvREFCNT_dec(_rerefcnt_dec); \
288b8c02
NC
750 })
751#else
85900e28
YO
752# define ReREFCNT_dec(re) SvREFCNT_dec(re)
753# define ReREFCNT_inc(re) ((REGEXP *) SvREFCNT_inc(re))
288b8c02 754#endif
85900e28 755#define ReANY(re) Perl_ReANY((const REGEXP *)(re))
288b8c02
NC
756
757/* FIXME for plugins. */
cf93c79d 758
85900e28
YO
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)
cf93c79d 764
85900e28 765#define FBMrf_MULTILINE 1
cad2e5aa 766
331b2dcc
DM
767struct regmatch_state;
768struct regmatch_slab;
8adc0f72 769
bf2039a9
DM
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 */
8adc0f72
DM
773
774typedef struct {
775 regexp *rex;
776 PMOP *curpm; /* saved PL_curpm */
777#ifdef PERL_ANY_COW
778 SV *saved_copy; /* saved saved_copy field from rex */
779#endif
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 */
1d48e83d 784 SV *sv; /* $_ during (?{}) */
8adc0f72 785 MAGIC *pos_magic; /* pos() magic attached to $_ */
ea3daa5d 786 SSize_t pos; /* the original value of pos() in pos_magic */
25fdce4a 787 U8 pos_flags; /* flags to be restored; currently only MGf_BYTES*/
bf2039a9
DM
788} regmatch_info_aux_eval;
789
790
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 */
794
795typedef struct {
796 regmatch_info_aux_eval *info_aux_eval;
331b2dcc
DM
797 struct regmatch_state *old_regmatch_state; /* saved PL_regmatch_state */
798 struct regmatch_slab *old_regmatch_slab; /* saved PL_regmatch_slab */
85900e28 799 char *poscache; /* S-L cache of fail positions of WHILEMs */
bf2039a9
DM
800} regmatch_info_aux;
801
8adc0f72 802
1f6e74eb
KW
803/*
804=for apidoc Ay||regmatch_info
805Some basic information about the current match that is created by
806Perl_regexec_flags and then passed to regtry(), regmatch() etc.
807It is allocated as a local var on the stack, so nothing should be
808stored in it that needs preserving or clearing up on croak().
809For that, see the aux_info and aux_info_eval members of the
810regmatch_state union.
811
812=cut
813*/
3b0527fe
DM
814
815typedef struct {
28d03b21 816 REGEXP *prog; /* the regex being executed */
9d9163fb 817 const char * strbeg; /* real start of string */
28d03b21
DM
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) */
bf2039a9
DM
823 regmatch_info_aux *info_aux; /* extra fields that need cleanup */
824 regmatch_info_aux_eval *info_aux_eval; /* extra saved state for (?{}) */
1cb48e53
DM
825 I32 poscache_maxiter; /* how many whilems todo before S-L cache kicks in */
826 I32 poscache_iter; /* current countdown from _maxiter to zero */
2ac8ff4b 827 STRLEN poscache_size; /* size of regmatch_info_aux.poscache */
02d5137b 828 bool intuit; /* re_intuit_start() is the top-level caller */
ba44c216
DM
829 bool is_utf8_pat; /* regex is utf8 */
830 bool is_utf8_target; /* string being matched is utf8 */
39819bd9 831 bool warned; /* we have issued a recursion warning; no need for more */
3b0527fe 832} regmatch_info;
1f4fbd3b 833
5d9a96ca
DM
834
835/* structures for holding and saving the state maintained by regmatch() */
836
d56b3014 837#ifndef MAX_RECURSE_EVAL_NOCHANGE_DEPTH
6c5474e8 838# define MAX_RECURSE_EVAL_NOCHANGE_DEPTH 10
d56b3014 839#endif
6bda09f9 840
52c50ff1
KW
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)
bb382562
KW
847
848struct next_matchable_info {
849 U8 first_byte_mask;
850 U8 first_byte_anded;
851 U32 mask32;
852 U32 anded32;
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];
859
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. */
865 U8 matches[18];
866};
867
5d9a96ca
DM
868typedef I32 CHECKPOINT;
869
a0374537 870typedef struct regmatch_state {
85900e28
YO
871 int resume_state; /* where to jump to on return */
872 char *locinput; /* where to backtrack in string on failure */
fd1dd2eb 873 char *loceol;
8e9f3eef 874 U8 *sr0; /* position of start of script run, or NULL */
e822a8b4
DM
875
876 union {
77cb431f 877
bf2039a9
DM
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.
895 */
896
897 regmatch_info_aux info_aux;
898
899 regmatch_info_aux_eval info_aux_eval;
900
1f4fbd3b
MS
901 /* this is a fake union member that matches the first element
902 * of each member that needs to store positive backtrack
903 * information */
904 struct {
905 struct regmatch_state *prev_yes_state;
906 } yes;
77cb431f 907
ee28315c
YO
908
909 /* NOTE: Regarding 'cp' and 'lastcp' in the following structs...
910 *
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.
914 *
915 * There may be a few cases where both are used always.
916 * Regardless they tend be used something like this:
917 *
918 * ST.cp = regcppush(rex, 0, maxopenparen);
919 * REGCP_SET(ST.lastcp);
920 *
921 * thus ST.cp holds the checkpoint from before we push parens,
922 * and ST.lastcp holds the checkpoint from afterwards.
923 */
924
fae667d5
YO
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 */
928 struct {
1f4fbd3b
MS
929 /* this first element must match u.yes */
930 struct regmatch_state *prev_yes_state;
ee28315c
YO
931 U32 lastparen;
932 U32 lastcloseparen;
933 CHECKPOINT cp; /* see note above "struct branchlike" */
934 CHECKPOINT lastcp; /* see note above "struct branchlike" */
935 U16 before_paren;
936 U16 after_paren;
1f4fbd3b 937
fae667d5 938 } branchlike;
1f4fbd3b
MS
939
940 struct {
941 /* the first elements must match u.branchlike */
942 struct regmatch_state *prev_yes_state;
ee28315c
YO
943 U32 lastparen;
944 U32 lastcloseparen;
945 CHECKPOINT cp; /* see note above "struct branchlike" */
946 CHECKPOINT lastcp; /* see note above "struct branchlike" */
947 U16 before_paren;
948 U16 after_paren;
949
950 regnode *next_branch; /* next branch node */
1f4fbd3b
MS
951 } branch;
952
953 struct {
954 /* the first elements must match u.branchlike */
955 struct regmatch_state *prev_yes_state;
ee28315c
YO
956 U32 lastparen;
957 U32 lastcloseparen;
958 CHECKPOINT cp; /* see note above "struct branchlike" */
959 CHECKPOINT lastcp; /* see note above "struct branchlike" */
960 U16 before_paren;
961 U16 after_paren;
962
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 */
acababb4
YO
966 U16 *j_before_paren;
967 U16 *j_after_paren;
ee28315c
YO
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 */
1f4fbd3b 973 } trie;
e822a8b4 974
fae667d5
YO
975 /* special types - these members are used to store state for special
976 regops like eval, if/then, lookaround and the markpoint state */
1f4fbd3b
MS
977 struct {
978 /* this first element must match u.yes */
979 struct regmatch_state *prev_yes_state;
980 struct regmatch_state *prev_curlyx;
d1c49ad5 981 struct regmatch_state *prev_eval;
85900e28 982 REGEXP *prev_rex;
ee28315c
YO
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 */
ba6840fb 987 char *prev_recurse_locinput;
1f4fbd3b 988 } eval;
e822a8b4 989
1f4fbd3b
MS
990 struct {
991 /* this first element must match u.yes */
992 struct regmatch_state *prev_yes_state;
ee28315c
YO
993 I32 wanted;
994 I32 logical; /* saved copy of 'logical' var */
995 U8 count; /* number of beginning positions */
996 char *start;
997 char *end;
998 regnode *me; /* the IFMATCH/SUSPEND/UNLESSM node */
999 char *prev_match_end;
1000 } ifmatch; /* and SUSPEND/UNLESSM */
1f4fbd3b
MS
1001
1002 struct {
1003 /* this first element must match u.yes */
1004 struct regmatch_state *prev_yes_state;
1005 struct regmatch_state *prev_mark;
ee28315c
YO
1006 SV *mark_name;
1007 char *mark_loc;
1f4fbd3b
MS
1008 } mark;
1009
1010 struct {
1011 int val;
1012 } keeper;
fae667d5
YO
1013
1014 /* quantifiers - these members are used for storing state for
a3815e44 1015 the regops used to implement quantifiers */
1f4fbd3b
MS
1016 struct {
1017 /* this first element must match u.yes */
1018 struct regmatch_state *prev_yes_state;
1019 struct regmatch_state *prev_curlyx; /* previous cur_curlyx */
ee28315c
YO
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" */
85900e28 1024 bool minmod;
ee28315c 1025 int parenfloor; /* how far back to strip paren data */
1f4fbd3b
MS
1026
1027 /* these two are modified by WHILEM */
ee28315c
YO
1028 int count; /* how many instances of A we've matched */
1029 char *lastloc; /* where previous A matched (0-len detect) */
1f4fbd3b
MS
1030 } curlyx;
1031
1032 struct {
1033 /* this first element must match u.yes */
1034 struct regmatch_state *prev_yes_state;
1035 struct regmatch_state *save_curlyx;
ee28315c
YO
1036 CHECKPOINT cp; /* see note above "struct branchlike" */
1037 CHECKPOINT lastcp; /* see note above "struct branchlike" */
1038 char *save_lastloc; /* previous curlyx.lastloc */
85900e28
YO
1039 I32 cache_offset;
1040 I32 cache_mask;
1f4fbd3b
MS
1041 } whilem;
1042
1043 struct {
1044 /* this first element must match u.yes */
1045 struct regmatch_state *prev_yes_state;
ee28315c
YO
1046 U32 lastparen;
1047 U32 lastcloseparen;
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 */
1051 I32 count;
1052 bool minmod;
1053 regnode *A, *B; /* the nodes corresponding to /A*B/ */
1054 regnode *me; /* the curlym node */
bb382562 1055 struct next_matchable_info Binfo;
1f4fbd3b
MS
1056 } curlym;
1057
1058 struct {
ee28315c
YO
1059 U32 paren;
1060 U32 lastparen;
1061 U32 lastcloseparen;
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 */
1066 int count;
1067 int min, max; /* {m,n} */
1068 regnode *A, *B; /* the nodes corresponding to /A*B/ */
bb382562 1069 struct next_matchable_info Binfo;
1f4fbd3b 1070 } curly; /* and CURLYN/PLUS/STAR */
dad79028 1071
59db1942 1072 struct {
ee28315c
YO
1073 CHECKPOINT cp;
1074 CHECKPOINT lastcp;
59db1942 1075 } backref; /* REF and friends */
d8319b27 1076 } u;
5d9a96ca
DM
1077} regmatch_state;
1078
ce12e254 1079
d5a00e4a 1080
5d9a96ca
DM
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. */
1f4fbd3b 1084
5d9a96ca
DM
1085#define PERL_REGMATCH_SLAB_SLOTS \
1086 ((4096 - 3 * sizeof (void*)) / sizeof(regmatch_state))
1087
1088typedef struct regmatch_slab {
1089 regmatch_state states[PERL_REGMATCH_SLAB_SLOTS];
1090 struct regmatch_slab *prev, *next;
1091} regmatch_slab;
1ade1aa1 1092
46ab3289 1093
fe5492d9 1094#define REG_FETCH_ABSOLUTE 1
cde0cee5 1095
1ade1aa1 1096/*
14d04a33 1097 * ex: set ts=8 sts=4 sw=4 et:
1ade1aa1 1098 */