This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
trivia in perlthrtut.pod
[perl5.git] / regexp.h
1 /*    regexp.h
2  *
3  *    Copyright (C) 1993, 1994, 1996, 1997, 1999, 2000, 2001, 2003,
4  *    2005, 2006 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 of
19    an external regex engine based on the core one - like re 'debug'*/
20
21 struct regnode {
22     U8  flags;
23     U8  type;
24     U16 next_off;
25 };
26
27 typedef struct regnode regnode;
28
29 struct reg_substr_data;
30
31 struct reg_data;
32
33 struct regexp_engine;
34
35 struct reg_substr_datum {
36     I32 min_offset;
37     I32 max_offset;
38     SV *substr;         /* non-utf8 variant */
39     SV *utf8_substr;    /* utf8 variant */
40     I32 end_shift;
41 };
42 struct reg_substr_data {
43     struct reg_substr_datum data[3];    /* Actual array */
44 };
45
46 #ifdef PERL_OLD_COPY_ON_WRITE
47 #define SV_SAVED_COPY   SV *saved_copy; /* If non-NULL, SV which is COW from original */
48 #else
49 #define SV_SAVED_COPY
50 #endif
51 /* this is ordered such that the most commonly used 
52    fields are at the start of the struct */
53 typedef struct regexp {
54         /* what engine created this regexp? */
55         const struct regexp_engine* engine; 
56         
57         /* Information about the match that the perl core uses to manage things */
58         U32 extflags;           /* Flags used both externally and internally */
59         I32 minlen;             /* mininum possible length of string to match */
60         I32 minlenret;          /* mininum possible length of $& */
61         U32 gofs;               /* chars left of pos that we search from */
62         struct reg_substr_data *substrs; /* substring data about strings that must appear
63                                    in the final match, used for optimisations */
64         U32 nparens;            /* number of capture buffers */
65
66         /* private engine specific data */
67         U32 intflags;           /* Engine Specific Internal flags */
68         void *pprivate;         /* Data private to the regex engine which 
69                                    created this object. */
70         
71         /* Data about the last/current match. These are modified during matching*/
72         U32 lastparen;          /* last open paren matched */
73         U32 lastcloseparen;     /* last close paren matched */
74         I32 *startp;            /* Array of offsets from start of string (@-) */
75         I32 *endp;              /* Array of offsets from start of string (@+) */
76         char *subbeg;           /* saved or original string 
77                                    so \digit works forever. */
78         I32 sublen;             /* Length of string pointed by subbeg */
79         SV_SAVED_COPY           /* If non-NULL, SV which is COW from original */
80         
81         
82         /* Information about the match that isn't often used */
83         char *precomp;          /* pre-compilation regular expression */
84         I32 prelen;             /* length of precomp */
85         I32 seen_evals;         /* number of eval groups in the pattern - for security checks */ 
86         HV *paren_names;        /* Optional hash of paren names */
87         
88         /* Refcount of this regexp */
89         I32 refcnt;             /* Refcount of this regexp */
90 } regexp;
91
92 /* used for high speed searches */
93 typedef struct re_scream_pos_data_s
94 {
95     char **scream_olds;         /* match pos */
96     I32 *scream_pos;            /* Internal iterator of scream. */
97 } re_scream_pos_data;
98
99 /* regexp_engine structure. This is the dispatch table for regexes.
100  * Any regex engine implementation must be able to build one of these.
101  */
102 typedef struct regexp_engine {
103     regexp* (*comp) (pTHX_ char* exp, char* xend, PMOP* pm);
104     I32     (*exec) (pTHX_ regexp* prog, char* stringarg, char* strend,
105                             char* strbeg, I32 minend, SV* screamer,
106                             void* data, U32 flags);
107     char*   (*intuit) (pTHX_ regexp *prog, SV *sv, char *strpos,
108                             char *strend, U32 flags,
109                             struct re_scream_pos_data_s *data);
110     SV*     (*checkstr) (pTHX_ regexp *prog);
111     void    (*free) (pTHX_ struct regexp* r);
112 #ifdef USE_ITHREADS
113     void* (*dupe) (pTHX_ const regexp *r, CLONE_PARAMS *param);
114 #endif    
115 } regexp_engine;
116
117 /* Flags stored in regexp->extflags 
118  * These are used by code external to the regexp engine
119  *
120  * Note that flags starting with RXf_PMf_ have exact equivalents
121  * stored in op_pmflags and which are defined in op.h, they are defined
122  * numerically here only for clarity.
123  */
124
125 /* Anchor and GPOS related stuff */
126 #define RXf_ANCH_BOL            0x00000001
127 #define RXf_ANCH_MBOL           0x00000002
128 #define RXf_ANCH_SBOL           0x00000004
129 #define RXf_ANCH_GPOS           0x00000008
130 #define RXf_GPOS_SEEN           0x00000010
131 #define RXf_GPOS_FLOAT          0x00000020
132 /* two bits here */
133 #define RXf_ANCH                (RXf_ANCH_BOL|RXf_ANCH_MBOL|RXf_ANCH_GPOS|RXf_ANCH_SBOL)
134 #define RXf_GPOS_CHECK          (RXf_GPOS_SEEN|RXf_ANCH_GPOS)
135 #define RXf_ANCH_SINGLE         (RXf_ANCH_SBOL|RXf_ANCH_GPOS)
136
137 /* Flags indicating special patterns */
138 #define RXf_START_ONLY          0x00000200 /* Pattern is /^/ */
139 #define RXf_WHITE               0x00000400 /* Pattern is /\s+/ */
140
141 /* 0xF800 of extflags is used by (RXf_)PMf_COMPILETIME */
142 #define RXf_PMf_LOCALE          0x00000800 /* use locale */
143 #define RXf_PMf_MULTILINE       0x00001000 /* /m         */
144 #define RXf_PMf_SINGLELINE      0x00002000 /* /s         */
145 #define RXf_PMf_FOLD            0x00004000 /* /i         */
146 #define RXf_PMf_EXTENDED        0x00008000 /* /x         */
147 /* these flags are transfered from the PMOP->op_pmflags member during compilation */
148 #define RXf_PMf_COMPILETIME     (RXf_PMf_MULTILINE|RXf_PMf_SINGLELINE|RXf_PMf_LOCALE|RXf_PMf_FOLD|RXf_PMf_EXTENDED)
149
150 /* What we have seen */
151 /* one bit here */
152 #define RXf_LOOKBEHIND_SEEN     0x00020000
153 #define RXf_EVAL_SEEN           0x00040000
154 #define RXf_CANY_SEEN           0x00080000
155
156 /* Special */
157 #define RXf_NOSCAN              0x00100000
158 #define RXf_CHECK_ALL           0x00200000
159
160 /* UTF8 related */
161 #define RXf_UTF8                0x00400000
162 #define RXf_MATCH_UTF8          0x00800000
163
164 /* Intuit related */
165 #define RXf_USE_INTUIT_NOML     0x01000000
166 #define RXf_USE_INTUIT_ML       0x02000000
167 #define RXf_INTUIT_TAIL         0x04000000
168 /* one bit here */
169 #define RXf_USE_INTUIT          (RXf_USE_INTUIT_NOML|RXf_USE_INTUIT_ML)
170
171 /* Copy and tainted info */
172 #define RXf_COPY_DONE           0x10000000
173 #define RXf_TAINTED_SEEN        0x20000000
174 /* two bits here  */
175
176
177 #define RX_HAS_CUTGROUP(prog) ((prog)->intflags & PREGf_CUTGROUP_SEEN)
178 #define RX_MATCH_TAINTED(prog)  ((prog)->extflags & RXf_TAINTED_SEEN)
179 #define RX_MATCH_TAINTED_on(prog) ((prog)->extflags |= RXf_TAINTED_SEEN)
180 #define RX_MATCH_TAINTED_off(prog) ((prog)->extflags &= ~RXf_TAINTED_SEEN)
181 #define RX_MATCH_TAINTED_set(prog, t) ((t) \
182                                        ? RX_MATCH_TAINTED_on(prog) \
183                                        : RX_MATCH_TAINTED_off(prog))
184
185 #define RX_MATCH_COPIED(prog)           ((prog)->extflags & RXf_COPY_DONE)
186 #define RX_MATCH_COPIED_on(prog)        ((prog)->extflags |= RXf_COPY_DONE)
187 #define RX_MATCH_COPIED_off(prog)       ((prog)->extflags &= ~RXf_COPY_DONE)
188 #define RX_MATCH_COPIED_set(prog,t)     ((t) \
189                                          ? RX_MATCH_COPIED_on(prog) \
190                                          : RX_MATCH_COPIED_off(prog))
191
192 #endif /* PLUGGABLE_RE_EXTENSION */
193
194 /* Stuff that needs to be included in the plugable extension goes below here */
195
196 #ifdef PERL_OLD_COPY_ON_WRITE
197 #define RX_MATCH_COPY_FREE(rx) \
198         STMT_START {if (rx->saved_copy) { \
199             SV_CHECK_THINKFIRST_COW_DROP(rx->saved_copy); \
200         } \
201         if (RX_MATCH_COPIED(rx)) { \
202             Safefree(rx->subbeg); \
203             RX_MATCH_COPIED_off(rx); \
204         }} STMT_END
205 #else
206 #define RX_MATCH_COPY_FREE(rx) \
207         STMT_START {if (RX_MATCH_COPIED(rx)) { \
208             Safefree(rx->subbeg); \
209             RX_MATCH_COPIED_off(rx); \
210         }} STMT_END
211 #endif
212
213 #define RX_MATCH_UTF8(prog)             ((prog)->extflags & RXf_MATCH_UTF8)
214 #define RX_MATCH_UTF8_on(prog)          ((prog)->extflags |= RXf_MATCH_UTF8)
215 #define RX_MATCH_UTF8_off(prog)         ((prog)->extflags &= ~RXf_MATCH_UTF8)
216 #define RX_MATCH_UTF8_set(prog, t)      ((t) \
217                         ? (RX_MATCH_UTF8_on(prog), (PL_reg_match_utf8 = 1)) \
218                         : (RX_MATCH_UTF8_off(prog), (PL_reg_match_utf8 = 0)))
219     
220 #define REXEC_COPY_STR  0x01            /* Need to copy the string. */
221 #define REXEC_CHECKED   0x02            /* check_substr already checked. */
222 #define REXEC_SCREAM    0x04            /* use scream table. */
223 #define REXEC_IGNOREPOS 0x08            /* \G matches at start. */
224 #define REXEC_NOT_FIRST 0x10            /* This is another iteration of //g. */
225
226 #define ReREFCNT_inc(re) ((void)(re && re->refcnt++), re)
227 #define ReREFCNT_dec(re) CALLREGFREE(re)
228
229 #define FBMcf_TAIL_DOLLAR       1
230 #define FBMcf_TAIL_DOLLARM      2
231 #define FBMcf_TAIL_Z            4
232 #define FBMcf_TAIL_z            8
233 #define FBMcf_TAIL              (FBMcf_TAIL_DOLLAR|FBMcf_TAIL_DOLLARM|FBMcf_TAIL_Z|FBMcf_TAIL_z)
234
235 #define FBMrf_MULTILINE 1
236
237 /* an accepting state/position*/
238 struct _reg_trie_accepted {
239     U8   *endpos;
240     U16  wordnum;
241 };
242 typedef struct _reg_trie_accepted reg_trie_accepted;
243
244 /* some basic information about the current match that is created by
245  * Perl_regexec_flags and then passed to regtry(), regmatch() etc */
246
247 typedef struct {
248     regexp *prog;
249     char *bol;
250     char *till;
251     SV *sv;
252     char *ganch;
253     char *cutpoint;
254 } regmatch_info;
255  
256
257 /* structures for holding and saving the state maintained by regmatch() */
258
259 #define MAX_RECURSE_EVAL_NOCHANGE_DEPTH 50
260
261 typedef I32 CHECKPOINT;
262
263 typedef struct regmatch_state {
264     int resume_state;           /* where to jump to on return */
265     char *locinput;             /* where to backtrack in string on failure */
266
267     union {
268
269         /* this is a fake union member that matches the first element
270          * of each member that needs to store positive backtrack
271          * information */
272         struct {
273             struct regmatch_state *prev_yes_state;
274         } yes;
275
276         struct {
277             /* this first element must match u.yes */
278             struct regmatch_state *prev_yes_state;
279             reg_trie_accepted *accept_buff;
280             U32         accepted; /* how many accepting states we have seen */
281             U16         *jump;  /* positive offsets from me */
282             regnode     *B;     /* node following the trie */
283             regnode     *me;    /* Which node am I - needed for jump tries*/
284         } trie;
285
286         struct {
287             /* this first element must match u.yes */
288             struct regmatch_state *prev_yes_state;
289             struct regmatch_state *prev_eval;
290             struct regmatch_state *prev_curlyx;
291             regexp      *prev_rex;
292             U32         toggle_reg_flags; /* what bits in PL_reg_flags to
293                                             flip when transitioning between
294                                             inner and outer rexen */
295             CHECKPOINT  cp;     /* remember current savestack indexes */
296             CHECKPOINT  lastcp;
297             regnode     *B;     /* the node following us  */
298             U32        close_paren; /* which close bracket is our end */
299         } eval;
300
301         struct {
302             /* this first element must match u.yes */
303             struct regmatch_state *prev_yes_state;
304             struct regmatch_state *prev_curlyx; /* previous cur_curlyx */
305             CHECKPOINT  cp;     /* remember current savestack index */
306             bool        minmod;
307             int         parenfloor;/* how far back to strip paren data */
308             int         min;    /* the minimal number of A's to match */
309             int         max;    /* the maximal number of A's to match */
310             regnode     *A, *B; /* the nodes corresponding to /A*B/  */
311
312             /* these two are modified by WHILEM */
313             int         count;  /* how many instances of A we've matched */
314             char        *lastloc;/* where previous A matched (0-len detect) */
315         } curlyx;
316
317         struct {
318             /* this first element must match u.yes */
319             struct regmatch_state *prev_yes_state;
320             struct regmatch_state *save_curlyx;
321             CHECKPOINT  cp;     /* remember current savestack indexes */
322             CHECKPOINT  lastcp;
323             char        *save_lastloc;  /* previous curlyx.lastloc */
324             I32         cache_offset;
325             I32         cache_mask;
326         } whilem;
327
328         struct {
329             /* this first element must match u.yes */
330             struct regmatch_state *prev_yes_state;
331             U32 lastparen;
332             regnode *next_branch; /* next branch node */
333             CHECKPOINT cp;
334         } branch;
335
336         struct {
337             /* this first element must match u.yes */
338             struct regmatch_state *prev_yes_state;
339             I32 c1, c2;         /* case fold search */
340             CHECKPOINT cp;
341             I32 alen;           /* length of first-matched A string */
342             I32 count;
343             bool minmod;
344             regnode *A, *B;     /* the nodes corresponding to /A*B/  */
345             regnode *me;        /* the curlym node */
346         } curlym;
347
348         struct {
349             U32 paren;
350             CHECKPOINT cp;
351             I32 c1, c2;         /* case fold search */
352             char *maxpos;       /* highest possible point in string to match */
353             char *oldloc;       /* the previous locinput */
354             int count;
355             int min, max;       /* {m,n} */
356             regnode *A, *B;     /* the nodes corresponding to /A*B/  */
357         } curly; /* and CURLYN/PLUS/STAR */
358
359         struct {
360             /* this first element must match u.yes */
361             struct regmatch_state *prev_yes_state;
362             I32 wanted;
363             I32 logical;        /* saved copy of 'logical' var */
364             regnode  *me; /* the IFMATCH/SUSPEND/UNLESSM node  */
365         } ifmatch; /* and SUSPEND/UNLESSM */
366         
367         struct {
368             /* this first element must match u.yes */
369             struct regmatch_state *prev_yes_state;
370             struct regmatch_state *prev_mark;
371             SV* mark_name;
372             char *mark_loc;
373         } mark;
374         
375         struct {
376             int val;
377         } keeper;
378     } u;
379 } regmatch_state;
380
381 /* how many regmatch_state structs to allocate as a single slab.
382  * We do it in 4K blocks for efficiency. The "3" is 2 for the next/prev
383  * pointers, plus 1 for any mythical malloc overhead. */
384  
385 #define PERL_REGMATCH_SLAB_SLOTS \
386     ((4096 - 3 * sizeof (void*)) / sizeof(regmatch_state))
387
388 typedef struct regmatch_slab {
389     regmatch_state states[PERL_REGMATCH_SLAB_SLOTS];
390     struct regmatch_slab *prev, *next;
391 } regmatch_slab;
392
393 #define PL_reg_flags            PL_reg_state.re_state_reg_flags
394 #define PL_bostr                PL_reg_state.re_state_bostr
395 #define PL_reginput             PL_reg_state.re_state_reginput
396 #define PL_regeol               PL_reg_state.re_state_regeol
397 #define PL_regstartp            PL_reg_state.re_state_regstartp
398 #define PL_regendp              PL_reg_state.re_state_regendp
399 #define PL_reglastparen         PL_reg_state.re_state_reglastparen
400 #define PL_reglastcloseparen    PL_reg_state.re_state_reglastcloseparen
401 #define PL_reg_start_tmp        PL_reg_state.re_state_reg_start_tmp
402 #define PL_reg_start_tmpl       PL_reg_state.re_state_reg_start_tmpl
403 #define PL_reg_eval_set         PL_reg_state.re_state_reg_eval_set
404 #define PL_reg_match_utf8       PL_reg_state.re_state_reg_match_utf8
405 #define PL_reg_magic            PL_reg_state.re_state_reg_magic
406 #define PL_reg_oldpos           PL_reg_state.re_state_reg_oldpos
407 #define PL_reg_oldcurpm         PL_reg_state.re_state_reg_oldcurpm
408 #define PL_reg_curpm            PL_reg_state.re_state_reg_curpm
409 #define PL_reg_oldsaved         PL_reg_state.re_state_reg_oldsaved
410 #define PL_reg_oldsavedlen      PL_reg_state.re_state_reg_oldsavedlen
411 #define PL_reg_maxiter          PL_reg_state.re_state_reg_maxiter
412 #define PL_reg_leftiter         PL_reg_state.re_state_reg_leftiter
413 #define PL_reg_poscache         PL_reg_state.re_state_reg_poscache
414 #define PL_reg_poscache_size    PL_reg_state.re_state_reg_poscache_size
415 #define PL_regsize              PL_reg_state.re_state_regsize
416 #define PL_reg_starttry         PL_reg_state.re_state_reg_starttry
417 #define PL_nrs                  PL_reg_state.re_state_nrs
418
419 struct re_save_state {
420     U32 re_state_reg_flags;             /* from regexec.c */
421     char *re_state_bostr;
422     char *re_state_reginput;            /* String-input pointer. */
423     char *re_state_regeol;              /* End of input, for $ check. */
424     I32 *re_state_regstartp;            /* Pointer to startp array. */
425     I32 *re_state_regendp;              /* Ditto for endp. */
426     U32 *re_state_reglastparen;         /* Similarly for lastparen. */
427     U32 *re_state_reglastcloseparen;    /* Similarly for lastcloseparen. */
428     char **re_state_reg_start_tmp;      /* from regexec.c */
429     U32 re_state_reg_start_tmpl;        /* from regexec.c */
430     I32 re_state_reg_eval_set;          /* from regexec.c */
431     bool re_state_reg_match_utf8;       /* from regexec.c */
432     MAGIC *re_state_reg_magic;          /* from regexec.c */
433     I32 re_state_reg_oldpos;            /* from regexec.c */
434     PMOP *re_state_reg_oldcurpm;        /* from regexec.c */
435     PMOP *re_state_reg_curpm;           /* from regexec.c */
436     char *re_state_reg_oldsaved;        /* old saved substr during match */
437     STRLEN re_state_reg_oldsavedlen;    /* old length of saved substr during match */
438     I32 re_state_reg_maxiter;           /* max wait until caching pos */
439     I32 re_state_reg_leftiter;          /* wait until caching pos */
440     char *re_state_reg_poscache;        /* cache of pos of WHILEM */
441     STRLEN re_state_reg_poscache_size;  /* size of pos cache of WHILEM */
442     U32 re_state_regsize;               /* from regexec.c */
443     char *re_state_reg_starttry;        /* from regexec.c */
444 #ifdef PERL_OLD_COPY_ON_WRITE
445     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 */
446 #endif
447 };
448
449 #define SAVESTACK_ALLOC_FOR_RE_SAVE_STATE \
450         (1 + ((sizeof(struct re_save_state) - 1) / sizeof(*PL_savestack)))
451 /*
452  * Local variables:
453  * c-indentation-style: bsd
454  * c-basic-offset: 4
455  * indent-tabs-mode: t
456  * End:
457  *
458  * ex: set ts=8 sts=4 sw=4 noet:
459  */