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