Commit | Line | Data |
a0d0e21e |
1 | /* regexp.h |
d6376244 |
2 | * |
4bb101f2 |
3 | * Copyright (C) 1993, 1994, 1996, 1997, 1999, 2000, 2001, 2003, |
4 | * by Larry Wall and others |
d6376244 |
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 |
9 | */ |
10 | |
378cc40b |
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 | |
378cc40b |
18 | |
c277df42 |
19 | struct regnode { |
20 | U8 flags; |
21 | U8 type; |
22 | U16 next_off; |
23 | }; |
24 | |
25 | typedef struct regnode regnode; |
26 | |
cad2e5aa |
27 | struct reg_substr_data; |
2779dcf1 |
28 | |
0ee3c8d0 |
29 | struct reg_data; |
30 | |
378cc40b |
31 | typedef struct regexp { |
cf93c79d |
32 | I32 *startp; |
33 | I32 *endp; |
c277df42 |
34 | regnode *regstclass; |
2779dcf1 |
35 | struct reg_substr_data *substrs; |
cf93c79d |
36 | char *precomp; /* pre-compilation regular expression */ |
c277df42 |
37 | struct reg_data *data; /* Additional data. */ |
cf93c79d |
38 | char *subbeg; /* saved or original string |
39 | so \digit works forever. */ |
f8c7b90f |
40 | #ifdef PERL_OLD_COPY_ON_WRITE |
ed252734 |
41 | SV *saved_copy; /* If non-NULL, SV which is COW from original */ |
42 | #endif |
fac92740 |
43 | U32 *offsets; /* offset annotations 20001228 MJD */ |
cf93c79d |
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 */ |
a01268b5 |
50 | U32 lastcloseparen; /* last paren matched */ |
cf93c79d |
51 | U32 reganch; /* Internal use only + |
52 | Tainted information used by regexec? */ |
c277df42 |
53 | regnode program[1]; /* Unwarranted chumminess with compiler. */ |
378cc40b |
54 | } regexp; |
55 | |
cad2e5aa |
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) |
a0ed51b3 |
58 | #define ROPT_ANCH_BOL 0x00001 |
59 | #define ROPT_ANCH_MBOL 0x00002 |
cad2e5aa |
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 |
cce850e4 |
69 | #define ROPT_CANY_SEEN 0x00800 |
70 | #define ROPT_SANY_SEEN ROPT_CANY_SEEN /* src bckwrd cmpt */ |
a0ed51b3 |
71 | |
8782bef2 |
72 | /* 0xf800 of reganch is used by PMf_COMPILETIME */ |
c277df42 |
73 | |
a0ed51b3 |
74 | #define ROPT_UTF8 0x10000 |
75 | #define ROPT_NAUGHTY 0x20000 /* how exponential is this pattern? */ |
cf93c79d |
76 | #define ROPT_COPY_DONE 0x40000 /* subbeg is a copy of the string */ |
bc9a0d2c |
77 | #define ROPT_TAINTED_SEEN 0x80000 |
a30b2f1f |
78 | #define ROPT_MATCH_UTF8 0x10000000 /* subbeg is utf-8 */ |
a0ed51b3 |
79 | |
cad2e5aa |
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 | |
c277df42 |
93 | #define RX_MATCH_TAINTED(prog) ((prog)->reganch & ROPT_TAINTED_SEEN) |
72311751 |
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)) |
c277df42 |
99 | |
cf93c79d |
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 | |
f8c7b90f |
107 | #ifdef PERL_OLD_COPY_ON_WRITE |
ed252734 |
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 | |
a30b2f1f |
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 | |
cad2e5aa |
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. */ |
cf93c79d |
135 | #define REXEC_NOT_FIRST 0x10 /* This is another iteration of //g. */ |
c277df42 |
136 | |
155aba94 |
137 | #define ReREFCNT_inc(re) ((void)(re && re->refcnt++), re) |
cad2e5aa |
138 | #define ReREFCNT_dec(re) CALLREGFREE(aTHX_ re) |
cf93c79d |
139 | |
140 | #define FBMcf_TAIL_DOLLAR 1 |
cad2e5aa |
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) |
cf93c79d |
145 | |
146 | #define FBMrf_MULTILINE 1 |
cad2e5aa |
147 | |
148 | struct re_scream_pos_data_s; |
5d9a96ca |
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 | /* Current curly descriptor */ |
163 | typedef struct curcur CURCUR; |
164 | struct curcur { |
165 | int parenfloor; /* how far back to strip paren data */ |
166 | int cur; /* how many instances of scan we've matched */ |
167 | int min; /* the minimal number of scans to match */ |
168 | int max; /* the maximal number of scans to match */ |
169 | int minmod; /* whether to work our way up or down */ |
170 | regnode * scan; /* the thing to match */ |
171 | regnode * next; /* what has to match after it */ |
172 | char * lastloc; /* where we started matching this scan */ |
173 | CURCUR * oldcc; /* current curly before we started this one */ |
174 | }; |
175 | |
176 | typedef struct re_cc_state |
177 | { |
178 | I32 ss; |
179 | regnode *node; |
180 | struct re_cc_state *prev; |
181 | CURCUR *cc; |
182 | regexp *re; |
183 | } re_cc_state; |
184 | |
185 | |
186 | |
187 | typedef enum { |
188 | resume_TRIE1, |
189 | resume_TRIE2, |
190 | resume_CURLYX, |
191 | resume_WHILEM1, |
192 | resume_WHILEM2, |
193 | resume_WHILEM3, |
194 | resume_WHILEM4, |
195 | resume_WHILEM5, |
196 | resume_WHILEM6, |
197 | resume_CURLYM1, |
198 | resume_CURLYM2, |
199 | resume_CURLYM3, |
200 | resume_CURLYM4, |
201 | resume_IFMATCH, |
202 | resume_PLUS1, |
203 | resume_PLUS2, |
204 | resume_PLUS3, |
205 | resume_PLUS4, |
206 | resume_END |
207 | } regmatch_resume_states; |
208 | |
209 | |
210 | typedef struct { |
211 | |
212 | /* these vars contain state that needs to be maintained |
213 | * across the main while loop ... */ |
214 | |
215 | regmatch_resume_states resume_state; /* where to jump to on return */ |
216 | regnode *scan; /* Current node. */ |
217 | regnode *next; /* Next node. */ |
218 | bool minmod; /* the next "{n.m}" is a "{n,m}?" */ |
219 | bool sw; /* the condition value in (?(cond)a|b) */ |
220 | int logical; |
221 | I32 unwind; /* savestack index of current unwind block */ |
222 | CURCUR *cc; /* current innermost curly struct */ |
223 | char *locinput; |
224 | |
e822a8b4 |
225 | /* ... while the rest of these are local to an individual branch */ |
5d9a96ca |
226 | |
227 | I32 n; /* no or next */ |
228 | I32 ln; /* len or last */ |
e822a8b4 |
229 | |
230 | union { |
231 | struct { |
232 | reg_trie_accepted *accept_buff; |
233 | U32 accepted; /* how many accepting states we have seen */ |
234 | } trie; |
235 | |
236 | struct { |
237 | CHECKPOINT cp; /* remember current savestack indexes */ |
238 | CHECKPOINT lastcp; |
239 | } eval; |
240 | |
241 | struct { |
242 | CHECKPOINT cp; /* remember current savestack indexes */ |
243 | CURCUR *savecc; |
244 | } curlyx; |
245 | |
246 | struct { |
247 | CHECKPOINT cp; /* remember current savestack indexes */ |
248 | CHECKPOINT lastcp; |
249 | CURCUR *savecc; |
250 | char *lastloc; /* Detection of 0-len. */ |
251 | I32 cache_offset; |
252 | I32 cache_bit; |
253 | } whilem; |
254 | |
255 | struct { |
256 | I32 paren; |
257 | I32 c1, c2; /* case fold search */ |
258 | CHECKPOINT lastcp; |
259 | I32 l; |
260 | I32 matches; |
261 | I32 maxwanted; |
262 | } curlym; |
263 | |
264 | struct { |
265 | I32 paren; |
266 | CHECKPOINT lastcp; |
267 | I32 c1, c2; /* case fold search */ |
268 | char *e; |
269 | char *old; |
270 | int count; |
271 | } plus; /* and CURLYN/CURLY/STAR */ |
272 | |
273 | struct { |
274 | CHECKPOINT cp; /* remember current savestack indexes */ |
275 | CHECKPOINT lastcp; |
276 | CURCUR *savecc; |
277 | re_cc_state *cur_call_cc; |
278 | regexp *end_re; |
279 | } end; |
280 | }; |
5d9a96ca |
281 | |
282 | re_cc_state *reg_call_cc; /* saved value of PL_reg_call_cc */ |
283 | } regmatch_state; |
284 | |
285 | /* how many regmatch_state structs to allocate as a single slab. |
286 | * We do it in 4K blocks for efficiency. The "3" is 2 for the next/prev |
287 | * pointers, plus 1 for any mythical malloc overhead. */ |
288 | |
289 | #define PERL_REGMATCH_SLAB_SLOTS \ |
290 | ((4096 - 3 * sizeof (void*)) / sizeof(regmatch_state)) |
291 | |
292 | typedef struct regmatch_slab { |
293 | regmatch_state states[PERL_REGMATCH_SLAB_SLOTS]; |
294 | struct regmatch_slab *prev, *next; |
295 | } regmatch_slab; |