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
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.
11 #if ! defined(PERL_REGCOMP_H_) && ( defined(PERL_CORE) \
12 || defined(PERL_EXT_RE_BUILD))
13 #define PERL_REGCOMP_H_
15 #include "regcharclass.h"
17 /* Convert branch sequences to more efficient trie ops? */
18 #define PERL_ENABLE_TRIE_OPTIMISATION 1
20 /* Be really aggressive about optimising patterns with trie sequences? */
21 #define PERL_ENABLE_EXTENDED_TRIE_OPTIMISATION 1
23 /* Should the optimiser take positive assertions into account? */
24 #define PERL_ENABLE_POSITIVE_ASSERTION_STUDY 0
26 /* Not for production use: */
27 #define PERL_ENABLE_EXPERIMENTAL_REGEX_OPTIMISATIONS 0
30 * Structure for regexp "program". This is essentially a linear encoding
31 * of a nondeterministic finite-state machine (aka syntax charts or
32 * "railroad normal form" in parsing technology). Each node is an opcode
33 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
34 * all nodes except BRANCH implement concatenation; a "next" pointer with
35 * a BRANCH on both ends of it is connecting two alternatives. (Here we
36 * have one of the subtle syntax dependencies: an individual BRANCH (as
37 * opposed to a collection of them) is never concatenated with anything
38 * because of operator precedence.) The operand of some types of node is
39 * a literal string; for others, it is a node leading into a sub-FSM. In
40 * particular, the operand of a BRANCH node is the first node of the branch.
41 * (NB this is *not* a tree structure: the tail of the branch connects
42 * to the thing following the set of BRANCHes.) The opcodes are defined
43 * in regnodes.h which is generated from regcomp.sym by regcomp.pl.
47 * A node is one char of opcode followed by two chars of "next" pointer.
48 * "Next" pointers are stored as two 8-bit pieces, high order first. The
49 * value is a positive offset from the opcode of the node containing it.
50 * An operand, if any, simply follows the node. (Note that much of the
51 * code generation knows about this implicit relationship.)
53 * Using two bytes for the "next" pointer is vast overkill for most things,
54 * but allows patterns to get big without disasters.
56 * [The "next" pointer is always aligned on an even
57 * boundary, and reads the offset directly as a short.]
60 /* This is the stuff that used to live in regexp.h that was truly
61 private to the engine itself. It now lives here. */
63 typedef struct regexp_internal {
64 regnode *regstclass; /* Optional startclass as identified or constructed
66 struct reg_data *data; /* Additional miscellaneous data used by the program.
67 Used to make it easier to clone and free arbitrary
68 data that the regops need. Often the ARG field of
69 a regop is an index into this structure. NOTE the
70 0th element of this structure is NEVER used and is
71 strictly reserved for internal purposes. */
72 struct reg_code_blocks *code_blocks;/* positions of literal (?{}) */
73 U32 proglen; /* size of the compiled program in regnodes */
74 U32 name_list_idx; /* Optional data index of an array of paren names,
75 only valid when RXp_PAREN_NAMES(prog) is true,
76 0 means "no value" like any other index into the
78 regnode program[1]; /* Unwarranted chumminess with compiler. */
81 #define RXi_SET(x,y) (x)->pprivate = (void*)(y)
82 #define RXi_GET(x) ((regexp_internal *)((x)->pprivate))
83 #define RXi_GET_DECL(r,ri) regexp_internal *ri = RXi_GET(r)
85 * Flags stored in regexp->intflags
86 * These are used only internally to the regexp engine
88 * See regexp.h for flags used externally to the regexp engine
90 #define RXp_INTFLAGS(rx) ((rx)->intflags)
91 #define RX_INTFLAGS(prog) RXp_INTFLAGS(ReANY(prog))
93 #define PREGf_SKIP 0x00000001
94 #define PREGf_IMPLICIT 0x00000002 /* Converted .* to ^.* */
95 #define PREGf_NAUGHTY 0x00000004 /* how exponential is this pattern? */
96 #define PREGf_VERBARG_SEEN 0x00000008
97 #define PREGf_CUTGROUP_SEEN 0x00000010
98 #define PREGf_USE_RE_EVAL 0x00000020 /* compiled with "use re 'eval'" */
99 /* these used to be extflags, but are now intflags */
100 #define PREGf_NOSCAN 0x00000040
102 #define PREGf_GPOS_SEEN 0x00000100
103 #define PREGf_GPOS_FLOAT 0x00000200
105 #define PREGf_ANCH_MBOL 0x00000400
106 #define PREGf_ANCH_SBOL 0x00000800
107 #define PREGf_ANCH_GPOS 0x00001000
108 #define PREGf_RECURSE_SEEN 0x00002000
111 ( PREGf_ANCH_SBOL | PREGf_ANCH_GPOS | PREGf_ANCH_MBOL )
113 /* this is where the old regcomp.h started */
116 /* Define the various regnode structures. These all should be a multiple
117 * of 32 bits large, and they should by and large correspond with each other
118 * in terms of naming, etc. Things can and will break in subtle ways if you
119 * change things without care. If you look at regexp.h you will see it
128 * This structure is the base unit of elements in the regexp program. When
129 * we increment our way through the program we increment by the size of this
130 * structure, and in all cases where regnode sizing is considered it is in
131 * units of this structure.
133 * This implies that no regnode style structure should contain 64 bit
134 * aligned members. Since the base regnode is 32 bits any member might
135 * not be 64 bit aligned no matter how you might try to pad out the
136 * struct itself (the regnode_ssc is special in this regard as it is
137 * never used in a program directly). If you want to store 64 bit
138 * members you need to store them specially. The struct regnode_p and the
139 * ARGp() and ARGp_SET() macros and related inline functions provide an example
140 * solution. Note they deal with a slightly more complicated problem than simple
141 * alignment, as pointers may be 32 bits or 64 bits depending on platform,
142 * but they illustrate the pattern to follow if you want to put a 64 bit value
145 * NOTE: Ideally we do not put pointers into the regnodes in a program. Instead
146 * we put them in the "data" part of the regexp structure and store the index into
147 * the data in the pointers in the regnode. This allows the pointer to be handled
148 * properly during clone/free operations (eg refcount bookkeeping). See S_add_data(),
149 * Perl_regdupe_internal(), Perl_regfree_internal() in regcomp.c for how the data
150 * array can be used, the letters 'arsSu' all refer to different types of SV that
151 * we already have support for in the data array.
154 struct regnode_string {
161 struct regnode_lstring { /* Constructed this way to keep the string aligned. */
165 U32 str_len; /* Only 18 bits allowed before would overflow 'next_off' */
169 struct regnode_anyofhs { /* Constructed this way to keep the string aligned. */
173 U32 arg1; /* set by set_ANYOF_arg() */
177 /* Argument bearing node - workhorse,
178 arg1 is often for the data field */
186 /* Node whose argument is 'SV *'. This needs to be used very carefully in
187 * situations where pointers won't become invalid because of, say re-mallocs.
189 * Note that this regnode type is problematic and should not be used or copied
190 * and will be removed in the future. Pointers should be stored in the data[]
191 * array and an index into the data array stored in the regnode, which allows the
192 * pointers to be handled properly during clone/free operations on the regexp
193 * data structure. As a byproduct it also saves space, often we use a 16 bit
194 * member to store indexes into the data[] array.
196 * Also note that the weird storage here is because regnodes are 32 bit aligned,
197 * which means we cannot have a 64 bit aligned member. To make things more annoying
198 * the size of a pointer may vary by platform. Thus we use a character array, and
199 * then use inline functions to copy the data in or out.
205 char arg1_sv_ptr_bytes[sizeof(SV *)];
208 /* Similar to a regnode_1 but with an extra signed argument */
217 /* 'Two field' -- Two 16 bit unsigned args */
226 #define REGNODE_BBM_BITMAP_LEN \
227 /* 6 info bits requires 64 bits; 5 => 32 */ \
228 ((1 << (UTF_CONTINUATION_BYTE_INFO_BITS)) / CHARBITS)
230 /* Used for matching any two-byte UTF-8 character whose start byte is known.
231 * The array is a bitmap capable of representing any possible continuation
237 U8 bitmap[REGNODE_BBM_BITMAP_LEN];
240 #define ANYOF_BITMAP_SIZE (NUM_ANYOF_CODE_POINTS / CHARBITS)
242 /* Note that these form structs which are supersets of the next smaller one, by
243 * appending fields. Alignment problems can occur if one of those optional
244 * fields requires stricter alignment than the base struct. And formal
245 * parameters that can really be two or more of the structs should be
246 * declared as the smallest one it could be. See commit message for
247 * 7dcac5f6a5195002b55c935ee1d67f67e1df280b. Regnode allocation is done
248 * without regard to alignment, and changing it to would also require changing
249 * the code that inserts and deletes regnodes. The basic single-argument
250 * regnode has a U32, which is what reganode() allocates as a unit. Therefore
251 * no field can require stricter alignment than U32. */
253 /* also used by trie */
254 struct regnode_charclass {
258 U32 arg1; /* set by set_ANYOF_arg() */
259 char bitmap[ANYOF_BITMAP_SIZE]; /* only compile-time */
262 /* has runtime (locale) \d, \w, ..., [:posix:] classes */
263 struct regnode_charclass_posixl {
264 U8 flags; /* ANYOF_MATCHES_POSIXL bit must go here */
268 char bitmap[ANYOF_BITMAP_SIZE]; /* both compile-time ... */
269 U32 classflags; /* and run-time */
272 /* A synthetic start class (SSC); is a regnode_charclass_posixl_fold, plus an
273 * extra SV*, used only during regex construction and which is not used by the
274 * main machinery in regexec.c and which does not get embedded in the final compiled
277 * Because it does not get embedded it does not have to comply with the alignment
278 * and sizing constraints required for a normal regnode structure: it MAY contain
279 * pointers or members of whatever size needed and the compiler will do the right
280 * thing. (Every other regnode type is 32 bit aligned.)
282 * Note that the 'next_off' field is unused, as the SSC stands alone, so there is
286 U8 flags; /* ANYOF_MATCHES_POSIXL bit must go here */
290 char bitmap[ANYOF_BITMAP_SIZE]; /* both compile-time ... */
291 U32 classflags; /* ... and run-time */
293 /* Auxiliary, only used during construction; NULL afterwards: list of code
298 /* We take advantage of 'next_off' not otherwise being used in the SSC by
299 * actually using it: by setting it to 1. This allows us to test and
300 * distinguish between an SSC and other ANYOF node types, as 'next_off' cannot
301 * otherwise be 1, because it is the offset to the next regnode expressed in
302 * units of regnodes. Since an ANYOF node contains extra fields, it adds up
303 * to 12 regnode units on 32-bit systems, (hence the minimum this can be (if
304 * not 0) is 11 there. Even if things get tightly packed on a 64-bit system,
305 * it still would be more than 1. */
306 #define set_ANYOF_SYNTHETIC(n) STMT_START{ OP(n) = ANYOF; \
309 #define is_ANYOF_SYNTHETIC(n) (PL_regnode_kind[OP(n)] == ANYOF && NEXT_OFF(n) == 1)
311 /* XXX fix this description.
312 Impose a limit of REG_INFTY on various pattern matching operations
313 to limit stack growth and to avoid "infinite" recursions.
315 /* The default size for REG_INFTY is U16_MAX, which is the same as
316 USHORT_MAX (see perl.h). Unfortunately U16 isn't necessarily 16 bits
317 (see handy.h). On the Cray C90, sizeof(short)==4 and hence U16_MAX is
318 ((1<<32)-1), while on the Cray T90, sizeof(short)==8 and U16_MAX is
319 ((1<<64)-1). To limit stack growth to reasonable sizes, supply a
321 --Andy Dougherty 11 June 1998
325 # define REG_INFTY nBIT_UMAX(16)
330 # define REG_INFTY U16_MAX
333 #define ARG_VALUE(arg) (arg)
334 #define ARG__SET(arg,val) ((arg) = (val))
340 #define ARG(p) ARG_VALUE(ARG_LOC(p))
341 #define ARGp(p) ARGp_VALUE_inline(p)
342 #define ARG1(p) ARG_VALUE(ARG1_LOC(p))
343 #define ARG2(p) ARG_VALUE(ARG2_LOC(p))
344 #define ARG2L(p) ARG_VALUE(ARG2L_LOC(p))
346 #define ARG_SET(p, val) ARG__SET(ARG_LOC(p), (val))
347 #define ARG1_SET(p, val) ARG__SET(ARG1_LOC(p), (val))
348 #define ARG2_SET(p, val) ARG__SET(ARG2_LOC(p), (val))
349 #define ARG2L_SET(p, val) ARG__SET(ARG2L_LOC(p), (val))
350 #define ARGp_SET(p, val) ARGp_SET_inline((p),(val))
355 #define NEXT_OFF(p) ((p)->next_off)
356 #define NODE_ALIGN(node)
357 /* the following define was set to 0xde in 075abff3
358 * as part of some linting logic. I have set it to 0
359 * as otherwise in every place where we /might/ set flags
360 * we have to set it 0 explicitly, which duplicates
361 * assignments and IMO adds an unacceptable level of
362 * surprise to working in the regex engine. If this
363 * is changed from 0 then at the very least make sure
364 * that SBOL for /^/ sets the flags to 0 explicitly.
366 #define NODE_ALIGN_FILL(node) ((node)->flags = 0)
368 #define SIZE_ALIGN NODE_ALIGN
374 #define OP(p) ((p)->type)
375 #define FLAGS(p) ((p)->flags) /* Caution: Doesn't apply to all \
376 regnode types. For some, it's the \
377 character set of the regnode */
378 #define STR_LENs(p) (__ASSERT_(OP(p) != LEXACT && OP(p) != LEXACT_REQ8) \
379 ((struct regnode_string *)p)->str_len)
380 #define STRINGs(p) (__ASSERT_(OP(p) != LEXACT && OP(p) != LEXACT_REQ8) \
381 ((struct regnode_string *)p)->string)
382 #define OPERANDs(p) STRINGs(p)
384 /* Long strings. Currently limited to length 18 bits, which handles a 262000
385 * byte string. The limiting factor is the 16 bit 'next_off' field, which
386 * points to the next regnode, so the furthest away it can be is 2**16. On
387 * most architectures, regnodes are 2**2 bytes long, so that yields 2**18
388 * bytes. Should a longer string be desired, we could increase it to 26 bits
389 * fairly easily, by changing this node to have longj type which causes the ARG
390 * field to be used for the link to the next regnode (although code would have
391 * to be changed to account for this), and then use a combination of the flags
392 * and next_off fields for the length. To get 34 bit length, also change the
393 * node to be an ARG2L, using the second 32 bit field for the length, and not
394 * using the flags nor next_off fields at all. One could have an llstring node
395 * and even an lllstring type. */
396 #define STR_LENl(p) (__ASSERT_(OP(p) == LEXACT || OP(p) == LEXACT_REQ8) \
397 (((struct regnode_lstring *)p)->str_len))
398 #define STRINGl(p) (__ASSERT_(OP(p) == LEXACT || OP(p) == LEXACT_REQ8) \
399 (((struct regnode_lstring *)p)->string))
400 #define OPERANDl(p) STRINGl(p)
402 #define STR_LEN(p) ((OP(p) == LEXACT || OP(p) == LEXACT_REQ8) \
403 ? STR_LENl(p) : STR_LENs(p))
404 #define STRING(p) ((OP(p) == LEXACT || OP(p) == LEXACT_REQ8) \
405 ? STRINGl(p) : STRINGs(p))
406 #define OPERAND(p) STRING(p)
408 /* The number of (smallest) regnode equivalents that a string of length l bytes
409 * occupies - Used by the REGNODE_AFTER() macros and functions. */
410 #define STR_SZ(l) (((l) + sizeof(regnode) - 1) / sizeof(regnode))
412 #define setSTR_LEN(p,v) \
414 if (OP(p) == LEXACT || OP(p) == LEXACT_REQ8) \
415 ((struct regnode_lstring *)(p))->str_len = (v); \
417 ((struct regnode_string *)(p))->str_len = (v); \
420 #define ANYOFR_BASE_BITS 20
421 #define ANYOFRbase(p) (ARG(p) & nBIT_MASK(ANYOFR_BASE_BITS))
422 #define ANYOFRdelta(p) (ARG(p) >> ANYOFR_BASE_BITS)
427 #define NODE_ALIGN(node)
428 #define ARG_LOC(p) (((struct regnode_1 *)p)->arg1)
429 #define ARGp_BYTES_LOC(p) (((struct regnode_p *)p)->arg1_sv_ptr_bytes)
430 #define ARG1_LOC(p) (((struct regnode_2 *)p)->arg1)
431 #define ARG2_LOC(p) (((struct regnode_2 *)p)->arg2)
432 #define ARG2L_LOC(p) (((struct regnode_2L *)p)->arg2)
434 /* These should no longer be used directly in most cases. Please use
435 * the REGNODE_AFTER() macros instead. */
436 #define NODE_STEP_REGNODE 1 /* sizeof(regnode)/sizeof(regnode) */
437 #define EXTRA_STEP_2ARGS EXTRA_SIZE(struct regnode_2)
439 /* Core macros for computing "the regnode after this one". See also
440 * Perl_regnode_after() in reginline.h
442 * At the struct level regnodes are a linked list, with each node pointing
443 * at the next (via offsets), usually via the C<next_off> field in the
444 * structure. Where there is a need for a node to have two children the
445 * immediate physical successor of the node in the compiled program is used
446 * to represent one of them. A good example is the BRANCH construct,
447 * consider the pattern C</head(?:[ab]foo|[cd]bar)tail/>
449 * 1: EXACT <head> (3)
452 * 6: EXACT <foo> (14)
455 * 11: EXACT <bar> (14)
457 * 14: EXACT <tail> (16)
460 * The numbers in parens at the end of each line show the "next_off" value
461 * for that regnode in the program. We can see that the C<next_off> of
462 * the first BRANCH node (#3) is the second BRANCH node (#8), and indicates
463 * where execution should go if the regnodes *following* the BRANCH node fail
464 * to accept the input string. Thus to find the "next BRANCH" we would do
465 * C<Perl_regnext()> and follow the C<next_off> pointer, and to find
466 * the "BRANCHes contents" we would use C<REGNODE_AFTER()>.
468 * Be aware that C<REGNODE_AFTER()> is not guaranteed to give a *useful*
469 * result once the regex peephole optimizer has run (it will be correct
470 * however!). By the time code in regexec.c executes various regnodes
471 * may have been optimized out of the the C<next_off> chain. An example
472 * can be seen above, node 13 will never be reached during execution
473 * flow as it has been stitched out of the C<next_off> chain. Both 6 and
474 * 11 would have pointed at it during compilation, but it exists only to
475 * facilitate the construction of the BRANCH structure and is effectively
476 * a NOOP, and thus the optimizer adjusts the links so it is skipped
477 * from execution time flow. In regexec.c it is only safe to use
478 * REGNODE_AFTER() on specific node types.
480 * Conversely during compilation C<Perl_regnext()> may not work properly
481 * as the C<next_off> may not be known until "later", (such as in the
482 * case of BRANCH nodes) and thus in regcomp.c the REGNODE_AFTER() macro
483 * is used very heavily instead.
485 * There are several variants of the REGNODE_AFTER_xxx() macros which
486 * are intended for use in different situations depending on how
487 * confident the code is about what type of node it is trying to find a
490 * So for instance if you know you are dealing with a known node type of
491 * constant size then you should use REGNODE_AFTER_type(n,TYPE).
493 * If you have a regnode pointer and you know you are dealing with a
494 * regnode type of constant size and you have already extracted its
495 * opcode use: REGNODE_AFTER_opcode(n,OPCODE).
497 * If you have a regnode and you know it is variable size then you
498 * you can produce optimized code by using REGNODE_AFTER_varies(n).
500 * If you have a regnode pointer and nothing else use: REGNODE_AFTER(n)
501 * This is the safest option and wraps C<Perl_regnode_after()>. It
502 * should produce the correct result regardless of its argument. The
503 * other options only produce correct results under specific
506 #define REGNODE_AFTER_PLUS(p,extra) ((p) + NODE_STEP_REGNODE + (extra))
507 /* under DEBUGGING we check that all REGNODE_AFTER optimized macros did the
508 * same thing that Perl_regnode_after() would have done. Note that when
509 * not compiled under DEBUGGING the assert_() macro is empty. Thus we
510 * don't have to implement different versions for DEBUGGING and not DEBUGGING,
511 * and explains why all the macros use REGNODE_AFTER_PLUS_DEBUG() under the
513 #define REGNODE_AFTER_PLUS_DEBUG(p,extra) \
514 (assert_(check_regnode_after(p,extra)) REGNODE_AFTER_PLUS((p),(extra)))
516 /* find the regnode after this p by using the opcode we previously extracted
518 #define REGNODE_AFTER_opcode(p,op) REGNODE_AFTER_PLUS_DEBUG((p),PL_regnode_arg_len[op])
520 /* find the regnode after this p by using the size of the struct associated with
521 * the opcode for p. use this when you *know* that p is pointer to a given type*/
522 #define REGNODE_AFTER_type(p,t) REGNODE_AFTER_PLUS_DEBUG((p),EXTRA_SIZE(t))
524 /* find the regnode after this p by using OP(p) to find the regnode type of p */
525 #define REGNODE_AFTER_varies(p) regnode_after(p,TRUE)
527 /* find the regnode after this p by using OP(p) to find the regnode type of p */
528 #define REGNODE_AFTER(p) regnode_after(p,FALSE)
531 /* REGNODE_BEFORE() is trickier to deal with in terms of validation, execution.
532 * All the places that use it assume that p will be one struct regnode large.
533 * So to validate it we do the math to go backwards and then validate that the
534 * type of regnode we landed on is actually one regnode large. In theory if
535 * things go wrong the opcode should be illegal or say the item should be larger
536 * than it is, etc. */
537 #define REGNODE_BEFORE_BASE(p) ((p) - NODE_STEP_REGNODE)
538 #define REGNODE_BEFORE_BASE_DEBUG(p) \
539 (assert_(check_regnode_after(REGNODE_BEFORE_BASE(p),0)) REGNODE_BEFORE_BASE(p))
540 #define REGNODE_BEFORE(p) REGNODE_BEFORE_BASE_DEBUG(p)
542 #define FILL_NODE(offset, op) \
544 OP(REGNODE_p(offset)) = op; \
545 NEXT_OFF(REGNODE_p(offset)) = 0; \
547 #define FILL_ADVANCE_NODE(offset, op) \
549 FILL_NODE(offset, op); \
552 #define FILL_ADVANCE_NODE_ARG(offset, op, arg) \
554 ARG_SET(REGNODE_p(offset), arg); \
555 FILL_ADVANCE_NODE(offset, op); \
556 /* This is used generically for other operations \
557 * that have a longer argument */ \
558 (offset) += PL_regnode_arg_len[op]; \
560 #define FILL_ADVANCE_NODE_ARGp(offset, op, arg) \
562 ARGp_SET(REGNODE_p(offset), arg); \
563 FILL_ADVANCE_NODE(offset, op); \
564 (offset) += PL_regnode_arg_len[op]; \
566 #define FILL_ADVANCE_NODE_2L_ARG(offset, op, arg1, arg2) \
568 ARG_SET(REGNODE_p(offset), arg1); \
569 ARG2L_SET(REGNODE_p(offset), arg2); \
570 FILL_ADVANCE_NODE(offset, op); \
574 /* define these after we define the normal macros, so we can use
575 * ARGp_BYTES_LOC(n) */
578 ARGp_VALUE_inline(struct regnode *node) {
580 memcpy(&ptr, ARGp_BYTES_LOC(node), sizeof(ptr));
586 ARGp_SET_inline(struct regnode *node, SV *ptr) {
587 memcpy(ARGp_BYTES_LOC(node), &ptr, sizeof(ptr));
590 #define REG_MAGIC 0234
592 /* An ANYOF node matches a single code point based on specified criteria. It
593 * now comes in several styles, but originally it was just a 256 element
594 * bitmap, indexed by the code point (which was always just a byte). If the
595 * corresponding bit for a code point is 1, the code point matches; if 0, it
596 * doesn't match (complemented if inverted). This worked fine before Unicode
597 * existed, but making a bit map long enough to accommodate a bit for every
598 * possible Unicode code point is prohibitively large. Therefore it is made
599 * much much smaller, and an inversion list is created to handle code points
600 * not represented by the bitmap. (It is now possible to compile the bitmap to
601 * a larger size to avoid the slower inversion list lookup for however big the
602 * bitmap is set to, but this is rarely done). If the bitmap is sufficient to
603 * specify all possible matches (with nothing outside it matching), no
604 * inversion list is needed nor included, and the argument to the ANYOF node is
605 * set to the following: */
607 #define ANYOF_MATCHES_ALL_OUTSIDE_BITMAP_VALUE U32_MAX
608 #define ANYOF_MATCHES_ALL_OUTSIDE_BITMAP(node) \
609 (ARG(node) == ANYOF_MATCHES_ALL_OUTSIDE_BITMAP_VALUE)
611 #define ANYOF_MATCHES_NONE_OUTSIDE_BITMAP_VALUE \
612 /* Assumes ALL is odd */ (ANYOF_MATCHES_ALL_OUTSIDE_BITMAP_VALUE - 1)
613 #define ANYOF_MATCHES_NONE_OUTSIDE_BITMAP(node) \
614 (ARG(node) == ANYOF_MATCHES_NONE_OUTSIDE_BITMAP_VALUE)
616 #define ANYOF_ONLY_HAS_BITMAP_MASK ANYOF_MATCHES_NONE_OUTSIDE_BITMAP_VALUE
617 #define ANYOF_ONLY_HAS_BITMAP(node) \
618 ((ARG(node) & ANYOF_ONLY_HAS_BITMAP_MASK) == ANYOF_ONLY_HAS_BITMAP_MASK)
620 #define ANYOF_HAS_AUX(node) (! ANYOF_ONLY_HAS_BITMAP(node))
622 /* There are also ANYOFM nodes, used when the bit patterns representing the
623 * matched code points happen to be such that they can be checked by ANDing
624 * with a mask. The regex compiler looks for and silently optimizes to using
625 * this node type in the few cases where it works out. The eight octal digits
626 * form such a group. These nodes are simple and fast and no further
627 * discussion is needed here.
629 * And, there are ANYOFH-ish nodes which match only code points that aren't in
630 * the bitmap (the H stands for High). These are common for expressing
631 * Unicode properties concerning non-Latin scripts. They dispense with the
632 * bitmap altogether and don't need any of the flags discussed below.
634 * And, there are ANYOFR-ish nodes which match within a single range.
636 * When there is a need to specify what matches outside the bitmap, it is done
637 * by allocating an AV as part of the pattern's compiled form, and the argument
638 * to the node instead of being ANYOF_ONLY_HAS_BITMAP, points to that AV.
640 * (Actually, that is an oversimplification. The AV is placed into the
641 * pattern's struct reg_data, and what is stored in the node's argument field
642 * is its index into that struct. And the inversion list is just one element,
643 * the zeroth, of the AV.)
645 * There are certain situations where a single inversion list can't handle all
646 * the complexity. These are dealt with by having extra elements in the AV, by
647 * specifying flag bits in the ANYOF node, and/or special code. As an example,
648 * there are instances where what the ANYOF node matches is not completely
649 * known until runtime. In these cases, a flag is set, and the bitmap has a 1
650 * for the code points which are known at compile time to be 1, and a 0 for the
651 * ones that are known to be 0, or require runtime resolution. Some missing
652 * information can be found by merely seeing if the pattern is UTF-8 or not;
653 * other cases require looking at the extra elements in the AV.
655 * There are 5 cases where the bitmap is insufficient. These are specified by
656 * flags in the node's flags field. We could use five bits to represent the 5
657 * cases, but to save flags bits (which are perennially in short supply), we
658 * play some games. The cases are:
660 * 1) As already mentioned, if some code points outside the bitmap match, and
661 * some do not, an inversion list is specified to indicate which ones.
663 * 2) Under /d rules, it can happen that code points that are in the upper
664 * latin1 range (\x80-\xFF or their equivalents on EBCDIC platforms) match
665 * only if the runtime target string being matched against is UTF-8. For
666 * example /[\w[:punct:]]/d. This happens only for certain posix classes,
667 * and all such ones also have above-bitmap matches.
669 * Note that /d rules are no longer encouraged; 'use 5.14' or higher
670 * deselects them. But they are still supported, and a flag is required
671 * so that they can be properly handled. But it can be a shared flag: see
674 * 3) Also under /d rules, something like /[\Wfoo]/ will match everything in
675 * the \x80-\xFF range, unless the string being matched against is UTF-8.
676 * An inversion list could be created for this case, but this is
677 * relatively common, and it turns out that it's all or nothing: if any
678 * one of these code points matches, they all do. Hence a single bit
679 * suffices. We use a shared flag that doesn't take up space by itself:
680 * ANYOFD_NON_UTF8_MATCHES_ALL_NON_ASCII__shared. This also means there
681 * is an inversion list for the things that don't fit into the bitmap.
683 * 4) A user-defined \p{} property may not have been defined by the time the
684 * regex is compiled. In this case, we don't know until runtime what it
685 * will match, so we have to assume it could match anything, including
686 * code points that ordinarily would be in the bitmap. A flag bit is
687 * necessary to indicate this, though we can use the
688 * ANYOF_HAS_EXTRA_RUNTIME_MATCHES flag, along with the node not being
689 * ANYOFD. The information required to construct the property is stored
690 * in the AV pointed to by the node's argument. This case is quite
691 * uncommon in the field, and the /(?[...])/ construct is a better way to
692 * accomplish what this feature does.
694 * 5) /[foo]/il may have folds that are only valid if the runtime locale is a
695 * UTF-8 one. The ANYOF_HAS_EXTRA_RUNTIME_MATCHES flag can also be used
696 * for these. The list is stored in a different element of the AV, so its
697 * existence differentiates this case from that of 4), along with the node
698 * being ANYOFL, with the ANYOFL_FOLD flag being set. There are a few
699 * additional folds valid only if the UTF-8 locale is a Turkic one which
700 * is tested for explicitly.
702 * Note that the user-defined property flag and the /il flag can affect whether
703 * an ASCII character matches in the bitmap or not.
705 * And this still isn't the end of the story. In some cases, warnings are
706 * supposed to be raised when matching certain categories of code points in the
707 * target string. Flags are set to indicate this. This adds up to a bunch of
708 * flags required, and we only have 8 available. That is why we share some.
709 * At the moment, there are two spare flag bits, but this could be increased by
712 * ANYOF_MATCHES_POSIXL is redundant with the node type ANYOFPOSIXL. That flag
713 * could be removed, but at the expense of having to write extra code, which
714 * would take up space, and writing this turns out to be not hard, but not
717 * If this is done, an extension would be to make all ANYOFL nodes contain the
718 * extra 32 bits that ANYOFPOSIXL ones do, doubling each instance's size. The
719 * posix flags only occupy 30 bits, so the ANYOFL_FOLD and
720 * ANYOFL_UTF8_LOCALE_REQD bits could be moved to that extra space, but it
721 * would also mean extra instructions, as there are currently places in the
722 * code that assume those two bits are zero.
724 * Some flags are not used in synthetic start class (SSC) nodes, so could be
725 * shared should new flags be needed for SSCs, like SSC_MATCHES_EMPTY_STRING
728 /* If this is set, the result of the match should be complemented. regexec.c
729 * is expecting this to be in the low bit. Never in an SSC */
730 #define ANYOF_INVERT 0x01
732 /* For the SSC node only, which cannot be inverted, so is shared with that bit.
733 * This is used only during regex compilation. */
734 #define SSC_MATCHES_EMPTY_STRING ANYOF_INVERT
736 /* Set if this is a regnode_charclass_posixl vs a regnode_charclass. This
737 * is used for runtime \d, \w, [:posix:], ..., which are used only in locale
738 * and the optimizer's synthetic start class. Non-locale \d, etc are resolved
739 * at compile-time. Only set under /l; can be in SSC */
740 #define ANYOF_MATCHES_POSIXL 0x02
742 /* The fold is calculated and stored in the bitmap where possible at compile
743 * time. However under locale, the actual folding varies depending on
744 * what the locale is at the time of execution, so it has to be deferred until
745 * then. Only set under /l; never in an SSC */
746 #define ANYOFL_FOLD 0x04
748 /* Warn if the runtime locale isn't a UTF-8 one (and the generated node assumes
750 #define ANYOFL_UTF8_LOCALE_REQD 0x08
752 /* Spare: Be sure to change ANYOF_FLAGS_ALL if this gets used 0x10 */
754 /* Spare: Be sure to change ANYOF_FLAGS_ALL if this gets used 0x20 */
756 /* Shared bit that indicates that there are potential additional matches stored
757 * outside the bitmap, as pointed to by the AV given by the node's argument.
758 * The node type is used at runtime (in conjunction with this flag and other
759 * information available then) to decide if the flag should be acted upon.
760 * This extra information is needed because of at least one of the following
762 * Under /d and the matched string is in UTF-8, it means the ANYOFD node
763 * matches more things than in the bitmap. Those things will be any
764 * code point too high for the bitmap, but crucially, any non-ASCII
765 * characters that match iff when using Unicode rules. These all are
768 * Under /l and ANYOFL_FOLD is set, this flag may indicate there are
769 * potential matches valid only if the locale is a UTF-8 one. If so,
770 * a list of them is stored in the AV.
772 * For any non-ANYOFD node, there may be a user-defined property that
773 * wasn't yet defined at the time the regex was compiled, and so must
774 * be looked up at runtime, The information required to do so will
777 * Note that an ANYOFL node may contain both a user-defined property, and
778 * folds not always valid. The important thing is that there is an AV to
780 #define ANYOF_HAS_EXTRA_RUNTIME_MATCHES 0x40
783 * Under /d it means the ANYOFD node matches all non-ASCII Latin1
784 * characters when the target string is not in utf8.
785 * When not under /d, it means the ANYOF node should raise a warning if
786 * matching against an above-Unicode code point.
787 * (These uses are mutually exclusive because the warning requires a \p{}, and
788 * \p{} implies /u which deselects /d). An SSC node only has this bit set if
789 * what is meant is the warning. The names are to make sure that you are
790 * cautioned about its shared nature */
791 #define ANYOFD_NON_UTF8_MATCHES_ALL_NON_ASCII__shared 0x80
792 #define ANYOF_WARN_SUPER__shared 0x80
794 #define ANYOF_FLAGS_ALL ((U8) ~(0x10|0x20))
796 #define ANYOF_LOCALE_FLAGS ( ANYOFL_FOLD \
797 | ANYOF_MATCHES_POSIXL \
798 | ANYOFL_UTF8_LOCALE_REQD)
800 /* These are the flags that apply to both regular ANYOF nodes and synthetic
801 * start class nodes during construction of the SSC. During finalization of
802 * the SSC, other of the flags may get added to it */
803 #define ANYOF_COMMON_FLAGS 0
805 /* Character classes for node->classflags of ANYOF */
806 /* Should be synchronized with a table in regprop() */
807 /* 2n should be the normal one, paired with its complement at 2n+1 */
809 #define ANYOF_ALPHA ((CC_ALPHA_) * 2)
810 #define ANYOF_NALPHA ((ANYOF_ALPHA) + 1)
811 #define ANYOF_ALPHANUMERIC ((CC_ALPHANUMERIC_) * 2) /* [[:alnum:]] isalnum(3), utf8::IsAlnum */
812 #define ANYOF_NALPHANUMERIC ((ANYOF_ALPHANUMERIC) + 1)
813 #define ANYOF_ASCII ((CC_ASCII_) * 2)
814 #define ANYOF_NASCII ((ANYOF_ASCII) + 1)
815 #define ANYOF_BLANK ((CC_BLANK_) * 2) /* GNU extension: space and tab: non-vertical space */
816 #define ANYOF_NBLANK ((ANYOF_BLANK) + 1)
817 #define ANYOF_CASED ((CC_CASED_) * 2) /* Pseudo class for [:lower:] or
818 [:upper:] under /i */
819 #define ANYOF_NCASED ((ANYOF_CASED) + 1)
820 #define ANYOF_CNTRL ((CC_CNTRL_) * 2)
821 #define ANYOF_NCNTRL ((ANYOF_CNTRL) + 1)
822 #define ANYOF_DIGIT ((CC_DIGIT_) * 2) /* \d */
823 #define ANYOF_NDIGIT ((ANYOF_DIGIT) + 1)
824 #define ANYOF_GRAPH ((CC_GRAPH_) * 2)
825 #define ANYOF_NGRAPH ((ANYOF_GRAPH) + 1)
826 #define ANYOF_LOWER ((CC_LOWER_) * 2)
827 #define ANYOF_NLOWER ((ANYOF_LOWER) + 1)
828 #define ANYOF_PRINT ((CC_PRINT_) * 2)
829 #define ANYOF_NPRINT ((ANYOF_PRINT) + 1)
830 #define ANYOF_PUNCT ((CC_PUNCT_) * 2)
831 #define ANYOF_NPUNCT ((ANYOF_PUNCT) + 1)
832 #define ANYOF_SPACE ((CC_SPACE_) * 2) /* \s */
833 #define ANYOF_NSPACE ((ANYOF_SPACE) + 1)
834 #define ANYOF_UPPER ((CC_UPPER_) * 2)
835 #define ANYOF_NUPPER ((ANYOF_UPPER) + 1)
836 #define ANYOF_WORDCHAR ((CC_WORDCHAR_) * 2) /* \w, PL_utf8_alnum, utf8::IsWord, ALNUM */
837 #define ANYOF_NWORDCHAR ((ANYOF_WORDCHAR) + 1)
838 #define ANYOF_XDIGIT ((CC_XDIGIT_) * 2)
839 #define ANYOF_NXDIGIT ((ANYOF_XDIGIT) + 1)
841 /* pseudo classes below this, not stored in the class bitmap, but used as flags
842 during compilation of char classes */
844 #define ANYOF_VERTWS ((CC_VERTSPACE_) * 2)
845 #define ANYOF_NVERTWS ((ANYOF_VERTWS)+1)
847 /* It is best if this is the last one, as all above it are stored as bits in a
848 * bitmap, and it isn't part of that bitmap */
849 #if CC_VERTSPACE_ != HIGHEST_REGCOMP_DOT_H_SYNC_
850 # error Problem with handy.h HIGHEST_REGCOMP_DOT_H_SYNC_ #define
853 #define ANYOF_POSIXL_MAX (ANYOF_VERTWS) /* So upper loop limit is written:
855 * Hence doesn't include VERTWS, as that
856 * is a pseudo class */
857 #define ANYOF_MAX ANYOF_POSIXL_MAX
859 #if (ANYOF_POSIXL_MAX > 32) /* Must fit in 32-bit word */
860 # error Problem with handy.h CC_foo_ #defines
863 #define ANYOF_HORIZWS ((ANYOF_POSIXL_MAX)+2) /* = (ANYOF_NVERTWS + 1) */
864 #define ANYOF_NHORIZWS ((ANYOF_POSIXL_MAX)+3)
866 #define ANYOF_UNIPROP ((ANYOF_POSIXL_MAX)+4) /* Used to indicate a Unicode
867 property: \p{} or \P{} */
869 /* Backward source code compatibility. */
871 #define ANYOF_ALNUML ANYOF_ALNUM
872 #define ANYOF_NALNUML ANYOF_NALNUM
873 #define ANYOF_SPACEL ANYOF_SPACE
874 #define ANYOF_NSPACEL ANYOF_NSPACE
875 #define ANYOF_ALNUM ANYOF_WORDCHAR
876 #define ANYOF_NALNUM ANYOF_NWORDCHAR
878 /* Utility macros for the bitmap and classes of ANYOF */
880 #define BITMAP_BYTE(p, c) (( (U8*) (p)) [ ( ( (UV) (c)) >> 3) ] )
881 #define BITMAP_BIT(c) (1U << ((c) & 7))
882 #define BITMAP_TEST(p, c) (BITMAP_BYTE(p, c) & BITMAP_BIT((U8)(c)))
884 #define ANYOF_FLAGS(p) ((p)->flags)
886 #define ANYOF_BIT(c) BITMAP_BIT(c)
888 #define ANYOF_POSIXL_BITMAP(p) (((regnode_charclass_posixl*) (p))->classflags)
890 #define POSIXL_SET(field, c) ((field) |= (1U << (c)))
891 #define ANYOF_POSIXL_SET(p, c) POSIXL_SET(ANYOF_POSIXL_BITMAP(p), (c))
893 #define POSIXL_CLEAR(field, c) ((field) &= ~ (1U <<(c)))
894 #define ANYOF_POSIXL_CLEAR(p, c) POSIXL_CLEAR(ANYOF_POSIXL_BITMAP(p), (c))
896 #define POSIXL_TEST(field, c) ((field) & (1U << (c)))
897 #define ANYOF_POSIXL_TEST(p, c) POSIXL_TEST(ANYOF_POSIXL_BITMAP(p), (c))
899 #define POSIXL_ZERO(field) STMT_START { (field) = 0; } STMT_END
900 #define ANYOF_POSIXL_ZERO(ret) POSIXL_ZERO(ANYOF_POSIXL_BITMAP(ret))
902 #define ANYOF_POSIXL_SET_TO_BITMAP(p, bits) \
903 STMT_START { ANYOF_POSIXL_BITMAP(p) = (bits); } STMT_END
905 /* Shifts a bit to get, eg. 0x4000_0000, then subtracts 1 to get 0x3FFF_FFFF */
906 #define ANYOF_POSIXL_SETALL(ret) \
908 ANYOF_POSIXL_BITMAP(ret) = nBIT_MASK(ANYOF_POSIXL_MAX); \
910 #define ANYOF_CLASS_SETALL(ret) ANYOF_POSIXL_SETALL(ret)
912 #define ANYOF_POSIXL_TEST_ANY_SET(p) \
913 ((ANYOF_FLAGS(p) & ANYOF_MATCHES_POSIXL) && ANYOF_POSIXL_BITMAP(p))
914 #define ANYOF_CLASS_TEST_ANY_SET(p) ANYOF_POSIXL_TEST_ANY_SET(p)
916 /* Since an SSC always has this field, we don't have to test for that; nor do
917 * we want to because the bit isn't set for SSC during its construction */
918 #define ANYOF_POSIXL_SSC_TEST_ANY_SET(p) \
919 cBOOL(((regnode_ssc*)(p))->classflags)
920 #define ANYOF_POSIXL_SSC_TEST_ALL_SET(p) /* Are all bits set? */ \
921 (((regnode_ssc*) (p))->classflags \
922 == nBIT_MASK(ANYOF_POSIXL_MAX))
924 #define ANYOF_POSIXL_TEST_ALL_SET(p) \
925 ((ANYOF_FLAGS(p) & ANYOF_MATCHES_POSIXL) \
926 && ANYOF_POSIXL_BITMAP(p) == nBIT_MASK(ANYOF_POSIXL_MAX))
928 #define ANYOF_POSIXL_OR(source, dest) STMT_START { (dest)->classflags |= (source)->classflags ; } STMT_END
929 #define ANYOF_CLASS_OR(source, dest) ANYOF_POSIXL_OR((source), (dest))
931 #define ANYOF_POSIXL_AND(source, dest) STMT_START { (dest)->classflags &= (source)->classflags ; } STMT_END
933 #define ANYOF_BITMAP_ZERO(ret) Zero(((regnode_charclass*)(ret))->bitmap, ANYOF_BITMAP_SIZE, char)
934 #define ANYOF_BITMAP(p) ((regnode_charclass*)(p))->bitmap
935 #define ANYOF_BITMAP_BYTE(p, c) BITMAP_BYTE(ANYOF_BITMAP(p), c)
936 #define ANYOF_BITMAP_SET(p, c) (ANYOF_BITMAP_BYTE(p, c) |= ANYOF_BIT(c))
937 #define ANYOF_BITMAP_CLEAR(p,c) (ANYOF_BITMAP_BYTE(p, c) &= ~ANYOF_BIT(c))
938 #define ANYOF_BITMAP_TEST(p, c) cBOOL(ANYOF_BITMAP_BYTE(p, c) & ANYOF_BIT(c))
940 #define ANYOF_BITMAP_SETALL(p) \
941 memset (ANYOF_BITMAP(p), 255, ANYOF_BITMAP_SIZE)
942 #define ANYOF_BITMAP_CLEARALL(p) \
943 Zero (ANYOF_BITMAP(p), ANYOF_BITMAP_SIZE)
946 * Utility definitions.
949 # define UCHARAT(p) ((int)*(const U8*)(p))
951 # define UCHARAT(p) ((int)*(p)&CHARMASK)
954 /* Number of regnode equivalents that 'guy' occupies beyond the size of the
955 * smallest regnode. */
956 #define EXTRA_SIZE(guy) ((sizeof(guy)-1)/sizeof(struct regnode))
958 #define REG_ZERO_LEN_SEEN 0x00000001
959 #define REG_LOOKBEHIND_SEEN 0x00000002
960 /* add a short form alias to keep the line length police happy */
961 #define REG_LB_SEEN REG_LOOKBEHIND_SEEN
962 #define REG_GPOS_SEEN 0x00000004
964 #define REG_RECURSE_SEEN 0x00000020
965 #define REG_TOP_LEVEL_BRANCHES_SEEN 0x00000040
966 #define REG_VERBARG_SEEN 0x00000080
967 #define REG_CUTGROUP_SEEN 0x00000100
968 #define REG_RUN_ON_COMMENT_SEEN 0x00000200
969 #define REG_UNFOLDED_MULTI_SEEN 0x00000400
971 #define REG_UNBOUNDED_QUANTIFIER_SEEN 0x00001000
976 #ifdef PLUGGABLE_RE_EXTENSION
977 #include "re_nodes.h"
979 #include "regnodes.h"
982 #ifndef PLUGGABLE_RE_EXTENSION
984 EXTCONST regexp_engine PL_core_reg_engine;
986 EXTCONST regexp_engine PL_core_reg_engine = {
989 Perl_re_intuit_start,
990 Perl_re_intuit_string,
991 Perl_regfree_internal,
992 Perl_reg_numbered_buff_fetch,
993 Perl_reg_numbered_buff_store,
994 Perl_reg_numbered_buff_length,
996 Perl_reg_named_buff_iter,
998 #if defined(USE_ITHREADS)
999 Perl_regdupe_internal,
1004 #endif /* PLUGGABLE_RE_EXTENSION */
1010 /* .what is a character array with one character for each member of .data
1011 * The character describes the function of the corresponding .data item:
1012 * a - AV for paren_name_list under DEBUGGING
1013 * f - start-class data for regstclass optimization
1014 * l - start op for literal (?{EVAL}) item
1015 * L - start op for literal (?{EVAL}) item, with separate CV (qr//)
1016 * r - pointer to an embedded code-containing qr, e.g. /ab$qr/
1017 * s - inversion list for Unicode-style character class, and the
1018 * multicharacter strings resulting from casefolding the single-character
1019 * entries in the character class
1021 * u - trie struct's widecharmap (a HV, so can't share, must dup)
1022 * also used for revcharmap and words under DEBUGGING
1023 * T - aho-trie struct
1024 * S - sv for named capture lookup
1025 * 20010712 mjd@plover.com
1026 * (Remember to update re_dup() and pregfree() if you add any items.)
1034 /* Code in S_to_utf8_substr() and S_to_byte_substr() in regexec.c accesses
1035 anchored* and float* via array indexes 0 and 1. */
1036 #define anchored_substr substrs->data[0].substr
1037 #define anchored_utf8 substrs->data[0].utf8_substr
1038 #define anchored_offset substrs->data[0].min_offset
1039 #define anchored_end_shift substrs->data[0].end_shift
1041 #define float_substr substrs->data[1].substr
1042 #define float_utf8 substrs->data[1].utf8_substr
1043 #define float_min_offset substrs->data[1].min_offset
1044 #define float_max_offset substrs->data[1].max_offset
1045 #define float_end_shift substrs->data[1].end_shift
1047 #define check_substr substrs->data[2].substr
1048 #define check_utf8 substrs->data[2].utf8_substr
1049 #define check_offset_min substrs->data[2].min_offset
1050 #define check_offset_max substrs->data[2].max_offset
1051 #define check_end_shift substrs->data[2].end_shift
1053 #define RX_ANCHORED_SUBSTR(rx) (ReANY(rx)->anchored_substr)
1054 #define RX_ANCHORED_UTF8(rx) (ReANY(rx)->anchored_utf8)
1055 #define RX_FLOAT_SUBSTR(rx) (ReANY(rx)->float_substr)
1056 #define RX_FLOAT_UTF8(rx) (ReANY(rx)->float_utf8)
1058 /* trie related stuff */
1060 /* a transition record for the state machine. the
1061 check field determines which state "owns" the
1062 transition. the char the transition is for is
1063 determined by offset from the owning states base
1064 field. the next field determines which state
1065 is to be transitioned to if any.
1067 struct _reg_trie_trans {
1072 /* a transition list element for the list based representation */
1073 struct _reg_trie_trans_list_elem {
1077 typedef struct _reg_trie_trans_list_elem reg_trie_trans_le;
1079 /* a state for compressed nodes. base is an offset
1080 into an array of reg_trie_trans array. If wordnum is
1081 nonzero the state is accepting. if base is zero then
1082 the state has no children (and will be accepting)
1084 struct _reg_trie_state {
1088 reg_trie_trans_le* list;
1092 /* info per word; indexed by wordnum */
1094 U16 prev; /* previous word in acceptance chain; eg in
1095 * zzz|abc|ab/ after matching the chars abc, the
1096 * accepted word is #2, and the previous accepted
1098 U32 len; /* how many chars long is this word? */
1099 U32 accept; /* accept state for this word */
1100 } reg_trie_wordinfo;
1103 typedef struct _reg_trie_state reg_trie_state;
1104 typedef struct _reg_trie_trans reg_trie_trans;
1107 /* anything in here that needs to be freed later
1108 should be dealt with in pregfree.
1109 refcount is first in both this and _reg_ac_data to allow a space
1110 optimisation in Perl_regdupe. */
1111 struct _reg_trie_data {
1112 U32 refcount; /* number of times this trie is referenced */
1113 U32 lasttrans; /* last valid transition element */
1114 U16 *charmap; /* byte to charid lookup array */
1115 reg_trie_state *states; /* state data */
1116 reg_trie_trans *trans; /* array of transition elements */
1117 char *bitmap; /* stclass bitmap */
1118 U16 *jump; /* optional 1 indexed array of offsets before tail
1119 for the node following a given word. */
1120 reg_trie_wordinfo *wordinfo; /* array of info per word */
1121 U16 uniquecharcount; /* unique chars in trie (width of trans table) */
1122 U32 startstate; /* initial state - used for common prefix optimisation */
1123 STRLEN minlen; /* minimum length of words in trie - build/opt only? */
1124 STRLEN maxlen; /* maximum length of words in trie - build/opt only? */
1125 U32 prefixlen; /* #chars in common prefix */
1126 U32 statecount; /* Build only - number of states in the states array
1127 (including the unused zero state) */
1128 U32 wordcount; /* Build only */
1130 STRLEN charcount; /* Build only */
1133 /* There is one (3 under DEBUGGING) pointers that logically belong in this
1134 structure, but are held outside as they need duplication on thread cloning,
1135 whereas the rest of the structure can be read only:
1136 HV *widecharmap; code points > 255 to charid
1138 AV *words; Array of words contained in trie, for dumping
1139 AV *revcharmap; Map of each charid back to its character representation
1143 #define TRIE_WORDS_OFFSET 2
1145 typedef struct _reg_trie_data reg_trie_data;
1147 /* refcount is first in both this and _reg_trie_data to allow a space
1148 optimisation in Perl_regdupe. */
1149 struct _reg_ac_data {
1153 reg_trie_state *states;
1155 typedef struct _reg_ac_data reg_ac_data;
1157 /* ANY_BIT doesn't use the structure, so we can borrow it here.
1158 This is simpler than refactoring all of it as wed end up with
1159 three different sets... */
1161 #define TRIE_BITMAP(p) (((reg_trie_data *)(p))->bitmap)
1162 #define TRIE_BITMAP_BYTE(p, c) BITMAP_BYTE(TRIE_BITMAP(p), c)
1163 #define TRIE_BITMAP_SET(p, c) (TRIE_BITMAP_BYTE(p, c) |= ANYOF_BIT((U8)c))
1164 #define TRIE_BITMAP_CLEAR(p,c) (TRIE_BITMAP_BYTE(p, c) &= ~ANYOF_BIT((U8)c))
1165 #define TRIE_BITMAP_TEST(p, c) (TRIE_BITMAP_BYTE(p, c) & ANYOF_BIT((U8)c))
1167 #define IS_ANYOF_TRIE(op) ((op)==TRIEC || (op)==AHOCORASICKC)
1168 #define IS_TRIE_AC(op) ((op)>=AHOCORASICK)
1170 /* these defines assume uniquecharcount is the correct variable, and state may be evaluated twice */
1171 #define TRIE_NODENUM(state) (((state)-1)/(trie->uniquecharcount)+1)
1172 #define SAFE_TRIE_NODENUM(state) ((state) ? (((state)-1)/(trie->uniquecharcount)+1) : (state))
1173 #define TRIE_NODEIDX(state) ((state) ? (((state)-1)*(trie->uniquecharcount)+1) : (state))
1176 #define TRIE_CHARCOUNT(trie) ((trie)->charcount)
1178 #define TRIE_CHARCOUNT(trie) (trie_charcount)
1181 #define RE_TRIE_MAXBUF_INIT 65536
1182 #define RE_TRIE_MAXBUF_NAME "\022E_TRIE_MAXBUF"
1183 #define RE_DEBUG_FLAGS "\022E_DEBUG_FLAGS"
1185 #define RE_COMPILE_RECURSION_INIT 1000
1186 #define RE_COMPILE_RECURSION_LIMIT "\022E_COMPILE_RECURSION_LIMIT"
1190 RE_DEBUG_FLAGS is used to control what debug output is emitted
1191 its divided into three groups of options, some of which interact.
1192 The three groups are: Compile, Execute, Extra. There is room for a
1193 further group, as currently only the low three bytes are used.
1212 If you modify any of these make sure you make corresponding changes to
1213 re.pm, especially to the documentation.
1219 #define RE_DEBUG_COMPILE_MASK 0x0000FF
1220 #define RE_DEBUG_COMPILE_PARSE 0x000001
1221 #define RE_DEBUG_COMPILE_OPTIMISE 0x000002
1222 #define RE_DEBUG_COMPILE_TRIE 0x000004
1223 #define RE_DEBUG_COMPILE_DUMP 0x000008
1224 #define RE_DEBUG_COMPILE_FLAGS 0x000010
1225 #define RE_DEBUG_COMPILE_TEST 0x000020
1228 #define RE_DEBUG_EXECUTE_MASK 0x00FF00
1229 #define RE_DEBUG_EXECUTE_INTUIT 0x000100
1230 #define RE_DEBUG_EXECUTE_MATCH 0x000200
1231 #define RE_DEBUG_EXECUTE_TRIE 0x000400
1234 #define RE_DEBUG_EXTRA_MASK 0x3FF0000
1235 #define RE_DEBUG_EXTRA_TRIE 0x0010000
1236 #define RE_DEBUG_EXTRA_STATE 0x0080000
1237 #define RE_DEBUG_EXTRA_OPTIMISE 0x0100000
1238 #define RE_DEBUG_EXTRA_BUFFERS 0x0400000
1239 #define RE_DEBUG_EXTRA_GPOS 0x0800000
1240 #define RE_DEBUG_EXTRA_DUMP_PRE_OPTIMIZE 0x1000000
1241 #define RE_DEBUG_EXTRA_WILDCARD 0x2000000
1243 #define RE_DEBUG_EXTRA_STACK 0x0280000
1245 #define RE_DEBUG_FLAG(x) (re_debug_flags & (x))
1247 #define DEBUG_COMPILE_r(x) DEBUG_r( \
1248 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_MASK)) x )
1249 #define DEBUG_PARSE_r(x) DEBUG_r( \
1250 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_PARSE)) x )
1251 #define DEBUG_OPTIMISE_r(x) DEBUG_r( \
1252 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_OPTIMISE)) x )
1253 #define DEBUG_DUMP_r(x) DEBUG_r( \
1254 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_DUMP)) x )
1255 #define DEBUG_TRIE_COMPILE_r(x) DEBUG_r( \
1256 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_TRIE)) x )
1257 #define DEBUG_FLAGS_r(x) DEBUG_r( \
1258 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_FLAGS)) x )
1259 #define DEBUG_TEST_r(x) DEBUG_r( \
1260 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_TEST)) x )
1262 #define DEBUG_EXECUTE_r(x) DEBUG_r( \
1263 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_MASK)) x )
1264 #define DEBUG_INTUIT_r(x) DEBUG_r( \
1265 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_INTUIT)) x )
1266 #define DEBUG_MATCH_r(x) DEBUG_r( \
1267 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_MATCH)) x )
1268 #define DEBUG_TRIE_EXECUTE_r(x) DEBUG_r( \
1269 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXECUTE_TRIE)) x )
1272 #define DEBUG_EXTRA_r(x) DEBUG_r( \
1273 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_MASK)) x )
1274 #define DEBUG_STATE_r(x) DEBUG_r( \
1275 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_STATE)) x )
1276 #define DEBUG_STACK_r(x) DEBUG_r( \
1277 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_STACK)) x )
1278 #define DEBUG_BUFFERS_r(x) DEBUG_r( \
1279 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_BUFFERS)) x )
1281 #define DEBUG_OPTIMISE_MORE_r(x) DEBUG_r( \
1282 if (DEBUG_v_TEST || ((RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE) == \
1283 RE_DEBUG_FLAG(RE_DEBUG_EXTRA_OPTIMISE|RE_DEBUG_COMPILE_OPTIMISE))) x )
1284 #define DEBUG_TRIE_COMPILE_MORE_r(x) DEBUG_TRIE_COMPILE_r( \
1285 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_TRIE)) x )
1286 #define DEBUG_TRIE_EXECUTE_MORE_r(x) DEBUG_TRIE_EXECUTE_r( \
1287 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_TRIE)) x )
1289 #define DEBUG_TRIE_r(x) DEBUG_r( \
1290 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_COMPILE_TRIE \
1291 | RE_DEBUG_EXECUTE_TRIE )) x )
1292 #define DEBUG_GPOS_r(x) DEBUG_r( \
1293 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_GPOS)) x )
1295 #define DEBUG_DUMP_PRE_OPTIMIZE_r(x) DEBUG_r( \
1296 if (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_DUMP_PRE_OPTIMIZE)) x )
1298 /* initialization */
1299 /* Get the debug flags for code not in regcomp.c nor regexec.c. This doesn't
1300 * initialize the variable if it isn't already there, instead it just assumes
1301 * the flags are 0 */
1302 #define DECLARE_AND_GET_RE_DEBUG_FLAGS_NON_REGEX \
1303 volatile IV re_debug_flags = 0; PERL_UNUSED_VAR(re_debug_flags); \
1305 SV * re_debug_flags_sv = NULL; \
1306 /* get_sv() can return NULL during global destruction. */ \
1307 re_debug_flags_sv = PL_curcop ? get_sv(RE_DEBUG_FLAGS, GV_ADD) : NULL; \
1308 if (re_debug_flags_sv && SvIOK(re_debug_flags_sv)) \
1309 re_debug_flags=SvIV(re_debug_flags_sv); \
1315 /* For use in regcomp.c and regexec.c, Get the debug flags, and initialize to
1316 * the defaults if not done already */
1317 #define DECLARE_AND_GET_RE_DEBUG_FLAGS \
1318 volatile IV re_debug_flags = 0; PERL_UNUSED_VAR(re_debug_flags); \
1320 SV * re_debug_flags_sv = NULL; \
1321 /* get_sv() can return NULL during global destruction. */ \
1322 re_debug_flags_sv = PL_curcop ? get_sv(RE_DEBUG_FLAGS, GV_ADD) : NULL; \
1323 if (re_debug_flags_sv) { \
1324 if (!SvIOK(re_debug_flags_sv)) /* If doesnt exist set to default */\
1325 sv_setuv(re_debug_flags_sv, \
1326 /* These defaults should be kept in sync with re.pm */ \
1327 RE_DEBUG_COMPILE_DUMP | RE_DEBUG_EXECUTE_MASK ); \
1328 re_debug_flags=SvIV(re_debug_flags_sv); \
1332 #define isDEBUG_WILDCARD (DEBUG_v_TEST || RE_DEBUG_FLAG(RE_DEBUG_EXTRA_WILDCARD))
1334 #define RE_PV_COLOR_DECL(rpv,rlen,isuni,dsv,pv,l,m,c1,c2) \
1335 const char * const rpv = \
1336 pv_pretty((dsv), (pv), (l), (m), \
1337 PL_colors[(c1)],PL_colors[(c2)], \
1338 PERL_PV_ESCAPE_RE|PERL_PV_ESCAPE_NONASCII |((isuni) ? PERL_PV_ESCAPE_UNI : 0) ); \
1339 const int rlen = SvCUR(dsv)
1341 /* This is currently unsed in the core */
1342 #define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m) \
1343 const char * const rpv = \
1344 pv_pretty((dsv), (SvPV_nolen_const(sv)), (SvCUR(sv)), (m), \
1345 PL_colors[(c1)],PL_colors[(c2)], \
1346 PERL_PV_ESCAPE_RE|PERL_PV_ESCAPE_NONASCII |((isuni) ? PERL_PV_ESCAPE_UNI : 0) )
1348 #define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m) \
1349 const char * const rpv = \
1350 pv_pretty((dsv), (pv), (l), (m), \
1351 PL_colors[0], PL_colors[1], \
1352 ( PERL_PV_PRETTY_QUOTE | PERL_PV_ESCAPE_RE | PERL_PV_ESCAPE_NONASCII | PERL_PV_PRETTY_ELLIPSES | \
1353 ((isuni) ? PERL_PV_ESCAPE_UNI : 0)) \
1356 #define RE_SV_DUMPLEN(ItEm) (SvCUR(ItEm) - (SvTAIL(ItEm)!=0))
1357 #define RE_SV_TAIL(ItEm) (SvTAIL(ItEm) ? "$" : "")
1359 #else /* if not DEBUGGING */
1361 #define DECLARE_AND_GET_RE_DEBUG_FLAGS dNOOP
1362 #define RE_PV_COLOR_DECL(rpv,rlen,isuni,dsv,pv,l,m,c1,c2) dNOOP
1363 #define RE_SV_ESCAPE(rpv,isuni,dsv,sv,m)
1364 #define RE_PV_QUOTED_DECL(rpv,isuni,dsv,pv,l,m) dNOOP
1365 #define RE_SV_DUMPLEN(ItEm)
1366 #define RE_SV_TAIL(ItEm)
1367 #define isDEBUG_WILDCARD 0
1369 #endif /* DEBUG RELATED DEFINES */
1371 #define FIRST_NON_ASCII_DECIMAL_DIGIT 0x660 /* ARABIC_INDIC_DIGIT_ZERO */
1374 TRADITIONAL_BOUND = CC_WORDCHAR_,
1381 /* This unpacks the FLAGS field of ANYOF[HR]x nodes. The value it contains
1382 * gives the strict lower bound for the UTF-8 start byte of any code point
1383 * matchable by the node, and a loose upper bound as well.
1385 * The low bound is stored as 0xC0 + ((the upper 6 bits) >> 2)
1386 * The loose upper bound is determined from the lowest 2 bits and the low bound
1387 * (called x) as follows:
1389 * 11 The upper limit of the range can be as much as (EF - x) / 8
1390 * 10 The upper limit of the range can be as much as (EF - x) / 4
1391 * 01 The upper limit of the range can be as much as (EF - x) / 2
1392 * 00 The upper limit of the range can be as much as EF
1394 * For motivation of this design, see commit message in
1395 * 3146c00a633e9cbed741e10146662fbcedfdb8d3 */
1397 # define MAX_ANYOF_HRx_BYTE 0xF4
1399 # define MAX_ANYOF_HRx_BYTE 0xEF
1401 #define LOWEST_ANYOF_HRx_BYTE(b) (((b) >> 2) + 0xC0)
1402 #define HIGHEST_ANYOF_HRx_BYTE(b) \
1403 (LOWEST_ANYOF_HRx_BYTE(b) \
1404 + ((MAX_ANYOF_HRx_BYTE - LOWEST_ANYOF_HRx_BYTE(b)) >> ((b) & 3)))
1406 #if !defined(PERL_IN_XSUB_RE) || defined(PLUGGABLE_RE_EXTENSION)
1407 # define GET_REGCLASS_AUX_DATA(a,b,c,d,e,f) get_regclass_aux_data(a,b,c,d,e,f)
1409 # define GET_REGCLASS_AUX_DATA(a,b,c,d,e,f) get_re_gclass_aux_data(a,b,c,d,e,f)
1412 #if defined(PERL_IN_REGCOMP_C) || defined(PERL_IN_REGEXEC_C)
1413 #include "reginline.h"
1416 #endif /* PERL_REGCOMP_H_ */
1419 * ex: set ts=8 sts=4 sw=4 et: