1e18e3516Sopenharmony_ciTechnical Notes about PCRE2 2e18e3516Sopenharmony_ci--------------------------- 3e18e3516Sopenharmony_ci 4e18e3516Sopenharmony_ciThese are very rough technical notes that record potentially useful information 5e18e3516Sopenharmony_ciabout PCRE2 internals. PCRE2 is a library based on the original PCRE library, 6e18e3516Sopenharmony_cibut with a revised (and incompatible) API. To avoid confusion, the original 7e18e3516Sopenharmony_cilibrary is referred to as PCRE1 below. For information about testing PCRE2, see 8e18e3516Sopenharmony_cithe pcre2test documentation and the comment at the head of the RunTest file. 9e18e3516Sopenharmony_ci 10e18e3516Sopenharmony_ciPCRE1 releases were up to 8.3x when PCRE2 was developed, and later bug fix 11e18e3516Sopenharmony_cireleases carried on the 8.xx series, up to the final 8.45 release. PCRE2 12e18e3516Sopenharmony_cireleases started at 10.00 to avoid confusion with PCRE1. 13e18e3516Sopenharmony_ci 14e18e3516Sopenharmony_ci 15e18e3516Sopenharmony_ciHistorical note 1 16e18e3516Sopenharmony_ci----------------- 17e18e3516Sopenharmony_ci 18e18e3516Sopenharmony_ciMany years ago I implemented some regular expression functions to an algorithm 19e18e3516Sopenharmony_cisuggested by Martin Richards. The rather simple patterns were not Unix-like in 20e18e3516Sopenharmony_ciform, and were quite restricted in what they could do by comparison with Perl. 21e18e3516Sopenharmony_ciThe interesting part about the algorithm was that the amount of space required 22e18e3516Sopenharmony_cito hold the compiled form of an expression was known in advance. The code to 23e18e3516Sopenharmony_ciapply an expression did not operate by backtracking, as the original Henry 24e18e3516Sopenharmony_ciSpencer code and current PCRE2 and Perl code does, but instead checked all 25e18e3516Sopenharmony_cipossibilities simultaneously by keeping a list of current states and checking 26e18e3516Sopenharmony_ciall of them as it advanced through the subject string. In the terminology of 27e18e3516Sopenharmony_ciJeffrey Friedl's book, it was a "DFA algorithm", though it was not a 28e18e3516Sopenharmony_citraditional Finite State Machine (FSM). When the pattern was all used up, all 29e18e3516Sopenharmony_ciremaining states were possible matches, and the one matching the longest subset 30e18e3516Sopenharmony_ciof the subject string was chosen. This did not necessarily maximize the 31e18e3516Sopenharmony_ciindividual wild portions of the pattern, as is expected in Unix and Perl-style 32e18e3516Sopenharmony_ciregular expressions. 33e18e3516Sopenharmony_ci 34e18e3516Sopenharmony_ci 35e18e3516Sopenharmony_ciHistorical note 2 36e18e3516Sopenharmony_ci----------------- 37e18e3516Sopenharmony_ci 38e18e3516Sopenharmony_ciBy contrast, the code originally written by Henry Spencer (which was 39e18e3516Sopenharmony_cisubsequently heavily modified for Perl) compiles the expression twice: once in 40e18e3516Sopenharmony_cia dummy mode in order to find out how much store will be needed, and then for 41e18e3516Sopenharmony_cireal. (The Perl version may or may not still do this; I'm talking about the 42e18e3516Sopenharmony_cioriginal library.) The execution function operates by backtracking and 43e18e3516Sopenharmony_cimaximizing (or, optionally, minimizing, in Perl) the amount of the subject that 44e18e3516Sopenharmony_cimatches individual wild portions of the pattern. This is an "NFA algorithm" in 45e18e3516Sopenharmony_ciFriedl's terminology. 46e18e3516Sopenharmony_ci 47e18e3516Sopenharmony_ci 48e18e3516Sopenharmony_ciOK, here's the real stuff 49e18e3516Sopenharmony_ci------------------------- 50e18e3516Sopenharmony_ci 51e18e3516Sopenharmony_ciFor the set of functions that formed the original PCRE1 library in 1997 (which 52e18e3516Sopenharmony_ciare unrelated to those mentioned above), I tried at first to invent an 53e18e3516Sopenharmony_cialgorithm that used an amount of store bounded by a multiple of the number of 54e18e3516Sopenharmony_cicharacters in the pattern, to save on compiling time. However, because of the 55e18e3516Sopenharmony_cigreater complexity in Perl regular expressions, I couldn't do this, even though 56e18e3516Sopenharmony_cithe then current Perl 5.004 patterns were much simpler than those supported 57e18e3516Sopenharmony_cinowadays. In any case, a first pass through the pattern is helpful for other 58e18e3516Sopenharmony_cireasons. 59e18e3516Sopenharmony_ci 60e18e3516Sopenharmony_ci 61e18e3516Sopenharmony_ciSupport for 16-bit and 32-bit data strings 62e18e3516Sopenharmony_ci------------------------------------------- 63e18e3516Sopenharmony_ci 64e18e3516Sopenharmony_ciThe PCRE2 library can be compiled in any combination of 8-bit, 16-bit or 32-bit 65e18e3516Sopenharmony_cimodes, creating up to three different libraries. In the description that 66e18e3516Sopenharmony_cifollows, the word "short" is used for a 16-bit data quantity, and the phrase 67e18e3516Sopenharmony_ci"code unit" is used for a quantity that is a byte in 8-bit mode, a short in 68e18e3516Sopenharmony_ci16-bit mode and a 32-bit word in 32-bit mode. The names of PCRE2 functions are 69e18e3516Sopenharmony_cigiven in generic form, without the _8, _16, or _32 suffix. 70e18e3516Sopenharmony_ci 71e18e3516Sopenharmony_ci 72e18e3516Sopenharmony_ciComputing the memory requirement: how it was 73e18e3516Sopenharmony_ci-------------------------------------------- 74e18e3516Sopenharmony_ci 75e18e3516Sopenharmony_ciUp to and including release 6.7, PCRE1 worked by running a very degenerate 76e18e3516Sopenharmony_cifirst pass to calculate a maximum memory requirement, and then a second pass to 77e18e3516Sopenharmony_cido the real compile - which might use a bit less than the predicted amount of 78e18e3516Sopenharmony_cimemory. The idea was that this would turn out faster than the Henry Spencer 79e18e3516Sopenharmony_cicode because the first pass is degenerate and the second pass can just store 80e18e3516Sopenharmony_cistuff straight into memory, which it knows is big enough. 81e18e3516Sopenharmony_ci 82e18e3516Sopenharmony_ci 83e18e3516Sopenharmony_ciComputing the memory requirement: how it is 84e18e3516Sopenharmony_ci------------------------------------------- 85e18e3516Sopenharmony_ci 86e18e3516Sopenharmony_ciBy the time I was working on a potential 6.8 release, the degenerate first pass 87e18e3516Sopenharmony_cihad become very complicated and hard to maintain. Indeed one of the early 88e18e3516Sopenharmony_cithings I did for 6.8 was to fix Yet Another Bug in the memory computation. Then 89e18e3516Sopenharmony_ciI had a flash of inspiration as to how I could run the real compile function in 90e18e3516Sopenharmony_cia "fake" mode that enables it to compute how much memory it would need, while 91e18e3516Sopenharmony_ciin most cases only ever using a small amount of working memory, and without too 92e18e3516Sopenharmony_cimany tests of the mode that might slow it down. So I refactored the compiling 93e18e3516Sopenharmony_cifunctions to work this way. This got rid of about 600 lines of source and made 94e18e3516Sopenharmony_cifurther maintenance and development easier. As this was such a major change, I 95e18e3516Sopenharmony_cinever released 6.8, instead upping the number to 7.0 (other quite major changes 96e18e3516Sopenharmony_ciwere also present in the 7.0 release). 97e18e3516Sopenharmony_ci 98e18e3516Sopenharmony_ciA side effect of this work was that the previous limit of 200 on the nesting 99e18e3516Sopenharmony_cidepth of parentheses was removed. However, there was a downside: compiling ran 100e18e3516Sopenharmony_cimore slowly than before (30% or more, depending on the pattern) because it now 101e18e3516Sopenharmony_cidid a full analysis of the pattern. My hope was that this would not be a big 102e18e3516Sopenharmony_ciissue, and in the event, nobody has commented on it. 103e18e3516Sopenharmony_ci 104e18e3516Sopenharmony_ciAt release 8.34, a limit on the nesting depth of parentheses was re-introduced 105e18e3516Sopenharmony_ci(default 250, settable at build time) so as to put a limit on the amount of 106e18e3516Sopenharmony_cisystem stack used by the compile function, which uses recursive function calls 107e18e3516Sopenharmony_cifor nested parenthesized groups. This is a safety feature for environments with 108e18e3516Sopenharmony_cismall stacks where the patterns are provided by users. 109e18e3516Sopenharmony_ci 110e18e3516Sopenharmony_ci 111e18e3516Sopenharmony_ciYet another pattern scan 112e18e3516Sopenharmony_ci------------------------ 113e18e3516Sopenharmony_ci 114e18e3516Sopenharmony_ciHistory repeated itself for PCRE2 release 10.20. A number of bugs relating to 115e18e3516Sopenharmony_cinamed subpatterns had been discovered by fuzzers. Most of these were related to 116e18e3516Sopenharmony_cithe handling of forward references when it was not known if the named group was 117e18e3516Sopenharmony_ciunique. (References to non-unique names use a different opcode and more 118e18e3516Sopenharmony_cimemory.) The use of duplicate group numbers (the (?| facility) also caused 119e18e3516Sopenharmony_ciissues. 120e18e3516Sopenharmony_ci 121e18e3516Sopenharmony_ciTo get around these problems I adopted a new approach by adding a third pass 122e18e3516Sopenharmony_ciover the pattern (really a "pre-pass"), which did nothing other than identify 123e18e3516Sopenharmony_ciall the named subpatterns and their corresponding group numbers. This means 124e18e3516Sopenharmony_cithat the actual compile (both the memory-computing dummy run and the real 125e18e3516Sopenharmony_cicompile) has full knowledge of group names and numbers throughout. Several 126e18e3516Sopenharmony_cidozen lines of messy code were eliminated, though the new pre-pass was not 127e18e3516Sopenharmony_cishort. In particular, parsing and skipping over [] classes is complicated. 128e18e3516Sopenharmony_ci 129e18e3516Sopenharmony_ciWhile working on 10.22 I realized that I could simplify yet again by moving 130e18e3516Sopenharmony_cimore of the parsing into the pre-pass, thus avoiding doing it in two places, so 131e18e3516Sopenharmony_ciafter 10.22 was released, the code underwent yet another big refactoring. This 132e18e3516Sopenharmony_ciis how it is from 10.23 onwards: 133e18e3516Sopenharmony_ci 134e18e3516Sopenharmony_ciThe function called parse_regex() scans the pattern characters, parsing them 135e18e3516Sopenharmony_ciinto literal data and meta characters. It converts escapes such as \x{123} 136e18e3516Sopenharmony_ciinto literals, handles \Q...\E, and skips over comments and non-significant 137e18e3516Sopenharmony_ciwhite space. The result of the scanning is put into a vector of 32-bit unsigned 138e18e3516Sopenharmony_ciintegers. Values less than 0x80000000 are literal data. Higher values represent 139e18e3516Sopenharmony_cimeta-characters. The top 16-bits of such values identify the meta-character, 140e18e3516Sopenharmony_ciand these are given names such as META_CAPTURE. The lower 16-bits are available 141e18e3516Sopenharmony_cifor data, for example, the capturing group number. The only situation in which 142e18e3516Sopenharmony_ciliteral data values greater than 0x7fffffff can appear is when the 32-bit 143e18e3516Sopenharmony_cilibrary is running in non-UTF mode. This is handled by having a special 144e18e3516Sopenharmony_cimeta-character that is followed by the 32-bit data value. 145e18e3516Sopenharmony_ci 146e18e3516Sopenharmony_ciThe size of the parsed pattern vector, when auto-callouts are not enabled, is 147e18e3516Sopenharmony_cibounded by the length of the pattern (with one exception). The code is written 148e18e3516Sopenharmony_ciso that each item in the pattern uses no more vector elements than the number 149e18e3516Sopenharmony_ciof code units in the item itself. The exception is the aforementioned large 150e18e3516Sopenharmony_ci32-bit number handling. For this reason, 32-bit non-UTF patterns are scanned in 151e18e3516Sopenharmony_ciadvance to check for such values. When auto-callouts are enabled, the generous 152e18e3516Sopenharmony_ciassumption is made that there will be a callout for each pattern code unit 153e18e3516Sopenharmony_ci(which of course is only actually true if all code units are literals) plus one 154e18e3516Sopenharmony_ciat the end. A default parsed pattern vector is defined on the system stack, to 155e18e3516Sopenharmony_ciminimize memory handling, but if this is not big enough, heap memory is used. 156e18e3516Sopenharmony_ci 157e18e3516Sopenharmony_ciAs before, the actual compiling function is run twice, the first time to 158e18e3516Sopenharmony_cidetermine the amount of memory needed for the final compiled pattern. It 159e18e3516Sopenharmony_cinow processes the parsed pattern vector, not the pattern itself, although some 160e18e3516Sopenharmony_ciof the parsed items refer to strings in the pattern - for example, group 161e18e3516Sopenharmony_cinames. As escapes and comments have already been processed, the code is a bit 162e18e3516Sopenharmony_cisimpler than before. 163e18e3516Sopenharmony_ci 164e18e3516Sopenharmony_ciMost errors can be diagnosed during the parsing scan. For those that cannot 165e18e3516Sopenharmony_ci(for example, "lookbehind assertion is not fixed length"), the parsed code 166e18e3516Sopenharmony_cicontains offsets into the pattern so that the actual compiling code can 167e18e3516Sopenharmony_cireport where errors are. 168e18e3516Sopenharmony_ci 169e18e3516Sopenharmony_ci 170e18e3516Sopenharmony_ciThe elements of the parsed pattern vector 171e18e3516Sopenharmony_ci----------------------------------------- 172e18e3516Sopenharmony_ci 173e18e3516Sopenharmony_ciThe word "offset" below means a code unit offset into the pattern. When 174e18e3516Sopenharmony_ciPCRE2_SIZE (which is usually size_t) is no bigger than uint32_t, an offset is 175e18e3516Sopenharmony_cistored in a single parsed pattern element. Otherwise (typically on 64-bit 176e18e3516Sopenharmony_cisystems) it occupies two elements. The following meta items occupy just one 177e18e3516Sopenharmony_cielement, with no data: 178e18e3516Sopenharmony_ci 179e18e3516Sopenharmony_ciMETA_ACCEPT (*ACCEPT) 180e18e3516Sopenharmony_ciMETA_ASTERISK * 181e18e3516Sopenharmony_ciMETA_ASTERISK_PLUS *+ 182e18e3516Sopenharmony_ciMETA_ASTERISK_QUERY *? 183e18e3516Sopenharmony_ciMETA_ATOMIC (?> start of atomic group 184e18e3516Sopenharmony_ciMETA_CIRCUMFLEX ^ metacharacter 185e18e3516Sopenharmony_ciMETA_CLASS [ start of non-empty class 186e18e3516Sopenharmony_ciMETA_CLASS_EMPTY [] empty class - only with PCRE2_ALLOW_EMPTY_CLASS 187e18e3516Sopenharmony_ciMETA_CLASS_EMPTY_NOT [^] negative empty class - ditto 188e18e3516Sopenharmony_ciMETA_CLASS_END ] end of non-empty class 189e18e3516Sopenharmony_ciMETA_CLASS_NOT [^ start non-empty negative class 190e18e3516Sopenharmony_ciMETA_COMMIT (*COMMIT) - no argument (see below for with argument) 191e18e3516Sopenharmony_ciMETA_COND_ASSERT (?(?assertion) 192e18e3516Sopenharmony_ciMETA_DOLLAR $ metacharacter 193e18e3516Sopenharmony_ciMETA_DOT . metacharacter 194e18e3516Sopenharmony_ciMETA_END End of pattern (this value is 0x80000000) 195e18e3516Sopenharmony_ciMETA_FAIL (*FAIL) 196e18e3516Sopenharmony_ciMETA_KET ) closing parenthesis 197e18e3516Sopenharmony_ciMETA_LOOKAHEAD (?= start of lookahead 198e18e3516Sopenharmony_ciMETA_LOOKAHEAD_NA (*napla: start of non-atomic lookahead 199e18e3516Sopenharmony_ciMETA_LOOKAHEADNOT (?! start of negative lookahead 200e18e3516Sopenharmony_ciMETA_NOCAPTURE (?: no capture parens 201e18e3516Sopenharmony_ciMETA_PLUS + 202e18e3516Sopenharmony_ciMETA_PLUS_PLUS ++ 203e18e3516Sopenharmony_ciMETA_PLUS_QUERY +? 204e18e3516Sopenharmony_ciMETA_PRUNE (*PRUNE) - no argument (see below for with argument) 205e18e3516Sopenharmony_ciMETA_QUERY ? 206e18e3516Sopenharmony_ciMETA_QUERY_PLUS ?+ 207e18e3516Sopenharmony_ciMETA_QUERY_QUERY ?? 208e18e3516Sopenharmony_ciMETA_RANGE_ESCAPED hyphen in class range with at least one escape 209e18e3516Sopenharmony_ciMETA_RANGE_LITERAL hyphen in class range defined literally 210e18e3516Sopenharmony_ciMETA_SKIP (*SKIP) - no argument (see below for with argument) 211e18e3516Sopenharmony_ciMETA_THEN (*THEN) - no argument (see below for with argument) 212e18e3516Sopenharmony_ci 213e18e3516Sopenharmony_ciThe two RANGE values occur only in character classes. They are positioned 214e18e3516Sopenharmony_cibetween two literals that define the start and end of the range. In an EBCDIC 215e18e3516Sopenharmony_cienvironment it is necessary to know whether either of the range values was 216e18e3516Sopenharmony_cispecified as an escape. In an ASCII/Unicode environment the distinction is not 217e18e3516Sopenharmony_cirelevant. 218e18e3516Sopenharmony_ci 219e18e3516Sopenharmony_ciThe following have data in the lower 16 bits, and may be followed by other data 220e18e3516Sopenharmony_cielements: 221e18e3516Sopenharmony_ci 222e18e3516Sopenharmony_ciMETA_ALT | alternation 223e18e3516Sopenharmony_ciMETA_BACKREF back reference 224e18e3516Sopenharmony_ciMETA_CAPTURE start of capturing group 225e18e3516Sopenharmony_ciMETA_ESCAPE non-literal escape sequence 226e18e3516Sopenharmony_ciMETA_RECURSE recursion call 227e18e3516Sopenharmony_ci 228e18e3516Sopenharmony_ciIf the data for META_ALT is non-zero, it is inside a lookbehind, and the data 229e18e3516Sopenharmony_ciis the length of its branch, for which OP_REVERSE must be generated. 230e18e3516Sopenharmony_ci 231e18e3516Sopenharmony_ciMETA_BACKREF, META_CAPTURE, and META_RECURSE have the capture group number as 232e18e3516Sopenharmony_citheir data in the lower 16 bits of the element. META_RECURSE is followed by an 233e18e3516Sopenharmony_cioffset, for use in error messages. 234e18e3516Sopenharmony_ci 235e18e3516Sopenharmony_ciMETA_BACKREF is followed by an offset if the back reference group number is 10 236e18e3516Sopenharmony_cior more. The offsets of the first occurrences of references to groups whose 237e18e3516Sopenharmony_cinumbers are less than 10 are put in cb->small_ref_offset[] (only the first 238e18e3516Sopenharmony_cioccurrence is useful). On 64-bit systems this avoids using more than two parsed 239e18e3516Sopenharmony_cipattern elements for items such as \3. The offset is used when an error occurs 240e18e3516Sopenharmony_cibecause the reference is to a non-existent group. 241e18e3516Sopenharmony_ci 242e18e3516Sopenharmony_ciMETA_ESCAPE has an ESC_xxx value as its data. For ESC_P and ESC_p, the next 243e18e3516Sopenharmony_cielement contains the 16-bit type and data property values, packed together. 244e18e3516Sopenharmony_ciESC_g and ESC_k are used only for named references - numerical ones are turned 245e18e3516Sopenharmony_ciinto META_RECURSE or META_BACKREF as appropriate. ESC_g and ESC_k are followed 246e18e3516Sopenharmony_ciby a length and an offset into the pattern to specify the name. 247e18e3516Sopenharmony_ci 248e18e3516Sopenharmony_ciThe following have one data item that follows in the next vector element: 249e18e3516Sopenharmony_ci 250e18e3516Sopenharmony_ciMETA_BIGVALUE Next is a literal >= META_END 251e18e3516Sopenharmony_ciMETA_OPTIONS (?i) and friends (data is new option bits) 252e18e3516Sopenharmony_ciMETA_POSIX POSIX class item (data identifies the class) 253e18e3516Sopenharmony_ciMETA_POSIX_NEG negative POSIX class item (ditto) 254e18e3516Sopenharmony_ci 255e18e3516Sopenharmony_ciThe following are followed by a length element, then a number of character code 256e18e3516Sopenharmony_civalues (which should match with the length): 257e18e3516Sopenharmony_ci 258e18e3516Sopenharmony_ciMETA_MARK (*MARK:xxxx) 259e18e3516Sopenharmony_ciMETA_COMMIT_ARG )*COMMIT:xxxx) 260e18e3516Sopenharmony_ciMETA_PRUNE_ARG (*PRUNE:xxx) 261e18e3516Sopenharmony_ciMETA_SKIP_ARG (*SKIP:xxxx) 262e18e3516Sopenharmony_ciMETA_THEN_ARG (*THEN:xxxx) 263e18e3516Sopenharmony_ci 264e18e3516Sopenharmony_ciThe following are followed by a length element, then an offset in the pattern 265e18e3516Sopenharmony_cithat identifies the name: 266e18e3516Sopenharmony_ci 267e18e3516Sopenharmony_ciMETA_COND_NAME (?(<name>) or (?('name') or (?(name) 268e18e3516Sopenharmony_ciMETA_COND_RNAME (?(R&name) 269e18e3516Sopenharmony_ciMETA_COND_RNUMBER (?(Rdigits) 270e18e3516Sopenharmony_ciMETA_RECURSE_BYNAME (?&name) 271e18e3516Sopenharmony_ciMETA_BACKREF_BYNAME \k'name' 272e18e3516Sopenharmony_ci 273e18e3516Sopenharmony_ciMETA_COND_RNUMBER is used for names that start with R and continue with digits, 274e18e3516Sopenharmony_cibecause this is an ambiguous case. It could be a back reference to a group with 275e18e3516Sopenharmony_cithat name, or it could be a recursion test on a numbered group. 276e18e3516Sopenharmony_ci 277e18e3516Sopenharmony_ciThis one is followed by an offset, for use in error messages, then a number: 278e18e3516Sopenharmony_ci 279e18e3516Sopenharmony_ciMETA_COND_NUMBER (?([+-]digits) 280e18e3516Sopenharmony_ci 281e18e3516Sopenharmony_ciThe following is followed just by an offset, for use in error messages: 282e18e3516Sopenharmony_ci 283e18e3516Sopenharmony_ciMETA_COND_DEFINE (?(DEFINE) 284e18e3516Sopenharmony_ci 285e18e3516Sopenharmony_ciThe following are also followed just by an offset, but also the lower 16 bits 286e18e3516Sopenharmony_ciof the main word contain the length of the first branch of the lookbehind 287e18e3516Sopenharmony_cigroup; this is used when generating OP_REVERSE for that branch. 288e18e3516Sopenharmony_ci 289e18e3516Sopenharmony_ciMETA_LOOKBEHIND (?<= start of lookbehind 290e18e3516Sopenharmony_ciMETA_LOOKBEHIND_NA (*naplb: start of non-atomic lookbehind 291e18e3516Sopenharmony_ciMETA_LOOKBEHINDNOT (?<! start of negative lookbehind 292e18e3516Sopenharmony_ci 293e18e3516Sopenharmony_ciThe following are followed by two elements, the minimum and maximum. The 294e18e3516Sopenharmony_cimaximum value is limited to 65535 (MAX_REPEAT). A maximum value of "unlimited" 295e18e3516Sopenharmony_ciis represented by UNLIMITED_REPEAT, which is bigger than MAX_REPEAT: 296e18e3516Sopenharmony_ci 297e18e3516Sopenharmony_ciMETA_MINMAX {n,m} repeat 298e18e3516Sopenharmony_ciMETA_MINMAX_PLUS {n,m}+ repeat 299e18e3516Sopenharmony_ciMETA_MINMAX_QUERY {n,m}? repeat 300e18e3516Sopenharmony_ci 301e18e3516Sopenharmony_ciThis one is followed by three elements. The first is 0 for '>' and 1 for '>='; 302e18e3516Sopenharmony_cithe next two are the major and minor numbers: 303e18e3516Sopenharmony_ci 304e18e3516Sopenharmony_ciMETA_COND_VERSION (?(VERSION<op>x.y) 305e18e3516Sopenharmony_ci 306e18e3516Sopenharmony_ciCallouts are converted into one of two items: 307e18e3516Sopenharmony_ci 308e18e3516Sopenharmony_ciMETA_CALLOUT_NUMBER (?C with numerical argument 309e18e3516Sopenharmony_ciMETA_CALLOUT_STRING (?C with string argument 310e18e3516Sopenharmony_ci 311e18e3516Sopenharmony_ciIn both cases, the next two elements contain the offset and length of the next 312e18e3516Sopenharmony_ciitem in the pattern. Then there is either one callout number, or a length and 313e18e3516Sopenharmony_cian offset for the string argument. The length includes both delimiters. 314e18e3516Sopenharmony_ci 315e18e3516Sopenharmony_ci 316e18e3516Sopenharmony_ciTraditional matching function 317e18e3516Sopenharmony_ci----------------------------- 318e18e3516Sopenharmony_ci 319e18e3516Sopenharmony_ciThe "traditional", and original, matching function is called pcre2_match(), and 320e18e3516Sopenharmony_ciit implements an NFA algorithm, similar to the original Henry Spencer algorithm 321e18e3516Sopenharmony_ciand the way that Perl works. This is not surprising, since it is intended to be 322e18e3516Sopenharmony_cias compatible with Perl as possible. This is the function most users of PCRE2 323e18e3516Sopenharmony_ciwill use most of the time. If PCRE2 is compiled with just-in-time (JIT) 324e18e3516Sopenharmony_cisupport, and studying a compiled pattern with JIT is successful, the JIT code 325e18e3516Sopenharmony_ciis run instead of the normal pcre2_match() code, but the result is the same. 326e18e3516Sopenharmony_ci 327e18e3516Sopenharmony_ci 328e18e3516Sopenharmony_ciSupplementary matching function 329e18e3516Sopenharmony_ci------------------------------- 330e18e3516Sopenharmony_ci 331e18e3516Sopenharmony_ciThere is also a supplementary matching function called pcre2_dfa_match(). This 332e18e3516Sopenharmony_ciimplements a DFA matching algorithm that searches simultaneously for all 333e18e3516Sopenharmony_cipossible matches that start at one point in the subject string. (Going back to 334e18e3516Sopenharmony_cimy roots: see Historical Note 1 above.) This function intreprets the same 335e18e3516Sopenharmony_cicompiled pattern data as pcre2_match(); however, not all the facilities are 336e18e3516Sopenharmony_ciavailable, and those that are do not always work in quite the same way. See the 337e18e3516Sopenharmony_ciuser documentation for details. 338e18e3516Sopenharmony_ci 339e18e3516Sopenharmony_ciThe algorithm that is used for pcre2_dfa_match() is not a traditional FSM, 340e18e3516Sopenharmony_cibecause it may have a number of states active at one time. More work would be 341e18e3516Sopenharmony_cineeded at compile time to produce a traditional FSM where only one state is 342e18e3516Sopenharmony_ciever active at once. I believe some other regex matchers work this way. JIT 343e18e3516Sopenharmony_cisupport is not available for this kind of matching. 344e18e3516Sopenharmony_ci 345e18e3516Sopenharmony_ci 346e18e3516Sopenharmony_ciChangeable options 347e18e3516Sopenharmony_ci------------------ 348e18e3516Sopenharmony_ci 349e18e3516Sopenharmony_ciThe /i, /m, or /s options (PCRE2_CASELESS, PCRE2_MULTILINE, PCRE2_DOTALL) and 350e18e3516Sopenharmony_cisome others may be changed in the middle of patterns by items such as (?i). 351e18e3516Sopenharmony_ciTheir processing is handled entirely at compile time by generating different 352e18e3516Sopenharmony_ciopcodes for the different settings. The runtime functions do not need to keep 353e18e3516Sopenharmony_citrack of an option's state. 354e18e3516Sopenharmony_ci 355e18e3516Sopenharmony_ciPCRE2_DUPNAMES, PCRE2_EXTENDED, PCRE2_EXTENDED_MORE, and PCRE2_NO_AUTO_CAPTURE 356e18e3516Sopenharmony_ciare tracked and processed during the parsing pre-pass. The others are handled 357e18e3516Sopenharmony_cifrom META_OPTIONS items during the main compile phase. 358e18e3516Sopenharmony_ci 359e18e3516Sopenharmony_ci 360e18e3516Sopenharmony_ciFormat of compiled patterns 361e18e3516Sopenharmony_ci--------------------------- 362e18e3516Sopenharmony_ci 363e18e3516Sopenharmony_ciThe compiled form of a pattern is a vector of unsigned code units (bytes in 364e18e3516Sopenharmony_ci8-bit mode, shorts in 16-bit mode, 32-bit words in 32-bit mode), containing 365e18e3516Sopenharmony_ciitems of variable length. The first code unit in an item contains an opcode, 366e18e3516Sopenharmony_ciand the length of the item is either implicit in the opcode or contained in the 367e18e3516Sopenharmony_cidata that follows it. 368e18e3516Sopenharmony_ci 369e18e3516Sopenharmony_ciIn many cases listed below, LINK_SIZE data values are specified for offsets 370e18e3516Sopenharmony_ciwithin the compiled pattern. LINK_SIZE always specifies a number of bytes. The 371e18e3516Sopenharmony_cidefault value for LINK_SIZE is 2, except for the 32-bit library, where it can 372e18e3516Sopenharmony_cionly be 4. The 8-bit library can be compiled to used 3-byte or 4-byte values, 373e18e3516Sopenharmony_ciand the 16-bit library can be compiled to use 4-byte values, though this 374e18e3516Sopenharmony_ciimpairs performance. Specifying a LINK_SIZE larger than 2 for these libraries is 375e18e3516Sopenharmony_cinecessary only when patterns whose compiled length is greater than 65535 code 376e18e3516Sopenharmony_ciunits are going to be processed. When a LINK_SIZE value uses more than one code 377e18e3516Sopenharmony_ciunit, the most significant unit is first. 378e18e3516Sopenharmony_ci 379e18e3516Sopenharmony_ciIn this description, we assume the "normal" compilation options. Data values 380e18e3516Sopenharmony_cithat are counts (e.g. quantifiers) are always two bytes long in 8-bit mode 381e18e3516Sopenharmony_ci(most significant byte first), and one code unit in 16-bit and 32-bit modes. 382e18e3516Sopenharmony_ci 383e18e3516Sopenharmony_ci 384e18e3516Sopenharmony_ciOpcodes with no following data 385e18e3516Sopenharmony_ci------------------------------ 386e18e3516Sopenharmony_ci 387e18e3516Sopenharmony_ciThese items are all just one unit long: 388e18e3516Sopenharmony_ci 389e18e3516Sopenharmony_ci OP_END end of pattern 390e18e3516Sopenharmony_ci OP_ANY match any one character other than newline 391e18e3516Sopenharmony_ci OP_ALLANY match any one character, including newline 392e18e3516Sopenharmony_ci OP_ANYBYTE match any single code unit, even in UTF-8/16 mode 393e18e3516Sopenharmony_ci OP_SOD match start of data: \A 394e18e3516Sopenharmony_ci OP_SOM, start of match (subject + offset): \G 395e18e3516Sopenharmony_ci OP_SET_SOM, set start of match (\K) 396e18e3516Sopenharmony_ci OP_CIRC ^ (start of data) 397e18e3516Sopenharmony_ci OP_CIRCM ^ multiline mode (start of data or after newline) 398e18e3516Sopenharmony_ci OP_NOT_WORD_BOUNDARY \W 399e18e3516Sopenharmony_ci OP_WORD_BOUNDARY \w 400e18e3516Sopenharmony_ci OP_NOT_DIGIT \D 401e18e3516Sopenharmony_ci OP_DIGIT \d 402e18e3516Sopenharmony_ci OP_NOT_HSPACE \H 403e18e3516Sopenharmony_ci OP_HSPACE \h 404e18e3516Sopenharmony_ci OP_NOT_WHITESPACE \S 405e18e3516Sopenharmony_ci OP_WHITESPACE \s 406e18e3516Sopenharmony_ci OP_NOT_VSPACE \V 407e18e3516Sopenharmony_ci OP_VSPACE \v 408e18e3516Sopenharmony_ci OP_NOT_WORDCHAR \W 409e18e3516Sopenharmony_ci OP_WORDCHAR \w 410e18e3516Sopenharmony_ci OP_EODN match end of data or newline at end: \Z 411e18e3516Sopenharmony_ci OP_EOD match end of data: \z 412e18e3516Sopenharmony_ci OP_DOLL $ (end of data, or before final newline) 413e18e3516Sopenharmony_ci OP_DOLLM $ multiline mode (end of data or before newline) 414e18e3516Sopenharmony_ci OP_EXTUNI match an extended Unicode grapheme cluster 415e18e3516Sopenharmony_ci OP_ANYNL match any Unicode newline sequence 416e18e3516Sopenharmony_ci 417e18e3516Sopenharmony_ci OP_ASSERT_ACCEPT ) 418e18e3516Sopenharmony_ci OP_ACCEPT ) These are Perl 5.10's "backtracking control 419e18e3516Sopenharmony_ci OP_COMMIT ) verbs". If OP_ACCEPT is inside capturing 420e18e3516Sopenharmony_ci OP_FAIL ) parentheses, it may be preceded by one or more 421e18e3516Sopenharmony_ci OP_PRUNE ) OP_CLOSE, each followed by a number that 422e18e3516Sopenharmony_ci OP_SKIP ) indicates which parentheses must be closed. 423e18e3516Sopenharmony_ci OP_THEN ) 424e18e3516Sopenharmony_ci 425e18e3516Sopenharmony_ciOP_ASSERT_ACCEPT is used when (*ACCEPT) is encountered within an assertion. 426e18e3516Sopenharmony_ciThis ends the assertion, not the entire pattern match. The assertion (?!) is 427e18e3516Sopenharmony_cialways optimized to OP_FAIL. 428e18e3516Sopenharmony_ci 429e18e3516Sopenharmony_ciOP_ALLANY is used for '.' when PCRE2_DOTALL is set. It is also used for \C in 430e18e3516Sopenharmony_cinon-UTF modes and in UTF-32 mode (since one code unit still equals one 431e18e3516Sopenharmony_cicharacter). Another use is for [^] when empty classes are permitted 432e18e3516Sopenharmony_ci(PCRE2_ALLOW_EMPTY_CLASS is set). 433e18e3516Sopenharmony_ci 434e18e3516Sopenharmony_ci 435e18e3516Sopenharmony_ciBacktracking control verbs 436e18e3516Sopenharmony_ci-------------------------- 437e18e3516Sopenharmony_ci 438e18e3516Sopenharmony_ciVerbs with no arguments generate opcodes with no following data (as listed 439e18e3516Sopenharmony_ciin the section above). 440e18e3516Sopenharmony_ci 441e18e3516Sopenharmony_ci(*MARK:NAME) generates OP_MARK followed by the mark name, preceded by a 442e18e3516Sopenharmony_cilength in one code unit, and followed by a binary zero. The name length is 443e18e3516Sopenharmony_cilimited by the size of the code unit. 444e18e3516Sopenharmony_ci 445e18e3516Sopenharmony_ci(*ACCEPT:NAME) and (*FAIL:NAME) are compiled as (*MARK:NAME)(*ACCEPT) and 446e18e3516Sopenharmony_ci(*MARK:NAME)(*FAIL) respectively. 447e18e3516Sopenharmony_ci 448e18e3516Sopenharmony_ciFor (*COMMIT:NAME), (*PRUNE:NAME), (*SKIP:NAME), and (*THEN:NAME), the opcodes 449e18e3516Sopenharmony_ciOP_COMMIT_ARG, OP_PRUNE_ARG, OP_SKIP_ARG, and OP_THEN_ARG are used, with the 450e18e3516Sopenharmony_ciname following in the same format as for OP_MARK. 451e18e3516Sopenharmony_ci 452e18e3516Sopenharmony_ci 453e18e3516Sopenharmony_ciMatching literal characters 454e18e3516Sopenharmony_ci--------------------------- 455e18e3516Sopenharmony_ci 456e18e3516Sopenharmony_ciThe OP_CHAR opcode is followed by a single character that is to be matched 457e18e3516Sopenharmony_cicasefully. For caseless matching of characters that have at most two 458e18e3516Sopenharmony_cicase-equivalent code points, OP_CHARI is used. In UTF-8 or UTF-16 modes, the 459e18e3516Sopenharmony_cicharacter may be more than one code unit long. In UTF-32 mode, characters are 460e18e3516Sopenharmony_cialways exactly one code unit long. 461e18e3516Sopenharmony_ci 462e18e3516Sopenharmony_ciIf there is only one character in a character class, OP_CHAR or OP_CHARI is 463e18e3516Sopenharmony_ciused for a positive class, and OP_NOT or OP_NOTI for a negative one (that is, 464e18e3516Sopenharmony_cifor something like [^a]). 465e18e3516Sopenharmony_ci 466e18e3516Sopenharmony_ciCaseless matching (positive or negative) of characters that have more than two 467e18e3516Sopenharmony_cicase-equivalent code points (which is possible only in UTF mode) is handled by 468e18e3516Sopenharmony_cicompiling a Unicode property item (see below), with the pseudo-property 469e18e3516Sopenharmony_ciPT_CLIST. The value of this property is an offset in a vector called 470e18e3516Sopenharmony_ci"ucd_caseless_sets" which identifies the start of a short list of case 471e18e3516Sopenharmony_ciequivalent characters, terminated by the value NOTACHAR (0xffffffff). 472e18e3516Sopenharmony_ci 473e18e3516Sopenharmony_ci 474e18e3516Sopenharmony_ciRepeating single characters 475e18e3516Sopenharmony_ci--------------------------- 476e18e3516Sopenharmony_ci 477e18e3516Sopenharmony_ciThe common repeats (*, +, ?), when applied to a single character, use the 478e18e3516Sopenharmony_cifollowing opcodes, which come in caseful and caseless versions: 479e18e3516Sopenharmony_ci 480e18e3516Sopenharmony_ci Caseful Caseless 481e18e3516Sopenharmony_ci OP_STAR OP_STARI 482e18e3516Sopenharmony_ci OP_MINSTAR OP_MINSTARI 483e18e3516Sopenharmony_ci OP_POSSTAR OP_POSSTARI 484e18e3516Sopenharmony_ci OP_PLUS OP_PLUSI 485e18e3516Sopenharmony_ci OP_MINPLUS OP_MINPLUSI 486e18e3516Sopenharmony_ci OP_POSPLUS OP_POSPLUSI 487e18e3516Sopenharmony_ci OP_QUERY OP_QUERYI 488e18e3516Sopenharmony_ci OP_MINQUERY OP_MINQUERYI 489e18e3516Sopenharmony_ci OP_POSQUERY OP_POSQUERYI 490e18e3516Sopenharmony_ci 491e18e3516Sopenharmony_ciEach opcode is followed by the character that is to be repeated. In ASCII or 492e18e3516Sopenharmony_ciUTF-32 modes, these are two-code-unit items; in UTF-8 or UTF-16 modes, the 493e18e3516Sopenharmony_cilength is variable. Those with "MIN" in their names are the minimizing 494e18e3516Sopenharmony_civersions. Those with "POS" in their names are possessive versions. Other kinds 495e18e3516Sopenharmony_ciof repeat make use of these opcodes: 496e18e3516Sopenharmony_ci 497e18e3516Sopenharmony_ci Caseful Caseless 498e18e3516Sopenharmony_ci OP_UPTO OP_UPTOI 499e18e3516Sopenharmony_ci OP_MINUPTO OP_MINUPTOI 500e18e3516Sopenharmony_ci OP_POSUPTO OP_POSUPTOI 501e18e3516Sopenharmony_ci OP_EXACT OP_EXACTI 502e18e3516Sopenharmony_ci 503e18e3516Sopenharmony_ciEach of these is followed by a count and then the repeated character. The count 504e18e3516Sopenharmony_ciis two bytes long in 8-bit mode (most significant byte first), or one code unit 505e18e3516Sopenharmony_ciin 16-bit and 32-bit modes. 506e18e3516Sopenharmony_ci 507e18e3516Sopenharmony_ciOP_UPTO matches from 0 to the given number. A repeat with a non-zero minimum 508e18e3516Sopenharmony_ciand a fixed maximum is coded as an OP_EXACT followed by an OP_UPTO (or 509e18e3516Sopenharmony_ciOP_MINUPTO or OPT_POSUPTO). 510e18e3516Sopenharmony_ci 511e18e3516Sopenharmony_ciAnother set of matching repeating opcodes (called OP_NOTSTAR, OP_NOTSTARI, 512e18e3516Sopenharmony_cietc.) are used for repeated, negated, single-character classes such as [^a]*. 513e18e3516Sopenharmony_ciThe normal single-character opcodes (OP_STAR, etc.) are used for repeated 514e18e3516Sopenharmony_cipositive single-character classes. 515e18e3516Sopenharmony_ci 516e18e3516Sopenharmony_ci 517e18e3516Sopenharmony_ciRepeating character types 518e18e3516Sopenharmony_ci------------------------- 519e18e3516Sopenharmony_ci 520e18e3516Sopenharmony_ciRepeats of things like \d are done exactly as for single characters, except 521e18e3516Sopenharmony_cithat instead of a character, the opcode for the type (e.g. OP_DIGIT) is stored 522e18e3516Sopenharmony_ciin the next code unit. The opcodes are: 523e18e3516Sopenharmony_ci 524e18e3516Sopenharmony_ci OP_TYPESTAR 525e18e3516Sopenharmony_ci OP_TYPEMINSTAR 526e18e3516Sopenharmony_ci OP_TYPEPOSSTAR 527e18e3516Sopenharmony_ci OP_TYPEPLUS 528e18e3516Sopenharmony_ci OP_TYPEMINPLUS 529e18e3516Sopenharmony_ci OP_TYPEPOSPLUS 530e18e3516Sopenharmony_ci OP_TYPEQUERY 531e18e3516Sopenharmony_ci OP_TYPEMINQUERY 532e18e3516Sopenharmony_ci OP_TYPEPOSQUERY 533e18e3516Sopenharmony_ci OP_TYPEUPTO 534e18e3516Sopenharmony_ci OP_TYPEMINUPTO 535e18e3516Sopenharmony_ci OP_TYPEPOSUPTO 536e18e3516Sopenharmony_ci OP_TYPEEXACT 537e18e3516Sopenharmony_ci 538e18e3516Sopenharmony_ci 539e18e3516Sopenharmony_ciMatch by Unicode property 540e18e3516Sopenharmony_ci------------------------- 541e18e3516Sopenharmony_ci 542e18e3516Sopenharmony_ciOP_PROP and OP_NOTPROP are used for positive and negative matches of a 543e18e3516Sopenharmony_cicharacter by testing its Unicode property (the \p and \P escape sequences). 544e18e3516Sopenharmony_ciEach is followed by two code units that encode the desired property as a type 545e18e3516Sopenharmony_ciand a value. The types are a set of #defines of the form PT_xxx, and the values 546e18e3516Sopenharmony_ciare enumerations of the form ucp_xx, defined in the pcre2_ucp.h source file. 547e18e3516Sopenharmony_ciThe value is relevant only for PT_GC (General Category), PT_PC (Particular 548e18e3516Sopenharmony_ciCategory), PT_SC (Script), PT_BIDICL (Bidi Class), PT_BOOL (Boolean property), 549e18e3516Sopenharmony_ciand the pseudo-property PT_CLIST, which is used to identify a list of 550e18e3516Sopenharmony_cicase-equivalent characters when there are three or more (see above). 551e18e3516Sopenharmony_ci 552e18e3516Sopenharmony_ciRepeats of these items use the OP_TYPESTAR etc. set of opcodes, followed by 553e18e3516Sopenharmony_cithree code units: OP_PROP or OP_NOTPROP, and then the desired property type and 554e18e3516Sopenharmony_civalue. 555e18e3516Sopenharmony_ci 556e18e3516Sopenharmony_ci 557e18e3516Sopenharmony_ciCharacter classes 558e18e3516Sopenharmony_ci----------------- 559e18e3516Sopenharmony_ci 560e18e3516Sopenharmony_ciIf there is only one character in a class, OP_CHAR or OP_CHARI is used for a 561e18e3516Sopenharmony_cipositive class, and OP_NOT or OP_NOTI for a negative one (that is, for 562e18e3516Sopenharmony_cisomething like [^a]), except when caselessly matching a character that has more 563e18e3516Sopenharmony_cithan two case-equivalent code points (which can happen only in UTF mode). In 564e18e3516Sopenharmony_cithis case a Unicode property item is used, as described above in "Matching 565e18e3516Sopenharmony_ciliteral characters". 566e18e3516Sopenharmony_ci 567e18e3516Sopenharmony_ciA set of repeating opcodes (called OP_NOTSTAR etc.) are used for repeated, 568e18e3516Sopenharmony_cinegated, single-character classes. The normal single-character opcodes 569e18e3516Sopenharmony_ci(OP_STAR, etc.) are used for repeated positive single-character classes. 570e18e3516Sopenharmony_ci 571e18e3516Sopenharmony_ciWhen there is more than one character in a class, and all the code points are 572e18e3516Sopenharmony_ciless than 256, OP_CLASS is used for a positive class, and OP_NCLASS for a 573e18e3516Sopenharmony_cinegative one. In either case, the opcode is followed by a 32-byte (16-short, 574e18e3516Sopenharmony_ci8-word) bit map containing a 1 bit for every character that is acceptable. The 575e18e3516Sopenharmony_cibits are counted from the least significant end of each unit. In caseless mode, 576e18e3516Sopenharmony_cibits for both cases are set. 577e18e3516Sopenharmony_ci 578e18e3516Sopenharmony_ciThe reason for having both OP_CLASS and OP_NCLASS is so that, in UTF-8 and 579e18e3516Sopenharmony_ci16-bit and 32-bit modes, subject characters with values greater than 255 can be 580e18e3516Sopenharmony_cihandled correctly. For OP_CLASS they do not match, whereas for OP_NCLASS they 581e18e3516Sopenharmony_cido. 582e18e3516Sopenharmony_ci 583e18e3516Sopenharmony_ciFor classes containing characters with values greater than 255 or that contain 584e18e3516Sopenharmony_ci\p or \P, OP_XCLASS is used. It optionally uses a bit map if any acceptable 585e18e3516Sopenharmony_cicode points are less than 256, followed by a list of pairs (for a range) and/or 586e18e3516Sopenharmony_cisingle characters and/or properties. In caseless mode, all equivalent 587e18e3516Sopenharmony_cicharacters are explicitly listed. 588e18e3516Sopenharmony_ci 589e18e3516Sopenharmony_ciOP_XCLASS is followed by a LINK_SIZE value containing the total length of the 590e18e3516Sopenharmony_ciopcode and its data. This is followed by a code unit containing flag bits: 591e18e3516Sopenharmony_ciXCL_NOT indicates that this is a negative class, and XCL_MAP indicates that a 592e18e3516Sopenharmony_cibit map is present. There follows the bit map, if XCL_MAP is set, and then a 593e18e3516Sopenharmony_cisequence of items coded as follows: 594e18e3516Sopenharmony_ci 595e18e3516Sopenharmony_ci XCL_END marks the end of the list 596e18e3516Sopenharmony_ci XCL_SINGLE one character follows 597e18e3516Sopenharmony_ci XCL_RANGE two characters follow 598e18e3516Sopenharmony_ci XCL_PROP a Unicode property (type, value) follows 599e18e3516Sopenharmony_ci XCL_NOTPROP a Unicode property (type, value) follows 600e18e3516Sopenharmony_ci 601e18e3516Sopenharmony_ciIf a range starts with a code point less than 256 and ends with one greater 602e18e3516Sopenharmony_cithan 255, it is split into two ranges, with characters less than 256 being 603e18e3516Sopenharmony_ciindicated in the bit map, and the rest with XCL_RANGE. 604e18e3516Sopenharmony_ci 605e18e3516Sopenharmony_ciWhen XCL_NOT is set, the bit map, if present, contains bits for characters that 606e18e3516Sopenharmony_ciare allowed (exactly as for OP_NCLASS), but the list of items that follow it 607e18e3516Sopenharmony_cispecifies characters and properties that are not allowed. 608e18e3516Sopenharmony_ci 609e18e3516Sopenharmony_ci 610e18e3516Sopenharmony_ciBack references 611e18e3516Sopenharmony_ci--------------- 612e18e3516Sopenharmony_ci 613e18e3516Sopenharmony_ciOP_REF (caseful) or OP_REFI (caseless) is followed by a count containing the 614e18e3516Sopenharmony_cireference number when the reference is to a unique capturing group (either by 615e18e3516Sopenharmony_cinumber or by name). When named groups are used, there may be more than one 616e18e3516Sopenharmony_cigroup with the same name. In this case, a reference to such a group by name 617e18e3516Sopenharmony_cigenerates OP_DNREF or OP_DNREFI. These are followed by two counts: the index 618e18e3516Sopenharmony_ci(not the byte offset) in the group name table of the first entry for the 619e18e3516Sopenharmony_cirequired name, followed by the number of groups with the same name. The 620e18e3516Sopenharmony_cimatching code can then search for the first one that is set. 621e18e3516Sopenharmony_ci 622e18e3516Sopenharmony_ci 623e18e3516Sopenharmony_ciRepeating character classes and back references 624e18e3516Sopenharmony_ci----------------------------------------------- 625e18e3516Sopenharmony_ci 626e18e3516Sopenharmony_ciSingle-character classes are handled specially (see above). This section 627e18e3516Sopenharmony_ciapplies to other classes and also to back references. In both cases, the repeat 628e18e3516Sopenharmony_ciinformation follows the base item. The matching code looks at the following 629e18e3516Sopenharmony_ciopcode to see if it is one of these: 630e18e3516Sopenharmony_ci 631e18e3516Sopenharmony_ci OP_CRSTAR 632e18e3516Sopenharmony_ci OP_CRMINSTAR 633e18e3516Sopenharmony_ci OP_CRPOSSTAR 634e18e3516Sopenharmony_ci OP_CRPLUS 635e18e3516Sopenharmony_ci OP_CRMINPLUS 636e18e3516Sopenharmony_ci OP_CRPOSPLUS 637e18e3516Sopenharmony_ci OP_CRQUERY 638e18e3516Sopenharmony_ci OP_CRMINQUERY 639e18e3516Sopenharmony_ci OP_CRPOSQUERY 640e18e3516Sopenharmony_ci OP_CRRANGE 641e18e3516Sopenharmony_ci OP_CRMINRANGE 642e18e3516Sopenharmony_ci OP_CRPOSRANGE 643e18e3516Sopenharmony_ci 644e18e3516Sopenharmony_ciAll but the last three are single-code-unit items, with no data. The range 645e18e3516Sopenharmony_ciopcodes are followed by the minimum and maximum repeat counts. 646e18e3516Sopenharmony_ci 647e18e3516Sopenharmony_ci 648e18e3516Sopenharmony_ciBrackets and alternation 649e18e3516Sopenharmony_ci------------------------ 650e18e3516Sopenharmony_ci 651e18e3516Sopenharmony_ciA pair of non-capturing round brackets is wrapped round each expression at 652e18e3516Sopenharmony_cicompile time, so alternation always happens in the context of brackets. 653e18e3516Sopenharmony_ci 654e18e3516Sopenharmony_ci[Note for North Americans: "bracket" to some English speakers, including 655e18e3516Sopenharmony_cimyself, can be round, square, curly, or pointy. Hence this usage rather than 656e18e3516Sopenharmony_ci"parentheses".] 657e18e3516Sopenharmony_ci 658e18e3516Sopenharmony_ciNon-capturing brackets use the opcode OP_BRA, capturing brackets use OP_CBRA. A 659e18e3516Sopenharmony_cibracket opcode is followed by a LINK_SIZE value which gives the offset to the 660e18e3516Sopenharmony_cinext alternative OP_ALT or, if there aren't any branches, to the terminating 661e18e3516Sopenharmony_ciopcode. Each OP_ALT is followed by a LINK_SIZE value giving the offset to the 662e18e3516Sopenharmony_cinext one, or to the final opcode. For capturing brackets, the bracket number is 663e18e3516Sopenharmony_cia count that immediately follows the offset. 664e18e3516Sopenharmony_ci 665e18e3516Sopenharmony_ciThere are several opcodes that mark the end of a subpattern group. OP_KET is 666e18e3516Sopenharmony_ciused for subpatterns that do not repeat indefinitely, OP_KETRMIN and 667e18e3516Sopenharmony_ciOP_KETRMAX are used for indefinite repetitions, minimally or maximally 668e18e3516Sopenharmony_cirespectively, and OP_KETRPOS for possessive repetitions (see below for more 669e18e3516Sopenharmony_cidetails). All four are followed by a LINK_SIZE value giving (as a positive 670e18e3516Sopenharmony_cinumber) the offset back to the matching opening bracket opcode. 671e18e3516Sopenharmony_ci 672e18e3516Sopenharmony_ciIf a subpattern is quantified such that it is permitted to match zero times, it 673e18e3516Sopenharmony_ciis preceded by one of OP_BRAZERO, OP_BRAMINZERO, or OP_SKIPZERO. These are 674e18e3516Sopenharmony_cisingle-unit opcodes that tell the matcher that skipping the following 675e18e3516Sopenharmony_cisubpattern entirely is a valid match. In the case of the first two, not 676e18e3516Sopenharmony_ciskipping the pattern is also valid (greedy and non-greedy). The third is used 677e18e3516Sopenharmony_ciwhen a pattern has the quantifier {0,0}. It cannot be entirely discarded, 678e18e3516Sopenharmony_cibecause it may be called as a subroutine from elsewhere in the pattern. 679e18e3516Sopenharmony_ci 680e18e3516Sopenharmony_ciA subpattern with an indefinite maximum repetition is replicated in the 681e18e3516Sopenharmony_cicompiled data its minimum number of times (or once with OP_BRAZERO if the 682e18e3516Sopenharmony_ciminimum is zero), with the final copy terminating with OP_KETRMIN or OP_KETRMAX 683e18e3516Sopenharmony_cias appropriate. 684e18e3516Sopenharmony_ci 685e18e3516Sopenharmony_ciA subpattern with a bounded maximum repetition is replicated in a nested 686e18e3516Sopenharmony_cifashion up to the maximum number of times, with OP_BRAZERO or OP_BRAMINZERO 687e18e3516Sopenharmony_cibefore each replication after the minimum, so that, for example, (abc){2,5} is 688e18e3516Sopenharmony_cicompiled as (abc)(abc)((abc)((abc)(abc)?)?)?, except that each bracketed group 689e18e3516Sopenharmony_cihas the same number. 690e18e3516Sopenharmony_ci 691e18e3516Sopenharmony_ciWhen a repeated subpattern has an unbounded upper limit, it is checked to see 692e18e3516Sopenharmony_ciwhether it could match an empty string. If this is the case, the opcode in the 693e18e3516Sopenharmony_cifinal replication is changed to OP_SBRA or OP_SCBRA. This tells the matcher 694e18e3516Sopenharmony_cithat it needs to check for matching an empty string when it hits OP_KETRMIN or 695e18e3516Sopenharmony_ciOP_KETRMAX, and if so, to break the loop. 696e18e3516Sopenharmony_ci 697e18e3516Sopenharmony_ci 698e18e3516Sopenharmony_ciPossessive brackets 699e18e3516Sopenharmony_ci------------------- 700e18e3516Sopenharmony_ci 701e18e3516Sopenharmony_ciWhen a repeated group (capturing or non-capturing) is marked as possessive by 702e18e3516Sopenharmony_cithe "+" notation, e.g. (abc)++, different opcodes are used. Their names all 703e18e3516Sopenharmony_cihave POS on the end, e.g. OP_BRAPOS instead of OP_BRA and OP_SCBRAPOS instead 704e18e3516Sopenharmony_ciof OP_SCBRA. The end of such a group is marked by OP_KETRPOS. If the minimum 705e18e3516Sopenharmony_cirepetition is zero, the group is preceded by OP_BRAPOSZERO. 706e18e3516Sopenharmony_ci 707e18e3516Sopenharmony_ci 708e18e3516Sopenharmony_ciOnce-only (atomic) groups 709e18e3516Sopenharmony_ci------------------------- 710e18e3516Sopenharmony_ci 711e18e3516Sopenharmony_ciThese are just like other subpatterns, but they start with the opcode OP_ONCE. 712e18e3516Sopenharmony_ciThe check for matching an empty string in an unbounded repeat is handled 713e18e3516Sopenharmony_cientirely at runtime, so there is just this one opcode for atomic groups. 714e18e3516Sopenharmony_ci 715e18e3516Sopenharmony_ci 716e18e3516Sopenharmony_ciAssertions 717e18e3516Sopenharmony_ci---------- 718e18e3516Sopenharmony_ci 719e18e3516Sopenharmony_ciForward assertions are also just like other subpatterns, but starting with one 720e18e3516Sopenharmony_ciof the opcodes OP_ASSERT, OP_ASSERT_NA (non-atomic assertion), or 721e18e3516Sopenharmony_ciOP_ASSERT_NOT. Backward assertions use the opcodes OP_ASSERTBACK, 722e18e3516Sopenharmony_ciOP_ASSERTBACK_NA, and OP_ASSERTBACK_NOT, and the first opcode inside the 723e18e3516Sopenharmony_ciassertion is OP_REVERSE, followed by a count of the number of characters to 724e18e3516Sopenharmony_cimove back the pointer in the subject string. In ASCII or UTF-32 mode, the count 725e18e3516Sopenharmony_ciis also the number of code units, but in UTF-8/16 mode each character may 726e18e3516Sopenharmony_cioccupy more than one code unit. A separate count is present in each alternative 727e18e3516Sopenharmony_ciof a lookbehind assertion, allowing each branch to have a different (but fixed) 728e18e3516Sopenharmony_cilength. 729e18e3516Sopenharmony_ci 730e18e3516Sopenharmony_ci 731e18e3516Sopenharmony_ciConditional subpatterns 732e18e3516Sopenharmony_ci----------------------- 733e18e3516Sopenharmony_ci 734e18e3516Sopenharmony_ciThese are like other subpatterns, but they start with the opcode OP_COND, or 735e18e3516Sopenharmony_ciOP_SCOND for one that might match an empty string in an unbounded repeat. 736e18e3516Sopenharmony_ci 737e18e3516Sopenharmony_ciIf the condition is a back reference, this is stored at the start of the 738e18e3516Sopenharmony_cisubpattern using the opcode OP_CREF followed by a count containing the 739e18e3516Sopenharmony_cireference number, provided that the reference is to a unique capturing group. 740e18e3516Sopenharmony_ciIf the reference was by name and there is more than one group with that name, 741e18e3516Sopenharmony_ciOP_DNCREF is used instead. It is followed by two counts: the index in the group 742e18e3516Sopenharmony_cinames table, and the number of groups with the same name. The allows the 743e18e3516Sopenharmony_cimatcher to check if any group with the given name is set. 744e18e3516Sopenharmony_ci 745e18e3516Sopenharmony_ciIf the condition is "in recursion" (coded as "(?(R)"), or "in recursion of 746e18e3516Sopenharmony_cigroup x" (coded as "(?(Rx)"), the group number is stored at the start of the 747e18e3516Sopenharmony_cisubpattern using the opcode OP_RREF (with a value of RREF_ANY (0xffff) for "the 748e18e3516Sopenharmony_ciwhole pattern") or OP_DNRREF (with data as for OP_DNCREF). 749e18e3516Sopenharmony_ci 750e18e3516Sopenharmony_ciFor a DEFINE condition, OP_FALSE is used (with no associated data). During 751e18e3516Sopenharmony_cicompilation, however, a DEFINE condition is coded as OP_DEFINE so that, when 752e18e3516Sopenharmony_cithe conditional group is complete, there can be a check to ensure that it 753e18e3516Sopenharmony_cicontains only one top-level branch. Once this has happened, the opcode is 754e18e3516Sopenharmony_cichanged to OP_FALSE, so the matcher never sees OP_DEFINE. 755e18e3516Sopenharmony_ci 756e18e3516Sopenharmony_ciThere is a special PCRE2-specific condition of the form (VERSION[>]=x.y), which 757e18e3516Sopenharmony_citests the PCRE2 version number. This compiles into one of the opcodes OP_TRUE 758e18e3516Sopenharmony_cior OP_FALSE. 759e18e3516Sopenharmony_ci 760e18e3516Sopenharmony_ciIf a condition is not a back reference, recursion test, DEFINE, or VERSION, it 761e18e3516Sopenharmony_cimust start with a parenthesized atomic assertion, whose opcode normally 762e18e3516Sopenharmony_ciimmediately follows OP_COND or OP_SCOND. However, if automatic callouts are 763e18e3516Sopenharmony_cienabled, a callout is inserted immediately before the assertion. It is also 764e18e3516Sopenharmony_cipossible to insert a manual callout at this point. Only assertion conditions 765e18e3516Sopenharmony_cimay have callouts preceding the condition. 766e18e3516Sopenharmony_ci 767e18e3516Sopenharmony_ciA condition that is the negative assertion (?!) is optimized to OP_FAIL in all 768e18e3516Sopenharmony_ciparts of the pattern, so this is another opcode that may appear as a condition. 769e18e3516Sopenharmony_ciIt is treated the same as OP_FALSE. 770e18e3516Sopenharmony_ci 771e18e3516Sopenharmony_ci 772e18e3516Sopenharmony_ciRecursion 773e18e3516Sopenharmony_ci--------- 774e18e3516Sopenharmony_ci 775e18e3516Sopenharmony_ciRecursion either matches the current pattern, or some subexpression. The opcode 776e18e3516Sopenharmony_ciOP_RECURSE is followed by a LINK_SIZE value that is the offset to the starting 777e18e3516Sopenharmony_cibracket from the start of the whole pattern. OP_RECURSE is also used for 778e18e3516Sopenharmony_ci"subroutine" calls, even though they are not strictly a recursion. Up till 779e18e3516Sopenharmony_cirelease 10.30 recursions were treated as atomic groups, making them 780e18e3516Sopenharmony_ciincompatible with Perl (but PCRE had them well before Perl did). From 10.30, 781e18e3516Sopenharmony_cibacktracking into recursions is supported. 782e18e3516Sopenharmony_ci 783e18e3516Sopenharmony_ciRepeated recursions used to be wrapped inside OP_ONCE brackets, which not only 784e18e3516Sopenharmony_ciforced no backtracking, but also allowed repetition to be handled as for other 785e18e3516Sopenharmony_cibracketed groups. From 10.30 onwards, repeated recursions are duplicated for 786e18e3516Sopenharmony_citheir minimum repetitions, and then wrapped in non-capturing brackets for the 787e18e3516Sopenharmony_ciremainder. For example, (?1){3} is treated as (?1)(?1)(?1), and (?1){2,4} is 788e18e3516Sopenharmony_citreated as (?1)(?1)(?:(?1)){0,2}. 789e18e3516Sopenharmony_ci 790e18e3516Sopenharmony_ci 791e18e3516Sopenharmony_ciCallouts 792e18e3516Sopenharmony_ci-------- 793e18e3516Sopenharmony_ci 794e18e3516Sopenharmony_ciA callout may have either a numerical argument or a string argument. These use 795e18e3516Sopenharmony_ciOP_CALLOUT or OP_CALLOUT_STR, respectively. In each case these are followed by 796e18e3516Sopenharmony_citwo LINK_SIZE values giving the offset in the pattern string to the start of 797e18e3516Sopenharmony_cithe following item, and another count giving the length of this item. These 798e18e3516Sopenharmony_civalues make it possible for pcre2test to output useful tracing information 799e18e3516Sopenharmony_ciusing callouts. 800e18e3516Sopenharmony_ci 801e18e3516Sopenharmony_ciIn the case of a numeric callout, after these two values there is a single code 802e18e3516Sopenharmony_ciunit containing the callout number, in the range 0-255, with 255 being used for 803e18e3516Sopenharmony_cicallouts that are automatically inserted as a result of the PCRE2_AUTO_CALLOUT 804e18e3516Sopenharmony_cioption. Thus, this opcode item is of fixed length: 805e18e3516Sopenharmony_ci 806e18e3516Sopenharmony_ci [OP_CALLOUT] [PATTERN_OFFSET] [PATTERN_LENGTH] [NUMBER] 807e18e3516Sopenharmony_ci 808e18e3516Sopenharmony_ciFor callouts with string arguments, OP_CALLOUT_STR has three more data items: 809e18e3516Sopenharmony_cia LINK_SIZE value giving the complete length of the entire opcode item, a 810e18e3516Sopenharmony_ciLINK_SIZE item containing the offset within the pattern string to the start of 811e18e3516Sopenharmony_cithe string argument, and the string itself, preceded by its starting delimiter 812e18e3516Sopenharmony_ciand followed by a binary zero. When a callout function is called, a pointer to 813e18e3516Sopenharmony_cithe actual string is passed, but the delimiter can be accessed as string[-1] if 814e18e3516Sopenharmony_cithe application needs it. In the 8-bit library, the callout in /X(?C'abc')Y/ is 815e18e3516Sopenharmony_cicompiled as the following bytes (decimal numbers represent binary values): 816e18e3516Sopenharmony_ci 817e18e3516Sopenharmony_ci [OP_CALLOUT_STR] [0] [10] [0] [1] [0] [14] [0] [5] ['] [a] [b] [c] [0] 818e18e3516Sopenharmony_ci -------- ------- -------- ------- 819e18e3516Sopenharmony_ci | | | | 820e18e3516Sopenharmony_ci ------- LINK_SIZE items ------ 821e18e3516Sopenharmony_ci 822e18e3516Sopenharmony_ciOpcode table checking 823e18e3516Sopenharmony_ci--------------------- 824e18e3516Sopenharmony_ci 825e18e3516Sopenharmony_ciThe last opcode that is defined in pcre2_internal.h is OP_TABLE_LENGTH. This is 826e18e3516Sopenharmony_cinot a real opcode, but is used to check at compile time that tables indexed by 827e18e3516Sopenharmony_ciopcode are the correct length, in order to catch updating errors. 828e18e3516Sopenharmony_ci 829e18e3516Sopenharmony_ciPhilip Hazel 830e18e3516Sopenharmony_ciApril 2022 831