This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
say() has the same syntax than print(), so forbid to override it
[perl5.git] / regcomp.h
1 /*    regcomp.h
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4  *    2000, 2001, 2002, 2003, 2005 by Larry Wall and others
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  *
9  */
10
11 typedef OP OP_4tree;                    /* Will be redefined later. */
12
13
14 #define PERL_ENABLE_TRIE_OPTIMISATION 1
15 #define PERL_ENABLE_EXTENDED_TRIE_OPTIMISATION 1
16 #define PERL_ENABLE_POSITIVE_ASSERTION_STUDY 1
17 #define PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS 0
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 */
24
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  *
30  * regstart     sv that must begin a match; NULL if none obvious
31  * reganch      is the match anchored (at beginning-of-line only)?
32  * regmust      string (pointer into program) that match must include, or NULL
33  *  [regmust changed to SV* for bminstr()--law]
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
40  * that pregcomp() supplies a regmust only if the r.e. contains something
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
43  * supplied because the test in pregexec() needs it and pregcomp() is computing
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
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.
70  */
71
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  *
82  * [The "next" pointer is always aligned on an even
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
88 struct regnode_string {
89     U8  str_len;
90     U8  type;
91     U16 next_off;
92     char string[1];
93 };
94
95 /* Argument bearing node - workhorse, 
96    arg1 is often for the data field */
97 struct regnode_1 {
98     U8  flags;
99     U8  type;
100     U16 next_off;
101     U32 arg1;
102 };
103
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 */
114 struct regnode_2 {
115     U8  flags;
116     U8  type;
117     U16 next_off;
118     U16 arg1;
119     U16 arg2;
120 };
121
122
123 #define ANYOF_BITMAP_SIZE       32      /* 256 b/(8 b/B) */
124 #define ANYOF_CLASSBITMAP_SIZE   4      /* up to 32 (8*4) named classes */
125
126 /* also used by trie */
127 struct regnode_charclass {
128     U8  flags;
129     U8  type;
130     U16 next_off;
131     U32 arg1;
132     char bitmap[ANYOF_BITMAP_SIZE];     /* only compile-time */
133 };
134
135 struct regnode_charclass_class {        /* has [[:blah:]] classes */
136     U8  flags;                          /* should have ANYOF_CLASS here */
137     U8  type;
138     U16 next_off;
139     U32 arg1;
140     char bitmap[ANYOF_BITMAP_SIZE];             /* both compile-time */
141     char classflags[ANYOF_CLASSBITMAP_SIZE];    /* and run-time */
142 };
143
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
163 #  define REG_INFTY I16_MAX
164 #endif
165
166 #define ARG_VALUE(arg) (arg)
167 #define ARG__SET(arg,val) ((arg) = (val))
168
169 #undef ARG
170 #undef ARG1
171 #undef ARG2
172
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))
176 #define ARG2L(p) ARG_VALUE(ARG2L_LOC(p))
177
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))
181 #define ARG2L_SET(p, val) ARG__SET(ARG2L_LOC(p), (val))
182
183 #undef NEXT_OFF
184 #undef NODE_ALIGN
185
186 #define NEXT_OFF(p) ((p)->next_off)
187 #define NODE_ALIGN(node)
188 #define NODE_ALIGN_FILL(node) ((node)->flags = 0xde) /* deadbeef */
189
190 #define SIZE_ALIGN NODE_ALIGN
191
192 #undef OP
193 #undef OPERAND
194 #undef MASK
195 #undef STRING
196
197 #define OP(p)           ((p)->type)
198 #define OPERAND(p)      (((struct regnode_string *)p)->string)
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
205 #undef NODE_ALIGN
206 #undef ARG_LOC
207 #undef NEXTOPER
208 #undef PREVOPER
209
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)
214 #define ARG2L_LOC(p)    (((struct regnode_2L *)p)->arg2)
215
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
220
221 #define NEXTOPER(p)     ((p) + NODE_STEP_REGNODE)
222 #define PREVOPER(p)     ((p) - NODE_STEP_REGNODE)
223
224 #define FILL_ADVANCE_NODE(ptr, op) STMT_START { \
225     (ptr)->type = op;    (ptr)->next_off = 0;   (ptr)++; } STMT_END
226 #define FILL_ADVANCE_NODE_ARG(ptr, op, arg) STMT_START { \
227     ARG_SET(ptr, arg);  FILL_ADVANCE_NODE(ptr, op); (ptr) += 1; } STMT_END
228
229 #define REG_MAGIC 0234
230
231 #define SIZE_ONLY (RExC_emit == &PL_regdummy)
232
233 /* Flags for node->flags of ANYOF */
234
235 #define ANYOF_CLASS             0x08    /* has [[:blah:]] classes */
236 #define ANYOF_INVERT            0x04
237 #define ANYOF_FOLD              0x02
238 #define ANYOF_LOCALE            0x01
239
240 /* Used for regstclass only */
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
245 #define ANYOF_UNICODE_ALL       0x40    /* Can match any char past 0xff */
246
247 /* size of node is large (includes class pointer) */
248 #define ANYOF_LARGE             0x80
249
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
254
255 /* Character classes for node->classflags of ANYOF */
256 /* Should be synchronized with a table in regprop() */
257 /* 2n should pair with 2n+1 */
258
259 #define ANYOF_ALNUM      0      /* \w, PL_utf8_alnum, utf8::IsWord, ALNUM */
260 #define ANYOF_NALNUM     1
261 #define ANYOF_SPACE      2      /* \s */
262 #define ANYOF_NSPACE     3
263 #define ANYOF_DIGIT      4
264 #define ANYOF_NDIGIT     5
265 #define ANYOF_ALNUMC     6      /* isalnum(3), utf8::IsAlnum, ALNUMC */
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
285 #define ANYOF_PSXSPC    26      /* POSIX space: \s plus the vertical tab */
286 #define ANYOF_NPSXSPC   27
287 #define ANYOF_BLANK     28      /* GNU extension: space and tab: non-vertical space */
288 #define ANYOF_NBLANK    29
289
290 #define ANYOF_MAX       32
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
301 #define ANYOF_SIZE              (sizeof(struct regnode_charclass))
302 #define ANYOF_CLASS_SIZE        (sizeof(struct regnode_charclass_class))
303
304 #define ANYOF_FLAGS(p)          ((p)->flags)
305
306 #define ANYOF_BIT(c)            (1 << ((c) & 7))
307
308 #define ANYOF_CLASS_BYTE(p, c)  (((struct regnode_charclass_class*)(p))->classflags[((c) >> 3) & 3])
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
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)
317 #define ANYOF_BITMAP_BYTE(p, c) (ANYOF_BITMAP(p)[(((U8)(c)) >> 3) & 31])
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
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
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)
333
334
335 /*
336  * Utility definitions.
337  */
338 #ifndef CHARMASK
339 #  define UCHARAT(p)    ((int)*(const U8*)(p))
340 #else
341 #  define UCHARAT(p)    ((int)*(p)&CHARMASK)
342 #endif
343
344 #define EXTRA_SIZE(guy) ((sizeof(guy)-1)/sizeof(struct regnode))
345
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
351 #define REG_SEEN_SANY           REG_SEEN_CANY /* src bckwrd cmpt */
352 #define REG_SEEN_RECURSE        0x00000020
353 #define REG_TOP_LEVEL_BRANCHES  0x00000040
354
355 START_EXTERN_C
356
357 #ifdef PLUGGABLE_RE_EXTENSION
358 #include "re_nodes.h"
359 #else
360 #include "regnodes.h"
361 #endif
362
363 /* The following have no fixed length. U8 so we can do strchr() on it. */
364 #ifndef DOINIT
365 EXTCONST U8 PL_varies[];
366 #else
367 EXTCONST U8 PL_varies[] = {
368     BRANCH, BACK, STAR, PLUS, CURLY, CURLYX, REF, REFF, REFFL,
369     WHILEM, CURLYM, CURLYN, BRANCHJ, IFTHEN, SUSPEND, CLUMP, 0
370 };
371 #endif
372
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".) */
375 #ifndef DOINIT
376 EXTCONST U8 PL_simple[];
377 #else
378 EXTCONST U8 PL_simple[] = {
379     REG_ANY,    SANY,   CANY,
380     ANYOF,
381     ALNUM,      ALNUML,
382     NALNUM,     NALNUML,
383     SPACE,      SPACEL,
384     NSPACE,     NSPACEL,
385     DIGIT,      NDIGIT,
386     0
387 };
388 #endif
389
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
408 END_EXTERN_C
409
410
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:
413  *   f - start-class data for regstclass optimization
414  *   n - Root of op tree for (?{EVAL}) item
415  *   o - Start op for (?{EVAL}) item
416  *   p - Pad for (?{EVAL}) item
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
420  *   t - trie struct
421  *   T - aho-trie struct
422  *   S - sv for named capture lookup
423  * 20010712 mjd@plover.com
424  * (Remember to update re_dup() and pregfree() if you add any items.)
425  */
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;
435     SV *substr;         /* non-utf8 variant */
436     SV *utf8_substr;    /* utf8 variant */
437     I32 end_shift;
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
445 #define anchored_utf8 substrs->data[0].utf8_substr
446 #define anchored_offset substrs->data[0].min_offset
447 #define anchored_end_shift substrs->data[0].end_shift
448
449 #define float_substr substrs->data[1].substr
450 #define float_utf8 substrs->data[1].utf8_substr
451 #define float_min_offset substrs->data[1].min_offset
452 #define float_max_offset substrs->data[1].max_offset
453 #define float_end_shift substrs->data[1].end_shift
454
455 #define check_substr substrs->data[2].substr
456 #define check_utf8 substrs->data[2].utf8_substr
457 #define check_offset_min substrs->data[2].min_offset
458 #define check_offset_max substrs->data[2].max_offset
459 #define check_end_shift substrs->data[2].end_shift
460
461
462
463 /* trie related stuff */
464
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
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
504    should be dealt with in pregfree */
505 struct _reg_trie_data {
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             statecount;      /* Build only - number of states in the states array 
523                                         (including the unused zero state) */
524     U32             wordcount;       /* Build only */
525 #ifdef DEBUGGING
526     STRLEN          charcount;       /* Build only */
527     AV              *words;          /* Array of words contained in trie, for dumping */
528     AV              *revcharmap;     /* Map of each charid back to its character representation */
529 #endif
530 };
531 typedef struct _reg_trie_data reg_trie_data;
532
533 struct _reg_ac_data {
534     U32              *fail;
535     reg_trie_state   *states;
536     reg_trie_data    *trie;
537     U32              refcount;
538 };
539 typedef struct _reg_ac_data reg_ac_data;
540
541 /* ANY_BIT doesnt use the structure, so we can borrow it here.
542    This is simpler than refactoring all of it as wed end up with
543    three different sets... */
544
545 #define TRIE_BITMAP(p)          (((reg_trie_data *)(p))->bitmap)
546 #define TRIE_BITMAP_BYTE(p, c)  (TRIE_BITMAP(p)[(((U8)(c)) >> 3) & 31])
547 #define TRIE_BITMAP_SET(p, c)   (TRIE_BITMAP_BYTE(p, c) |=  ANYOF_BIT((U8)c))
548 #define TRIE_BITMAP_CLEAR(p,c)  (TRIE_BITMAP_BYTE(p, c) &= ~ANYOF_BIT((U8)c))
549 #define TRIE_BITMAP_TEST(p, c)  (TRIE_BITMAP_BYTE(p, c) &   ANYOF_BIT((U8)c))
550
551 #define IS_ANYOF_TRIE(op) ((op)==TRIEC || (op)==AHOCORASICKC)
552 #define IS_TRIE_AC(op) ((op)>=AHOCORASICK)
553
554
555 #define BITMAP_BYTE(p, c)       (((U8*)p)[(((U8)(c)) >> 3) & 31])
556 #define BITMAP_TEST(p, c)       (BITMAP_BYTE(p, c) &   ANYOF_BIT((U8)c))
557
558 /* these defines assume uniquecharcount is the correct variable, and state may be evaluated twice */
559 #define TRIE_NODENUM(state) (((state)-1)/(trie->uniquecharcount)+1)
560 #define SAFE_TRIE_NODENUM(state) ((state) ? (((state)-1)/(trie->uniquecharcount)+1) : (state))
561 #define TRIE_NODEIDX(state) ((state) ? (((state)-1)*(trie->uniquecharcount)+1) : (state))
562
563 #ifdef DEBUGGING
564 #define TRIE_CHARCOUNT(trie) ((trie)->charcount)
565 #define TRIE_REVCHARMAP(trie) ((trie)->revcharmap)
566 #else
567 #define TRIE_CHARCOUNT(trie) (trie_charcount)
568 #define TRIE_REVCHARMAP(trie) (trie_revcharmap)
569 #endif
570
571 #define RE_TRIE_MAXBUF_INIT 65536
572 #define RE_TRIE_MAXBUF_NAME "\022E_TRIE_MAXBUF"
573 #define RE_DEBUG_FLAGS "\022E_DEBUG_FLAGS"
574
575 /*
576
577 RE_DEBUG_FLAGS is used to control what debug output is emitted
578 its divided into three groups of options, some of which interact.
579 The three groups are: Compile, Execute, Extra. There is room for a
580 further group, as currently only the low three bytes are used.
581
582     Compile Options:
583     
584     PARSE
585     PEEP
586     TRIE
587     PROGRAM
588     OFFSETS
589
590     Execute Options:
591
592     INTUIT
593     MATCH
594     TRIE
595
596     Extra Options
597
598     TRIE
599     OFFSETS
600
601 If you modify any of these make sure you make corresponding changes to
602 re.pm, especially to the documentation.
603
604 */
605
606
607 /* Compile */
608 #define RE_DEBUG_COMPILE_MASK      0x0000FF
609 #define RE_DEBUG_COMPILE_PARSE     0x000001
610 #define RE_DEBUG_COMPILE_OPTIMISE  0x000002
611 #define RE_DEBUG_COMPILE_TRIE      0x000004
612 #define RE_DEBUG_COMPILE_DUMP      0x000008
613
614 /* Execute */
615 #define RE_DEBUG_EXECUTE_MASK      0x00FF00
616 #define RE_DEBUG_EXECUTE_INTUIT    0x000100
617 #define RE_DEBUG_EXECUTE_MATCH     0x000200
618 #define RE_DEBUG_EXECUTE_TRIE      0x000400
619
620 /* Extra */
621 #define RE_DEBUG_EXTRA_MASK        0xFF0000
622 #define RE_DEBUG_EXTRA_TRIE        0x010000
623 #define RE_DEBUG_EXTRA_OFFSETS     0x020000
624 #define RE_DEBUG_EXTRA_OFFDEBUG    0x040000
625 #define RE_DEBUG_EXTRA_STATE       0x080000
626 #define RE_DEBUG_EXTRA_OPTIMISE    0x100000
627 /* combined */
628 #define RE_DEBUG_EXTRA_STACK       0x280000
629
630 #define RE_DEBUG_FLAG(x) (re_debug_flags & x)
631 /* Compile */
632 #define DEBUG_COMPILE_r(x) DEBUG_r( \
633     if (re_debug_flags & RE_DEBUG_COMPILE_MASK) x  )
634 #define DEBUG_PARSE_r(x) DEBUG_r( \
635     if (re_debug_flags & RE_DEBUG_COMPILE_PARSE) x  )
636 #define DEBUG_OPTIMISE_r(x) DEBUG_r( \
637     if (re_debug_flags & RE_DEBUG_COMPILE_OPTIMISE) x  )
638 #define DEBUG_PARSE_r(x) DEBUG_r( \
639     if (re_debug_flags & RE_DEBUG_COMPILE_PARSE) x  )
640 #define DEBUG_DUMP_r(x) DEBUG_r( \
641     if (re_debug_flags & RE_DEBUG_COMPILE_DUMP) x  )
642 #define DEBUG_TRIE_COMPILE_r(x) DEBUG_r( \
643     if (re_debug_flags & RE_DEBUG_COMPILE_TRIE) x )
644
645 /* Execute */
646 #define DEBUG_EXECUTE_r(x) DEBUG_r( \
647     if (re_debug_flags & RE_DEBUG_EXECUTE_MASK) x  )
648 #define DEBUG_INTUIT_r(x) DEBUG_r( \
649     if (re_debug_flags & RE_DEBUG_EXECUTE_INTUIT) x  )
650 #define DEBUG_MATCH_r(x) DEBUG_r( \
651     if (re_debug_flags & RE_DEBUG_EXECUTE_MATCH) x  )
652 #define DEBUG_TRIE_EXECUTE_r(x) DEBUG_r( \
653     if (re_debug_flags & RE_DEBUG_EXECUTE_TRIE) x )
654
655 /* Extra */
656 #define DEBUG_EXTRA_r(x) DEBUG_r( \
657     if (re_debug_flags & RE_DEBUG_EXTRA_MASK) x  )
658 #define DEBUG_OFFSETS_r(x) DEBUG_r( \
659     if (re_debug_flags & RE_DEBUG_EXTRA_OFFSETS) x  )
660 #define DEBUG_STATE_r(x) DEBUG_r( \
661     if (re_debug_flags & RE_DEBUG_EXTRA_STATE) x )
662 #define DEBUG_STACK_r(x) DEBUG_r( \
663     if (re_debug_flags & RE_DEBUG_EXTRA_STACK) x )
664 #define DEBUG_OPTIMISE_MORE_r(x) DEBUG_r( \
665     if ((RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE) == \
666          (re_debug_flags & (RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE)) ) x )
667 #define MJD_OFFSET_DEBUG(x) DEBUG_r( \
668     if (re_debug_flags & RE_DEBUG_EXTRA_OFFDEBUG) \
669         Perl_warn_nocontext x )
670 #define DEBUG_TRIE_COMPILE_MORE_r(x) DEBUG_TRIE_COMPILE_r( \
671     if (re_debug_flags & RE_DEBUG_EXTRA_TRIE) x )
672 #define DEBUG_TRIE_EXECUTE_MORE_r(x) DEBUG_TRIE_EXECUTE_r( \
673     if (re_debug_flags & RE_DEBUG_EXTRA_TRIE) x )
674
675 #define DEBUG_TRIE_r(x) DEBUG_r( \
676     if (re_debug_flags & (RE_DEBUG_COMPILE_TRIE \
677         | RE_DEBUG_EXECUTE_TRIE )) x )
678
679 /* initialization */
680 /* get_sv() can return NULL during global destruction. */
681 #define GET_RE_DEBUG_FLAGS DEBUG_r({ \
682         SV * re_debug_flags_sv = NULL; \
683         re_debug_flags_sv = get_sv(RE_DEBUG_FLAGS, 1); \
684         if (re_debug_flags_sv) { \
685             if (!SvIOK(re_debug_flags_sv)) \
686                 sv_setuv(re_debug_flags_sv, RE_DEBUG_COMPILE_DUMP | RE_DEBUG_EXECUTE_MASK ); \
687             re_debug_flags=SvIV(re_debug_flags_sv); \
688         }\
689 })
690
691 #ifdef DEBUGGING
692
693 #define GET_RE_DEBUG_FLAGS_DECL IV re_debug_flags = 0; GET_RE_DEBUG_FLAGS;
694
695 #define RE_PV_COLOR_DECL(rpv,rlen,isuni,dsv,pv,l,m,c1,c2) \
696     const char * const rpv =                          \
697         pv_pretty((dsv), (pv), (l), (m), \
698             PL_colors[(c1)],PL_colors[(c2)], \
699             ((isuni) ? PERL_PV_ESCAPE_UNI : 0) );         \
700     const int rlen = SvCUR(dsv)
701
702 #define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m) \
703     const char * const rpv =                          \
704         pv_pretty((dsv), (SvPV_nolen_const(sv)), (SvCUR(sv)), (m), \
705             PL_colors[(c1)],PL_colors[(c2)], \
706             ((isuni) ? PERL_PV_ESCAPE_UNI : 0) )
707
708 #define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m)                    \
709     const char * const rpv =                                       \
710         pv_pretty((dsv), (pv), (l), (m), \
711             PL_colors[0], PL_colors[1], \
712             ( PERL_PV_PRETTY_QUOTE | PERL_PV_PRETTY_ELIPSES |      \
713               ((isuni) ? PERL_PV_ESCAPE_UNI : 0))                  \
714         )                                                  
715
716 #define RE_SV_DUMPLEN(ItEm) (SvCUR(ItEm) - (SvTAIL(ItEm)!=0))
717 #define RE_SV_TAIL(ItEm) (SvTAIL(ItEm) ? "$" : "")
718     
719 #else /* if not DEBUGGING */
720
721 #define GET_RE_DEBUG_FLAGS_DECL
722 #define RE_PV_COLOR_DECL(rpv,rlen,isuni,dsv,pv,l,m,c1,c2)
723 #define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m)
724 #define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m)
725 #define RE_SV_DUMPLEN(ItEm)
726 #define RE_SV_TAIL(ItEm)
727
728 #endif /* DEBUG RELATED DEFINES */
729