Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* regexp.h |
d6376244 | 2 | * |
4bb101f2 JH |
3 | * Copyright (C) 1993, 1994, 1996, 1997, 1999, 2000, 2001, 2003, |
4 | * 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 | */ | |
17 | ||
378cc40b | 18 | |
c277df42 IZ |
19 | struct regnode { |
20 | U8 flags; | |
21 | U8 type; | |
22 | U16 next_off; | |
23 | }; | |
24 | ||
25 | typedef struct regnode regnode; | |
26 | ||
cad2e5aa | 27 | struct reg_substr_data; |
2779dcf1 | 28 | |
0ee3c8d0 JH |
29 | struct reg_data; |
30 | ||
378cc40b | 31 | typedef struct regexp { |
cf93c79d IZ |
32 | I32 *startp; |
33 | I32 *endp; | |
c277df42 | 34 | regnode *regstclass; |
2779dcf1 | 35 | struct reg_substr_data *substrs; |
cf93c79d | 36 | char *precomp; /* pre-compilation regular expression */ |
c277df42 | 37 | struct reg_data *data; /* Additional data. */ |
cf93c79d IZ |
38 | char *subbeg; /* saved or original string |
39 | so \digit works forever. */ | |
f8c7b90f | 40 | #ifdef PERL_OLD_COPY_ON_WRITE |
ed252734 NC |
41 | SV *saved_copy; /* If non-NULL, SV which is COW from original */ |
42 | #endif | |
fac92740 | 43 | U32 *offsets; /* offset annotations 20001228 MJD */ |
cf93c79d IZ |
44 | I32 sublen; /* Length of string pointed by subbeg */ |
45 | I32 refcnt; | |
46 | I32 minlen; /* mininum possible length of $& */ | |
47 | I32 prelen; /* length of precomp */ | |
48 | U32 nparens; /* number of parentheses */ | |
49 | U32 lastparen; /* last paren matched */ | |
a01268b5 | 50 | U32 lastcloseparen; /* last paren matched */ |
cf93c79d IZ |
51 | U32 reganch; /* Internal use only + |
52 | Tainted information used by regexec? */ | |
c277df42 | 53 | regnode program[1]; /* Unwarranted chumminess with compiler. */ |
378cc40b LW |
54 | } regexp; |
55 | ||
cad2e5aa JH |
56 | #define ROPT_ANCH (ROPT_ANCH_BOL|ROPT_ANCH_MBOL|ROPT_ANCH_GPOS|ROPT_ANCH_SBOL) |
57 | #define ROPT_ANCH_SINGLE (ROPT_ANCH_SBOL|ROPT_ANCH_GPOS) | |
f2278c82 YO |
58 | #define ROPT_ANCH_BOL 0x00000001 |
59 | #define ROPT_ANCH_MBOL 0x00000002 | |
60 | #define ROPT_ANCH_SBOL 0x00000004 | |
61 | #define ROPT_ANCH_GPOS 0x00000008 | |
62 | #define ROPT_SKIP 0x00000010 | |
63 | #define ROPT_IMPLICIT 0x00000020 /* Converted .* to ^.* */ | |
64 | #define ROPT_NOSCAN 0x00000040 /* Check-string always at start. */ | |
65 | #define ROPT_GPOS_SEEN 0x00000080 | |
66 | #define ROPT_CHECK_ALL 0x00000100 | |
67 | #define ROPT_LOOKBEHIND_SEEN 0x00000200 | |
68 | #define ROPT_EVAL_SEEN 0x00000400 | |
69 | #define ROPT_CANY_SEEN 0x00000800 | |
cce850e4 | 70 | #define ROPT_SANY_SEEN ROPT_CANY_SEEN /* src bckwrd cmpt */ |
a0ed51b3 | 71 | |
8782bef2 | 72 | /* 0xf800 of reganch is used by PMf_COMPILETIME */ |
c277df42 | 73 | |
f2278c82 YO |
74 | #define ROPT_UTF8 0x00010000 |
75 | #define ROPT_NAUGHTY 0x00020000 /* how exponential is this pattern? */ | |
76 | #define ROPT_COPY_DONE 0x00040000 /* subbeg is a copy of the string */ | |
77 | #define ROPT_TAINTED_SEEN 0x00080000 | |
a30b2f1f | 78 | #define ROPT_MATCH_UTF8 0x10000000 /* subbeg is utf-8 */ |
a0ed51b3 | 79 | |
f2278c82 YO |
80 | #define RE_USE_INTUIT_NOML 0x00100000 /* Best to intuit before matching */ |
81 | #define RE_USE_INTUIT_ML 0x00200000 | |
82 | #define REINT_AUTORITATIVE_NOML 0x00400000 /* Can trust a positive answer */ | |
83 | #define REINT_AUTORITATIVE_ML 0x00800000 | |
84 | #define REINT_ONCE_NOML 0x01000000 /* Intuit can succed once only. */ | |
85 | #define REINT_ONCE_ML 0x02000000 | |
86 | #define RE_INTUIT_ONECHAR 0x04000000 | |
87 | #define RE_INTUIT_TAIL 0x08000000 | |
88 | ||
89 | #define RE_DEBUG_BIT 0x20000000 | |
cad2e5aa JH |
90 | |
91 | #define RE_USE_INTUIT (RE_USE_INTUIT_NOML|RE_USE_INTUIT_ML) | |
92 | #define REINT_AUTORITATIVE (REINT_AUTORITATIVE_NOML|REINT_AUTORITATIVE_ML) | |
93 | #define REINT_ONCE (REINT_ONCE_NOML|REINT_ONCE_ML) | |
94 | ||
f2278c82 YO |
95 | #define RX_DEBUG(prog) ((prog)->reganch & RE_DEBUG_BIT) |
96 | #define RX_DEBUG_on(prog) ((prog)->reganch |= RE_DEBUG_BIT) | |
97 | ||
98 | ||
c277df42 | 99 | #define RX_MATCH_TAINTED(prog) ((prog)->reganch & ROPT_TAINTED_SEEN) |
72311751 GS |
100 | #define RX_MATCH_TAINTED_on(prog) ((prog)->reganch |= ROPT_TAINTED_SEEN) |
101 | #define RX_MATCH_TAINTED_off(prog) ((prog)->reganch &= ~ROPT_TAINTED_SEEN) | |
102 | #define RX_MATCH_TAINTED_set(prog, t) ((t) \ | |
103 | ? RX_MATCH_TAINTED_on(prog) \ | |
104 | : RX_MATCH_TAINTED_off(prog)) | |
c277df42 | 105 | |
cf93c79d IZ |
106 | #define RX_MATCH_COPIED(prog) ((prog)->reganch & ROPT_COPY_DONE) |
107 | #define RX_MATCH_COPIED_on(prog) ((prog)->reganch |= ROPT_COPY_DONE) | |
108 | #define RX_MATCH_COPIED_off(prog) ((prog)->reganch &= ~ROPT_COPY_DONE) | |
109 | #define RX_MATCH_COPIED_set(prog,t) ((t) \ | |
110 | ? RX_MATCH_COPIED_on(prog) \ | |
111 | : RX_MATCH_COPIED_off(prog)) | |
112 | ||
f8c7b90f | 113 | #ifdef PERL_OLD_COPY_ON_WRITE |
ed252734 NC |
114 | #define RX_MATCH_COPY_FREE(rx) \ |
115 | STMT_START {if (rx->saved_copy) { \ | |
116 | SV_CHECK_THINKFIRST_COW_DROP(rx->saved_copy); \ | |
117 | } \ | |
118 | if (RX_MATCH_COPIED(rx)) { \ | |
119 | Safefree(rx->subbeg); \ | |
120 | RX_MATCH_COPIED_off(rx); \ | |
121 | }} STMT_END | |
122 | #else | |
123 | #define RX_MATCH_COPY_FREE(rx) \ | |
124 | STMT_START {if (RX_MATCH_COPIED(rx)) { \ | |
125 | Safefree(rx->subbeg); \ | |
126 | RX_MATCH_COPIED_off(rx); \ | |
127 | }} STMT_END | |
128 | #endif | |
129 | ||
a30b2f1f RGS |
130 | #define RX_MATCH_UTF8(prog) ((prog)->reganch & ROPT_MATCH_UTF8) |
131 | #define RX_MATCH_UTF8_on(prog) ((prog)->reganch |= ROPT_MATCH_UTF8) | |
132 | #define RX_MATCH_UTF8_off(prog) ((prog)->reganch &= ~ROPT_MATCH_UTF8) | |
133 | #define RX_MATCH_UTF8_set(prog, t) ((t) \ | |
134 | ? (RX_MATCH_UTF8_on(prog), (PL_reg_match_utf8 = 1)) \ | |
135 | : (RX_MATCH_UTF8_off(prog), (PL_reg_match_utf8 = 0))) | |
136 | ||
cad2e5aa JH |
137 | #define REXEC_COPY_STR 0x01 /* Need to copy the string. */ |
138 | #define REXEC_CHECKED 0x02 /* check_substr already checked. */ | |
139 | #define REXEC_SCREAM 0x04 /* use scream table. */ | |
140 | #define REXEC_IGNOREPOS 0x08 /* \G matches at start. */ | |
cf93c79d | 141 | #define REXEC_NOT_FIRST 0x10 /* This is another iteration of //g. */ |
c277df42 | 142 | |
155aba94 | 143 | #define ReREFCNT_inc(re) ((void)(re && re->refcnt++), re) |
cad2e5aa | 144 | #define ReREFCNT_dec(re) CALLREGFREE(aTHX_ re) |
cf93c79d IZ |
145 | |
146 | #define FBMcf_TAIL_DOLLAR 1 | |
cad2e5aa JH |
147 | #define FBMcf_TAIL_DOLLARM 2 |
148 | #define FBMcf_TAIL_Z 4 | |
149 | #define FBMcf_TAIL_z 8 | |
150 | #define FBMcf_TAIL (FBMcf_TAIL_DOLLAR|FBMcf_TAIL_DOLLARM|FBMcf_TAIL_Z|FBMcf_TAIL_z) | |
cf93c79d IZ |
151 | |
152 | #define FBMrf_MULTILINE 1 | |
cad2e5aa JH |
153 | |
154 | struct re_scream_pos_data_s; | |
5d9a96ca DM |
155 | |
156 | /* an accepting state/position*/ | |
157 | struct _reg_trie_accepted { | |
158 | U8 *endpos; | |
159 | U16 wordnum; | |
160 | }; | |
161 | typedef struct _reg_trie_accepted reg_trie_accepted; | |
162 | ||
3b0527fe DM |
163 | /* some basic information about the current match that is created by |
164 | * Perl_regexec_flags and then passed to regtry(), regmatch() etc */ | |
165 | ||
166 | typedef struct { | |
167 | regexp *prog; | |
168 | char *bol; | |
169 | char *till; | |
170 | SV *sv; | |
171 | char *ganch; | |
172 | } regmatch_info; | |
173 | ||
5d9a96ca DM |
174 | |
175 | /* structures for holding and saving the state maintained by regmatch() */ | |
176 | ||
177 | typedef I32 CHECKPOINT; | |
178 | ||
5d9a96ca DM |
179 | typedef enum { |
180 | resume_TRIE1, | |
181 | resume_TRIE2, | |
aa283a38 | 182 | resume_EVAL, |
5d9a96ca DM |
183 | resume_CURLYX, |
184 | resume_WHILEM1, | |
185 | resume_WHILEM2, | |
186 | resume_WHILEM3, | |
187 | resume_WHILEM4, | |
188 | resume_WHILEM5, | |
189 | resume_WHILEM6, | |
190 | resume_CURLYM1, | |
191 | resume_CURLYM2, | |
192 | resume_CURLYM3, | |
5d9a96ca DM |
193 | resume_IFMATCH, |
194 | resume_PLUS1, | |
195 | resume_PLUS2, | |
196 | resume_PLUS3, | |
aa283a38 | 197 | resume_PLUS4 |
5d9a96ca DM |
198 | } regmatch_resume_states; |
199 | ||
200 | ||
a0374537 | 201 | typedef struct regmatch_state { |
5d9a96ca DM |
202 | |
203 | /* these vars contain state that needs to be maintained | |
204 | * across the main while loop ... */ | |
205 | ||
206 | regmatch_resume_states resume_state; /* where to jump to on return */ | |
207 | regnode *scan; /* Current node. */ | |
208 | regnode *next; /* Next node. */ | |
aa283a38 | 209 | bool minmod; /* the next "{n,m}" is a "{n,m}?" */ |
5d9a96ca DM |
210 | bool sw; /* the condition value in (?(cond)a|b) */ |
211 | int logical; | |
212 | I32 unwind; /* savestack index of current unwind block */ | |
a0374537 | 213 | struct regmatch_state *cc; /* current innermost curly state */ |
5d9a96ca DM |
214 | char *locinput; |
215 | ||
e822a8b4 | 216 | /* ... while the rest of these are local to an individual branch */ |
5d9a96ca DM |
217 | |
218 | I32 n; /* no or next */ | |
219 | I32 ln; /* len or last */ | |
e822a8b4 DM |
220 | |
221 | union { | |
77cb431f DM |
222 | |
223 | /* this is a fake union member that matches the first element | |
224 | * of each member that needs to store positive backtrack | |
225 | * information */ | |
226 | struct { | |
227 | struct regmatch_state *prev_yes_state; | |
228 | } yes; | |
229 | ||
e822a8b4 DM |
230 | struct { |
231 | reg_trie_accepted *accept_buff; | |
232 | U32 accepted; /* how many accepting states we have seen */ | |
233 | } trie; | |
234 | ||
235 | struct { | |
77cb431f DM |
236 | /* this first element must match u.yes */ |
237 | struct regmatch_state *prev_yes_state; | |
aa283a38 DM |
238 | regexp *prev_rex; |
239 | int toggleutf; | |
240 | CHECKPOINT cp; /* remember current savestack indexes */ | |
241 | CHECKPOINT lastcp; | |
e822a8b4 DM |
242 | } eval; |
243 | ||
244 | struct { | |
245 | CHECKPOINT cp; /* remember current savestack indexes */ | |
a0374537 DM |
246 | struct regmatch_state *outercc; /* outer CURLYX state if any */ |
247 | ||
248 | /* these contain the current curly state, and are accessed | |
249 | * by subsequent WHILEMs */ | |
250 | int parenfloor;/* how far back to strip paren data */ | |
251 | int cur; /* how many instances of scan we've matched */ | |
252 | int min; /* the minimal number of scans to match */ | |
253 | int max; /* the maximal number of scans to match */ | |
254 | regnode * scan; /* the thing to match */ | |
255 | char * lastloc;/* where we started matching this scan */ | |
e822a8b4 DM |
256 | } curlyx; |
257 | ||
258 | struct { | |
259 | CHECKPOINT cp; /* remember current savestack indexes */ | |
260 | CHECKPOINT lastcp; | |
a0374537 | 261 | struct regmatch_state *savecc; |
e822a8b4 DM |
262 | char *lastloc; /* Detection of 0-len. */ |
263 | I32 cache_offset; | |
264 | I32 cache_bit; | |
265 | } whilem; | |
266 | ||
267 | struct { | |
dad79028 DM |
268 | /* this first element must match u.yes */ |
269 | struct regmatch_state *prev_yes_state; | |
e822a8b4 DM |
270 | I32 paren; |
271 | I32 c1, c2; /* case fold search */ | |
272 | CHECKPOINT lastcp; | |
273 | I32 l; | |
274 | I32 matches; | |
275 | I32 maxwanted; | |
0cadcf80 | 276 | bool minmod; |
e822a8b4 DM |
277 | } curlym; |
278 | ||
279 | struct { | |
280 | I32 paren; | |
281 | CHECKPOINT lastcp; | |
282 | I32 c1, c2; /* case fold search */ | |
283 | char *e; | |
284 | char *old; | |
285 | int count; | |
286 | } plus; /* and CURLYN/CURLY/STAR */ | |
dad79028 DM |
287 | |
288 | struct { | |
289 | /* this first element must match u.yes */ | |
290 | struct regmatch_state *prev_yes_state; | |
291 | I32 wanted; | |
292 | } ifmatch; /* and SUSPEND/UNLESSM */ | |
d8319b27 | 293 | } u; |
5d9a96ca DM |
294 | } regmatch_state; |
295 | ||
296 | /* how many regmatch_state structs to allocate as a single slab. | |
297 | * We do it in 4K blocks for efficiency. The "3" is 2 for the next/prev | |
298 | * pointers, plus 1 for any mythical malloc overhead. */ | |
299 | ||
300 | #define PERL_REGMATCH_SLAB_SLOTS \ | |
301 | ((4096 - 3 * sizeof (void*)) / sizeof(regmatch_state)) | |
302 | ||
303 | typedef struct regmatch_slab { | |
304 | regmatch_state states[PERL_REGMATCH_SLAB_SLOTS]; | |
305 | struct regmatch_slab *prev, *next; | |
306 | } regmatch_slab; | |
1ade1aa1 | 307 | |
46ab3289 NC |
308 | #define PL_reg_flags PL_reg_state.re_state_reg_flags |
309 | #define PL_bostr PL_reg_state.re_state_bostr | |
310 | #define PL_reginput PL_reg_state.re_state_reginput | |
46ab3289 NC |
311 | #define PL_regeol PL_reg_state.re_state_regeol |
312 | #define PL_regstartp PL_reg_state.re_state_regstartp | |
313 | #define PL_regendp PL_reg_state.re_state_regendp | |
314 | #define PL_reglastparen PL_reg_state.re_state_reglastparen | |
315 | #define PL_reglastcloseparen PL_reg_state.re_state_reglastcloseparen | |
46ab3289 NC |
316 | #define PL_reg_start_tmp PL_reg_state.re_state_reg_start_tmp |
317 | #define PL_reg_start_tmpl PL_reg_state.re_state_reg_start_tmpl | |
318 | #define PL_reg_eval_set PL_reg_state.re_state_reg_eval_set | |
46ab3289 | 319 | #define PL_regindent PL_reg_state.re_state_regindent |
46ab3289 NC |
320 | #define PL_reg_match_utf8 PL_reg_state.re_state_reg_match_utf8 |
321 | #define PL_reg_magic PL_reg_state.re_state_reg_magic | |
322 | #define PL_reg_oldpos PL_reg_state.re_state_reg_oldpos | |
323 | #define PL_reg_oldcurpm PL_reg_state.re_state_reg_oldcurpm | |
324 | #define PL_reg_curpm PL_reg_state.re_state_reg_curpm | |
325 | #define PL_reg_oldsaved PL_reg_state.re_state_reg_oldsaved | |
326 | #define PL_reg_oldsavedlen PL_reg_state.re_state_reg_oldsavedlen | |
327 | #define PL_reg_maxiter PL_reg_state.re_state_reg_maxiter | |
328 | #define PL_reg_leftiter PL_reg_state.re_state_reg_leftiter | |
329 | #define PL_reg_poscache PL_reg_state.re_state_reg_poscache | |
330 | #define PL_reg_poscache_size PL_reg_state.re_state_reg_poscache_size | |
331 | #define PL_regsize PL_reg_state.re_state_regsize | |
332 | #define PL_reg_starttry PL_reg_state.re_state_reg_starttry | |
333 | #define PL_nrs PL_reg_state.re_state_nrs | |
334 | ||
1ade1aa1 NC |
335 | struct re_save_state { |
336 | U32 re_state_reg_flags; /* from regexec.c */ | |
337 | char *re_state_bostr; | |
338 | char *re_state_reginput; /* String-input pointer. */ | |
1ade1aa1 NC |
339 | char *re_state_regeol; /* End of input, for $ check. */ |
340 | I32 *re_state_regstartp; /* Pointer to startp array. */ | |
341 | I32 *re_state_regendp; /* Ditto for endp. */ | |
342 | U32 *re_state_reglastparen; /* Similarly for lastparen. */ | |
343 | U32 *re_state_reglastcloseparen; /* Similarly for lastcloseparen. */ | |
1ade1aa1 NC |
344 | char **re_state_reg_start_tmp; /* from regexec.c */ |
345 | U32 re_state_reg_start_tmpl; /* from regexec.c */ | |
346 | I32 re_state_reg_eval_set; /* from regexec.c */ | |
1ade1aa1 | 347 | int re_state_regindent; /* from regexec.c */ |
1ade1aa1 NC |
348 | bool re_state_reg_match_utf8; /* from regexec.c */ |
349 | MAGIC *re_state_reg_magic; /* from regexec.c */ | |
350 | I32 re_state_reg_oldpos; /* from regexec.c */ | |
351 | PMOP *re_state_reg_oldcurpm; /* from regexec.c */ | |
352 | PMOP *re_state_reg_curpm; /* from regexec.c */ | |
353 | char *re_state_reg_oldsaved; /* old saved substr during match */ | |
354 | STRLEN re_state_reg_oldsavedlen; /* old length of saved substr during match */ | |
355 | I32 re_state_reg_maxiter; /* max wait until caching pos */ | |
356 | I32 re_state_reg_leftiter; /* wait until caching pos */ | |
357 | char *re_state_reg_poscache; /* cache of pos of WHILEM */ | |
358 | STRLEN re_state_reg_poscache_size; /* size of pos cache of WHILEM */ | |
359 | I32 re_state_regsize; /* from regexec.c */ | |
360 | char *re_state_reg_starttry; /* from regexec.c */ | |
361 | #ifdef PERL_OLD_COPY_ON_WRITE | |
46ab3289 | 362 | SV *re_state_nrs; /* was placeholder: unused since 5.8.0 (5.7.2 patch #12027 for bug ID 20010815.012). Used to save rx->saved_copy */ |
1ade1aa1 NC |
363 | #endif |
364 | }; | |
365 | ||
366 | #define SAVESTACK_ALLOC_FOR_RE_SAVE_STATE \ | |
367 | (1 + ((sizeof(struct re_save_state) - 1) / sizeof(*PL_savestack))) | |
368 | /* | |
369 | * Local variables: | |
370 | * c-indentation-style: bsd | |
371 | * c-basic-offset: 4 | |
372 | * indent-tabs-mode: t | |
373 | * End: | |
374 | * | |
375 | * ex: set ts=8 sts=4 sw=4 noet: | |
376 | */ |