This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Increase Data::Dumper’s version
[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, 2006, 2007, 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 #include "regcharclass.h"
11
12 typedef OP OP_4tree;                    /* Will be redefined later. */
13
14
15 /* Convert branch sequences to more efficient trie ops? */
16 #define PERL_ENABLE_TRIE_OPTIMISATION 1
17
18 /* Be really agressive about optimising patterns with trie sequences? */
19 #define PERL_ENABLE_EXTENDED_TRIE_OPTIMISATION 1
20
21 /* Use old style unicode mappings for perl and posix character classes
22  *
23  * NOTE: Enabling this essentially breaks character class matching against unicode 
24  * strings, so that POSIX char classes match when they shouldn't, and \d matches 
25  * way more than 10 characters, and sometimes a charclass and its complement either
26  * both match or neither match.
27  * NOTE: Disabling this will cause various backwards compatibility issues to rear 
28  * their head, and tests to fail. However it will make the charclass behaviour 
29  * consistant regardless of internal string type, and make character class inversions
30  * consistant. The tests that fail in the regex engine are basically broken tests.
31  *
32  * Personally I think 5.12 should disable this for sure. Its a bit more debatable for
33  * 5.10, so for now im leaving it enabled.
34  * XXX: It is now enabled for 5.11/5.12
35  *
36  * -demerphq
37  */
38 #define PERL_LEGACY_UNICODE_CHARCLASS_MAPPINGS 1
39
40 /* Should the optimiser take positive assertions into account? */
41 #define PERL_ENABLE_POSITIVE_ASSERTION_STUDY 0
42
43 /* Not for production use: */
44 #define PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS 0
45
46 /* Activate offsets code - set to if 1 to enable */
47 #ifdef DEBUGGING
48 #define RE_TRACK_PATTERN_OFFSETS
49 #endif
50
51 /* Unless the next line is uncommented it is illegal to combine lazy 
52    matching with possessive matching. Frankly it doesn't make much sense 
53    to allow it as X*?+ matches nothing, X+?+ matches a single char only, 
54    and X{min,max}?+ matches min times only.
55  */
56 /* #define REG_ALLOW_MINMOD_SUSPEND */
57
58 /*
59  * The "internal use only" fields in regexp.h are present to pass info from
60  * compile to execute that permits the execute phase to run lots faster on
61  * simple cases.  They are:
62  *
63  * regstart     sv that must begin a match; NULL if none obvious
64  * reganch      is the match anchored (at beginning-of-line only)?
65  * regmust      string (pointer into program) that match must include, or NULL
66  *  [regmust changed to SV* for bminstr()--law]
67  * regmlen      length of regmust string
68  *  [regmlen not used currently]
69  *
70  * Regstart and reganch permit very fast decisions on suitable starting points
71  * for a match, cutting down the work a lot.  Regmust permits fast rejection
72  * of lines that cannot possibly match.  The regmust tests are costly enough
73  * that pregcomp() supplies a regmust only if the r.e. contains something
74  * potentially expensive (at present, the only such thing detected is * or +
75  * at the start of the r.e., which can involve a lot of backup).  Regmlen is
76  * supplied because the test in pregexec() needs it and pregcomp() is computing
77  * it anyway.
78  * [regmust is now supplied always.  The tests that use regmust have a
79  * heuristic that disables the test if it usually matches.]
80  *
81  * [In fact, we now use regmust in many cases to locate where the search
82  * starts in the string, so if regback is >= 0, the regmust search is never
83  * wasted effort.  The regback variable says how many characters back from
84  * where regmust matched is the earliest possible start of the match.
85  * For instance, /[a-z].foo/ has a regmust of 'foo' and a regback of 2.]
86  */
87
88 /*
89  * Structure for regexp "program".  This is essentially a linear encoding
90  * of a nondeterministic finite-state machine (aka syntax charts or
91  * "railroad normal form" in parsing technology).  Each node is an opcode
92  * plus a "next" pointer, possibly plus an operand.  "Next" pointers of
93  * all nodes except BRANCH implement concatenation; a "next" pointer with
94  * a BRANCH on both ends of it is connecting two alternatives.  (Here we
95  * have one of the subtle syntax dependencies:  an individual BRANCH (as
96  * opposed to a collection of them) is never concatenated with anything
97  * because of operator precedence.)  The operand of some types of node is
98  * a literal string; for others, it is a node leading into a sub-FSM.  In
99  * particular, the operand of a BRANCH node is the first node of the branch.
100  * (NB this is *not* a tree structure:  the tail of the branch connects
101  * to the thing following the set of BRANCHes.)  The opcodes are defined
102  * in regnodes.h which is generated from regcomp.sym by regcomp.pl.
103  */
104
105 /*
106  * A node is one char of opcode followed by two chars of "next" pointer.
107  * "Next" pointers are stored as two 8-bit pieces, high order first.  The
108  * value is a positive offset from the opcode of the node containing it.
109  * An operand, if any, simply follows the node.  (Note that much of the
110  * code generation knows about this implicit relationship.)
111  *
112  * Using two bytes for the "next" pointer is vast overkill for most things,
113  * but allows patterns to get big without disasters.
114  *
115  * [The "next" pointer is always aligned on an even
116  * boundary, and reads the offset directly as a short.  Also, there is no
117  * special test to reverse the sign of BACK pointers since the offset is
118  * stored negative.]
119  */
120
121 /* This is the stuff that used to live in regexp.h that was truly
122    private to the engine itself. It now lives here. */
123
124
125
126  typedef struct regexp_internal {
127         int name_list_idx;      /* Optional data index of an array of paren names */
128         union {
129             U32 *offsets;           /* offset annotations 20001228 MJD
130                                        data about mapping the program to the
131                                        string -
132                                        offsets[0] is proglen when this is used
133                                        */
134             U32 proglen;
135         } u;
136
137         regnode *regstclass;    /* Optional startclass as identified or constructed
138                                    by the optimiser */
139         struct reg_data *data;  /* Additional miscellaneous data used by the program.
140                                    Used to make it easier to clone and free arbitrary
141                                    data that the regops need. Often the ARG field of
142                                    a regop is an index into this structure */
143         regnode program[1];     /* Unwarranted chumminess with compiler. */
144 } regexp_internal;
145
146 #define RXi_SET(x,y) (x)->pprivate = (void*)(y)   
147 #define RXi_GET(x)   ((regexp_internal *)((x)->pprivate))
148 #define RXi_GET_DECL(r,ri) regexp_internal *ri = RXi_GET(r)
149 /*
150  * Flags stored in regexp->intflags
151  * These are used only internally to the regexp engine
152  *
153  * See regexp.h for flags used externally to the regexp engine
154  */
155 #define PREGf_SKIP              0x00000001
156 #define PREGf_IMPLICIT          0x00000002 /* Converted .* to ^.* */
157 #define PREGf_NAUGHTY           0x00000004 /* how exponential is this pattern? */
158 #define PREGf_VERBARG_SEEN      0x00000008
159 #define PREGf_CUTGROUP_SEEN     0x00000010
160
161
162 /* this is where the old regcomp.h started */
163
164 struct regnode_string {
165     U8  str_len;
166     U8  type;
167     U16 next_off;
168     char string[1];
169 };
170
171 /* Argument bearing node - workhorse, 
172    arg1 is often for the data field */
173 struct regnode_1 {
174     U8  flags;
175     U8  type;
176     U16 next_off;
177     U32 arg1;
178 };
179
180 /* Similar to a regnode_1 but with an extra signed argument */
181 struct regnode_2L {
182     U8  flags;
183     U8  type;
184     U16 next_off;
185     U32 arg1;
186     I32 arg2;
187 };
188
189 /* 'Two field' -- Two 16 bit unsigned args */
190 struct regnode_2 {
191     U8  flags;
192     U8  type;
193     U16 next_off;
194     U16 arg1;
195     U16 arg2;
196 };
197
198
199 #define ANYOF_BITMAP_SIZE       32      /* 256 b/(8 b/B) */
200 #define ANYOF_CLASSBITMAP_SIZE   4      /* up to 32 (8*4) named classes */
201
202 /* also used by trie */
203 struct regnode_charclass {
204     U8  flags;
205     U8  type;
206     U16 next_off;
207     U32 arg1;                           /* used as ptr in S_regclass */
208     char bitmap[ANYOF_BITMAP_SIZE];     /* only compile-time */
209 };
210
211 /* has runtime (locale) \d, \w, ..., [:posix:] classes */
212 struct regnode_charclass_class {
213     U8  flags;                          /* ANYOF_CLASS bit must go here */
214     U8  type;
215     U16 next_off;
216     U32 arg1;                                   /* used as ptr in S_regclass */
217     char bitmap[ANYOF_BITMAP_SIZE];             /* both compile-time */
218     char classflags[ANYOF_CLASSBITMAP_SIZE];    /* and run-time */
219 };
220
221 /* XXX fix this description.
222    Impose a limit of REG_INFTY on various pattern matching operations
223    to limit stack growth and to avoid "infinite" recursions.
224 */
225 /* The default size for REG_INFTY is I16_MAX, which is the same as
226    SHORT_MAX (see perl.h).  Unfortunately I16 isn't necessarily 16 bits
227    (see handy.h).  On the Cray C90, sizeof(short)==4 and hence I16_MAX is
228    ((1<<31)-1), while on the Cray T90, sizeof(short)==8 and I16_MAX is
229    ((1<<63)-1).  To limit stack growth to reasonable sizes, supply a
230    smaller default.
231         --Andy Dougherty  11 June 1998
232 */
233 #if SHORTSIZE > 2
234 #  ifndef REG_INFTY
235 #    define REG_INFTY ((1<<15)-1)
236 #  endif
237 #endif
238
239 #ifndef REG_INFTY
240 #  define REG_INFTY I16_MAX
241 #endif
242
243 #define ARG_VALUE(arg) (arg)
244 #define ARG__SET(arg,val) ((arg) = (val))
245
246 #undef ARG
247 #undef ARG1
248 #undef ARG2
249
250 #define ARG(p) ARG_VALUE(ARG_LOC(p))
251 #define ARG1(p) ARG_VALUE(ARG1_LOC(p))
252 #define ARG2(p) ARG_VALUE(ARG2_LOC(p))
253 #define ARG2L(p) ARG_VALUE(ARG2L_LOC(p))
254
255 #define ARG_SET(p, val) ARG__SET(ARG_LOC(p), (val))
256 #define ARG1_SET(p, val) ARG__SET(ARG1_LOC(p), (val))
257 #define ARG2_SET(p, val) ARG__SET(ARG2_LOC(p), (val))
258 #define ARG2L_SET(p, val) ARG__SET(ARG2L_LOC(p), (val))
259
260 #undef NEXT_OFF
261 #undef NODE_ALIGN
262
263 #define NEXT_OFF(p) ((p)->next_off)
264 #define NODE_ALIGN(node)
265 #define NODE_ALIGN_FILL(node) ((node)->flags = 0xde) /* deadbeef */
266
267 #define SIZE_ALIGN NODE_ALIGN
268
269 #undef OP
270 #undef OPERAND
271 #undef MASK
272 #undef STRING
273
274 #define OP(p)           ((p)->type)
275 #define FLAGS(p)        ((p)->flags)    /* Caution: Doesn't apply to all \
276                                            regnode types */
277 #define OPERAND(p)      (((struct regnode_string *)p)->string)
278 #define MASK(p)         ((char*)OPERAND(p))
279 #define STR_LEN(p)      (((struct regnode_string *)p)->str_len)
280 #define STRING(p)       (((struct regnode_string *)p)->string)
281 #define STR_SZ(l)       ((l + sizeof(regnode) - 1) / sizeof(regnode))
282 #define NODE_SZ_STR(p)  (STR_SZ(STR_LEN(p))+1)
283
284 #undef NODE_ALIGN
285 #undef ARG_LOC
286 #undef NEXTOPER
287 #undef PREVOPER
288
289 #define NODE_ALIGN(node)
290 #define ARG_LOC(p)      (((struct regnode_1 *)p)->arg1)
291 #define ARG1_LOC(p)     (((struct regnode_2 *)p)->arg1)
292 #define ARG2_LOC(p)     (((struct regnode_2 *)p)->arg2)
293 #define ARG2L_LOC(p)    (((struct regnode_2L *)p)->arg2)
294
295 #define NODE_STEP_REGNODE       1       /* sizeof(regnode)/sizeof(regnode) */
296 #define EXTRA_STEP_2ARGS        EXTRA_SIZE(struct regnode_2)
297
298 #define NODE_STEP_B     4
299
300 #define NEXTOPER(p)     ((p) + NODE_STEP_REGNODE)
301 #define PREVOPER(p)     ((p) - NODE_STEP_REGNODE)
302
303 #define FILL_ADVANCE_NODE(ptr, op) STMT_START { \
304     (ptr)->type = op;    (ptr)->next_off = 0;   (ptr)++; } STMT_END
305 #define FILL_ADVANCE_NODE_ARG(ptr, op, arg) STMT_START { \
306     ARG_SET(ptr, arg);  FILL_ADVANCE_NODE(ptr, op); (ptr) += 1; } STMT_END
307
308 #define REG_MAGIC 0234
309
310 #define SIZE_ONLY (RExC_emit == &PL_regdummy)
311
312 /* Flags for node->flags of several of the node types */
313 #define USE_UNI                0x01
314
315 /* Flags for node->flags of ANYOF */
316
317 #define ANYOF_CLASS             0x08    /* has runtime \d, \w, [:posix:], ... */
318 #define ANYOF_LARGE      ANYOF_CLASS    /* Same; name retained for back compat */
319 #define ANYOF_INVERT            0x04
320 #define ANYOF_FOLD              0x02
321 #define ANYOF_LOCALE            0x01
322
323 /* EOS used for regstclass only */
324 #define ANYOF_EOS               0x10    /* Can match an empty string too */
325
326 #define ANYOF_UNICODE           0x20    /* Matches >= one thing past 0xff */
327 #define ANYOF_UNICODE_ALL       0x40    /* Matches 0x100 - infinity */
328
329 #define ANYOF_FLAGS_ALL         0xff
330
331 /* Character classes for node->classflags of ANYOF */
332 /* Should be synchronized with a table in regprop() */
333 /* 2n should pair with 2n+1 */
334
335 #define ANYOF_ALNUM      0      /* \w, PL_utf8_alnum, utf8::IsWord, ALNUM */
336 #define ANYOF_NALNUM     1
337 #define ANYOF_SPACE      2      /* \s */
338 #define ANYOF_NSPACE     3
339 #define ANYOF_DIGIT      4      /* \d */
340 #define ANYOF_NDIGIT     5
341 #define ANYOF_ALNUMC     6      /* [[:alnum:]] isalnum(3), utf8::IsAlnum, ALNUMC */
342 #define ANYOF_NALNUMC    7
343 #define ANYOF_ALPHA      8
344 #define ANYOF_NALPHA     9
345 #define ANYOF_ASCII     10
346 #define ANYOF_NASCII    11
347 #define ANYOF_CNTRL     12
348 #define ANYOF_NCNTRL    13
349 #define ANYOF_GRAPH     14
350 #define ANYOF_NGRAPH    15
351 #define ANYOF_LOWER     16
352 #define ANYOF_NLOWER    17
353 #define ANYOF_PRINT     18
354 #define ANYOF_NPRINT    19
355 #define ANYOF_PUNCT     20
356 #define ANYOF_NPUNCT    21
357 #define ANYOF_UPPER     22
358 #define ANYOF_NUPPER    23
359 #define ANYOF_XDIGIT    24
360 #define ANYOF_NXDIGIT   25
361 #define ANYOF_PSXSPC    26      /* POSIX space: \s plus the vertical tab */
362 #define ANYOF_NPSXSPC   27
363 #define ANYOF_BLANK     28      /* GNU extension: space and tab: non-vertical space */
364 #define ANYOF_NBLANK    29
365
366 #define ANYOF_MAX       32
367
368 /* pseudo classes, not stored in the class bitmap, but used as flags
369    during compilation of char classes */
370
371 #define ANYOF_VERTWS    (ANYOF_MAX+1)
372 #define ANYOF_NVERTWS   (ANYOF_MAX+2)
373 #define ANYOF_HORIZWS   (ANYOF_MAX+3)
374 #define ANYOF_NHORIZWS  (ANYOF_MAX+4)
375
376 /* Backward source code compatibility. */
377
378 #define ANYOF_ALNUML     ANYOF_ALNUM
379 #define ANYOF_NALNUML    ANYOF_NALNUM
380 #define ANYOF_SPACEL     ANYOF_SPACE
381 #define ANYOF_NSPACEL    ANYOF_NSPACE
382
383 /* Utility macros for the bitmap and classes of ANYOF */
384
385 #define ANYOF_SIZE              (sizeof(struct regnode_charclass))
386 #define ANYOF_CLASS_SIZE        (sizeof(struct regnode_charclass_class))
387
388 #define ANYOF_FLAGS(p)          ((p)->flags)
389
390 #define ANYOF_BIT(c)            (1 << ((c) & 7))
391
392 #define ANYOF_CLASS_BYTE(p, c)  (((struct regnode_charclass_class*)(p))->classflags[((c) >> 3) & 3])
393 #define ANYOF_CLASS_SET(p, c)   (ANYOF_CLASS_BYTE(p, c) |=  ANYOF_BIT(c))
394 #define ANYOF_CLASS_CLEAR(p, c) (ANYOF_CLASS_BYTE(p, c) &= ~ANYOF_BIT(c))
395 #define ANYOF_CLASS_TEST(p, c)  (ANYOF_CLASS_BYTE(p, c) &   ANYOF_BIT(c))
396
397 #define ANYOF_CLASS_ZERO(ret)   Zero(((struct regnode_charclass_class*)(ret))->classflags, ANYOF_CLASSBITMAP_SIZE, char)
398 #define ANYOF_BITMAP_ZERO(ret)  Zero(((struct regnode_charclass*)(ret))->bitmap, ANYOF_BITMAP_SIZE, char)
399
400 #define ANYOF_BITMAP(p)         (((struct regnode_charclass*)(p))->bitmap)
401 #define ANYOF_BITMAP_BYTE(p, c) (ANYOF_BITMAP(p)[(((U8)(c)) >> 3) & 31])
402 #define ANYOF_BITMAP_SET(p, c)  (ANYOF_BITMAP_BYTE(p, c) |=  ANYOF_BIT(c))
403 #define ANYOF_BITMAP_CLEAR(p,c) (ANYOF_BITMAP_BYTE(p, c) &= ~ANYOF_BIT(c))
404 #define ANYOF_BITMAP_TEST(p, c) (ANYOF_BITMAP_BYTE(p, c) &   ANYOF_BIT(c))
405
406 #define ANYOF_BITMAP_SETALL(p)          \
407         memset (ANYOF_BITMAP(p), 255, ANYOF_BITMAP_SIZE)
408 #define ANYOF_BITMAP_CLEARALL(p)        \
409         Zero (ANYOF_BITMAP(p), ANYOF_BITMAP_SIZE)
410 /* Check that all 256 bits are all set.  Used in S_cl_is_anything()  */
411 #define ANYOF_BITMAP_TESTALLSET(p)      \
412         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)
413
414 #define ANYOF_SKIP              ((ANYOF_SIZE - 1)/sizeof(regnode))
415 #define ANYOF_CLASS_SKIP        ((ANYOF_CLASS_SIZE - 1)/sizeof(regnode))
416 #define ANYOF_CLASS_ADD_SKIP    (ANYOF_CLASS_SKIP - ANYOF_SKIP)
417
418
419 /*
420  * Utility definitions.
421  */
422 #ifndef CHARMASK
423 #  define UCHARAT(p)    ((int)*(const U8*)(p))
424 #else
425 #  define UCHARAT(p)    ((int)*(p)&CHARMASK)
426 #endif
427
428 #define EXTRA_SIZE(guy) ((sizeof(guy)-1)/sizeof(struct regnode))
429
430 #define REG_SEEN_ZERO_LEN       0x00000001
431 #define REG_SEEN_LOOKBEHIND     0x00000002
432 #define REG_SEEN_GPOS           0x00000004
433 #define REG_SEEN_EVAL           0x00000008
434 #define REG_SEEN_CANY           0x00000010
435 #define REG_SEEN_SANY           REG_SEEN_CANY /* src bckwrd cmpt */
436 #define REG_SEEN_RECURSE        0x00000020
437 #define REG_TOP_LEVEL_BRANCHES  0x00000040
438 #define REG_SEEN_VERBARG        0x00000080
439 #define REG_SEEN_CUTGROUP       0x00000100
440 #define REG_SEEN_RUN_ON_COMMENT 0x00000200
441
442 START_EXTERN_C
443
444 #ifdef PLUGGABLE_RE_EXTENSION
445 #include "re_nodes.h"
446 #else
447 #include "regnodes.h"
448 #endif
449
450 #ifndef PLUGGABLE_RE_EXTENSION
451 #ifndef DOINIT
452 EXTCONST regexp_engine PL_core_reg_engine;
453 #else /* DOINIT */
454 EXTCONST regexp_engine PL_core_reg_engine = { 
455         Perl_re_compile,
456         Perl_regexec_flags,
457         Perl_re_intuit_start,
458         Perl_re_intuit_string, 
459         Perl_regfree_internal,
460         Perl_reg_numbered_buff_fetch,
461         Perl_reg_numbered_buff_store,
462         Perl_reg_numbered_buff_length,
463         Perl_reg_named_buff,
464         Perl_reg_named_buff_iter,
465         Perl_reg_qr_package,
466 #if defined(USE_ITHREADS)        
467         Perl_regdupe_internal
468 #endif        
469 };
470 #endif /* DOINIT */
471 #endif /* PLUGGABLE_RE_EXTENSION */
472
473
474 END_EXTERN_C
475
476
477 /* .what is a character array with one character for each member of .data
478  * The character describes the function of the corresponding .data item:
479  *   a - AV for paren_name_list under DEBUGGING
480  *   f - start-class data for regstclass optimization
481  *   n - Root of op tree for (?{EVAL}) item
482  *   o - Start op for (?{EVAL}) item
483  *   p - Pad for (?{EVAL}) item
484  *   s - swash for Unicode-style character class, and the multicharacter
485  *       strings resulting from casefolding the single-character entries
486  *       in the character class
487  *   t - trie struct
488  *   u - trie struct's widecharmap (a HV, so can't share, must dup)
489  *       also used for revcharmap and words under DEBUGGING
490  *   T - aho-trie struct
491  *   S - sv for named capture lookup
492  * 20010712 mjd@plover.com
493  * (Remember to update re_dup() and pregfree() if you add any items.)
494  */
495 struct reg_data {
496     U32 count;
497     U8 *what;
498     void* data[1];
499 };
500
501 /* Code in S_to_utf8_substr() and S_to_byte_substr() in regexec.c accesses
502    anchored* and float* via array indexes 0 and 1.  */
503 #define anchored_substr substrs->data[0].substr
504 #define anchored_utf8 substrs->data[0].utf8_substr
505 #define anchored_offset substrs->data[0].min_offset
506 #define anchored_end_shift substrs->data[0].end_shift
507
508 #define float_substr substrs->data[1].substr
509 #define float_utf8 substrs->data[1].utf8_substr
510 #define float_min_offset substrs->data[1].min_offset
511 #define float_max_offset substrs->data[1].max_offset
512 #define float_end_shift substrs->data[1].end_shift
513
514 #define check_substr substrs->data[2].substr
515 #define check_utf8 substrs->data[2].utf8_substr
516 #define check_offset_min substrs->data[2].min_offset
517 #define check_offset_max substrs->data[2].max_offset
518 #define check_end_shift substrs->data[2].end_shift
519
520 #define RX_ANCHORED_SUBSTR(rx)  (((struct regexp *)SvANY(rx))->anchored_substr)
521 #define RX_ANCHORED_UTF8(rx)    (((struct regexp *)SvANY(rx))->anchored_utf8)
522 #define RX_FLOAT_SUBSTR(rx)     (((struct regexp *)SvANY(rx))->float_substr)
523 #define RX_FLOAT_UTF8(rx)       (((struct regexp *)SvANY(rx))->float_utf8)
524
525 /* trie related stuff */
526
527 /* a transition record for the state machine. the
528    check field determines which state "owns" the
529    transition. the char the transition is for is
530    determined by offset from the owning states base
531    field.  the next field determines which state
532    is to be transitioned to if any.
533 */
534 struct _reg_trie_trans {
535   U32 next;
536   U32 check;
537 };
538
539 /* a transition list element for the list based representation */
540 struct _reg_trie_trans_list_elem {
541     U16 forid;
542     U32 newstate;
543 };
544 typedef struct _reg_trie_trans_list_elem reg_trie_trans_le;
545
546 /* a state for compressed nodes. base is an offset
547   into an array of reg_trie_trans array. If wordnum is
548   nonzero the state is accepting. if base is zero then
549   the state has no children (and will be accepting)
550 */
551 struct _reg_trie_state {
552   U16 wordnum;
553   union {
554     U32                base;
555     reg_trie_trans_le* list;
556   } trans;
557 };
558
559 /* info per word; indexed by wordnum */
560 typedef struct {
561     U16  prev;  /* previous word in acceptance chain; eg in
562                  * zzz|abc|ab/ after matching the chars abc, the
563                  * accepted word is #2, and the previous accepted
564                  * word is #3 */
565     U32 len;    /* how many chars long is this word? */
566     U32 accept; /* accept state for this word */
567 } reg_trie_wordinfo;
568
569
570 typedef struct _reg_trie_state    reg_trie_state;
571 typedef struct _reg_trie_trans    reg_trie_trans;
572
573
574 /* anything in here that needs to be freed later
575    should be dealt with in pregfree.
576    refcount is first in both this and _reg_ac_data to allow a space
577    optimisation in Perl_regdupe.  */
578 struct _reg_trie_data {
579     U32             refcount;        /* number of times this trie is referenced */
580     U32             lasttrans;       /* last valid transition element */
581     U16             *charmap;        /* byte to charid lookup array */
582     reg_trie_state  *states;         /* state data */
583     reg_trie_trans  *trans;          /* array of transition elements */
584     char            *bitmap;         /* stclass bitmap */
585     U16             *jump;           /* optional 1 indexed array of offsets before tail 
586                                         for the node following a given word. */
587     reg_trie_wordinfo *wordinfo;     /* array of info per word */
588     U16             uniquecharcount; /* unique chars in trie (width of trans table) */
589     U32             startstate;      /* initial state - used for common prefix optimisation */
590     STRLEN          minlen;          /* minimum length of words in trie - build/opt only? */
591     STRLEN          maxlen;          /* maximum length of words in trie - build/opt only? */
592     U32             prefixlen;       /* #chars in common prefix */
593     U32             statecount;      /* Build only - number of states in the states array 
594                                         (including the unused zero state) */
595     U32             wordcount;       /* Build only */
596 #ifdef DEBUGGING
597     STRLEN          charcount;       /* Build only */
598 #endif
599 };
600 /* There is one (3 under DEBUGGING) pointers that logically belong in this
601    structure, but are held outside as they need duplication on thread cloning,
602    whereas the rest of the structure can be read only:
603     HV              *widecharmap;    code points > 255 to charid
604 #ifdef DEBUGGING
605     AV              *words;          Array of words contained in trie, for dumping
606     AV              *revcharmap;     Map of each charid back to its character representation
607 #endif
608 */
609
610 #define TRIE_WORDS_OFFSET 2
611
612 typedef struct _reg_trie_data reg_trie_data;
613
614 /* refcount is first in both this and _reg_trie_data to allow a space
615    optimisation in Perl_regdupe.  */
616 struct _reg_ac_data {
617     U32              refcount;
618     U32              trie;
619     U32              *fail;
620     reg_trie_state   *states;
621 };
622 typedef struct _reg_ac_data reg_ac_data;
623
624 /* ANY_BIT doesnt use the structure, so we can borrow it here.
625    This is simpler than refactoring all of it as wed end up with
626    three different sets... */
627
628 #define TRIE_BITMAP(p)          (((reg_trie_data *)(p))->bitmap)
629 #define TRIE_BITMAP_BYTE(p, c)  (TRIE_BITMAP(p)[(((U8)(c)) >> 3) & 31])
630 #define TRIE_BITMAP_SET(p, c)   (TRIE_BITMAP_BYTE(p, c) |=  ANYOF_BIT((U8)c))
631 #define TRIE_BITMAP_CLEAR(p,c)  (TRIE_BITMAP_BYTE(p, c) &= ~ANYOF_BIT((U8)c))
632 #define TRIE_BITMAP_TEST(p, c)  (TRIE_BITMAP_BYTE(p, c) &   ANYOF_BIT((U8)c))
633
634 #define IS_ANYOF_TRIE(op) ((op)==TRIEC || (op)==AHOCORASICKC)
635 #define IS_TRIE_AC(op) ((op)>=AHOCORASICK)
636
637
638 #define BITMAP_BYTE(p, c)       (((U8*)p)[(((U8)(c)) >> 3) & 31])
639 #define BITMAP_TEST(p, c)       (BITMAP_BYTE(p, c) &   ANYOF_BIT((U8)c))
640
641 /* these defines assume uniquecharcount is the correct variable, and state may be evaluated twice */
642 #define TRIE_NODENUM(state) (((state)-1)/(trie->uniquecharcount)+1)
643 #define SAFE_TRIE_NODENUM(state) ((state) ? (((state)-1)/(trie->uniquecharcount)+1) : (state))
644 #define TRIE_NODEIDX(state) ((state) ? (((state)-1)*(trie->uniquecharcount)+1) : (state))
645
646 #ifdef DEBUGGING
647 #define TRIE_CHARCOUNT(trie) ((trie)->charcount)
648 #else
649 #define TRIE_CHARCOUNT(trie) (trie_charcount)
650 #endif
651
652 #define RE_TRIE_MAXBUF_INIT 65536
653 #define RE_TRIE_MAXBUF_NAME "\022E_TRIE_MAXBUF"
654 #define RE_DEBUG_FLAGS "\022E_DEBUG_FLAGS"
655
656 /*
657
658 RE_DEBUG_FLAGS is used to control what debug output is emitted
659 its divided into three groups of options, some of which interact.
660 The three groups are: Compile, Execute, Extra. There is room for a
661 further group, as currently only the low three bytes are used.
662
663     Compile Options:
664     
665     PARSE
666     PEEP
667     TRIE
668     PROGRAM
669     OFFSETS
670
671     Execute Options:
672
673     INTUIT
674     MATCH
675     TRIE
676
677     Extra Options
678
679     TRIE
680     OFFSETS
681
682 If you modify any of these make sure you make corresponding changes to
683 re.pm, especially to the documentation.
684
685 */
686
687
688 /* Compile */
689 #define RE_DEBUG_COMPILE_MASK      0x0000FF
690 #define RE_DEBUG_COMPILE_PARSE     0x000001
691 #define RE_DEBUG_COMPILE_OPTIMISE  0x000002
692 #define RE_DEBUG_COMPILE_TRIE      0x000004
693 #define RE_DEBUG_COMPILE_DUMP      0x000008
694 #define RE_DEBUG_COMPILE_FLAGS     0x000010
695
696 /* Execute */
697 #define RE_DEBUG_EXECUTE_MASK      0x00FF00
698 #define RE_DEBUG_EXECUTE_INTUIT    0x000100
699 #define RE_DEBUG_EXECUTE_MATCH     0x000200
700 #define RE_DEBUG_EXECUTE_TRIE      0x000400
701
702 /* Extra */
703 #define RE_DEBUG_EXTRA_MASK        0xFF0000
704 #define RE_DEBUG_EXTRA_TRIE        0x010000
705 #define RE_DEBUG_EXTRA_OFFSETS     0x020000
706 #define RE_DEBUG_EXTRA_OFFDEBUG    0x040000
707 #define RE_DEBUG_EXTRA_STATE       0x080000
708 #define RE_DEBUG_EXTRA_OPTIMISE    0x100000
709 #define RE_DEBUG_EXTRA_BUFFERS     0x400000
710 #define RE_DEBUG_EXTRA_GPOS        0x800000
711 /* combined */
712 #define RE_DEBUG_EXTRA_STACK       0x280000
713
714 #define RE_DEBUG_FLAG(x) (re_debug_flags & x)
715 /* Compile */
716 #define DEBUG_COMPILE_r(x) DEBUG_r( \
717     if (re_debug_flags & RE_DEBUG_COMPILE_MASK) x  )
718 #define DEBUG_PARSE_r(x) DEBUG_r( \
719     if (re_debug_flags & RE_DEBUG_COMPILE_PARSE) x  )
720 #define DEBUG_OPTIMISE_r(x) DEBUG_r( \
721     if (re_debug_flags & RE_DEBUG_COMPILE_OPTIMISE) x  )
722 #define DEBUG_PARSE_r(x) DEBUG_r( \
723     if (re_debug_flags & RE_DEBUG_COMPILE_PARSE) x  )
724 #define DEBUG_DUMP_r(x) DEBUG_r( \
725     if (re_debug_flags & RE_DEBUG_COMPILE_DUMP) x  )
726 #define DEBUG_TRIE_COMPILE_r(x) DEBUG_r( \
727     if (re_debug_flags & RE_DEBUG_COMPILE_TRIE) x )
728 #define DEBUG_FLAGS_r(x) DEBUG_r( \
729     if (re_debug_flags & RE_DEBUG_COMPILE_FLAGS) x )
730 /* Execute */
731 #define DEBUG_EXECUTE_r(x) DEBUG_r( \
732     if (re_debug_flags & RE_DEBUG_EXECUTE_MASK) x  )
733 #define DEBUG_INTUIT_r(x) DEBUG_r( \
734     if (re_debug_flags & RE_DEBUG_EXECUTE_INTUIT) x  )
735 #define DEBUG_MATCH_r(x) DEBUG_r( \
736     if (re_debug_flags & RE_DEBUG_EXECUTE_MATCH) x  )
737 #define DEBUG_TRIE_EXECUTE_r(x) DEBUG_r( \
738     if (re_debug_flags & RE_DEBUG_EXECUTE_TRIE) x )
739
740 /* Extra */
741 #define DEBUG_EXTRA_r(x) DEBUG_r( \
742     if (re_debug_flags & RE_DEBUG_EXTRA_MASK) x  )
743 #define DEBUG_OFFSETS_r(x) DEBUG_r( \
744     if (re_debug_flags & RE_DEBUG_EXTRA_OFFSETS) x  )
745 #define DEBUG_STATE_r(x) DEBUG_r( \
746     if (re_debug_flags & RE_DEBUG_EXTRA_STATE) x )
747 #define DEBUG_STACK_r(x) DEBUG_r( \
748     if (re_debug_flags & RE_DEBUG_EXTRA_STACK) x )
749 #define DEBUG_BUFFERS_r(x) DEBUG_r( \
750     if (re_debug_flags & RE_DEBUG_EXTRA_BUFFERS) x )
751
752 #define DEBUG_OPTIMISE_MORE_r(x) DEBUG_r( \
753     if ((RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE) == \
754          (re_debug_flags & (RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE)) ) x )
755 #define MJD_OFFSET_DEBUG(x) DEBUG_r( \
756     if (re_debug_flags & RE_DEBUG_EXTRA_OFFDEBUG) \
757         Perl_warn_nocontext x )
758 #define DEBUG_TRIE_COMPILE_MORE_r(x) DEBUG_TRIE_COMPILE_r( \
759     if (re_debug_flags & RE_DEBUG_EXTRA_TRIE) x )
760 #define DEBUG_TRIE_EXECUTE_MORE_r(x) DEBUG_TRIE_EXECUTE_r( \
761     if (re_debug_flags & RE_DEBUG_EXTRA_TRIE) x )
762
763 #define DEBUG_TRIE_r(x) DEBUG_r( \
764     if (re_debug_flags & (RE_DEBUG_COMPILE_TRIE \
765         | RE_DEBUG_EXECUTE_TRIE )) x )
766 #define DEBUG_GPOS_r(x) DEBUG_r( \
767     if (re_debug_flags & RE_DEBUG_EXTRA_GPOS) x )
768
769 /* initialization */
770 /* get_sv() can return NULL during global destruction.  re_debug_flags can get
771  * clobbered by a longjmp, so must be initialized */
772 #define GET_RE_DEBUG_FLAGS DEBUG_r({ \
773         SV * re_debug_flags_sv = NULL; \
774         re_debug_flags = 0;            \
775         re_debug_flags_sv = get_sv(RE_DEBUG_FLAGS, 1); \
776         if (re_debug_flags_sv) { \
777             if (!SvIOK(re_debug_flags_sv)) \
778                 sv_setuv(re_debug_flags_sv, RE_DEBUG_COMPILE_DUMP | RE_DEBUG_EXECUTE_MASK ); \
779             re_debug_flags=SvIV(re_debug_flags_sv); \
780         }\
781 })
782
783 #ifdef DEBUGGING
784
785 #define GET_RE_DEBUG_FLAGS_DECL VOL IV re_debug_flags = 0; GET_RE_DEBUG_FLAGS;
786
787 #define RE_PV_COLOR_DECL(rpv,rlen,isuni,dsv,pv,l,m,c1,c2) \
788     const char * const rpv =                          \
789         pv_pretty((dsv), (pv), (l), (m), \
790             PL_colors[(c1)],PL_colors[(c2)], \
791             PERL_PV_ESCAPE_RE |((isuni) ? PERL_PV_ESCAPE_UNI : 0) );         \
792     const int rlen = SvCUR(dsv)
793
794 #define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m) \
795     const char * const rpv =                          \
796         pv_pretty((dsv), (SvPV_nolen_const(sv)), (SvCUR(sv)), (m), \
797             PL_colors[(c1)],PL_colors[(c2)], \
798             PERL_PV_ESCAPE_RE |((isuni) ? PERL_PV_ESCAPE_UNI : 0) )
799
800 #define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m)                    \
801     const char * const rpv =                                       \
802         pv_pretty((dsv), (pv), (l), (m), \
803             PL_colors[0], PL_colors[1], \
804             ( PERL_PV_PRETTY_QUOTE | PERL_PV_ESCAPE_RE | PERL_PV_PRETTY_ELLIPSES | \
805               ((isuni) ? PERL_PV_ESCAPE_UNI : 0))                  \
806         )
807
808 #define RE_SV_DUMPLEN(ItEm) (SvCUR(ItEm) - (SvTAIL(ItEm)!=0))
809 #define RE_SV_TAIL(ItEm) (SvTAIL(ItEm) ? "$" : "")
810     
811 #else /* if not DEBUGGING */
812
813 #define GET_RE_DEBUG_FLAGS_DECL
814 #define RE_PV_COLOR_DECL(rpv,rlen,isuni,dsv,pv,l,m,c1,c2)
815 #define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m)
816 #define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m)
817 #define RE_SV_DUMPLEN(ItEm)
818 #define RE_SV_TAIL(ItEm)
819
820 #endif /* DEBUG RELATED DEFINES */
821
822 /*
823  * Local variables:
824  * c-indentation-style: bsd
825  * c-basic-offset: 4
826  * indent-tabs-mode: t
827  * End:
828  *
829  * ex: set ts=8 sts=4 sw=4 noet:
830  */