eliminate PL_reg_re
[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
18
19 struct regnode {
20     U8  flags;
21     U8  type;
22     U16 next_off;
23 };
24
25 typedef struct regnode regnode;
26
27 struct reg_substr_data;
28
29 struct reg_data;
30
31 typedef struct regexp {
32         I32 *startp;
33         I32 *endp;
34         regnode *regstclass;
35         struct reg_substr_data *substrs;
36         char *precomp;          /* pre-compilation regular expression */
37         struct reg_data *data;  /* Additional data. */
38         char *subbeg;           /* saved or original string 
39                                    so \digit works forever. */
40 #ifdef PERL_OLD_COPY_ON_WRITE
41         SV *saved_copy;         /* If non-NULL, SV which is COW from original */
42 #endif
43         U32 *offsets;           /* offset annotations 20001228 MJD */
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 */
50         U32 lastcloseparen;     /* last paren matched */
51         U32 reganch;            /* Internal use only +
52                                    Tainted information used by regexec? */
53         regnode program[1];     /* Unwarranted chumminess with compiler. */
54 } regexp;
55
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)
58 #define ROPT_ANCH_BOL           0x00001
59 #define ROPT_ANCH_MBOL          0x00002
60 #define ROPT_ANCH_SBOL          0x00004
61 #define ROPT_ANCH_GPOS          0x00008
62 #define ROPT_SKIP               0x00010
63 #define ROPT_IMPLICIT           0x00020 /* Converted .* to ^.* */
64 #define ROPT_NOSCAN             0x00040 /* Check-string always at start. */
65 #define ROPT_GPOS_SEEN          0x00080
66 #define ROPT_CHECK_ALL          0x00100
67 #define ROPT_LOOKBEHIND_SEEN    0x00200
68 #define ROPT_EVAL_SEEN          0x00400
69 #define ROPT_CANY_SEEN          0x00800
70 #define ROPT_SANY_SEEN          ROPT_CANY_SEEN /* src bckwrd cmpt */
71
72 /* 0xf800 of reganch is used by PMf_COMPILETIME */
73
74 #define ROPT_UTF8               0x10000
75 #define ROPT_NAUGHTY            0x20000 /* how exponential is this pattern? */
76 #define ROPT_COPY_DONE          0x40000 /* subbeg is a copy of the string */
77 #define ROPT_TAINTED_SEEN       0x80000
78 #define ROPT_MATCH_UTF8         0x10000000 /* subbeg is utf-8 */
79
80 #define RE_USE_INTUIT_NOML      0x0100000 /* Best to intuit before matching */
81 #define RE_USE_INTUIT_ML        0x0200000
82 #define REINT_AUTORITATIVE_NOML 0x0400000 /* Can trust a positive answer */
83 #define REINT_AUTORITATIVE_ML   0x0800000 
84 #define REINT_ONCE_NOML         0x1000000 /* Intuit can succed once only. */
85 #define REINT_ONCE_ML           0x2000000
86 #define RE_INTUIT_ONECHAR       0x4000000
87 #define RE_INTUIT_TAIL          0x8000000
88
89 #define RE_USE_INTUIT           (RE_USE_INTUIT_NOML|RE_USE_INTUIT_ML)
90 #define REINT_AUTORITATIVE      (REINT_AUTORITATIVE_NOML|REINT_AUTORITATIVE_ML)
91 #define REINT_ONCE              (REINT_ONCE_NOML|REINT_ONCE_ML)
92
93 #define RX_MATCH_TAINTED(prog)  ((prog)->reganch & ROPT_TAINTED_SEEN)
94 #define RX_MATCH_TAINTED_on(prog) ((prog)->reganch |= ROPT_TAINTED_SEEN)
95 #define RX_MATCH_TAINTED_off(prog) ((prog)->reganch &= ~ROPT_TAINTED_SEEN)
96 #define RX_MATCH_TAINTED_set(prog, t) ((t) \
97                                        ? RX_MATCH_TAINTED_on(prog) \
98                                        : RX_MATCH_TAINTED_off(prog))
99
100 #define RX_MATCH_COPIED(prog)           ((prog)->reganch & ROPT_COPY_DONE)
101 #define RX_MATCH_COPIED_on(prog)        ((prog)->reganch |= ROPT_COPY_DONE)
102 #define RX_MATCH_COPIED_off(prog)       ((prog)->reganch &= ~ROPT_COPY_DONE)
103 #define RX_MATCH_COPIED_set(prog,t)     ((t) \
104                                          ? RX_MATCH_COPIED_on(prog) \
105                                          : RX_MATCH_COPIED_off(prog))
106
107 #ifdef PERL_OLD_COPY_ON_WRITE
108 #define RX_MATCH_COPY_FREE(rx) \
109         STMT_START {if (rx->saved_copy) { \
110             SV_CHECK_THINKFIRST_COW_DROP(rx->saved_copy); \
111         } \
112         if (RX_MATCH_COPIED(rx)) { \
113             Safefree(rx->subbeg); \
114             RX_MATCH_COPIED_off(rx); \
115         }} STMT_END
116 #else
117 #define RX_MATCH_COPY_FREE(rx) \
118         STMT_START {if (RX_MATCH_COPIED(rx)) { \
119             Safefree(rx->subbeg); \
120             RX_MATCH_COPIED_off(rx); \
121         }} STMT_END
122 #endif
123
124 #define RX_MATCH_UTF8(prog)             ((prog)->reganch & ROPT_MATCH_UTF8)
125 #define RX_MATCH_UTF8_on(prog)          ((prog)->reganch |= ROPT_MATCH_UTF8)
126 #define RX_MATCH_UTF8_off(prog)         ((prog)->reganch &= ~ROPT_MATCH_UTF8)
127 #define RX_MATCH_UTF8_set(prog, t)      ((t) \
128                         ? (RX_MATCH_UTF8_on(prog), (PL_reg_match_utf8 = 1)) \
129                         : (RX_MATCH_UTF8_off(prog), (PL_reg_match_utf8 = 0)))
130     
131 #define REXEC_COPY_STR  0x01            /* Need to copy the string. */
132 #define REXEC_CHECKED   0x02            /* check_substr already checked. */
133 #define REXEC_SCREAM    0x04            /* use scream table. */
134 #define REXEC_IGNOREPOS 0x08            /* \G matches at start. */
135 #define REXEC_NOT_FIRST 0x10            /* This is another iteration of //g. */
136
137 #define ReREFCNT_inc(re) ((void)(re && re->refcnt++), re)
138 #define ReREFCNT_dec(re) CALLREGFREE(aTHX_ re)
139
140 #define FBMcf_TAIL_DOLLAR       1
141 #define FBMcf_TAIL_DOLLARM      2
142 #define FBMcf_TAIL_Z            4
143 #define FBMcf_TAIL_z            8
144 #define FBMcf_TAIL              (FBMcf_TAIL_DOLLAR|FBMcf_TAIL_DOLLARM|FBMcf_TAIL_Z|FBMcf_TAIL_z)
145
146 #define FBMrf_MULTILINE 1
147
148 struct re_scream_pos_data_s;
149
150 /* an accepting state/position*/
151 struct _reg_trie_accepted {
152     U8   *endpos;
153     U16  wordnum;
154 };
155 typedef struct _reg_trie_accepted reg_trie_accepted;
156
157
158 /* structures for holding and saving the state maintained by regmatch() */
159
160 typedef I32 CHECKPOINT;
161
162 typedef enum {
163     resume_TRIE1,
164     resume_TRIE2,
165     resume_EVAL,
166     resume_CURLYX,
167     resume_WHILEM1,
168     resume_WHILEM2,
169     resume_WHILEM3,
170     resume_WHILEM4,
171     resume_WHILEM5,
172     resume_WHILEM6,
173     resume_CURLYM1,
174     resume_CURLYM2,
175     resume_CURLYM3,
176     resume_CURLYM4,
177     resume_IFMATCH,
178     resume_PLUS1,
179     resume_PLUS2,
180     resume_PLUS3,
181     resume_PLUS4
182 } regmatch_resume_states;
183
184
185 typedef struct regmatch_state {
186
187     /* these vars contain state that needs to be maintained
188      * across the main while loop ... */
189
190     regmatch_resume_states resume_state; /* where to jump to on return */
191     regnode *scan;              /* Current node. */
192     regnode *next;              /* Next node. */
193     bool minmod;                /* the next "{n,m}" is a "{n,m}?" */
194     bool sw;                    /* the condition value in (?(cond)a|b) */
195     int logical;
196     I32 unwind;                 /* savestack index of current unwind block */
197     struct regmatch_state  *cc; /* current innermost curly state */
198     char *locinput;
199
200     /* ... while the rest of these are local to an individual branch */
201
202     I32 n;                      /* no or next */
203     I32 ln;                     /* len or last */
204
205     union {
206         struct {
207             reg_trie_accepted *accept_buff;
208             U32 accepted;       /* how many accepting states we have seen */
209         } trie;
210
211         struct {
212             regexp      *prev_rex;
213             int         toggleutf;
214             CHECKPOINT  cp;     /* remember current savestack indexes */
215             CHECKPOINT  lastcp;
216             struct regmatch_state  *prev_eval; /* save cur_eval */
217             struct regmatch_slab   *prev_slab;
218             int depth;
219
220         } eval;
221
222         struct {
223             CHECKPOINT cp;      /* remember current savestack indexes */
224             struct regmatch_state *outercc; /* outer CURLYX state if any */
225
226             /* these contain the current curly state, and are accessed
227              * by subsequent WHILEMs */
228             int         parenfloor;/* how far back to strip paren data */
229             int         cur;    /* how many instances of scan we've matched */
230             int         min;    /* the minimal number of scans to match */
231             int         max;    /* the maximal number of scans to match */
232             regnode *   scan;   /* the thing to match */
233             char *      lastloc;/* where we started matching this scan */
234         } curlyx;
235
236         struct {
237             CHECKPOINT cp;      /* remember current savestack indexes */
238             CHECKPOINT lastcp;
239             struct regmatch_state *savecc;
240             char *lastloc;      /* Detection of 0-len. */
241             I32 cache_offset;
242             I32 cache_bit;
243         } whilem;
244
245         struct {
246             I32 paren;
247             I32 c1, c2;         /* case fold search */
248             CHECKPOINT lastcp;
249             I32 l;
250             I32 matches;
251             I32 maxwanted;
252         } curlym;
253
254         struct {
255             I32 paren;
256             CHECKPOINT lastcp;
257             I32 c1, c2;         /* case fold search */
258             char *e;
259             char *old;
260             int count;
261         } plus; /* and CURLYN/CURLY/STAR */
262     } u;
263 } regmatch_state;
264
265 /* how many regmatch_state structs to allocate as a single slab.
266  * We do it in 4K blocks for efficiency. The "3" is 2 for the next/prev
267  * pointers, plus 1 for any mythical malloc overhead. */
268  
269 #define PERL_REGMATCH_SLAB_SLOTS \
270     ((4096 - 3 * sizeof (void*)) / sizeof(regmatch_state))
271
272 typedef struct regmatch_slab {
273     regmatch_state states[PERL_REGMATCH_SLAB_SLOTS];
274     struct regmatch_slab *prev, *next;
275 } regmatch_slab;
276
277 #define PL_reg_flags            PL_reg_state.re_state_reg_flags
278 #define PL_bostr                PL_reg_state.re_state_bostr
279 #define PL_reginput             PL_reg_state.re_state_reginput
280 #define PL_regbol               PL_reg_state.re_state_regbol
281 #define PL_regeol               PL_reg_state.re_state_regeol
282 #define PL_regstartp            PL_reg_state.re_state_regstartp
283 #define PL_regendp              PL_reg_state.re_state_regendp
284 #define PL_reglastparen         PL_reg_state.re_state_reglastparen
285 #define PL_reglastcloseparen    PL_reg_state.re_state_reglastcloseparen
286 #define PL_regtill              PL_reg_state.re_state_regtill
287 #define PL_reg_start_tmp        PL_reg_state.re_state_reg_start_tmp
288 #define PL_reg_start_tmpl       PL_reg_state.re_state_reg_start_tmpl
289 #define PL_reg_eval_set         PL_reg_state.re_state_reg_eval_set
290 #define PL_regnarrate           PL_reg_state.re_state_regnarrate
291 #define PL_regindent            PL_reg_state.re_state_regindent
292 #define PL_reg_ganch            PL_reg_state.re_state_reg_ganch
293 #define PL_reg_sv               PL_reg_state.re_state_reg_sv
294 #define PL_reg_match_utf8       PL_reg_state.re_state_reg_match_utf8
295 #define PL_reg_magic            PL_reg_state.re_state_reg_magic
296 #define PL_reg_oldpos           PL_reg_state.re_state_reg_oldpos
297 #define PL_reg_oldcurpm         PL_reg_state.re_state_reg_oldcurpm
298 #define PL_reg_curpm            PL_reg_state.re_state_reg_curpm
299 #define PL_reg_oldsaved         PL_reg_state.re_state_reg_oldsaved
300 #define PL_reg_oldsavedlen      PL_reg_state.re_state_reg_oldsavedlen
301 #define PL_reg_maxiter          PL_reg_state.re_state_reg_maxiter
302 #define PL_reg_leftiter         PL_reg_state.re_state_reg_leftiter
303 #define PL_reg_poscache         PL_reg_state.re_state_reg_poscache
304 #define PL_reg_poscache_size    PL_reg_state.re_state_reg_poscache_size
305 #define PL_regsize              PL_reg_state.re_state_regsize
306 #define PL_reg_starttry         PL_reg_state.re_state_reg_starttry
307 #define PL_nrs                  PL_reg_state.re_state_nrs
308
309 struct re_save_state {
310     U32 re_state_reg_flags;             /* from regexec.c */
311     char *re_state_bostr;
312     char *re_state_reginput;            /* String-input pointer. */
313     char *re_state_regbol;              /* Beginning of input, for ^ check. */
314     char *re_state_regeol;              /* End of input, for $ check. */
315     I32 *re_state_regstartp;            /* Pointer to startp array. */
316     I32 *re_state_regendp;              /* Ditto for endp. */
317     U32 *re_state_reglastparen;         /* Similarly for lastparen. */
318     U32 *re_state_reglastcloseparen;    /* Similarly for lastcloseparen. */
319     char *re_state_regtill;             /* How far we are required to go. */
320     char **re_state_reg_start_tmp;      /* from regexec.c */
321     U32 re_state_reg_start_tmpl;        /* from regexec.c */
322     I32 re_state_reg_eval_set;          /* from regexec.c */
323     I32 re_state_regnarrate;            /* from regexec.c */
324     int re_state_regindent;             /* from regexec.c */
325     char *re_state_reg_ganch;           /* from regexec.c */
326     SV *re_state_reg_sv;                /* from regexec.c */
327     bool re_state_reg_match_utf8;       /* from regexec.c */
328     MAGIC *re_state_reg_magic;          /* from regexec.c */
329     I32 re_state_reg_oldpos;            /* from regexec.c */
330     PMOP *re_state_reg_oldcurpm;        /* from regexec.c */
331     PMOP *re_state_reg_curpm;           /* from regexec.c */
332     char *re_state_reg_oldsaved;        /* old saved substr during match */
333     STRLEN re_state_reg_oldsavedlen;    /* old length of saved substr during match */
334     I32 re_state_reg_maxiter;           /* max wait until caching pos */
335     I32 re_state_reg_leftiter;          /* wait until caching pos */
336     char *re_state_reg_poscache;        /* cache of pos of WHILEM */
337     STRLEN re_state_reg_poscache_size;  /* size of pos cache of WHILEM */
338     I32 re_state_regsize;               /* from regexec.c */
339     char *re_state_reg_starttry;        /* from regexec.c */
340 #ifdef PERL_OLD_COPY_ON_WRITE
341     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 */
342 #endif
343 };
344
345 #define SAVESTACK_ALLOC_FOR_RE_SAVE_STATE \
346         (1 + ((sizeof(struct re_save_state) - 1) / sizeof(*PL_savestack)))
347 /*
348  * Local variables:
349  * c-indentation-style: bsd
350  * c-basic-offset: 4
351  * indent-tabs-mode: t
352  * End:
353  *
354  * ex: set ts=8 sts=4 sw=4 noet:
355  */