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