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