Commit | Line | Data |
a0d0e21e |
1 | /* regcomp.h |
d6376244 |
2 | * |
4bb101f2 |
3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
1d325971 |
4 | * 2000, 2001, 2002, 2003, 2005 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 | * |
a687059c |
9 | */ |
10 | |
c277df42 |
11 | typedef OP OP_4tree; /* Will be redefined later. */ |
12 | |
07be1b83 |
13 | |
14 | #define PERL_ENABLE_TRIE_OPTIMISATION 1 |
15 | #define PERL_ENABLE_EXTENDED_TRIE_OPTIMISATION 1 |
1de06328 |
16 | #define PERL_ENABLE_POSITIVE_ASSERTION_STUDY 1 |
07be1b83 |
17 | #define PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS 0 |
b9b4dddf |
18 | /* Unless the next line is uncommented it is illegal to combine lazy |
19 | matching with possessive matching. Frankly it doesn't make much sense |
20 | to allow it as X*?+ matches nothing, X+?+ matches a single char only, |
21 | and X{min,max}?+ matches min times only. |
22 | */ |
23 | /* #define REG_ALLOW_MINMOD_SUSPEND */ |
07be1b83 |
24 | |
a687059c |
25 | /* |
26 | * The "internal use only" fields in regexp.h are present to pass info from |
27 | * compile to execute that permits the execute phase to run lots faster on |
28 | * simple cases. They are: |
29 | * |
bd61b366 |
30 | * regstart sv that must begin a match; NULL if none obvious |
a687059c |
31 | * reganch is the match anchored (at beginning-of-line only)? |
32 | * regmust string (pointer into program) that match must include, or NULL |
79072805 |
33 | * [regmust changed to SV* for bminstr()--law] |
a687059c |
34 | * regmlen length of regmust string |
35 | * [regmlen not used currently] |
36 | * |
37 | * Regstart and reganch permit very fast decisions on suitable starting points |
38 | * for a match, cutting down the work a lot. Regmust permits fast rejection |
39 | * of lines that cannot possibly match. The regmust tests are costly enough |
e50aee73 |
40 | * that pregcomp() supplies a regmust only if the r.e. contains something |
a687059c |
41 | * potentially expensive (at present, the only such thing detected is * or + |
42 | * at the start of the r.e., which can involve a lot of backup). Regmlen is |
e50aee73 |
43 | * supplied because the test in pregexec() needs it and pregcomp() is computing |
a687059c |
44 | * it anyway. |
45 | * [regmust is now supplied always. The tests that use regmust have a |
46 | * heuristic that disables the test if it usually matches.] |
47 | * |
48 | * [In fact, we now use regmust in many cases to locate where the search |
49 | * starts in the string, so if regback is >= 0, the regmust search is never |
50 | * wasted effort. The regback variable says how many characters back from |
51 | * where regmust matched is the earliest possible start of the match. |
52 | * For instance, /[a-z].foo/ has a regmust of 'foo' and a regback of 2.] |
53 | */ |
54 | |
55 | /* |
56 | * Structure for regexp "program". This is essentially a linear encoding |
57 | * of a nondeterministic finite-state machine (aka syntax charts or |
58 | * "railroad normal form" in parsing technology). Each node is an opcode |
59 | * plus a "next" pointer, possibly plus an operand. "Next" pointers of |
60 | * all nodes except BRANCH implement concatenation; a "next" pointer with |
61 | * a BRANCH on both ends of it is connecting two alternatives. (Here we |
62 | * have one of the subtle syntax dependencies: an individual BRANCH (as |
63 | * opposed to a collection of them) is never concatenated with anything |
64 | * because of operator precedence.) The operand of some types of node is |
65 | * a literal string; for others, it is a node leading into a sub-FSM. In |
66 | * particular, the operand of a BRANCH node is the first node of the branch. |
67 | * (NB this is *not* a tree structure: the tail of the branch connects |
a3621e74 |
68 | * to the thing following the set of BRANCHes.) The opcodes are defined |
69 | * in regnodes.h which is generated from regcomp.sym by regcomp.pl. |
a687059c |
70 | */ |
71 | |
a687059c |
72 | /* |
73 | * A node is one char of opcode followed by two chars of "next" pointer. |
74 | * "Next" pointers are stored as two 8-bit pieces, high order first. The |
75 | * value is a positive offset from the opcode of the node containing it. |
76 | * An operand, if any, simply follows the node. (Note that much of the |
77 | * code generation knows about this implicit relationship.) |
78 | * |
79 | * Using two bytes for the "next" pointer is vast overkill for most things, |
80 | * but allows patterns to get big without disasters. |
81 | * |
e91177ed |
82 | * [The "next" pointer is always aligned on an even |
a687059c |
83 | * boundary, and reads the offset directly as a short. Also, there is no |
84 | * special test to reverse the sign of BACK pointers since the offset is |
85 | * stored negative.] |
86 | */ |
87 | |
c277df42 |
88 | struct regnode_string { |
cd439c50 |
89 | U8 str_len; |
c277df42 |
90 | U8 type; |
91 | U16 next_off; |
cd439c50 |
92 | char string[1]; |
c277df42 |
93 | }; |
a687059c |
94 | |
6bda09f9 |
95 | /* Argument bearing node - workhorse, |
96 | arg1 is often for the data field */ |
c277df42 |
97 | struct regnode_1 { |
98 | U8 flags; |
99 | U8 type; |
100 | U16 next_off; |
101 | U32 arg1; |
102 | }; |
103 | |
6bda09f9 |
104 | /* Similar to a regnode_1 but with an extra signed argument */ |
105 | struct regnode_2L { |
106 | U8 flags; |
107 | U8 type; |
108 | U16 next_off; |
109 | U32 arg1; |
110 | I32 arg2; |
111 | }; |
112 | |
113 | /* 'Two field' -- Two 16 bit unsigned args */ |
c277df42 |
114 | struct regnode_2 { |
115 | U8 flags; |
116 | U8 type; |
117 | U16 next_off; |
118 | U16 arg1; |
119 | U16 arg2; |
120 | }; |
121 | |
6bda09f9 |
122 | |
936ed897 |
123 | #define ANYOF_BITMAP_SIZE 32 /* 256 b/(8 b/B) */ |
ffc61ed2 |
124 | #define ANYOF_CLASSBITMAP_SIZE 4 /* up to 32 (8*4) named classes */ |
936ed897 |
125 | |
786e8c11 |
126 | /* also used by trie */ |
936ed897 |
127 | struct regnode_charclass { |
128 | U8 flags; |
129 | U8 type; |
130 | U16 next_off; |
ffc61ed2 |
131 | U32 arg1; |
19103b91 |
132 | char bitmap[ANYOF_BITMAP_SIZE]; /* only compile-time */ |
936ed897 |
133 | }; |
134 | |
19103b91 |
135 | struct regnode_charclass_class { /* has [[:blah:]] classes */ |
136 | U8 flags; /* should have ANYOF_CLASS here */ |
936ed897 |
137 | U8 type; |
138 | U16 next_off; |
ffc61ed2 |
139 | U32 arg1; |
19103b91 |
140 | char bitmap[ANYOF_BITMAP_SIZE]; /* both compile-time */ |
141 | char classflags[ANYOF_CLASSBITMAP_SIZE]; /* and run-time */ |
936ed897 |
142 | }; |
143 | |
9c7e81e8 |
144 | /* XXX fix this description. |
145 | Impose a limit of REG_INFTY on various pattern matching operations |
146 | to limit stack growth and to avoid "infinite" recursions. |
147 | */ |
148 | /* The default size for REG_INFTY is I16_MAX, which is the same as |
149 | SHORT_MAX (see perl.h). Unfortunately I16 isn't necessarily 16 bits |
150 | (see handy.h). On the Cray C90, sizeof(short)==4 and hence I16_MAX is |
151 | ((1<<31)-1), while on the Cray T90, sizeof(short)==8 and I16_MAX is |
152 | ((1<<63)-1). To limit stack growth to reasonable sizes, supply a |
153 | smaller default. |
154 | --Andy Dougherty 11 June 1998 |
155 | */ |
156 | #if SHORTSIZE > 2 |
157 | # ifndef REG_INFTY |
158 | # define REG_INFTY ((1<<15)-1) |
159 | # endif |
160 | #endif |
161 | |
162 | #ifndef REG_INFTY |
83cfe48c |
163 | # define REG_INFTY I16_MAX |
164 | #endif |
a687059c |
165 | |
e91177ed |
166 | #define ARG_VALUE(arg) (arg) |
167 | #define ARG__SET(arg,val) ((arg) = (val)) |
c277df42 |
168 | |
e8e3b0fb |
169 | #undef ARG |
170 | #undef ARG1 |
171 | #undef ARG2 |
172 | |
c277df42 |
173 | #define ARG(p) ARG_VALUE(ARG_LOC(p)) |
174 | #define ARG1(p) ARG_VALUE(ARG1_LOC(p)) |
175 | #define ARG2(p) ARG_VALUE(ARG2_LOC(p)) |
6bda09f9 |
176 | #define ARG2L(p) ARG_VALUE(ARG2L_LOC(p)) |
786e8c11 |
177 | |
c277df42 |
178 | #define ARG_SET(p, val) ARG__SET(ARG_LOC(p), (val)) |
179 | #define ARG1_SET(p, val) ARG__SET(ARG1_LOC(p), (val)) |
180 | #define ARG2_SET(p, val) ARG__SET(ARG2_LOC(p), (val)) |
6bda09f9 |
181 | #define ARG2L_SET(p, val) ARG__SET(ARG2L_LOC(p), (val)) |
c277df42 |
182 | |
e8e3b0fb |
183 | #undef NEXT_OFF |
184 | #undef NODE_ALIGN |
185 | |
075abff3 |
186 | #define NEXT_OFF(p) ((p)->next_off) |
187 | #define NODE_ALIGN(node) |
188 | #define NODE_ALIGN_FILL(node) ((node)->flags = 0xde) /* deadbeef */ |
a687059c |
189 | |
c277df42 |
190 | #define SIZE_ALIGN NODE_ALIGN |
191 | |
e8e3b0fb |
192 | #undef OP |
193 | #undef OPERAND |
194 | #undef MASK |
195 | #undef STRING |
196 | |
e91177ed |
197 | #define OP(p) ((p)->type) |
198 | #define OPERAND(p) (((struct regnode_string *)p)->string) |
cd439c50 |
199 | #define MASK(p) ((char*)OPERAND(p)) |
200 | #define STR_LEN(p) (((struct regnode_string *)p)->str_len) |
201 | #define STRING(p) (((struct regnode_string *)p)->string) |
202 | #define STR_SZ(l) ((l + sizeof(regnode) - 1) / sizeof(regnode)) |
203 | #define NODE_SZ_STR(p) (STR_SZ(STR_LEN(p))+1) |
204 | |
e8e3b0fb |
205 | #undef NODE_ALIGN |
206 | #undef ARG_LOC |
207 | #undef NEXTOPER |
208 | #undef PREVOPER |
209 | |
e91177ed |
210 | #define NODE_ALIGN(node) |
211 | #define ARG_LOC(p) (((struct regnode_1 *)p)->arg1) |
212 | #define ARG1_LOC(p) (((struct regnode_2 *)p)->arg1) |
213 | #define ARG2_LOC(p) (((struct regnode_2 *)p)->arg2) |
6bda09f9 |
214 | #define ARG2L_LOC(p) (((struct regnode_2L *)p)->arg2) |
786e8c11 |
215 | |
e91177ed |
216 | #define NODE_STEP_REGNODE 1 /* sizeof(regnode)/sizeof(regnode) */ |
217 | #define EXTRA_STEP_2ARGS EXTRA_SIZE(struct regnode_2) |
218 | |
219 | #define NODE_STEP_B 4 |
c277df42 |
220 | |
221 | #define NEXTOPER(p) ((p) + NODE_STEP_REGNODE) |
222 | #define PREVOPER(p) ((p) - NODE_STEP_REGNODE) |
223 | |
e91177ed |
224 | #define FILL_ADVANCE_NODE(ptr, op) STMT_START { \ |
c277df42 |
225 | (ptr)->type = op; (ptr)->next_off = 0; (ptr)++; } STMT_END |
e91177ed |
226 | #define FILL_ADVANCE_NODE_ARG(ptr, op, arg) STMT_START { \ |
c277df42 |
227 | ARG_SET(ptr, arg); FILL_ADVANCE_NODE(ptr, op); (ptr) += 1; } STMT_END |
a687059c |
228 | |
22c35a8c |
229 | #define REG_MAGIC 0234 |
a687059c |
230 | |
830247a4 |
231 | #define SIZE_ONLY (RExC_emit == &PL_regdummy) |
c277df42 |
232 | |
936ed897 |
233 | /* Flags for node->flags of ANYOF */ |
b8c5462f |
234 | |
19103b91 |
235 | #define ANYOF_CLASS 0x08 /* has [[:blah:]] classes */ |
ffc61ed2 |
236 | #define ANYOF_INVERT 0x04 |
237 | #define ANYOF_FOLD 0x02 |
238 | #define ANYOF_LOCALE 0x01 |
b8c5462f |
239 | |
653099ff |
240 | /* Used for regstclass only */ |
ffc61ed2 |
241 | #define ANYOF_EOS 0x10 /* Can match an empty string too */ |
242 | |
243 | /* There is a character or a range past 0xff */ |
244 | #define ANYOF_UNICODE 0x20 |
1aa99e6b |
245 | #define ANYOF_UNICODE_ALL 0x40 /* Can match any char past 0xff */ |
ffc61ed2 |
246 | |
4f66b38d |
247 | /* size of node is large (includes class pointer) */ |
248 | #define ANYOF_LARGE 0x80 |
249 | |
ffc61ed2 |
250 | /* Are there any runtime flags on in this node? */ |
251 | #define ANYOF_RUNTIME(s) (ANYOF_FLAGS(s) & 0x0f) |
252 | |
253 | #define ANYOF_FLAGS_ALL 0xff |
653099ff |
254 | |
936ed897 |
255 | /* Character classes for node->classflags of ANYOF */ |
653099ff |
256 | /* Should be synchronized with a table in regprop() */ |
257 | /* 2n should pair with 2n+1 */ |
b8c5462f |
258 | |
289d4f09 |
259 | #define ANYOF_ALNUM 0 /* \w, PL_utf8_alnum, utf8::IsWord, ALNUM */ |
b8c5462f |
260 | #define ANYOF_NALNUM 1 |
aaa51d5e |
261 | #define ANYOF_SPACE 2 /* \s */ |
b8c5462f |
262 | #define ANYOF_NSPACE 3 |
263 | #define ANYOF_DIGIT 4 |
264 | #define ANYOF_NDIGIT 5 |
289d4f09 |
265 | #define ANYOF_ALNUMC 6 /* isalnum(3), utf8::IsAlnum, ALNUMC */ |
b8c5462f |
266 | #define ANYOF_NALNUMC 7 |
267 | #define ANYOF_ALPHA 8 |
268 | #define ANYOF_NALPHA 9 |
269 | #define ANYOF_ASCII 10 |
270 | #define ANYOF_NASCII 11 |
271 | #define ANYOF_CNTRL 12 |
272 | #define ANYOF_NCNTRL 13 |
273 | #define ANYOF_GRAPH 14 |
274 | #define ANYOF_NGRAPH 15 |
275 | #define ANYOF_LOWER 16 |
276 | #define ANYOF_NLOWER 17 |
277 | #define ANYOF_PRINT 18 |
278 | #define ANYOF_NPRINT 19 |
279 | #define ANYOF_PUNCT 20 |
280 | #define ANYOF_NPUNCT 21 |
281 | #define ANYOF_UPPER 22 |
282 | #define ANYOF_NUPPER 23 |
283 | #define ANYOF_XDIGIT 24 |
284 | #define ANYOF_NXDIGIT 25 |
aaa51d5e |
285 | #define ANYOF_PSXSPC 26 /* POSIX space: \s plus the vertical tab */ |
286 | #define ANYOF_NPSXSPC 27 |
ffc61ed2 |
287 | #define ANYOF_BLANK 28 /* GNU extension: space and tab: non-vertical space */ |
289d4f09 |
288 | #define ANYOF_NBLANK 29 |
b8c5462f |
289 | |
aaa51d5e |
290 | #define ANYOF_MAX 32 |
b8c5462f |
291 | |
292 | /* Backward source code compatibility. */ |
293 | |
294 | #define ANYOF_ALNUML ANYOF_ALNUM |
295 | #define ANYOF_NALNUML ANYOF_NALNUM |
296 | #define ANYOF_SPACEL ANYOF_SPACE |
297 | #define ANYOF_NSPACEL ANYOF_NSPACE |
298 | |
299 | /* Utility macros for the bitmap and classes of ANYOF */ |
300 | |
936ed897 |
301 | #define ANYOF_SIZE (sizeof(struct regnode_charclass)) |
302 | #define ANYOF_CLASS_SIZE (sizeof(struct regnode_charclass_class)) |
b8c5462f |
303 | |
936ed897 |
304 | #define ANYOF_FLAGS(p) ((p)->flags) |
b8c5462f |
305 | |
306 | #define ANYOF_BIT(c) (1 << ((c) & 7)) |
307 | |
936ed897 |
308 | #define ANYOF_CLASS_BYTE(p, c) (((struct regnode_charclass_class*)(p))->classflags[((c) >> 3) & 3]) |
b8c5462f |
309 | #define ANYOF_CLASS_SET(p, c) (ANYOF_CLASS_BYTE(p, c) |= ANYOF_BIT(c)) |
310 | #define ANYOF_CLASS_CLEAR(p, c) (ANYOF_CLASS_BYTE(p, c) &= ~ANYOF_BIT(c)) |
311 | #define ANYOF_CLASS_TEST(p, c) (ANYOF_CLASS_BYTE(p, c) & ANYOF_BIT(c)) |
312 | |
936ed897 |
313 | #define ANYOF_CLASS_ZERO(ret) Zero(((struct regnode_charclass_class*)(ret))->classflags, ANYOF_CLASSBITMAP_SIZE, char) |
314 | #define ANYOF_BITMAP_ZERO(ret) Zero(((struct regnode_charclass*)(ret))->bitmap, ANYOF_BITMAP_SIZE, char) |
315 | |
316 | #define ANYOF_BITMAP(p) (((struct regnode_charclass*)(p))->bitmap) |
786e8c11 |
317 | #define ANYOF_BITMAP_BYTE(p, c) (ANYOF_BITMAP(p)[(((U8)(c)) >> 3) & 31]) |
b8c5462f |
318 | #define ANYOF_BITMAP_SET(p, c) (ANYOF_BITMAP_BYTE(p, c) |= ANYOF_BIT(c)) |
319 | #define ANYOF_BITMAP_CLEAR(p,c) (ANYOF_BITMAP_BYTE(p, c) &= ~ANYOF_BIT(c)) |
320 | #define ANYOF_BITMAP_TEST(p, c) (ANYOF_BITMAP_BYTE(p, c) & ANYOF_BIT(c)) |
321 | |
f8bef550 |
322 | #define ANYOF_BITMAP_SETALL(p) \ |
323 | memset (ANYOF_BITMAP(p), 255, ANYOF_BITMAP_SIZE) |
324 | #define ANYOF_BITMAP_CLEARALL(p) \ |
325 | Zero (ANYOF_BITMAP(p), ANYOF_BITMAP_SIZE) |
326 | /* Check that all 256 bits are all set. Used in S_cl_is_anything() */ |
327 | #define ANYOF_BITMAP_TESTALLSET(p) \ |
328 | memEQ (ANYOF_BITMAP(p), "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377", ANYOF_BITMAP_SIZE) |
329 | |
936ed897 |
330 | #define ANYOF_SKIP ((ANYOF_SIZE - 1)/sizeof(regnode)) |
331 | #define ANYOF_CLASS_SKIP ((ANYOF_CLASS_SIZE - 1)/sizeof(regnode)) |
332 | #define ANYOF_CLASS_ADD_SKIP (ANYOF_CLASS_SKIP - ANYOF_SKIP) |
c277df42 |
333 | |
786e8c11 |
334 | |
a687059c |
335 | /* |
336 | * Utility definitions. |
337 | */ |
2304df62 |
338 | #ifndef CHARMASK |
075abff3 |
339 | # define UCHARAT(p) ((int)*(const U8*)(p)) |
a687059c |
340 | #else |
075abff3 |
341 | # define UCHARAT(p) ((int)*(p)&CHARMASK) |
a687059c |
342 | #endif |
a687059c |
343 | |
c277df42 |
344 | #define EXTRA_SIZE(guy) ((sizeof(guy)-1)/sizeof(struct regnode)) |
345 | |
3dab1dad |
346 | #define REG_SEEN_ZERO_LEN 0x00000001 |
347 | #define REG_SEEN_LOOKBEHIND 0x00000002 |
348 | #define REG_SEEN_GPOS 0x00000004 |
349 | #define REG_SEEN_EVAL 0x00000008 |
350 | #define REG_SEEN_CANY 0x00000010 |
cce850e4 |
351 | #define REG_SEEN_SANY REG_SEEN_CANY /* src bckwrd cmpt */ |
6bda09f9 |
352 | #define REG_SEEN_RECURSE 0x00000020 |
eaf3ca90 |
353 | #define REG_TOP_LEVEL_BRANCHES 0x00000040 |
d09b2d29 |
354 | |
73c4f7a1 |
355 | START_EXTERN_C |
356 | |
be8e71aa |
357 | #ifdef PLUGGABLE_RE_EXTENSION |
358 | #include "re_nodes.h" |
359 | #else |
d09b2d29 |
360 | #include "regnodes.h" |
be8e71aa |
361 | #endif |
d09b2d29 |
362 | |
4d61ec05 |
363 | /* The following have no fixed length. U8 so we can do strchr() on it. */ |
d09b2d29 |
364 | #ifndef DOINIT |
4d61ec05 |
365 | EXTCONST U8 PL_varies[]; |
d09b2d29 |
366 | #else |
4d61ec05 |
367 | EXTCONST U8 PL_varies[] = { |
0111c4fd |
368 | BRANCH, BACK, STAR, PLUS, CURLY, CURLYX, REF, REFF, REFFL, |
a0ed51b3 |
369 | WHILEM, CURLYM, CURLYN, BRANCHJ, IFTHEN, SUSPEND, CLUMP, 0 |
c277df42 |
370 | }; |
d09b2d29 |
371 | #endif |
c277df42 |
372 | |
4d61ec05 |
373 | /* The following always have a length of 1. U8 we can do strchr() on it. */ |
374 | /* (Note that length 1 means "one character" under UTF8, not "one octet".) */ |
d09b2d29 |
375 | #ifndef DOINIT |
4d61ec05 |
376 | EXTCONST U8 PL_simple[]; |
d09b2d29 |
377 | #else |
4d61ec05 |
378 | EXTCONST U8 PL_simple[] = { |
f33976b4 |
379 | REG_ANY, SANY, CANY, |
ffc61ed2 |
380 | ANYOF, |
381 | ALNUM, ALNUML, |
382 | NALNUM, NALNUML, |
383 | SPACE, SPACEL, |
384 | NSPACE, NSPACEL, |
385 | DIGIT, NDIGIT, |
386 | 0 |
c277df42 |
387 | }; |
388 | #endif |
389 | |
f9f4320a |
390 | #ifndef PLUGGABLE_RE_EXTENSION |
391 | #ifndef DOINIT |
392 | EXTCONST regexp_engine PL_core_reg_engine; |
393 | #else /* DOINIT */ |
394 | EXTCONST regexp_engine PL_core_reg_engine = { |
395 | Perl_pregcomp, |
396 | Perl_regexec_flags, |
397 | Perl_re_intuit_start, |
398 | Perl_re_intuit_string, |
399 | Perl_pregfree, |
400 | #if defined(USE_ITHREADS) |
401 | Perl_regdupe |
402 | #endif |
403 | }; |
404 | #endif /* DOINIT */ |
405 | #endif /* PLUGGABLE_RE_EXTENSION */ |
406 | |
407 | |
73c4f7a1 |
408 | END_EXTERN_C |
cad2e5aa |
409 | |
cad2e5aa |
410 | |
261faec3 |
411 | /* .what is a character array with one character for each member of .data |
412 | * The character describes the function of the corresponding .data item: |
0111c4fd |
413 | * f - start-class data for regstclass optimization |
261faec3 |
414 | * n - Root of op tree for (?{EVAL}) item |
415 | * o - Start op for (?{EVAL}) item |
3dab1dad |
416 | * p - Pad for (?{EVAL}) item |
9e55ce06 |
417 | * s - swash for unicode-style character class, and the multicharacter |
418 | * strings resulting from casefolding the single-character entries |
419 | * in the character class |
a3621e74 |
420 | * t - trie struct |
07be1b83 |
421 | * T - aho-trie struct |
81714fb9 |
422 | * S - sv for named capture lookup |
261faec3 |
423 | * 20010712 mjd@plover.com |
c3613ed7 |
424 | * (Remember to update re_dup() and pregfree() if you add any items.) |
261faec3 |
425 | */ |
cad2e5aa |
426 | struct reg_data { |
427 | U32 count; |
428 | U8 *what; |
429 | void* data[1]; |
430 | }; |
431 | |
432 | struct reg_substr_datum { |
433 | I32 min_offset; |
434 | I32 max_offset; |
33b8afdf |
435 | SV *substr; /* non-utf8 variant */ |
436 | SV *utf8_substr; /* utf8 variant */ |
1de06328 |
437 | I32 end_shift; |
cad2e5aa |
438 | }; |
439 | |
440 | struct reg_substr_data { |
441 | struct reg_substr_datum data[3]; /* Actual array */ |
442 | }; |
443 | |
444 | #define anchored_substr substrs->data[0].substr |
33b8afdf |
445 | #define anchored_utf8 substrs->data[0].utf8_substr |
cad2e5aa |
446 | #define anchored_offset substrs->data[0].min_offset |
1de06328 |
447 | #define anchored_end_shift substrs->data[0].end_shift |
448 | |
cad2e5aa |
449 | #define float_substr substrs->data[1].substr |
33b8afdf |
450 | #define float_utf8 substrs->data[1].utf8_substr |
cad2e5aa |
451 | #define float_min_offset substrs->data[1].min_offset |
452 | #define float_max_offset substrs->data[1].max_offset |
1de06328 |
453 | #define float_end_shift substrs->data[1].end_shift |
454 | |
cad2e5aa |
455 | #define check_substr substrs->data[2].substr |
33b8afdf |
456 | #define check_utf8 substrs->data[2].utf8_substr |
cad2e5aa |
457 | #define check_offset_min substrs->data[2].min_offset |
458 | #define check_offset_max substrs->data[2].max_offset |
1de06328 |
459 | #define check_end_shift substrs->data[2].end_shift |
a3621e74 |
460 | |
461 | |
462 | |
463 | /* trie related stuff */ |
5d9a96ca |
464 | |
a3621e74 |
465 | /* a transition record for the state machine. the |
466 | check field determines which state "owns" the |
467 | transition. the char the transition is for is |
468 | determined by offset from the owning states base |
469 | field. the next field determines which state |
470 | is to be transitioned to if any. |
471 | */ |
472 | struct _reg_trie_trans { |
473 | U32 next; |
474 | U32 check; |
475 | }; |
476 | |
477 | /* a transition list element for the list based representation */ |
478 | struct _reg_trie_trans_list_elem { |
479 | U16 forid; |
480 | U32 newstate; |
481 | }; |
482 | typedef struct _reg_trie_trans_list_elem reg_trie_trans_le; |
483 | |
484 | /* a state for compressed nodes. base is an offset |
485 | into an array of reg_trie_trans array. If wordnum is |
486 | nonzero the state is accepting. if base is zero then |
487 | the state has no children (and will be accepting) |
488 | */ |
489 | struct _reg_trie_state { |
490 | U16 wordnum; |
491 | union { |
492 | U32 base; |
493 | reg_trie_trans_le* list; |
494 | } trans; |
495 | }; |
496 | |
497 | |
498 | |
a3621e74 |
499 | typedef struct _reg_trie_state reg_trie_state; |
500 | typedef struct _reg_trie_trans reg_trie_trans; |
501 | |
502 | |
503 | /* anything in here that needs to be freed later |
8e11feef |
504 | should be dealt with in pregfree */ |
a3621e74 |
505 | struct _reg_trie_data { |
786e8c11 |
506 | U16 uniquecharcount; /* unique chars in trie (width of trans table) */ |
507 | U32 lasttrans; /* last valid transition element */ |
508 | U16 *charmap; /* byte to charid lookup array */ |
509 | HV *widecharmap; /* code points > 255 to charid */ |
510 | reg_trie_state *states; /* state data */ |
511 | reg_trie_trans *trans; /* array of transition elements */ |
512 | char *bitmap; /* stclass bitmap */ |
513 | U32 refcount; /* number of times this trie is referenced */ |
514 | U32 startstate; /* initial state - used for common prefix optimisation */ |
515 | STRLEN minlen; /* minimum length of words in trie - build/opt only? */ |
516 | STRLEN maxlen; /* maximum length of words in trie - build/opt only? */ |
517 | U32 *wordlen; /* array of lengths of words */ |
518 | U16 *jump; /* optional 1 indexed array of offsets before tail |
519 | for the node following a given word. */ |
520 | U16 *nextword; /* optional 1 indexed array to support linked list |
521 | of duplicate wordnums */ |
522 | U32 laststate; /* Build only */ |
523 | U32 wordcount; /* Build only */ |
a3621e74 |
524 | #ifdef DEBUGGING |
786e8c11 |
525 | STRLEN charcount; /* Build only */ |
526 | AV *words; /* Array of words contained in trie, for dumping */ |
527 | AV *revcharmap; /* Map of each charid back to its character representation */ |
a3621e74 |
528 | #endif |
529 | }; |
a3621e74 |
530 | typedef struct _reg_trie_data reg_trie_data; |
531 | |
07be1b83 |
532 | struct _reg_ac_data { |
533 | U32 *fail; |
534 | reg_trie_state *states; |
535 | reg_trie_data *trie; |
536 | U32 refcount; |
537 | }; |
538 | typedef struct _reg_ac_data reg_ac_data; |
539 | |
3dab1dad |
540 | /* ANY_BIT doesnt use the structure, so we can borrow it here. |
541 | This is simpler than refactoring all of it as wed end up with |
542 | three different sets... */ |
543 | |
544 | #define TRIE_BITMAP(p) (((reg_trie_data *)(p))->bitmap) |
786e8c11 |
545 | #define TRIE_BITMAP_BYTE(p, c) (TRIE_BITMAP(p)[(((U8)(c)) >> 3) & 31]) |
5dfe062f |
546 | #define TRIE_BITMAP_SET(p, c) (TRIE_BITMAP_BYTE(p, c) |= ANYOF_BIT((U8)c)) |
547 | #define TRIE_BITMAP_CLEAR(p,c) (TRIE_BITMAP_BYTE(p, c) &= ~ANYOF_BIT((U8)c)) |
548 | #define TRIE_BITMAP_TEST(p, c) (TRIE_BITMAP_BYTE(p, c) & ANYOF_BIT((U8)c)) |
3dab1dad |
549 | |
1de06328 |
550 | #define IS_ANYOF_TRIE(op) ((op)==TRIEC || (op)==AHOCORASICKC) |
551 | #define IS_TRIE_AC(op) ((op)>=AHOCORASICK) |
552 | |
553 | |
786e8c11 |
554 | #define BITMAP_BYTE(p, c) (((U8*)p)[(((U8)(c)) >> 3) & 31]) |
555 | #define BITMAP_TEST(p, c) (BITMAP_BYTE(p, c) & ANYOF_BIT((U8)c)) |
3dab1dad |
556 | |
a3621e74 |
557 | /* these defines assume uniquecharcount is the correct variable, and state may be evaluated twice */ |
558 | #define TRIE_NODENUM(state) (((state)-1)/(trie->uniquecharcount)+1) |
559 | #define SAFE_TRIE_NODENUM(state) ((state) ? (((state)-1)/(trie->uniquecharcount)+1) : (state)) |
560 | #define TRIE_NODEIDX(state) ((state) ? (((state)-1)*(trie->uniquecharcount)+1) : (state)) |
561 | |
3dab1dad |
562 | #ifdef DEBUGGING |
3dab1dad |
563 | #define TRIE_CHARCOUNT(trie) ((trie)->charcount) |
3dab1dad |
564 | #define TRIE_REVCHARMAP(trie) ((trie)->revcharmap) |
565 | #else |
3dab1dad |
566 | #define TRIE_CHARCOUNT(trie) (trie_charcount) |
3dab1dad |
567 | #define TRIE_REVCHARMAP(trie) (trie_revcharmap) |
568 | #endif |
a3621e74 |
569 | |
0111c4fd |
570 | #define RE_TRIE_MAXBUF_INIT 65536 |
571 | #define RE_TRIE_MAXBUF_NAME "\022E_TRIE_MAXBUF" |
a3621e74 |
572 | #define RE_DEBUG_FLAGS "\022E_DEBUG_FLAGS" |
573 | |
be8e71aa |
574 | /* |
575 | |
576 | RE_DEBUG_FLAGS is used to control what debug output is emitted |
577 | its divided into three groups of options, some of which interact. |
578 | The three groups are: Compile, Execute, Extra. There is room for a |
579 | further group, as currently only the low three bytes are used. |
580 | |
581 | Compile Options: |
07be1b83 |
582 | |
be8e71aa |
583 | PARSE |
584 | PEEP |
585 | TRIE |
586 | PROGRAM |
587 | OFFSETS |
a3621e74 |
588 | |
be8e71aa |
589 | Execute Options: |
a3621e74 |
590 | |
be8e71aa |
591 | INTUIT |
592 | MATCH |
593 | TRIE |
a3621e74 |
594 | |
be8e71aa |
595 | Extra Options |
a3621e74 |
596 | |
be8e71aa |
597 | TRIE |
598 | OFFSETS |
599 | |
600 | If you modify any of these make sure you make corresponding changes to |
601 | re.pm, especially to the documentation. |
a3621e74 |
602 | |
be8e71aa |
603 | */ |
604 | |
605 | |
606 | /* Compile */ |
607 | #define RE_DEBUG_COMPILE_MASK 0x0000FF |
608 | #define RE_DEBUG_COMPILE_PARSE 0x000001 |
609 | #define RE_DEBUG_COMPILE_OPTIMISE 0x000002 |
610 | #define RE_DEBUG_COMPILE_TRIE 0x000004 |
611 | #define RE_DEBUG_COMPILE_DUMP 0x000008 |
be8e71aa |
612 | |
613 | /* Execute */ |
614 | #define RE_DEBUG_EXECUTE_MASK 0x00FF00 |
615 | #define RE_DEBUG_EXECUTE_INTUIT 0x000100 |
616 | #define RE_DEBUG_EXECUTE_MATCH 0x000200 |
617 | #define RE_DEBUG_EXECUTE_TRIE 0x000400 |
618 | |
619 | /* Extra */ |
620 | #define RE_DEBUG_EXTRA_MASK 0xFF0000 |
621 | #define RE_DEBUG_EXTRA_TRIE 0x010000 |
622 | #define RE_DEBUG_EXTRA_OFFSETS 0x020000 |
a5ca303d |
623 | #define RE_DEBUG_EXTRA_OFFDEBUG 0x040000 |
624 | #define RE_DEBUG_EXTRA_STATE 0x080000 |
625 | #define RE_DEBUG_EXTRA_OPTIMISE 0x100000 |
be8e71aa |
626 | |
e68ec53f |
627 | #define RE_DEBUG_FLAG(x) (re_debug_flags & x) |
be8e71aa |
628 | /* Compile */ |
629 | #define DEBUG_COMPILE_r(x) DEBUG_r( \ |
e68ec53f |
630 | if (re_debug_flags & RE_DEBUG_COMPILE_MASK) x ) |
be8e71aa |
631 | #define DEBUG_PARSE_r(x) DEBUG_r( \ |
e68ec53f |
632 | if (re_debug_flags & RE_DEBUG_COMPILE_PARSE) x ) |
be8e71aa |
633 | #define DEBUG_OPTIMISE_r(x) DEBUG_r( \ |
e68ec53f |
634 | if (re_debug_flags & RE_DEBUG_COMPILE_OPTIMISE) x ) |
be8e71aa |
635 | #define DEBUG_PARSE_r(x) DEBUG_r( \ |
e68ec53f |
636 | if (re_debug_flags & RE_DEBUG_COMPILE_PARSE) x ) |
be8e71aa |
637 | #define DEBUG_DUMP_r(x) DEBUG_r( \ |
e68ec53f |
638 | if (re_debug_flags & RE_DEBUG_COMPILE_DUMP) x ) |
be8e71aa |
639 | #define DEBUG_TRIE_COMPILE_r(x) DEBUG_r( \ |
e68ec53f |
640 | if (re_debug_flags & RE_DEBUG_COMPILE_TRIE) x ) |
be8e71aa |
641 | |
642 | /* Execute */ |
643 | #define DEBUG_EXECUTE_r(x) DEBUG_r( \ |
e68ec53f |
644 | if (re_debug_flags & RE_DEBUG_EXECUTE_MASK) x ) |
be8e71aa |
645 | #define DEBUG_INTUIT_r(x) DEBUG_r( \ |
e68ec53f |
646 | if (re_debug_flags & RE_DEBUG_EXECUTE_INTUIT) x ) |
be8e71aa |
647 | #define DEBUG_MATCH_r(x) DEBUG_r( \ |
e68ec53f |
648 | if (re_debug_flags & RE_DEBUG_EXECUTE_MATCH) x ) |
be8e71aa |
649 | #define DEBUG_TRIE_EXECUTE_r(x) DEBUG_r( \ |
e68ec53f |
650 | if (re_debug_flags & RE_DEBUG_EXECUTE_TRIE) x ) |
be8e71aa |
651 | |
652 | /* Extra */ |
653 | #define DEBUG_EXTRA_r(x) DEBUG_r( \ |
e68ec53f |
654 | if (re_debug_flags & RE_DEBUG_EXTRA_MASK) x ) |
a5ca303d |
655 | #define DEBUG_OFFSETS_r(x) DEBUG_r( \ |
656 | if (re_debug_flags & RE_DEBUG_EXTRA_OFFSETS) x ) |
ab3bbdeb |
657 | #define DEBUG_STATE_r(x) DEBUG_r( \ |
658 | if (re_debug_flags & RE_DEBUG_EXTRA_STATE) x ) |
a5ca303d |
659 | #define DEBUG_OPTIMISE_MORE_r(x) DEBUG_r( \ |
660 | if ((RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE) == \ |
661 | (re_debug_flags & (RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE)) ) x ) |
07be1b83 |
662 | #define MJD_OFFSET_DEBUG(x) DEBUG_r( \ |
a5ca303d |
663 | if (re_debug_flags & RE_DEBUG_EXTRA_OFFDEBUG) \ |
be8e71aa |
664 | Perl_warn_nocontext x ) |
665 | #define DEBUG_TRIE_COMPILE_MORE_r(x) DEBUG_TRIE_COMPILE_r( \ |
e68ec53f |
666 | if (re_debug_flags & RE_DEBUG_EXTRA_TRIE) x ) |
be8e71aa |
667 | #define DEBUG_TRIE_EXECUTE_MORE_r(x) DEBUG_TRIE_EXECUTE_r( \ |
e68ec53f |
668 | if (re_debug_flags & RE_DEBUG_EXTRA_TRIE) x ) |
be8e71aa |
669 | |
670 | #define DEBUG_TRIE_r(x) DEBUG_r( \ |
e68ec53f |
671 | if (re_debug_flags & (RE_DEBUG_COMPILE_TRIE \ |
be8e71aa |
672 | | RE_DEBUG_EXECUTE_TRIE )) x ) |
673 | |
674 | /* initialization */ |
675 | /* get_sv() can return NULL during global destruction. */ |
e68ec53f |
676 | #define GET_RE_DEBUG_FLAGS DEBUG_r({ \ |
677 | SV * re_debug_flags_sv = NULL; \ |
678 | re_debug_flags_sv = get_sv(RE_DEBUG_FLAGS, 1); \ |
679 | if (re_debug_flags_sv) { \ |
8e9a8a48 |
680 | if (!SvIOK(re_debug_flags_sv)) \ |
e68ec53f |
681 | sv_setuv(re_debug_flags_sv, RE_DEBUG_COMPILE_DUMP | RE_DEBUG_EXECUTE_MASK ); \ |
8e9a8a48 |
682 | re_debug_flags=SvIV(re_debug_flags_sv); \ |
e68ec53f |
683 | }\ |
684 | }) |
a3621e74 |
685 | |
686 | #ifdef DEBUGGING |
ab3bbdeb |
687 | |
8e9a8a48 |
688 | #define GET_RE_DEBUG_FLAGS_DECL IV re_debug_flags = 0; GET_RE_DEBUG_FLAGS; |
ab3bbdeb |
689 | |
690 | #define RE_PV_COLOR_DECL(rpv,rlen,isuni,dsv,pv,l,m,c1,c2) \ |
691 | const char * const rpv = \ |
ddc5bc0f |
692 | pv_pretty((dsv), (pv), (l), (m), \ |
ab3bbdeb |
693 | PL_colors[(c1)],PL_colors[(c2)], \ |
694 | ((isuni) ? PERL_PV_ESCAPE_UNI : 0) ); \ |
0df25f3d |
695 | const int rlen = SvCUR(dsv) |
ab3bbdeb |
696 | |
697 | #define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m) \ |
698 | const char * const rpv = \ |
ddc5bc0f |
699 | pv_pretty((dsv), (SvPV_nolen_const(sv)), (SvCUR(sv)), (m), \ |
ab3bbdeb |
700 | PL_colors[(c1)],PL_colors[(c2)], \ |
701 | ((isuni) ? PERL_PV_ESCAPE_UNI : 0) ) |
702 | |
703 | #define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m) \ |
704 | const char * const rpv = \ |
ddc5bc0f |
705 | pv_pretty((dsv), (pv), (l), (m), \ |
ab3bbdeb |
706 | PL_colors[0], PL_colors[1], \ |
707 | ( PERL_PV_PRETTY_QUOTE | PERL_PV_PRETTY_ELIPSES | \ |
708 | ((isuni) ? PERL_PV_ESCAPE_UNI : 0)) \ |
709 | ) |
710 | |
711 | #define RE_SV_DUMPLEN(ItEm) (SvCUR(ItEm) - (SvTAIL(ItEm)!=0)) |
712 | #define RE_SV_TAIL(ItEm) (SvTAIL(ItEm) ? "$" : "") |
713 | |
714 | #else /* if not DEBUGGING */ |
715 | |
a3621e74 |
716 | #define GET_RE_DEBUG_FLAGS_DECL |
ab3bbdeb |
717 | #define RE_PV_COLOR_DECL(rpv,rlen,isuni,dsv,pv,l,m,c1,c2) |
718 | #define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m) |
719 | #define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m) |
720 | #define RE_SV_DUMPLEN(ItEm) |
721 | #define RE_SV_TAIL(ItEm) |
722 | |
723 | #endif /* DEBUG RELATED DEFINES */ |
a3621e74 |
724 | |