This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix a problem with jump-tries, add (?FAIL) pattern.
[perl5.git] / regexp.h
... / ...
CommitLineData
1/* regexp.h
2 *
3 * Copyright (C) 1993, 1994, 1996, 1997, 1999, 2000, 2001, 2003,
4 * by Larry Wall and others
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 *
9 */
10
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#ifndef PLUGGABLE_RE_EXTENSION
18/* we don't want to include this stuff if we are inside Nicholas'
19 * pluggable regex engine code */
20
21struct regnode {
22 U8 flags;
23 U8 type;
24 U16 next_off;
25};
26
27typedef struct regnode regnode;
28
29struct reg_substr_data;
30
31struct reg_data;
32
33struct regexp_engine;
34
35typedef struct regexp {
36 I32 *startp;
37 I32 *endp;
38 regnode *regstclass;
39 struct reg_substr_data *substrs;
40 char *precomp; /* pre-compilation regular expression */
41 struct reg_data *data; /* Additional data. */
42 char *subbeg; /* saved or original string
43 so \digit works forever. */
44#ifdef PERL_OLD_COPY_ON_WRITE
45 SV *saved_copy; /* If non-NULL, SV which is COW from original */
46#endif
47 U32 *offsets; /* offset annotations 20001228 MJD */
48 I32 sublen; /* Length of string pointed by subbeg */
49 I32 refcnt;
50 I32 minlen; /* mininum possible length of $& */
51 I32 prelen; /* length of precomp */
52 U32 nparens; /* number of parentheses */
53 U32 lastparen; /* last paren matched */
54 U32 lastcloseparen; /* last paren matched */
55 U32 reganch; /* Internal use only +
56 Tainted information used by regexec? */
57 HV *paren_names; /* Paren names */
58 const struct regexp_engine* engine;
59 regnode program[1]; /* Unwarranted chumminess with compiler. */
60} regexp;
61
62
63typedef struct re_scream_pos_data_s
64{
65 char **scream_olds; /* match pos */
66 I32 *scream_pos; /* Internal iterator of scream. */
67} re_scream_pos_data;
68
69typedef struct regexp_engine {
70 regexp* (*comp) (pTHX_ char* exp, char* xend, PMOP* pm);
71 I32 (*exec) (pTHX_ regexp* prog, char* stringarg, char* strend,
72 char* strbeg, I32 minend, SV* screamer,
73 void* data, U32 flags);
74 char* (*intuit) (pTHX_ regexp *prog, SV *sv, char *strpos,
75 char *strend, U32 flags,
76 struct re_scream_pos_data_s *data);
77 SV* (*checkstr) (pTHX_ regexp *prog);
78 void (*free) (pTHX_ struct regexp* r);
79#ifdef USE_ITHREADS
80 regexp* (*dupe) (pTHX_ const regexp *r, CLONE_PARAMS *param);
81#endif
82} regexp_engine;
83
84#define ROPT_ANCH (ROPT_ANCH_BOL|ROPT_ANCH_MBOL|ROPT_ANCH_GPOS|ROPT_ANCH_SBOL)
85#define ROPT_ANCH_SINGLE (ROPT_ANCH_SBOL|ROPT_ANCH_GPOS)
86#define ROPT_ANCH_BOL 0x00000001
87#define ROPT_ANCH_MBOL 0x00000002
88#define ROPT_ANCH_SBOL 0x00000004
89#define ROPT_ANCH_GPOS 0x00000008
90#define ROPT_SKIP 0x00000010
91#define ROPT_IMPLICIT 0x00000020 /* Converted .* to ^.* */
92#define ROPT_NOSCAN 0x00000040 /* Check-string always at start. */
93#define ROPT_GPOS_SEEN 0x00000080
94#define ROPT_CHECK_ALL 0x00000100
95#define ROPT_LOOKBEHIND_SEEN 0x00000200
96#define ROPT_EVAL_SEEN 0x00000400
97#define ROPT_CANY_SEEN 0x00000800
98#define ROPT_SANY_SEEN ROPT_CANY_SEEN /* src bckwrd cmpt */
99#define ROPT_GPOS_CHECK (ROPT_GPOS_SEEN|ROPT_ANCH_GPOS)
100#define ROPT_RECURSE_SEEN 0x00001000
101
102/* 0xf800 of reganch is used by PMf_COMPILETIME */
103
104#define ROPT_UTF8 0x00010000
105#define ROPT_NAUGHTY 0x00020000 /* how exponential is this pattern? */
106#define ROPT_COPY_DONE 0x00040000 /* subbeg is a copy of the string */
107#define ROPT_TAINTED_SEEN 0x00080000
108#define ROPT_MATCH_UTF8 0x10000000 /* subbeg is utf-8 */
109
110#define RE_USE_INTUIT_NOML 0x00100000 /* Best to intuit before matching */
111#define RE_USE_INTUIT_ML 0x00200000
112#define REINT_AUTORITATIVE_NOML 0x00400000 /* Can trust a positive answer */
113#define REINT_AUTORITATIVE_ML 0x00800000
114#define REINT_ONCE_NOML 0x01000000 /* Intuit can succed once only. */
115#define REINT_ONCE_ML 0x02000000
116#define RE_INTUIT_ONECHAR 0x04000000
117#define RE_INTUIT_TAIL 0x08000000
118
119
120#define RE_USE_INTUIT (RE_USE_INTUIT_NOML|RE_USE_INTUIT_ML)
121#define REINT_AUTORITATIVE (REINT_AUTORITATIVE_NOML|REINT_AUTORITATIVE_ML)
122#define REINT_ONCE (REINT_ONCE_NOML|REINT_ONCE_ML)
123
124#define RX_MATCH_TAINTED(prog) ((prog)->reganch & ROPT_TAINTED_SEEN)
125#define RX_MATCH_TAINTED_on(prog) ((prog)->reganch |= ROPT_TAINTED_SEEN)
126#define RX_MATCH_TAINTED_off(prog) ((prog)->reganch &= ~ROPT_TAINTED_SEEN)
127#define RX_MATCH_TAINTED_set(prog, t) ((t) \
128 ? RX_MATCH_TAINTED_on(prog) \
129 : RX_MATCH_TAINTED_off(prog))
130
131#define RX_MATCH_COPIED(prog) ((prog)->reganch & ROPT_COPY_DONE)
132#define RX_MATCH_COPIED_on(prog) ((prog)->reganch |= ROPT_COPY_DONE)
133#define RX_MATCH_COPIED_off(prog) ((prog)->reganch &= ~ROPT_COPY_DONE)
134#define RX_MATCH_COPIED_set(prog,t) ((t) \
135 ? RX_MATCH_COPIED_on(prog) \
136 : RX_MATCH_COPIED_off(prog))
137
138#endif /* PLUGGABLE_RE_EXTENSION */
139
140/* Stuff that needs to be included in the plugable extension goes below here */
141
142#define RE_DEBUG_BIT 0x20000000
143#define RX_DEBUG(prog) ((prog)->reganch & RE_DEBUG_BIT)
144#define RX_DEBUG_on(prog) ((prog)->reganch |= RE_DEBUG_BIT)
145
146#ifdef PERL_OLD_COPY_ON_WRITE
147#define RX_MATCH_COPY_FREE(rx) \
148 STMT_START {if (rx->saved_copy) { \
149 SV_CHECK_THINKFIRST_COW_DROP(rx->saved_copy); \
150 } \
151 if (RX_MATCH_COPIED(rx)) { \
152 Safefree(rx->subbeg); \
153 RX_MATCH_COPIED_off(rx); \
154 }} STMT_END
155#else
156#define RX_MATCH_COPY_FREE(rx) \
157 STMT_START {if (RX_MATCH_COPIED(rx)) { \
158 Safefree(rx->subbeg); \
159 RX_MATCH_COPIED_off(rx); \
160 }} STMT_END
161#endif
162
163#define RX_MATCH_UTF8(prog) ((prog)->reganch & ROPT_MATCH_UTF8)
164#define RX_MATCH_UTF8_on(prog) ((prog)->reganch |= ROPT_MATCH_UTF8)
165#define RX_MATCH_UTF8_off(prog) ((prog)->reganch &= ~ROPT_MATCH_UTF8)
166#define RX_MATCH_UTF8_set(prog, t) ((t) \
167 ? (RX_MATCH_UTF8_on(prog), (PL_reg_match_utf8 = 1)) \
168 : (RX_MATCH_UTF8_off(prog), (PL_reg_match_utf8 = 0)))
169
170#define REXEC_COPY_STR 0x01 /* Need to copy the string. */
171#define REXEC_CHECKED 0x02 /* check_substr already checked. */
172#define REXEC_SCREAM 0x04 /* use scream table. */
173#define REXEC_IGNOREPOS 0x08 /* \G matches at start. */
174#define REXEC_NOT_FIRST 0x10 /* This is another iteration of //g. */
175
176#define ReREFCNT_inc(re) ((void)(re && re->refcnt++), re)
177#define ReREFCNT_dec(re) CALLREGFREE(re)
178
179#define FBMcf_TAIL_DOLLAR 1
180#define FBMcf_TAIL_DOLLARM 2
181#define FBMcf_TAIL_Z 4
182#define FBMcf_TAIL_z 8
183#define FBMcf_TAIL (FBMcf_TAIL_DOLLAR|FBMcf_TAIL_DOLLARM|FBMcf_TAIL_Z|FBMcf_TAIL_z)
184
185#define FBMrf_MULTILINE 1
186
187struct re_scream_pos_data_s;
188
189/* an accepting state/position*/
190struct _reg_trie_accepted {
191 U8 *endpos;
192 U16 wordnum;
193};
194typedef struct _reg_trie_accepted reg_trie_accepted;
195
196/* some basic information about the current match that is created by
197 * Perl_regexec_flags and then passed to regtry(), regmatch() etc */
198
199typedef struct {
200 regexp *prog;
201 char *bol;
202 char *till;
203 SV *sv;
204 char *ganch;
205} regmatch_info;
206
207
208/* structures for holding and saving the state maintained by regmatch() */
209
210#define MAX_RECURSE_EVAL_NOCHANGE_DEPTH 50
211
212typedef I32 CHECKPOINT;
213
214typedef struct regmatch_state {
215 int resume_state; /* where to jump to on return */
216 char *locinput; /* where to backtrack in string on failure */
217
218 union {
219
220 /* this is a fake union member that matches the first element
221 * of each member that needs to store positive backtrack
222 * information */
223 struct {
224 struct regmatch_state *prev_yes_state;
225 } yes;
226
227 struct {
228 reg_trie_accepted *accept_buff;
229 U32 accepted; /* how many accepting states we have seen */
230 U16 *jump; /* positive offsets from me */
231 regnode *B; /* node following the trie */
232 regnode *me; /* Which node am I - needed for jump tries*/
233 } trie;
234
235 struct {
236 /* this first element must match u.yes */
237 struct regmatch_state *prev_yes_state;
238 struct regmatch_state *prev_eval;
239 struct regmatch_state *prev_curlyx;
240 regexp *prev_rex;
241 U32 toggle_reg_flags; /* what bits in PL_reg_flags to
242 flip when transitioning between
243 inner and outer rexen */
244 CHECKPOINT cp; /* remember current savestack indexes */
245 CHECKPOINT lastcp;
246 regnode *B; /* the node following us */
247 U32 close_paren; /* which close bracket is our end */
248 } eval;
249
250 struct {
251 /* this first element must match u.yes */
252 struct regmatch_state *prev_yes_state;
253 struct regmatch_state *prev_curlyx; /* previous cur_curlyx */
254 CHECKPOINT cp; /* remember current savestack index */
255 bool minmod;
256 int parenfloor;/* how far back to strip paren data */
257 int min; /* the minimal number of A's to match */
258 int max; /* the maximal number of A's to match */
259 regnode *A, *B; /* the nodes corresponding to /A*B/ */
260
261 /* these two are modified by WHILEM */
262 int count; /* how many instances of A we've matched */
263 char *lastloc;/* where previous A matched (0-len detect) */
264 } curlyx;
265
266 struct {
267 /* this first element must match u.yes */
268 struct regmatch_state *prev_yes_state;
269 struct regmatch_state *save_curlyx;
270 CHECKPOINT cp; /* remember current savestack indexes */
271 CHECKPOINT lastcp;
272 char *save_lastloc; /* previous curlyx.lastloc */
273 I32 cache_offset;
274 I32 cache_mask;
275 } whilem;
276
277 struct {
278 I32 lastparen;
279 regnode *next_branch; /* next branch node */
280 CHECKPOINT cp;
281 } branch;
282
283 struct {
284 /* this first element must match u.yes */
285 struct regmatch_state *prev_yes_state;
286 I32 c1, c2; /* case fold search */
287 CHECKPOINT cp;
288 I32 alen; /* length of first-matched A string */
289 I32 count;
290 bool minmod;
291 regnode *A, *B; /* the nodes corresponding to /A*B/ */
292 regnode *me; /* the curlym node */
293 } curlym;
294
295 struct {
296 I32 paren;
297 CHECKPOINT cp;
298 I32 c1, c2; /* case fold search */
299 char *maxpos; /* highest possible point in string to match */
300 char *oldloc; /* the previous locinput */
301 int count;
302 int min, max; /* {m,n} */
303 regnode *A, *B; /* the nodes corresponding to /A*B/ */
304 } curly; /* and CURLYN/PLUS/STAR */
305
306 struct {
307 /* this first element must match u.yes */
308 struct regmatch_state *prev_yes_state;
309 I32 wanted;
310 I32 logical; /* saved copy of 'logical' var */
311 regnode *me; /* the IFMATCH/SUSPEND/UNLESSM node */
312 } ifmatch; /* and SUSPEND/UNLESSM */
313 } u;
314} regmatch_state;
315
316/* how many regmatch_state structs to allocate as a single slab.
317 * We do it in 4K blocks for efficiency. The "3" is 2 for the next/prev
318 * pointers, plus 1 for any mythical malloc overhead. */
319
320#define PERL_REGMATCH_SLAB_SLOTS \
321 ((4096 - 3 * sizeof (void*)) / sizeof(regmatch_state))
322
323typedef struct regmatch_slab {
324 regmatch_state states[PERL_REGMATCH_SLAB_SLOTS];
325 struct regmatch_slab *prev, *next;
326} regmatch_slab;
327
328#define PL_reg_flags PL_reg_state.re_state_reg_flags
329#define PL_bostr PL_reg_state.re_state_bostr
330#define PL_reginput PL_reg_state.re_state_reginput
331#define PL_regeol PL_reg_state.re_state_regeol
332#define PL_regstartp PL_reg_state.re_state_regstartp
333#define PL_regendp PL_reg_state.re_state_regendp
334#define PL_reglastparen PL_reg_state.re_state_reglastparen
335#define PL_reglastcloseparen PL_reg_state.re_state_reglastcloseparen
336#define PL_reg_start_tmp PL_reg_state.re_state_reg_start_tmp
337#define PL_reg_start_tmpl PL_reg_state.re_state_reg_start_tmpl
338#define PL_reg_eval_set PL_reg_state.re_state_reg_eval_set
339#define PL_reg_match_utf8 PL_reg_state.re_state_reg_match_utf8
340#define PL_reg_magic PL_reg_state.re_state_reg_magic
341#define PL_reg_oldpos PL_reg_state.re_state_reg_oldpos
342#define PL_reg_oldcurpm PL_reg_state.re_state_reg_oldcurpm
343#define PL_reg_curpm PL_reg_state.re_state_reg_curpm
344#define PL_reg_oldsaved PL_reg_state.re_state_reg_oldsaved
345#define PL_reg_oldsavedlen PL_reg_state.re_state_reg_oldsavedlen
346#define PL_reg_maxiter PL_reg_state.re_state_reg_maxiter
347#define PL_reg_leftiter PL_reg_state.re_state_reg_leftiter
348#define PL_reg_poscache PL_reg_state.re_state_reg_poscache
349#define PL_reg_poscache_size PL_reg_state.re_state_reg_poscache_size
350#define PL_regsize PL_reg_state.re_state_regsize
351#define PL_reg_starttry PL_reg_state.re_state_reg_starttry
352#define PL_nrs PL_reg_state.re_state_nrs
353
354struct re_save_state {
355 U32 re_state_reg_flags; /* from regexec.c */
356 char *re_state_bostr;
357 char *re_state_reginput; /* String-input pointer. */
358 char *re_state_regeol; /* End of input, for $ check. */
359 I32 *re_state_regstartp; /* Pointer to startp array. */
360 I32 *re_state_regendp; /* Ditto for endp. */
361 U32 *re_state_reglastparen; /* Similarly for lastparen. */
362 U32 *re_state_reglastcloseparen; /* Similarly for lastcloseparen. */
363 char **re_state_reg_start_tmp; /* from regexec.c */
364 U32 re_state_reg_start_tmpl; /* from regexec.c */
365 I32 re_state_reg_eval_set; /* from regexec.c */
366 bool re_state_reg_match_utf8; /* from regexec.c */
367 MAGIC *re_state_reg_magic; /* from regexec.c */
368 I32 re_state_reg_oldpos; /* from regexec.c */
369 PMOP *re_state_reg_oldcurpm; /* from regexec.c */
370 PMOP *re_state_reg_curpm; /* from regexec.c */
371 char *re_state_reg_oldsaved; /* old saved substr during match */
372 STRLEN re_state_reg_oldsavedlen; /* old length of saved substr during match */
373 I32 re_state_reg_maxiter; /* max wait until caching pos */
374 I32 re_state_reg_leftiter; /* wait until caching pos */
375 char *re_state_reg_poscache; /* cache of pos of WHILEM */
376 STRLEN re_state_reg_poscache_size; /* size of pos cache of WHILEM */
377 I32 re_state_regsize; /* from regexec.c */
378 char *re_state_reg_starttry; /* from regexec.c */
379#ifdef PERL_OLD_COPY_ON_WRITE
380 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 */
381#endif
382};
383
384#define SAVESTACK_ALLOC_FOR_RE_SAVE_STATE \
385 (1 + ((sizeof(struct re_save_state) - 1) / sizeof(*PL_savestack)))
386/*
387 * Local variables:
388 * c-indentation-style: bsd
389 * c-basic-offset: 4
390 * indent-tabs-mode: t
391 * End:
392 *
393 * ex: set ts=8 sts=4 sw=4 noet:
394 */