This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [perl #27053] /^/m doesn't match after a newline at the end of the string
[perl5.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
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 *
a687059c
LW
9 */
10
c277df42
IZ
11typedef OP OP_4tree; /* Will be redefined later. */
12
07be1b83
YO
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
YO
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
LW
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
LW
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
LW
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
LW
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
LW
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
YO
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
LW
70 */
71
a687059c
LW
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
LW
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
IZ
90 U8 type;
91 U16 next_off;
cd439c50 92 char string[1];
c277df42 93};
a687059c 94
6bda09f9
YO
95/* Argument bearing node - workhorse,
96 arg1 is often for the data field */
c277df42
IZ
97struct regnode_1 {
98 U8 flags;
99 U8 type;
100 U16 next_off;
101 U32 arg1;
102};
103
6bda09f9
YO
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
IZ
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
IZ
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
IZ
133};
134
19103b91
JH
135struct regnode_charclass_class { /* has [[:blah:]] classes */
136 U8 flags; /* should have ANYOF_CLASS here */
936ed897
IZ
137 U8 type;
138 U16 next_off;
ffc61ed2 139 U32 arg1;
19103b91
JH
140 char bitmap[ANYOF_BITMAP_SIZE]; /* both compile-time */
141 char classflags[ANYOF_CLASSBITMAP_SIZE]; /* and run-time */
936ed897
IZ
142};
143
9c7e81e8
AD
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
IZ
163# define REG_INFTY I16_MAX
164#endif
a687059c 165
e91177ed
IZ
166#define ARG_VALUE(arg) (arg)
167#define ARG__SET(arg,val) ((arg) = (val))
c277df42 168
e8e3b0fb
JH
169#undef ARG
170#undef ARG1
171#undef ARG2
172
c277df42
IZ
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
IZ
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
JH
183#undef NEXT_OFF
184#undef NODE_ALIGN
185
075abff3
AL
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
IZ
190#define SIZE_ALIGN NODE_ALIGN
191
e8e3b0fb
JH
192#undef OP
193#undef OPERAND
194#undef MASK
195#undef STRING
196
e91177ed
IZ
197#define OP(p) ((p)->type)
198#define OPERAND(p) (((struct regnode_string *)p)->string)
cd439c50
IZ
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
JH
205#undef NODE_ALIGN
206#undef ARG_LOC
207#undef NEXTOPER
208#undef PREVOPER
209
e91177ed
IZ
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
IZ
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
IZ
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
JH
236#define ANYOF_INVERT 0x04
237#define ANYOF_FOLD 0x02
238#define ANYOF_LOCALE 0x01
b8c5462f 239
653099ff 240/* Used for regstclass only */
ffc61ed2
JH
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
HS
247/* size of node is large (includes class pointer) */
248#define ANYOF_LARGE 0x80
249
ffc61ed2
JH
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
GS
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
JH
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
JH
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
JF
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
JH
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
IZ
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
JH
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
JH
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
IZ
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
JH
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
NC
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
IZ
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
LW
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
IZ
344#define EXTRA_SIZE(guy) ((sizeof(guy)-1)/sizeof(struct regnode))
345
3dab1dad
YO
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
GS
356START_EXTERN_C
357
be8e71aa
YO
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
GS
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
JH
381 ANYOF,
382 ALNUM, ALNUML,
383 NALNUM, NALNUML,
384 SPACE, SPACEL,
385 NSPACE, NSPACEL,
386 DIGIT, NDIGIT,
387 0
c277df42
IZ
388};
389#endif
390
f9f4320a
YO
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,
de8c5301 401 Perl_reg_stringify,
f9f4320a
YO
402#if defined(USE_ITHREADS)
403 Perl_regdupe
404#endif
405};
406#endif /* DOINIT */
407#endif /* PLUGGABLE_RE_EXTENSION */
408
409
73c4f7a1 410END_EXTERN_C
cad2e5aa 411
cad2e5aa 412
261faec3
MJD
413/* .what is a character array with one character for each member of .data
414 * The character describes the function of the corresponding .data item:
0111c4fd 415 * f - start-class data for regstclass optimization
261faec3
MJD
416 * n - Root of op tree for (?{EVAL}) item
417 * o - Start op for (?{EVAL}) item
3dab1dad 418 * p - Pad for (?{EVAL}) item
9e55ce06
JH
419 * s - swash for unicode-style character class, and the multicharacter
420 * strings resulting from casefolding the single-character entries
421 * in the character class
a3621e74 422 * t - trie struct
07be1b83 423 * T - aho-trie struct
81714fb9 424 * S - sv for named capture lookup
261faec3 425 * 20010712 mjd@plover.com
c3613ed7 426 * (Remember to update re_dup() and pregfree() if you add any items.)
261faec3 427 */
cad2e5aa
JH
428struct reg_data {
429 U32 count;
430 U8 *what;
431 void* data[1];
432};
433
434struct reg_substr_datum {
435 I32 min_offset;
436 I32 max_offset;
33b8afdf
JH
437 SV *substr; /* non-utf8 variant */
438 SV *utf8_substr; /* utf8 variant */
1de06328 439 I32 end_shift;
cad2e5aa
JH
440};
441
442struct reg_substr_data {
443 struct reg_substr_datum data[3]; /* Actual array */
444};
445
446#define anchored_substr substrs->data[0].substr
33b8afdf 447#define anchored_utf8 substrs->data[0].utf8_substr
cad2e5aa 448#define anchored_offset substrs->data[0].min_offset
1de06328
YO
449#define anchored_end_shift substrs->data[0].end_shift
450
cad2e5aa 451#define float_substr substrs->data[1].substr
33b8afdf 452#define float_utf8 substrs->data[1].utf8_substr
cad2e5aa
JH
453#define float_min_offset substrs->data[1].min_offset
454#define float_max_offset substrs->data[1].max_offset
1de06328
YO
455#define float_end_shift substrs->data[1].end_shift
456
cad2e5aa 457#define check_substr substrs->data[2].substr
33b8afdf 458#define check_utf8 substrs->data[2].utf8_substr
cad2e5aa
JH
459#define check_offset_min substrs->data[2].min_offset
460#define check_offset_max substrs->data[2].max_offset
1de06328 461#define check_end_shift substrs->data[2].end_shift
a3621e74
YO
462
463
464
465/* trie related stuff */
5d9a96ca 466
a3621e74
YO
467/* a transition record for the state machine. the
468 check field determines which state "owns" the
469 transition. the char the transition is for is
470 determined by offset from the owning states base
471 field. the next field determines which state
472 is to be transitioned to if any.
473*/
474struct _reg_trie_trans {
475 U32 next;
476 U32 check;
477};
478
479/* a transition list element for the list based representation */
480struct _reg_trie_trans_list_elem {
481 U16 forid;
482 U32 newstate;
483};
484typedef struct _reg_trie_trans_list_elem reg_trie_trans_le;
485
486/* a state for compressed nodes. base is an offset
487 into an array of reg_trie_trans array. If wordnum is
488 nonzero the state is accepting. if base is zero then
489 the state has no children (and will be accepting)
490*/
491struct _reg_trie_state {
492 U16 wordnum;
493 union {
494 U32 base;
495 reg_trie_trans_le* list;
496 } trans;
497};
498
499
500
a3621e74
YO
501typedef struct _reg_trie_state reg_trie_state;
502typedef struct _reg_trie_trans reg_trie_trans;
503
504
505/* anything in here that needs to be freed later
8e11feef 506 should be dealt with in pregfree */
a3621e74 507struct _reg_trie_data {
786e8c11
YO
508 U16 uniquecharcount; /* unique chars in trie (width of trans table) */
509 U32 lasttrans; /* last valid transition element */
510 U16 *charmap; /* byte to charid lookup array */
511 HV *widecharmap; /* code points > 255 to charid */
512 reg_trie_state *states; /* state data */
513 reg_trie_trans *trans; /* array of transition elements */
514 char *bitmap; /* stclass bitmap */
515 U32 refcount; /* number of times this trie is referenced */
516 U32 startstate; /* initial state - used for common prefix optimisation */
517 STRLEN minlen; /* minimum length of words in trie - build/opt only? */
518 STRLEN maxlen; /* maximum length of words in trie - build/opt only? */
519 U32 *wordlen; /* array of lengths of words */
520 U16 *jump; /* optional 1 indexed array of offsets before tail
521 for the node following a given word. */
522 U16 *nextword; /* optional 1 indexed array to support linked list
523 of duplicate wordnums */
1e2e3d02
YO
524 U32 statecount; /* Build only - number of states in the states array
525 (including the unused zero state) */
786e8c11 526 U32 wordcount; /* Build only */
a3621e74 527#ifdef DEBUGGING
786e8c11
YO
528 STRLEN charcount; /* Build only */
529 AV *words; /* Array of words contained in trie, for dumping */
530 AV *revcharmap; /* Map of each charid back to its character representation */
a3621e74
YO
531#endif
532};
a3621e74
YO
533typedef struct _reg_trie_data reg_trie_data;
534
07be1b83
YO
535struct _reg_ac_data {
536 U32 *fail;
537 reg_trie_state *states;
538 reg_trie_data *trie;
539 U32 refcount;
540};
541typedef struct _reg_ac_data reg_ac_data;
542
3dab1dad
YO
543/* ANY_BIT doesnt use the structure, so we can borrow it here.
544 This is simpler than refactoring all of it as wed end up with
545 three different sets... */
546
547#define TRIE_BITMAP(p) (((reg_trie_data *)(p))->bitmap)
786e8c11 548#define TRIE_BITMAP_BYTE(p, c) (TRIE_BITMAP(p)[(((U8)(c)) >> 3) & 31])
5dfe062f
YO
549#define TRIE_BITMAP_SET(p, c) (TRIE_BITMAP_BYTE(p, c) |= ANYOF_BIT((U8)c))
550#define TRIE_BITMAP_CLEAR(p,c) (TRIE_BITMAP_BYTE(p, c) &= ~ANYOF_BIT((U8)c))
551#define TRIE_BITMAP_TEST(p, c) (TRIE_BITMAP_BYTE(p, c) & ANYOF_BIT((U8)c))
3dab1dad 552
1de06328
YO
553#define IS_ANYOF_TRIE(op) ((op)==TRIEC || (op)==AHOCORASICKC)
554#define IS_TRIE_AC(op) ((op)>=AHOCORASICK)
555
556
786e8c11
YO
557#define BITMAP_BYTE(p, c) (((U8*)p)[(((U8)(c)) >> 3) & 31])
558#define BITMAP_TEST(p, c) (BITMAP_BYTE(p, c) & ANYOF_BIT((U8)c))
3dab1dad 559
a3621e74
YO
560/* these defines assume uniquecharcount is the correct variable, and state may be evaluated twice */
561#define TRIE_NODENUM(state) (((state)-1)/(trie->uniquecharcount)+1)
562#define SAFE_TRIE_NODENUM(state) ((state) ? (((state)-1)/(trie->uniquecharcount)+1) : (state))
563#define TRIE_NODEIDX(state) ((state) ? (((state)-1)*(trie->uniquecharcount)+1) : (state))
564
3dab1dad 565#ifdef DEBUGGING
3dab1dad 566#define TRIE_CHARCOUNT(trie) ((trie)->charcount)
3dab1dad
YO
567#define TRIE_REVCHARMAP(trie) ((trie)->revcharmap)
568#else
3dab1dad 569#define TRIE_CHARCOUNT(trie) (trie_charcount)
3dab1dad
YO
570#define TRIE_REVCHARMAP(trie) (trie_revcharmap)
571#endif
a3621e74 572
0111c4fd
RGS
573#define RE_TRIE_MAXBUF_INIT 65536
574#define RE_TRIE_MAXBUF_NAME "\022E_TRIE_MAXBUF"
a3621e74
YO
575#define RE_DEBUG_FLAGS "\022E_DEBUG_FLAGS"
576
be8e71aa
YO
577/*
578
579RE_DEBUG_FLAGS is used to control what debug output is emitted
580its divided into three groups of options, some of which interact.
581The three groups are: Compile, Execute, Extra. There is room for a
582further group, as currently only the low three bytes are used.
583
584 Compile Options:
07be1b83 585
be8e71aa
YO
586 PARSE
587 PEEP
588 TRIE
589 PROGRAM
590 OFFSETS
a3621e74 591
be8e71aa 592 Execute Options:
a3621e74 593
be8e71aa
YO
594 INTUIT
595 MATCH
596 TRIE
a3621e74 597
be8e71aa 598 Extra Options
a3621e74 599
be8e71aa
YO
600 TRIE
601 OFFSETS
602
603If you modify any of these make sure you make corresponding changes to
604re.pm, especially to the documentation.
a3621e74 605
be8e71aa
YO
606*/
607
608
609/* Compile */
610#define RE_DEBUG_COMPILE_MASK 0x0000FF
611#define RE_DEBUG_COMPILE_PARSE 0x000001
612#define RE_DEBUG_COMPILE_OPTIMISE 0x000002
613#define RE_DEBUG_COMPILE_TRIE 0x000004
614#define RE_DEBUG_COMPILE_DUMP 0x000008
be8e71aa
YO
615
616/* Execute */
617#define RE_DEBUG_EXECUTE_MASK 0x00FF00
618#define RE_DEBUG_EXECUTE_INTUIT 0x000100
619#define RE_DEBUG_EXECUTE_MATCH 0x000200
620#define RE_DEBUG_EXECUTE_TRIE 0x000400
621
622/* Extra */
623#define RE_DEBUG_EXTRA_MASK 0xFF0000
624#define RE_DEBUG_EXTRA_TRIE 0x010000
625#define RE_DEBUG_EXTRA_OFFSETS 0x020000
a5ca303d
YO
626#define RE_DEBUG_EXTRA_OFFDEBUG 0x040000
627#define RE_DEBUG_EXTRA_STATE 0x080000
628#define RE_DEBUG_EXTRA_OPTIMISE 0x100000
24b23f37
YO
629/* combined */
630#define RE_DEBUG_EXTRA_STACK 0x280000
be8e71aa 631
e68ec53f 632#define RE_DEBUG_FLAG(x) (re_debug_flags & x)
be8e71aa
YO
633/* Compile */
634#define DEBUG_COMPILE_r(x) DEBUG_r( \
e68ec53f 635 if (re_debug_flags & RE_DEBUG_COMPILE_MASK) x )
be8e71aa 636#define DEBUG_PARSE_r(x) DEBUG_r( \
e68ec53f 637 if (re_debug_flags & RE_DEBUG_COMPILE_PARSE) x )
be8e71aa 638#define DEBUG_OPTIMISE_r(x) DEBUG_r( \
e68ec53f 639 if (re_debug_flags & RE_DEBUG_COMPILE_OPTIMISE) x )
be8e71aa 640#define DEBUG_PARSE_r(x) DEBUG_r( \
e68ec53f 641 if (re_debug_flags & RE_DEBUG_COMPILE_PARSE) x )
be8e71aa 642#define DEBUG_DUMP_r(x) DEBUG_r( \
e68ec53f 643 if (re_debug_flags & RE_DEBUG_COMPILE_DUMP) x )
be8e71aa 644#define DEBUG_TRIE_COMPILE_r(x) DEBUG_r( \
e68ec53f 645 if (re_debug_flags & RE_DEBUG_COMPILE_TRIE) x )
be8e71aa
YO
646
647/* Execute */
648#define DEBUG_EXECUTE_r(x) DEBUG_r( \
e68ec53f 649 if (re_debug_flags & RE_DEBUG_EXECUTE_MASK) x )
be8e71aa 650#define DEBUG_INTUIT_r(x) DEBUG_r( \
e68ec53f 651 if (re_debug_flags & RE_DEBUG_EXECUTE_INTUIT) x )
be8e71aa 652#define DEBUG_MATCH_r(x) DEBUG_r( \
e68ec53f 653 if (re_debug_flags & RE_DEBUG_EXECUTE_MATCH) x )
be8e71aa 654#define DEBUG_TRIE_EXECUTE_r(x) DEBUG_r( \
e68ec53f 655 if (re_debug_flags & RE_DEBUG_EXECUTE_TRIE) x )
be8e71aa
YO
656
657/* Extra */
658#define DEBUG_EXTRA_r(x) DEBUG_r( \
e68ec53f 659 if (re_debug_flags & RE_DEBUG_EXTRA_MASK) x )
a5ca303d
YO
660#define DEBUG_OFFSETS_r(x) DEBUG_r( \
661 if (re_debug_flags & RE_DEBUG_EXTRA_OFFSETS) x )
ab3bbdeb
YO
662#define DEBUG_STATE_r(x) DEBUG_r( \
663 if (re_debug_flags & RE_DEBUG_EXTRA_STATE) x )
24b23f37
YO
664#define DEBUG_STACK_r(x) DEBUG_r( \
665 if (re_debug_flags & RE_DEBUG_EXTRA_STACK) x )
a5ca303d
YO
666#define DEBUG_OPTIMISE_MORE_r(x) DEBUG_r( \
667 if ((RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE) == \
668 (re_debug_flags & (RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE)) ) x )
07be1b83 669#define MJD_OFFSET_DEBUG(x) DEBUG_r( \
a5ca303d 670 if (re_debug_flags & RE_DEBUG_EXTRA_OFFDEBUG) \
be8e71aa
YO
671 Perl_warn_nocontext x )
672#define DEBUG_TRIE_COMPILE_MORE_r(x) DEBUG_TRIE_COMPILE_r( \
e68ec53f 673 if (re_debug_flags & RE_DEBUG_EXTRA_TRIE) x )
be8e71aa 674#define DEBUG_TRIE_EXECUTE_MORE_r(x) DEBUG_TRIE_EXECUTE_r( \
e68ec53f 675 if (re_debug_flags & RE_DEBUG_EXTRA_TRIE) x )
be8e71aa
YO
676
677#define DEBUG_TRIE_r(x) DEBUG_r( \
e68ec53f 678 if (re_debug_flags & (RE_DEBUG_COMPILE_TRIE \
be8e71aa
YO
679 | RE_DEBUG_EXECUTE_TRIE )) x )
680
681/* initialization */
682/* get_sv() can return NULL during global destruction. */
e68ec53f
YO
683#define GET_RE_DEBUG_FLAGS DEBUG_r({ \
684 SV * re_debug_flags_sv = NULL; \
685 re_debug_flags_sv = get_sv(RE_DEBUG_FLAGS, 1); \
686 if (re_debug_flags_sv) { \
8e9a8a48 687 if (!SvIOK(re_debug_flags_sv)) \
e68ec53f 688 sv_setuv(re_debug_flags_sv, RE_DEBUG_COMPILE_DUMP | RE_DEBUG_EXECUTE_MASK ); \
8e9a8a48 689 re_debug_flags=SvIV(re_debug_flags_sv); \
e68ec53f
YO
690 }\
691})
a3621e74
YO
692
693#ifdef DEBUGGING
ab3bbdeb 694
8e9a8a48 695#define GET_RE_DEBUG_FLAGS_DECL IV re_debug_flags = 0; GET_RE_DEBUG_FLAGS;
ab3bbdeb
YO
696
697#define RE_PV_COLOR_DECL(rpv,rlen,isuni,dsv,pv,l,m,c1,c2) \
698 const char * const rpv = \
ddc5bc0f 699 pv_pretty((dsv), (pv), (l), (m), \
ab3bbdeb
YO
700 PL_colors[(c1)],PL_colors[(c2)], \
701 ((isuni) ? PERL_PV_ESCAPE_UNI : 0) ); \
0df25f3d 702 const int rlen = SvCUR(dsv)
ab3bbdeb
YO
703
704#define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m) \
705 const char * const rpv = \
ddc5bc0f 706 pv_pretty((dsv), (SvPV_nolen_const(sv)), (SvCUR(sv)), (m), \
ab3bbdeb
YO
707 PL_colors[(c1)],PL_colors[(c2)], \
708 ((isuni) ? PERL_PV_ESCAPE_UNI : 0) )
709
710#define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m) \
711 const char * const rpv = \
ddc5bc0f 712 pv_pretty((dsv), (pv), (l), (m), \
ab3bbdeb
YO
713 PL_colors[0], PL_colors[1], \
714 ( PERL_PV_PRETTY_QUOTE | PERL_PV_PRETTY_ELIPSES | \
715 ((isuni) ? PERL_PV_ESCAPE_UNI : 0)) \
716 )
717
718#define RE_SV_DUMPLEN(ItEm) (SvCUR(ItEm) - (SvTAIL(ItEm)!=0))
719#define RE_SV_TAIL(ItEm) (SvTAIL(ItEm) ? "$" : "")
720
721#else /* if not DEBUGGING */
722
a3621e74 723#define GET_RE_DEBUG_FLAGS_DECL
ab3bbdeb
YO
724#define RE_PV_COLOR_DECL(rpv,rlen,isuni,dsv,pv,l,m,c1,c2)
725#define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m)
726#define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m)
727#define RE_SV_DUMPLEN(ItEm)
728#define RE_SV_TAIL(ItEm)
729
730#endif /* DEBUG RELATED DEFINES */
a3621e74 731