Add to Attribute::Handlers the ability to report caller's file and line
[p5sagit/p5-mst-13.2.git] / regcomp.h
CommitLineData
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 11typedef 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 88struct 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 97struct 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 */
105struct 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 114struct 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 127struct 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 135struct 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
e2e6a0f1 354#define REG_SEEN_VERBARG 0x00000080
d09b2d29 355
73c4f7a1 356START_EXTERN_C
357
be8e71aa 358#ifdef PLUGGABLE_RE_EXTENSION
359#include "re_nodes.h"
360#else
d09b2d29 361#include "regnodes.h"
be8e71aa 362#endif
d09b2d29 363
4d61ec05 364/* The following have no fixed length. U8 so we can do strchr() on it. */
d09b2d29 365#ifndef DOINIT
4d61ec05 366EXTCONST U8 PL_varies[];
d09b2d29 367#else
4d61ec05 368EXTCONST U8 PL_varies[] = {
0111c4fd 369 BRANCH, BACK, STAR, PLUS, CURLY, CURLYX, REF, REFF, REFFL,
a0ed51b3 370 WHILEM, CURLYM, CURLYN, BRANCHJ, IFTHEN, SUSPEND, CLUMP, 0
c277df42 371};
d09b2d29 372#endif
c277df42 373
4d61ec05 374/* The following always have a length of 1. U8 we can do strchr() on it. */
375/* (Note that length 1 means "one character" under UTF8, not "one octet".) */
d09b2d29 376#ifndef DOINIT
4d61ec05 377EXTCONST U8 PL_simple[];
d09b2d29 378#else
4d61ec05 379EXTCONST U8 PL_simple[] = {
f33976b4 380 REG_ANY, SANY, CANY,
ffc61ed2 381 ANYOF,
382 ALNUM, ALNUML,
383 NALNUM, NALNUML,
384 SPACE, SPACEL,
385 NSPACE, NSPACEL,
386 DIGIT, NDIGIT,
387 0
c277df42 388};
389#endif
390
f9f4320a 391#ifndef PLUGGABLE_RE_EXTENSION
392#ifndef DOINIT
393EXTCONST regexp_engine PL_core_reg_engine;
394#else /* DOINIT */
395EXTCONST regexp_engine PL_core_reg_engine = {
396 Perl_pregcomp,
397 Perl_regexec_flags,
398 Perl_re_intuit_start,
399 Perl_re_intuit_string,
400 Perl_pregfree,
401#if defined(USE_ITHREADS)
402 Perl_regdupe
403#endif
404};
405#endif /* DOINIT */
406#endif /* PLUGGABLE_RE_EXTENSION */
407
408
73c4f7a1 409END_EXTERN_C
cad2e5aa 410
cad2e5aa 411
261faec3 412/* .what is a character array with one character for each member of .data
413 * The character describes the function of the corresponding .data item:
0111c4fd 414 * f - start-class data for regstclass optimization
261faec3 415 * n - Root of op tree for (?{EVAL}) item
416 * o - Start op for (?{EVAL}) item
3dab1dad 417 * p - Pad for (?{EVAL}) item
9e55ce06 418 * s - swash for unicode-style character class, and the multicharacter
419 * strings resulting from casefolding the single-character entries
420 * in the character class
a3621e74 421 * t - trie struct
07be1b83 422 * T - aho-trie struct
81714fb9 423 * S - sv for named capture lookup
261faec3 424 * 20010712 mjd@plover.com
c3613ed7 425 * (Remember to update re_dup() and pregfree() if you add any items.)
261faec3 426 */
cad2e5aa 427struct reg_data {
428 U32 count;
429 U8 *what;
430 void* data[1];
431};
432
433struct reg_substr_datum {
434 I32 min_offset;
435 I32 max_offset;
33b8afdf 436 SV *substr; /* non-utf8 variant */
437 SV *utf8_substr; /* utf8 variant */
1de06328 438 I32 end_shift;
cad2e5aa 439};
440
441struct reg_substr_data {
442 struct reg_substr_datum data[3]; /* Actual array */
443};
444
445#define anchored_substr substrs->data[0].substr
33b8afdf 446#define anchored_utf8 substrs->data[0].utf8_substr
cad2e5aa 447#define anchored_offset substrs->data[0].min_offset
1de06328 448#define anchored_end_shift substrs->data[0].end_shift
449
cad2e5aa 450#define float_substr substrs->data[1].substr
33b8afdf 451#define float_utf8 substrs->data[1].utf8_substr
cad2e5aa 452#define float_min_offset substrs->data[1].min_offset
453#define float_max_offset substrs->data[1].max_offset
1de06328 454#define float_end_shift substrs->data[1].end_shift
455
cad2e5aa 456#define check_substr substrs->data[2].substr
33b8afdf 457#define check_utf8 substrs->data[2].utf8_substr
cad2e5aa 458#define check_offset_min substrs->data[2].min_offset
459#define check_offset_max substrs->data[2].max_offset
1de06328 460#define check_end_shift substrs->data[2].end_shift
a3621e74 461
462
463
464/* trie related stuff */
5d9a96ca 465
a3621e74 466/* a transition record for the state machine. the
467 check field determines which state "owns" the
468 transition. the char the transition is for is
469 determined by offset from the owning states base
470 field. the next field determines which state
471 is to be transitioned to if any.
472*/
473struct _reg_trie_trans {
474 U32 next;
475 U32 check;
476};
477
478/* a transition list element for the list based representation */
479struct _reg_trie_trans_list_elem {
480 U16 forid;
481 U32 newstate;
482};
483typedef struct _reg_trie_trans_list_elem reg_trie_trans_le;
484
485/* a state for compressed nodes. base is an offset
486 into an array of reg_trie_trans array. If wordnum is
487 nonzero the state is accepting. if base is zero then
488 the state has no children (and will be accepting)
489*/
490struct _reg_trie_state {
491 U16 wordnum;
492 union {
493 U32 base;
494 reg_trie_trans_le* list;
495 } trans;
496};
497
498
499
a3621e74 500typedef struct _reg_trie_state reg_trie_state;
501typedef struct _reg_trie_trans reg_trie_trans;
502
503
504/* anything in here that needs to be freed later
8e11feef 505 should be dealt with in pregfree */
a3621e74 506struct _reg_trie_data {
786e8c11 507 U16 uniquecharcount; /* unique chars in trie (width of trans table) */
508 U32 lasttrans; /* last valid transition element */
509 U16 *charmap; /* byte to charid lookup array */
510 HV *widecharmap; /* code points > 255 to charid */
511 reg_trie_state *states; /* state data */
512 reg_trie_trans *trans; /* array of transition elements */
513 char *bitmap; /* stclass bitmap */
514 U32 refcount; /* number of times this trie is referenced */
515 U32 startstate; /* initial state - used for common prefix optimisation */
516 STRLEN minlen; /* minimum length of words in trie - build/opt only? */
517 STRLEN maxlen; /* maximum length of words in trie - build/opt only? */
518 U32 *wordlen; /* array of lengths of words */
519 U16 *jump; /* optional 1 indexed array of offsets before tail
520 for the node following a given word. */
521 U16 *nextword; /* optional 1 indexed array to support linked list
522 of duplicate wordnums */
1e2e3d02 523 U32 statecount; /* Build only - number of states in the states array
524 (including the unused zero state) */
786e8c11 525 U32 wordcount; /* Build only */
a3621e74 526#ifdef DEBUGGING
786e8c11 527 STRLEN charcount; /* Build only */
528 AV *words; /* Array of words contained in trie, for dumping */
529 AV *revcharmap; /* Map of each charid back to its character representation */
a3621e74 530#endif
531};
a3621e74 532typedef struct _reg_trie_data reg_trie_data;
533
07be1b83 534struct _reg_ac_data {
535 U32 *fail;
536 reg_trie_state *states;
537 reg_trie_data *trie;
538 U32 refcount;
539};
540typedef struct _reg_ac_data reg_ac_data;
541
3dab1dad 542/* ANY_BIT doesnt use the structure, so we can borrow it here.
543 This is simpler than refactoring all of it as wed end up with
544 three different sets... */
545
546#define TRIE_BITMAP(p) (((reg_trie_data *)(p))->bitmap)
786e8c11 547#define TRIE_BITMAP_BYTE(p, c) (TRIE_BITMAP(p)[(((U8)(c)) >> 3) & 31])
5dfe062f 548#define TRIE_BITMAP_SET(p, c) (TRIE_BITMAP_BYTE(p, c) |= ANYOF_BIT((U8)c))
549#define TRIE_BITMAP_CLEAR(p,c) (TRIE_BITMAP_BYTE(p, c) &= ~ANYOF_BIT((U8)c))
550#define TRIE_BITMAP_TEST(p, c) (TRIE_BITMAP_BYTE(p, c) & ANYOF_BIT((U8)c))
3dab1dad 551
1de06328 552#define IS_ANYOF_TRIE(op) ((op)==TRIEC || (op)==AHOCORASICKC)
553#define IS_TRIE_AC(op) ((op)>=AHOCORASICK)
554
555
786e8c11 556#define BITMAP_BYTE(p, c) (((U8*)p)[(((U8)(c)) >> 3) & 31])
557#define BITMAP_TEST(p, c) (BITMAP_BYTE(p, c) & ANYOF_BIT((U8)c))
3dab1dad 558
a3621e74 559/* these defines assume uniquecharcount is the correct variable, and state may be evaluated twice */
560#define TRIE_NODENUM(state) (((state)-1)/(trie->uniquecharcount)+1)
561#define SAFE_TRIE_NODENUM(state) ((state) ? (((state)-1)/(trie->uniquecharcount)+1) : (state))
562#define TRIE_NODEIDX(state) ((state) ? (((state)-1)*(trie->uniquecharcount)+1) : (state))
563
3dab1dad 564#ifdef DEBUGGING
3dab1dad 565#define TRIE_CHARCOUNT(trie) ((trie)->charcount)
3dab1dad 566#define TRIE_REVCHARMAP(trie) ((trie)->revcharmap)
567#else
3dab1dad 568#define TRIE_CHARCOUNT(trie) (trie_charcount)
3dab1dad 569#define TRIE_REVCHARMAP(trie) (trie_revcharmap)
570#endif
a3621e74 571
0111c4fd 572#define RE_TRIE_MAXBUF_INIT 65536
573#define RE_TRIE_MAXBUF_NAME "\022E_TRIE_MAXBUF"
a3621e74 574#define RE_DEBUG_FLAGS "\022E_DEBUG_FLAGS"
575
be8e71aa 576/*
577
578RE_DEBUG_FLAGS is used to control what debug output is emitted
579its divided into three groups of options, some of which interact.
580The three groups are: Compile, Execute, Extra. There is room for a
581further group, as currently only the low three bytes are used.
582
583 Compile Options:
07be1b83 584
be8e71aa 585 PARSE
586 PEEP
587 TRIE
588 PROGRAM
589 OFFSETS
a3621e74 590
be8e71aa 591 Execute Options:
a3621e74 592
be8e71aa 593 INTUIT
594 MATCH
595 TRIE
a3621e74 596
be8e71aa 597 Extra Options
a3621e74 598
be8e71aa 599 TRIE
600 OFFSETS
601
602If you modify any of these make sure you make corresponding changes to
603re.pm, especially to the documentation.
a3621e74 604
be8e71aa 605*/
606
607
608/* Compile */
609#define RE_DEBUG_COMPILE_MASK 0x0000FF
610#define RE_DEBUG_COMPILE_PARSE 0x000001
611#define RE_DEBUG_COMPILE_OPTIMISE 0x000002
612#define RE_DEBUG_COMPILE_TRIE 0x000004
613#define RE_DEBUG_COMPILE_DUMP 0x000008
be8e71aa 614
615/* Execute */
616#define RE_DEBUG_EXECUTE_MASK 0x00FF00
617#define RE_DEBUG_EXECUTE_INTUIT 0x000100
618#define RE_DEBUG_EXECUTE_MATCH 0x000200
619#define RE_DEBUG_EXECUTE_TRIE 0x000400
620
621/* Extra */
622#define RE_DEBUG_EXTRA_MASK 0xFF0000
623#define RE_DEBUG_EXTRA_TRIE 0x010000
624#define RE_DEBUG_EXTRA_OFFSETS 0x020000
a5ca303d 625#define RE_DEBUG_EXTRA_OFFDEBUG 0x040000
626#define RE_DEBUG_EXTRA_STATE 0x080000
627#define RE_DEBUG_EXTRA_OPTIMISE 0x100000
24b23f37 628/* combined */
629#define RE_DEBUG_EXTRA_STACK 0x280000
be8e71aa 630
e68ec53f 631#define RE_DEBUG_FLAG(x) (re_debug_flags & x)
be8e71aa 632/* Compile */
633#define DEBUG_COMPILE_r(x) DEBUG_r( \
e68ec53f 634 if (re_debug_flags & RE_DEBUG_COMPILE_MASK) 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_OPTIMISE_r(x) DEBUG_r( \
e68ec53f 638 if (re_debug_flags & RE_DEBUG_COMPILE_OPTIMISE) x )
be8e71aa 639#define DEBUG_PARSE_r(x) DEBUG_r( \
e68ec53f 640 if (re_debug_flags & RE_DEBUG_COMPILE_PARSE) x )
be8e71aa 641#define DEBUG_DUMP_r(x) DEBUG_r( \
e68ec53f 642 if (re_debug_flags & RE_DEBUG_COMPILE_DUMP) x )
be8e71aa 643#define DEBUG_TRIE_COMPILE_r(x) DEBUG_r( \
e68ec53f 644 if (re_debug_flags & RE_DEBUG_COMPILE_TRIE) x )
be8e71aa 645
646/* Execute */
647#define DEBUG_EXECUTE_r(x) DEBUG_r( \
e68ec53f 648 if (re_debug_flags & RE_DEBUG_EXECUTE_MASK) x )
be8e71aa 649#define DEBUG_INTUIT_r(x) DEBUG_r( \
e68ec53f 650 if (re_debug_flags & RE_DEBUG_EXECUTE_INTUIT) x )
be8e71aa 651#define DEBUG_MATCH_r(x) DEBUG_r( \
e68ec53f 652 if (re_debug_flags & RE_DEBUG_EXECUTE_MATCH) x )
be8e71aa 653#define DEBUG_TRIE_EXECUTE_r(x) DEBUG_r( \
e68ec53f 654 if (re_debug_flags & RE_DEBUG_EXECUTE_TRIE) x )
be8e71aa 655
656/* Extra */
657#define DEBUG_EXTRA_r(x) DEBUG_r( \
e68ec53f 658 if (re_debug_flags & RE_DEBUG_EXTRA_MASK) x )
a5ca303d 659#define DEBUG_OFFSETS_r(x) DEBUG_r( \
660 if (re_debug_flags & RE_DEBUG_EXTRA_OFFSETS) x )
ab3bbdeb 661#define DEBUG_STATE_r(x) DEBUG_r( \
662 if (re_debug_flags & RE_DEBUG_EXTRA_STATE) x )
24b23f37 663#define DEBUG_STACK_r(x) DEBUG_r( \
664 if (re_debug_flags & RE_DEBUG_EXTRA_STACK) x )
a5ca303d 665#define DEBUG_OPTIMISE_MORE_r(x) DEBUG_r( \
666 if ((RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE) == \
667 (re_debug_flags & (RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE)) ) x )
07be1b83 668#define MJD_OFFSET_DEBUG(x) DEBUG_r( \
a5ca303d 669 if (re_debug_flags & RE_DEBUG_EXTRA_OFFDEBUG) \
be8e71aa 670 Perl_warn_nocontext x )
671#define DEBUG_TRIE_COMPILE_MORE_r(x) DEBUG_TRIE_COMPILE_r( \
e68ec53f 672 if (re_debug_flags & RE_DEBUG_EXTRA_TRIE) x )
be8e71aa 673#define DEBUG_TRIE_EXECUTE_MORE_r(x) DEBUG_TRIE_EXECUTE_r( \
e68ec53f 674 if (re_debug_flags & RE_DEBUG_EXTRA_TRIE) x )
be8e71aa 675
676#define DEBUG_TRIE_r(x) DEBUG_r( \
e68ec53f 677 if (re_debug_flags & (RE_DEBUG_COMPILE_TRIE \
be8e71aa 678 | RE_DEBUG_EXECUTE_TRIE )) x )
679
680/* initialization */
681/* get_sv() can return NULL during global destruction. */
e68ec53f 682#define GET_RE_DEBUG_FLAGS DEBUG_r({ \
683 SV * re_debug_flags_sv = NULL; \
684 re_debug_flags_sv = get_sv(RE_DEBUG_FLAGS, 1); \
685 if (re_debug_flags_sv) { \
8e9a8a48 686 if (!SvIOK(re_debug_flags_sv)) \
e68ec53f 687 sv_setuv(re_debug_flags_sv, RE_DEBUG_COMPILE_DUMP | RE_DEBUG_EXECUTE_MASK ); \
8e9a8a48 688 re_debug_flags=SvIV(re_debug_flags_sv); \
e68ec53f 689 }\
690})
a3621e74 691
692#ifdef DEBUGGING
ab3bbdeb 693
8e9a8a48 694#define GET_RE_DEBUG_FLAGS_DECL IV re_debug_flags = 0; GET_RE_DEBUG_FLAGS;
ab3bbdeb 695
696#define RE_PV_COLOR_DECL(rpv,rlen,isuni,dsv,pv,l,m,c1,c2) \
697 const char * const rpv = \
ddc5bc0f 698 pv_pretty((dsv), (pv), (l), (m), \
ab3bbdeb 699 PL_colors[(c1)],PL_colors[(c2)], \
700 ((isuni) ? PERL_PV_ESCAPE_UNI : 0) ); \
0df25f3d 701 const int rlen = SvCUR(dsv)
ab3bbdeb 702
703#define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m) \
704 const char * const rpv = \
ddc5bc0f 705 pv_pretty((dsv), (SvPV_nolen_const(sv)), (SvCUR(sv)), (m), \
ab3bbdeb 706 PL_colors[(c1)],PL_colors[(c2)], \
707 ((isuni) ? PERL_PV_ESCAPE_UNI : 0) )
708
709#define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m) \
710 const char * const rpv = \
ddc5bc0f 711 pv_pretty((dsv), (pv), (l), (m), \
ab3bbdeb 712 PL_colors[0], PL_colors[1], \
713 ( PERL_PV_PRETTY_QUOTE | PERL_PV_PRETTY_ELIPSES | \
714 ((isuni) ? PERL_PV_ESCAPE_UNI : 0)) \
715 )
716
717#define RE_SV_DUMPLEN(ItEm) (SvCUR(ItEm) - (SvTAIL(ItEm)!=0))
718#define RE_SV_TAIL(ItEm) (SvTAIL(ItEm) ? "$" : "")
719
720#else /* if not DEBUGGING */
721
a3621e74 722#define GET_RE_DEBUG_FLAGS_DECL
ab3bbdeb 723#define RE_PV_COLOR_DECL(rpv,rlen,isuni,dsv,pv,l,m,c1,c2)
724#define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m)
725#define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m)
726#define RE_SV_DUMPLEN(ItEm)
727#define RE_SV_TAIL(ItEm)
728
729#endif /* DEBUG RELATED DEFINES */
a3621e74 730