Blame HACKING

Packit Service 02e2fd
Technical Notes about PCRE2
Packit Service 02e2fd
---------------------------
Packit Service 02e2fd
Packit Service 02e2fd
These are very rough technical notes that record potentially useful information
Packit Service 02e2fd
about PCRE2 internals. PCRE2 is a library based on the original PCRE library,
Packit Service 02e2fd
but with a revised (and incompatible) API. To avoid confusion, the original
Packit Service 02e2fd
library is referred to as PCRE1 below. For information about testing PCRE2, see
Packit Service 02e2fd
the pcre2test documentation and the comment at the head of the RunTest file.
Packit Service 02e2fd
Packit Service 02e2fd
PCRE1 releases were up to 8.3x when PCRE2 was developed, and later bug fix
Packit Service 02e2fd
releases remain in the 8.xx series. PCRE2 releases started at 10.00 to avoid
Packit Service 02e2fd
confusion with PCRE1.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Historical note 1
Packit Service 02e2fd
-----------------
Packit Service 02e2fd
Packit Service 02e2fd
Many years ago I implemented some regular expression functions to an algorithm
Packit Service 02e2fd
suggested by Martin Richards. The rather simple patterns were not Unix-like in
Packit Service 02e2fd
form, and were quite restricted in what they could do by comparison with Perl.
Packit Service 02e2fd
The interesting part about the algorithm was that the amount of space required
Packit Service 02e2fd
to hold the compiled form of an expression was known in advance. The code to
Packit Service 02e2fd
apply an expression did not operate by backtracking, as the original Henry
Packit Service 02e2fd
Spencer code and current PCRE2 and Perl code does, but instead checked all
Packit Service 02e2fd
possibilities simultaneously by keeping a list of current states and checking
Packit Service 02e2fd
all of them as it advanced through the subject string. In the terminology of
Packit Service 02e2fd
Jeffrey Friedl's book, it was a "DFA algorithm", though it was not a
Packit Service 02e2fd
traditional Finite State Machine (FSM). When the pattern was all used up, all
Packit Service 02e2fd
remaining states were possible matches, and the one matching the longest subset
Packit Service 02e2fd
of the subject string was chosen. This did not necessarily maximize the
Packit Service 02e2fd
individual wild portions of the pattern, as is expected in Unix and Perl-style
Packit Service 02e2fd
regular expressions.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Historical note 2
Packit Service 02e2fd
-----------------
Packit Service 02e2fd
Packit Service 02e2fd
By contrast, the code originally written by Henry Spencer (which was
Packit Service 02e2fd
subsequently heavily modified for Perl) compiles the expression twice: once in
Packit Service 02e2fd
a dummy mode in order to find out how much store will be needed, and then for
Packit Service 02e2fd
real. (The Perl version probably doesn't do this any more; I'm talking about
Packit Service 02e2fd
the original library.) The execution function operates by backtracking and
Packit Service 02e2fd
maximizing (or, optionally, minimizing, in Perl) the amount of the subject that
Packit Service 02e2fd
matches individual wild portions of the pattern. This is an "NFA algorithm" in
Packit Service 02e2fd
Friedl's terminology.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
OK, here's the real stuff
Packit Service 02e2fd
-------------------------
Packit Service 02e2fd
Packit Service 02e2fd
For the set of functions that formed the original PCRE1 library in 1997 (which
Packit Service 02e2fd
are unrelated to those mentioned above), I tried at first to invent an
Packit Service 02e2fd
algorithm that used an amount of store bounded by a multiple of the number of
Packit Service 02e2fd
characters in the pattern, to save on compiling time. However, because of the
Packit Service 02e2fd
greater complexity in Perl regular expressions, I couldn't do this, even though
Packit Service 02e2fd
the then current Perl 5.004 patterns were much simpler than those supported
Packit Service 02e2fd
nowadays. In any case, a first pass through the pattern is helpful for other
Packit Service 02e2fd
reasons.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Support for 16-bit and 32-bit data strings
Packit Service 02e2fd
-------------------------------------------
Packit Service 02e2fd
Packit Service 02e2fd
The PCRE2 library can be compiled in any combination of 8-bit, 16-bit or 32-bit
Packit Service 02e2fd
modes, creating up to three different libraries. In the description that
Packit Service 02e2fd
follows, the word "short" is used for a 16-bit data quantity, and the phrase
Packit Service 02e2fd
"code unit" is used for a quantity that is a byte in 8-bit mode, a short in
Packit Service 02e2fd
16-bit mode and a 32-bit word in 32-bit mode. The names of PCRE2 functions are
Packit Service 02e2fd
given in generic form, without the _8, _16, or _32 suffix.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Computing the memory requirement: how it was
Packit Service 02e2fd
--------------------------------------------
Packit Service 02e2fd
Packit Service 02e2fd
Up to and including release 6.7, PCRE1 worked by running a very degenerate
Packit Service 02e2fd
first pass to calculate a maximum memory requirement, and then a second pass to
Packit Service 02e2fd
do the real compile - which might use a bit less than the predicted amount of
Packit Service 02e2fd
memory. The idea was that this would turn out faster than the Henry Spencer
Packit Service 02e2fd
code because the first pass is degenerate and the second pass can just store
Packit Service 02e2fd
stuff straight into memory, which it knows is big enough.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Computing the memory requirement: how it is
Packit Service 02e2fd
-------------------------------------------
Packit Service 02e2fd
Packit Service 02e2fd
By the time I was working on a potential 6.8 release, the degenerate first pass
Packit Service 02e2fd
had become very complicated and hard to maintain. Indeed one of the early
Packit Service 02e2fd
things I did for 6.8 was to fix Yet Another Bug in the memory computation. Then
Packit Service 02e2fd
I had a flash of inspiration as to how I could run the real compile function in
Packit Service 02e2fd
a "fake" mode that enables it to compute how much memory it would need, while
Packit Service 02e2fd
in most cases only ever using a small amount of working memory, and without too
Packit Service 02e2fd
many tests of the mode that might slow it down. So I refactored the compiling
Packit Service 02e2fd
functions to work this way. This got rid of about 600 lines of source and made
Packit Service 02e2fd
further maintenance and development easier. As this was such a major change, I
Packit Service 02e2fd
never released 6.8, instead upping the number to 7.0 (other quite major changes
Packit Service 02e2fd
were also present in the 7.0 release).
Packit Service 02e2fd
Packit Service 02e2fd
A side effect of this work was that the previous limit of 200 on the nesting
Packit Service 02e2fd
depth of parentheses was removed. However, there was a downside: compiling ran
Packit Service 02e2fd
more slowly than before (30% or more, depending on the pattern) because it now
Packit Service 02e2fd
did a full analysis of the pattern. My hope was that this would not be a big
Packit Service 02e2fd
issue, and in the event, nobody has commented on it.
Packit Service 02e2fd
Packit Service 02e2fd
At release 8.34, a limit on the nesting depth of parentheses was re-introduced
Packit Service 02e2fd
(default 250, settable at build time) so as to put a limit on the amount of
Packit Service 02e2fd
system stack used by the compile function, which uses recursive function calls
Packit Service 02e2fd
for nested parenthesized groups. This is a safety feature for environments with
Packit Service 02e2fd
small stacks where the patterns are provided by users.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Yet another pattern scan
Packit Service 02e2fd
------------------------
Packit Service 02e2fd
Packit Service 02e2fd
History repeated itself for PCRE2 release 10.20. A number of bugs relating to
Packit Service 02e2fd
named subpatterns had been discovered by fuzzers. Most of these were related to
Packit Service 02e2fd
the handling of forward references when it was not known if the named group was
Packit Service 02e2fd
unique. (References to non-unique names use a different opcode and more
Packit Service 02e2fd
memory.) The use of duplicate group numbers (the (?| facility) also caused
Packit Service 02e2fd
issues.
Packit Service 02e2fd
Packit Service 02e2fd
To get around these problems I adopted a new approach by adding a third pass
Packit Service 02e2fd
over the pattern (really a "pre-pass"), which did nothing other than identify
Packit Service 02e2fd
all the named subpatterns and their corresponding group numbers. This means
Packit Service 02e2fd
that the actual compile (both the memory-computing dummy run and the real
Packit Service 02e2fd
compile) has full knowledge of group names and numbers throughout. Several
Packit Service 02e2fd
dozen lines of messy code were eliminated, though the new pre-pass was not
Packit Service 02e2fd
short. In particular, parsing and skipping over [] classes is complicated.
Packit Service 02e2fd
Packit Service 02e2fd
While working on 10.22 I realized that I could simplify yet again by moving
Packit Service 02e2fd
more of the parsing into the pre-pass, thus avoiding doing it in two places, so
Packit Service 02e2fd
after 10.22 was released, the code underwent yet another big refactoring. This
Packit Service 02e2fd
is how it is from 10.23 onwards:
Packit Service 02e2fd
Packit Service 02e2fd
The function called parse_regex() scans the pattern characters, parsing them
Packit Service 02e2fd
into literal data and meta characters. It converts escapes such as \x{123}
Packit Service 02e2fd
into literals, handles \Q...\E, and skips over comments and non-significant
Packit Service 02e2fd
white space. The result of the scanning is put into a vector of 32-bit unsigned
Packit Service 02e2fd
integers. Values less than 0x80000000 are literal data. Higher values represent
Packit Service 02e2fd
meta-characters. The top 16-bits of such values identify the meta-character,
Packit Service 02e2fd
and these are given names such as META_CAPTURE. The lower 16-bits are available
Packit Service 02e2fd
for data, for example, the capturing group number. The only situation in which
Packit Service 02e2fd
literal data values greater than 0x7fffffff can appear is when the 32-bit
Packit Service 02e2fd
library is running in non-UTF mode. This is handled by having a special
Packit Service 02e2fd
meta-character that is followed by the 32-bit data value.
Packit Service 02e2fd
Packit Service 02e2fd
The size of the parsed pattern vector, when auto-callouts are not enabled, is
Packit Service 02e2fd
bounded by the length of the pattern (with one exception). The code is written
Packit Service 02e2fd
so that each item in the pattern uses no more vector elements than the number
Packit Service 02e2fd
of code units in the item itself. The exception is the aforementioned large
Packit Service 02e2fd
32-bit number handling. For this reason, 32-bit non-UTF patterns are scanned in
Packit Service 02e2fd
advance to check for such values. When auto-callouts are enabled, the generous
Packit Service 02e2fd
assumption is made that there will be a callout for each pattern code unit
Packit Service 02e2fd
(which of course is only actually true if all code units are literals) plus one
Packit Service 02e2fd
at the end. There is a default parsed pattern vector on the system stack, but
Packit Service 02e2fd
if this is not big enough, heap memory is used.
Packit Service 02e2fd
Packit Service 02e2fd
As before, the actual compiling function is run twice, the first time to
Packit Service 02e2fd
determine the amount of memory needed for the final compiled pattern. It
Packit Service 02e2fd
now processes the parsed pattern vector, not the pattern itself, although some
Packit Service 02e2fd
of the parsed items refer to strings in the pattern - for example, group
Packit Service 02e2fd
names. As escapes and comments have already been processed, the code is a bit
Packit Service 02e2fd
simpler than before.
Packit Service 02e2fd
Packit Service 02e2fd
Most errors can be diagnosed during the parsing scan. For those that cannot
Packit Service 02e2fd
(for example, "lookbehind assertion is not fixed length"), the parsed code
Packit Service 02e2fd
contains offsets into the pattern so that the actual compiling code can
Packit Service 02e2fd
report where errors are.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
The elements of the parsed pattern vector
Packit Service 02e2fd
-----------------------------------------
Packit Service 02e2fd
Packit Service 02e2fd
The word "offset" below means a code unit offset into the pattern. When
Packit Service 02e2fd
PCRE2_SIZE (which is usually size_t) is no bigger than uint32_t, an offset is
Packit Service 02e2fd
stored in a single parsed pattern element. Otherwise (typically on 64-bit
Packit Service 02e2fd
systems) it occupies two elements. The following meta items occupy just one
Packit Service 02e2fd
element, with no data:
Packit Service 02e2fd
Packit Service 02e2fd
META_ACCEPT           (*ACCEPT)
Packit Service 02e2fd
META_ASTERISK         *
Packit Service 02e2fd
META_ASTERISK_PLUS    *+
Packit Service 02e2fd
META_ASTERISK_QUERY   *?
Packit Service 02e2fd
META_ATOMIC           (?> start of atomic group
Packit Service 02e2fd
META_CIRCUMFLEX       ^ metacharacter
Packit Service 02e2fd
META_CLASS            [ start of non-empty class
Packit Service 02e2fd
META_CLASS_EMPTY      [] empty class - only with PCRE2_ALLOW_EMPTY_CLASS
Packit Service 02e2fd
META_CLASS_EMPTY_NOT  [^] negative empty class - ditto
Packit Service 02e2fd
META_CLASS_END        ] end of non-empty class
Packit Service 02e2fd
META_CLASS_NOT        [^ start non-empty negative class
Packit Service 02e2fd
META_COMMIT           (*COMMIT)
Packit Service 02e2fd
META_COND_ASSERT      (?(?assertion)
Packit Service 02e2fd
META_DOLLAR           $ metacharacter
Packit Service 02e2fd
META_DOT              . metacharacter
Packit Service 02e2fd
META_END              End of pattern (this value is 0x80000000)
Packit Service 02e2fd
META_FAIL             (*FAIL)
Packit Service 02e2fd
META_KET              ) closing parenthesis
Packit Service 02e2fd
META_LOOKAHEAD        (?= start of lookahead
Packit Service 02e2fd
META_LOOKAHEADNOT     (?! start of negative lookahead
Packit Service 02e2fd
META_NOCAPTURE        (?: no capture parens
Packit Service 02e2fd
META_PLUS             +
Packit Service 02e2fd
META_PLUS_PLUS        ++
Packit Service 02e2fd
META_PLUS_QUERY       +?
Packit Service 02e2fd
META_PRUNE            (*PRUNE) - no argument
Packit Service 02e2fd
META_QUERY            ?
Packit Service 02e2fd
META_QUERY_PLUS       ?+
Packit Service 02e2fd
META_QUERY_QUERY      ??
Packit Service 02e2fd
META_RANGE_ESCAPED    hyphen in class range with at least one escape
Packit Service 02e2fd
META_RANGE_LITERAL    hyphen in class range defined literally
Packit Service 02e2fd
META_SKIP             (*SKIP) - no argument
Packit Service 02e2fd
META_THEN             (*THEN) - no argument
Packit Service 02e2fd
Packit Service 02e2fd
The two RANGE values occur only in character classes. They are positioned
Packit Service 02e2fd
between two literals that define the start and end of the range. In an EBCDIC
Packit Service 02e2fd
evironment it is necessary to know whether either of the range values was
Packit Service 02e2fd
specified as an escape. In an ASCII/Unicode environment the distinction is not
Packit Service 02e2fd
relevant.
Packit Service 02e2fd
Packit Service 02e2fd
The following have data in the lower 16 bits, and may be followed by other data
Packit Service 02e2fd
elements:
Packit Service 02e2fd
Packit Service 02e2fd
META_ALT              | alternation
Packit Service 02e2fd
META_BACKREF          back reference
Packit Service 02e2fd
META_CAPTURE          start of capturing group
Packit Service 02e2fd
META_ESCAPE           non-literal escape sequence
Packit Service 02e2fd
META_RECURSE          recursion call
Packit Service 02e2fd
Packit Service 02e2fd
If the data for META_ALT is non-zero, it is inside a lookbehind, and the data
Packit Service 02e2fd
is the length of its branch, for which OP_REVERSE must be generated.
Packit Service 02e2fd
Packit Service 02e2fd
META_BACKREF, META_CAPTURE, and META_RECURSE have the capture group number as
Packit Service 02e2fd
their data in the lower 16 bits of the element.
Packit Service 02e2fd
Packit Service 02e2fd
META_BACKREF is followed by an offset if the back reference group number is 10
Packit Service 02e2fd
or more. The offsets of the first ocurrences of references to groups whose
Packit Service 02e2fd
numbers are less than 10 are put in cb->small_ref_offset[] (only the first
Packit Service 02e2fd
occurrence is useful). On 64-bit systems this avoids using more than two parsed
Packit Service 02e2fd
pattern elements for items such as \3. The offset is used when an error occurs
Packit Service 02e2fd
because the reference is to a non-existent group.
Packit Service 02e2fd
Packit Service 02e2fd
META_RECURSE is always followed by an offset, for use in error messages.
Packit Service 02e2fd
Packit Service 02e2fd
META_ESCAPE has an ESC_xxx value as its data. For ESC_P and ESC_p, the next
Packit Service 02e2fd
element contains the 16-bit type and data property values, packed together.
Packit Service 02e2fd
ESC_g and ESC_k are used only for named references - numerical ones are turned
Packit Service 02e2fd
into META_RECURSE or META_BACKREF as appropriate. ESC_g and ESC_k are followed
Packit Service 02e2fd
by a length and an offset into the pattern to specify the name.
Packit Service 02e2fd
Packit Service 02e2fd
The following have one data item that follows in the next vector element:
Packit Service 02e2fd
Packit Service 02e2fd
META_BIGVALUE         Next is a literal >= META_END
Packit Service 02e2fd
META_OPTIONS          (?i) and friends (data is new option bits)
Packit Service 02e2fd
META_POSIX            POSIX class item (data identifies the class)
Packit Service 02e2fd
META_POSIX_NEG        negative POSIX class item (ditto)
Packit Service 02e2fd
Packit Service 02e2fd
The following are followed by a length element, then a number of character code
Packit Service 02e2fd
values (which should match with the length):
Packit Service 02e2fd
Packit Service 02e2fd
META_MARK             (*MARK:xxxx)
Packit Service 02e2fd
META_COMMIT_ARG       )*COMMIT:xxxx)
Packit Service 02e2fd
META_PRUNE_ARG        (*PRUNE:xxx)
Packit Service 02e2fd
META_SKIP_ARG         (*SKIP:xxxx)
Packit Service 02e2fd
META_THEN_ARG         (*THEN:xxxx)
Packit Service 02e2fd
Packit Service 02e2fd
The following are followed by a length element, then an offset in the pattern
Packit Service 02e2fd
that identifies the name:
Packit Service 02e2fd
Packit Service 02e2fd
META_COND_NAME        (?(<name>) or (?('name') or (?(name)
Packit Service 02e2fd
META_COND_RNAME       (?(R&name)
Packit Service 02e2fd
META_COND_RNUMBER     (?(Rdigits)
Packit Service 02e2fd
META_RECURSE_BYNAME   (?&name)
Packit Service 02e2fd
META_BACKREF_BYNAME   \k'name'
Packit Service 02e2fd
Packit Service 02e2fd
META_COND_RNUMBER is used for names that start with R and continue with digits,
Packit Service 02e2fd
because this is an ambiguous case. It could be a back reference to a group with
Packit Service 02e2fd
that name, or it could be a recursion test on a numbered group.
Packit Service 02e2fd
Packit Service 02e2fd
This one is followed by an offset, for use in error messages, then a number:
Packit Service 02e2fd
Packit Service 02e2fd
META_COND_NUMBER       (?([+-]digits)
Packit Service 02e2fd
Packit Service 02e2fd
The following is followed just by an offset, for use in error messages:
Packit Service 02e2fd
Packit Service 02e2fd
META_COND_DEFINE      (?(DEFINE)
Packit Service 02e2fd
Packit Service 02e2fd
The following are also followed just by an offset, but also the lower 16 bits
Packit Service 02e2fd
of the main word contain the length of the first branch of the lookbehind
Packit Service 02e2fd
group; this is used when generating OP_REVERSE for that branch.
Packit Service 02e2fd
Packit Service 02e2fd
META_LOOKBEHIND       (?<=
Packit Service 02e2fd
META_LOOKBEHINDNOT    (?
Packit Service 02e2fd
Packit Service 02e2fd
The following are followed by two elements, the minimum and maximum. Repeat
Packit Service 02e2fd
values are limited to 65535 (MAX_REPEAT). A maximum value of "unlimited" is
Packit Service 02e2fd
represented by UNLIMITED_REPEAT, which is bigger than MAX_REPEAT:
Packit Service 02e2fd
Packit Service 02e2fd
META_MINMAX           {n,m}  repeat
Packit Service 02e2fd
META_MINMAX_PLUS      {n,m}+ repeat
Packit Service 02e2fd
META_MINMAX_QUERY     {n,m}? repeat
Packit Service 02e2fd
Packit Service 02e2fd
This one is followed by three elements. The first is 0 for '>' and 1 for '>=';
Packit Service 02e2fd
the next two are the major and minor numbers:
Packit Service 02e2fd
Packit Service 02e2fd
META_COND_VERSION     (?(VERSION<op>x.y)
Packit Service 02e2fd
Packit Service 02e2fd
Callouts are converted into one of two items:
Packit Service 02e2fd
Packit Service 02e2fd
META_CALLOUT_NUMBER   (?C with numerical argument
Packit Service 02e2fd
META_CALLOUT_STRING   (?C with string argument
Packit Service 02e2fd
Packit Service 02e2fd
In both cases, the next two elements contain the offset and length of the next
Packit Service 02e2fd
item in the pattern. Then there is either one callout number, or a length and
Packit Service 02e2fd
an offset for the string argument. The length includes both delimiters.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Traditional matching function
Packit Service 02e2fd
-----------------------------
Packit Service 02e2fd
Packit Service 02e2fd
The "traditional", and original, matching function is called pcre2_match(), and
Packit Service 02e2fd
it implements an NFA algorithm, similar to the original Henry Spencer algorithm
Packit Service 02e2fd
and the way that Perl works. This is not surprising, since it is intended to be
Packit Service 02e2fd
as compatible with Perl as possible. This is the function most users of PCRE2
Packit Service 02e2fd
will use most of the time. If PCRE2 is compiled with just-in-time (JIT)
Packit Service 02e2fd
support, and studying a compiled pattern with JIT is successful, the JIT code
Packit Service 02e2fd
is run instead of the normal pcre2_match() code, but the result is the same.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Supplementary matching function
Packit Service 02e2fd
-------------------------------
Packit Service 02e2fd
Packit Service 02e2fd
There is also a supplementary matching function called pcre2_dfa_match(). This
Packit Service 02e2fd
implements a DFA matching algorithm that searches simultaneously for all
Packit Service 02e2fd
possible matches that start at one point in the subject string. (Going back to
Packit Service 02e2fd
my roots: see Historical Note 1 above.) This function intreprets the same
Packit Service 02e2fd
compiled pattern data as pcre2_match(); however, not all the facilities are
Packit Service 02e2fd
available, and those that are do not always work in quite the same way. See the
Packit Service 02e2fd
user documentation for details.
Packit Service 02e2fd
Packit Service 02e2fd
The algorithm that is used for pcre2_dfa_match() is not a traditional FSM,
Packit Service 02e2fd
because it may have a number of states active at one time. More work would be
Packit Service 02e2fd
needed at compile time to produce a traditional FSM where only one state is
Packit Service 02e2fd
ever active at once. I believe some other regex matchers work this way. JIT
Packit Service 02e2fd
support is not available for this kind of matching.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Changeable options
Packit Service 02e2fd
------------------
Packit Service 02e2fd
Packit Service 02e2fd
The /i, /m, or /s options (PCRE2_CASELESS, PCRE2_MULTILINE, PCRE2_DOTALL, and
Packit Service 02e2fd
others) may be changed in the middle of patterns by items such as (?i). Their
Packit Service 02e2fd
processing is handled entirely at compile time by generating different opcodes
Packit Service 02e2fd
for the different settings. The runtime functions do not need to keep track of
Packit Service 02e2fd
an option's state.
Packit Service 02e2fd
Packit Service 02e2fd
PCRE2_DUPNAMES, PCRE2_EXTENDED, PCRE2_EXTENDED_MORE, and PCRE2_NO_AUTO_CAPTURE
Packit Service 02e2fd
are tracked and processed during the parsing pre-pass. The others are handled
Packit Service 02e2fd
from META_OPTIONS items during the main compile phase.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Format of compiled patterns
Packit Service 02e2fd
---------------------------
Packit Service 02e2fd
Packit Service 02e2fd
The compiled form of a pattern is a vector of unsigned code units (bytes in
Packit Service 02e2fd
8-bit mode, shorts in 16-bit mode, 32-bit words in 32-bit mode), containing
Packit Service 02e2fd
items of variable length. The first code unit in an item contains an opcode,
Packit Service 02e2fd
and the length of the item is either implicit in the opcode or contained in the
Packit Service 02e2fd
data that follows it.
Packit Service 02e2fd
Packit Service 02e2fd
In many cases listed below, LINK_SIZE data values are specified for offsets
Packit Service 02e2fd
within the compiled pattern. LINK_SIZE always specifies a number of bytes. The
Packit Service 02e2fd
default value for LINK_SIZE is 2, except for the 32-bit library, where it can
Packit Service 02e2fd
only be 4. The 8-bit library can be compiled to used 3-byte or 4-byte values,
Packit Service 02e2fd
and the 16-bit library can be compiled to use 4-byte values, though this
Packit Service 02e2fd
impairs performance. Specifing a LINK_SIZE larger than 2 for these libraries is
Packit Service 02e2fd
necessary only when patterns whose compiled length is greater than 65535 code
Packit Service 02e2fd
units are going to be processed. When a LINK_SIZE value uses more than one code
Packit Service 02e2fd
unit, the most significant unit is first.
Packit Service 02e2fd
Packit Service 02e2fd
In this description, we assume the "normal" compilation options. Data values
Packit Service 02e2fd
that are counts (e.g. quantifiers) are always two bytes long in 8-bit mode
Packit Service 02e2fd
(most significant byte first), and one code unit in 16-bit and 32-bit modes.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Opcodes with no following data
Packit Service 02e2fd
------------------------------
Packit Service 02e2fd
Packit Service 02e2fd
These items are all just one unit long:
Packit Service 02e2fd
Packit Service 02e2fd
  OP_END                 end of pattern
Packit Service 02e2fd
  OP_ANY                 match any one character other than newline
Packit Service 02e2fd
  OP_ALLANY              match any one character, including newline
Packit Service 02e2fd
  OP_ANYBYTE             match any single code unit, even in UTF-8/16 mode
Packit Service 02e2fd
  OP_SOD                 match start of data: \A
Packit Service 02e2fd
  OP_SOM,                start of match (subject + offset): \G
Packit Service 02e2fd
  OP_SET_SOM,            set start of match (\K)
Packit Service 02e2fd
  OP_CIRC                ^ (start of data)
Packit Service 02e2fd
  OP_CIRCM               ^ multiline mode (start of data or after newline)
Packit Service 02e2fd
  OP_NOT_WORD_BOUNDARY   \W
Packit Service 02e2fd
  OP_WORD_BOUNDARY       \w
Packit Service 02e2fd
  OP_NOT_DIGIT           \D
Packit Service 02e2fd
  OP_DIGIT               \d
Packit Service 02e2fd
  OP_NOT_HSPACE          \H
Packit Service 02e2fd
  OP_HSPACE              \h
Packit Service 02e2fd
  OP_NOT_WHITESPACE      \S
Packit Service 02e2fd
  OP_WHITESPACE          \s
Packit Service 02e2fd
  OP_NOT_VSPACE          \V
Packit Service 02e2fd
  OP_VSPACE              \v
Packit Service 02e2fd
  OP_NOT_WORDCHAR        \W
Packit Service 02e2fd
  OP_WORDCHAR            \w
Packit Service 02e2fd
  OP_EODN                match end of data or newline at end: \Z
Packit Service 02e2fd
  OP_EOD                 match end of data: \z
Packit Service 02e2fd
  OP_DOLL                $ (end of data, or before final newline)
Packit Service 02e2fd
  OP_DOLLM               $ multiline mode (end of data or before newline)
Packit Service 02e2fd
  OP_EXTUNI              match an extended Unicode grapheme cluster
Packit Service 02e2fd
  OP_ANYNL               match any Unicode newline sequence
Packit Service 02e2fd
Packit Service 02e2fd
  OP_ASSERT_ACCEPT       )
Packit Service 02e2fd
  OP_ACCEPT              ) These are Perl 5.10's "backtracking control
Packit Service 02e2fd
  OP_COMMIT              ) verbs". If OP_ACCEPT is inside capturing
Packit Service 02e2fd
  OP_FAIL                ) parentheses, it may be preceded by one or more
Packit Service 02e2fd
  OP_PRUNE               ) OP_CLOSE, each followed by a number that
Packit Service 02e2fd
  OP_SKIP                ) indicates which parentheses must be closed.
Packit Service 02e2fd
  OP_THEN                )
Packit Service 02e2fd
Packit Service 02e2fd
OP_ASSERT_ACCEPT is used when (*ACCEPT) is encountered within an assertion.
Packit Service 02e2fd
This ends the assertion, not the entire pattern match. The assertion (?!) is
Packit Service 02e2fd
always optimized to OP_FAIL.
Packit Service 02e2fd
Packit Service 02e2fd
OP_ALLANY is used for '.' when PCRE2_DOTALL is set. It is also used for \C in
Packit Service 02e2fd
non-UTF modes and in UTF-32 mode (since one code unit still equals one
Packit Service 02e2fd
character). Another use is for [^] when empty classes are permitted
Packit Service 02e2fd
(PCRE2_ALLOW_EMPTY_CLASS is set).
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Backtracking control verbs
Packit Service 02e2fd
--------------------------
Packit Service 02e2fd
Packit Service 02e2fd
Verbs with no arguments generate opcodes with no following data (as listed
Packit Service 02e2fd
in the section above). 
Packit Service 02e2fd
Packit Service 02e2fd
(*MARK:NAME) generates OP_MARK followed by the mark name, preceded by a
Packit Service 02e2fd
length in one code unit, and followed by a binary zero. The name length is
Packit Service 02e2fd
limited by the size of the code unit.
Packit Service 02e2fd
Packit Service 02e2fd
(*ACCEPT:NAME) and (*FAIL:NAME) are compiled as (*MARK:NAME)(*ACCEPT) and
Packit Service 02e2fd
(*MARK:NAME)(*FAIL) respectively.
Packit Service 02e2fd
Packit Service 02e2fd
For (*COMMIT:NAME), (*PRUNE:NAME), (*SKIP:NAME), and (*THEN:NAME), the opcodes
Packit Service 02e2fd
OP_COMMIT_ARG, OP_PRUNE_ARG, OP_SKIP_ARG, and OP_THEN_ARG are used, with the
Packit Service 02e2fd
name following in the same format as for OP_MARK.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Matching literal characters
Packit Service 02e2fd
---------------------------
Packit Service 02e2fd
Packit Service 02e2fd
The OP_CHAR opcode is followed by a single character that is to be matched
Packit Service 02e2fd
casefully. For caseless matching of characters that have at most two
Packit Service 02e2fd
case-equivalent code points, OP_CHARI is used. In UTF-8 or UTF-16 modes, the
Packit Service 02e2fd
character may be more than one code unit long. In UTF-32 mode, characters are
Packit Service 02e2fd
always exactly one code unit long.
Packit Service 02e2fd
Packit Service 02e2fd
If there is only one character in a character class, OP_CHAR or OP_CHARI is
Packit Service 02e2fd
used for a positive class, and OP_NOT or OP_NOTI for a negative one (that is,
Packit Service 02e2fd
for something like [^a]).
Packit Service 02e2fd
Packit Service 02e2fd
Caseless matching (positive or negative) of characters that have more than two
Packit Service 02e2fd
case-equivalent code points (which is possible only in UTF mode) is handled by
Packit Service 02e2fd
compiling a Unicode property item (see below), with the pseudo-property
Packit Service 02e2fd
PT_CLIST. The value of this property is an offset in a vector called
Packit Service 02e2fd
"ucd_caseless_sets" which identifies the start of a short list of equivalent
Packit Service 02e2fd
characters, terminated by the value NOTACHAR (0xffffffff).
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Repeating single characters
Packit Service 02e2fd
---------------------------
Packit Service 02e2fd
Packit Service 02e2fd
The common repeats (*, +, ?), when applied to a single character, use the
Packit Service 02e2fd
following opcodes, which come in caseful and caseless versions:
Packit Service 02e2fd
Packit Service 02e2fd
  Caseful         Caseless
Packit Service 02e2fd
  OP_STAR         OP_STARI
Packit Service 02e2fd
  OP_MINSTAR      OP_MINSTARI
Packit Service 02e2fd
  OP_POSSTAR      OP_POSSTARI
Packit Service 02e2fd
  OP_PLUS         OP_PLUSI
Packit Service 02e2fd
  OP_MINPLUS      OP_MINPLUSI
Packit Service 02e2fd
  OP_POSPLUS      OP_POSPLUSI
Packit Service 02e2fd
  OP_QUERY        OP_QUERYI
Packit Service 02e2fd
  OP_MINQUERY     OP_MINQUERYI
Packit Service 02e2fd
  OP_POSQUERY     OP_POSQUERYI
Packit Service 02e2fd
Packit Service 02e2fd
Each opcode is followed by the character that is to be repeated. In ASCII or
Packit Service 02e2fd
UTF-32 modes, these are two-code-unit items; in UTF-8 or UTF-16 modes, the
Packit Service 02e2fd
length is variable. Those with "MIN" in their names are the minimizing
Packit Service 02e2fd
versions. Those with "POS" in their names are possessive versions. Other kinds
Packit Service 02e2fd
of repeat make use of these opcodes:
Packit Service 02e2fd
Packit Service 02e2fd
  Caseful         Caseless
Packit Service 02e2fd
  OP_UPTO         OP_UPTOI
Packit Service 02e2fd
  OP_MINUPTO      OP_MINUPTOI
Packit Service 02e2fd
  OP_POSUPTO      OP_POSUPTOI
Packit Service 02e2fd
  OP_EXACT        OP_EXACTI
Packit Service 02e2fd
Packit Service 02e2fd
Each of these is followed by a count and then the repeated character. The count
Packit Service 02e2fd
is two bytes long in 8-bit mode (most significant byte first), or one code unit
Packit Service 02e2fd
in 16-bit and 32-bit modes.
Packit Service 02e2fd
Packit Service 02e2fd
OP_UPTO matches from 0 to the given number. A repeat with a non-zero minimum
Packit Service 02e2fd
and a fixed maximum is coded as an OP_EXACT followed by an OP_UPTO (or
Packit Service 02e2fd
OP_MINUPTO or OPT_POSUPTO).
Packit Service 02e2fd
Packit Service 02e2fd
Another set of matching repeating opcodes (called OP_NOTSTAR, OP_NOTSTARI,
Packit Service 02e2fd
etc.) are used for repeated, negated, single-character classes such as [^a]*.
Packit Service 02e2fd
The normal single-character opcodes (OP_STAR, etc.) are used for repeated
Packit Service 02e2fd
positive single-character classes.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Repeating character types
Packit Service 02e2fd
-------------------------
Packit Service 02e2fd
Packit Service 02e2fd
Repeats of things like \d are done exactly as for single characters, except
Packit Service 02e2fd
that instead of a character, the opcode for the type (e.g. OP_DIGIT) is stored
Packit Service 02e2fd
in the next code unit. The opcodes are:
Packit Service 02e2fd
Packit Service 02e2fd
  OP_TYPESTAR
Packit Service 02e2fd
  OP_TYPEMINSTAR
Packit Service 02e2fd
  OP_TYPEPOSSTAR
Packit Service 02e2fd
  OP_TYPEPLUS
Packit Service 02e2fd
  OP_TYPEMINPLUS
Packit Service 02e2fd
  OP_TYPEPOSPLUS
Packit Service 02e2fd
  OP_TYPEQUERY
Packit Service 02e2fd
  OP_TYPEMINQUERY
Packit Service 02e2fd
  OP_TYPEPOSQUERY
Packit Service 02e2fd
  OP_TYPEUPTO
Packit Service 02e2fd
  OP_TYPEMINUPTO
Packit Service 02e2fd
  OP_TYPEPOSUPTO
Packit Service 02e2fd
  OP_TYPEEXACT
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Match by Unicode property
Packit Service 02e2fd
-------------------------
Packit Service 02e2fd
Packit Service 02e2fd
OP_PROP and OP_NOTPROP are used for positive and negative matches of a
Packit Service 02e2fd
character by testing its Unicode property (the \p and \P escape sequences).
Packit Service 02e2fd
Each is followed by two code units that encode the desired property as a type
Packit Service 02e2fd
and a value. The types are a set of #defines of the form PT_xxx, and the values
Packit Service 02e2fd
are enumerations of the form ucp_xx, defined in the pcre2_ucp.h source file.
Packit Service 02e2fd
The value is relevant only for PT_GC (General Category), PT_PC (Particular
Packit Service 02e2fd
Category), PT_SC (Script), and the pseudo-property PT_CLIST, which is used to
Packit Service 02e2fd
identify a list of case-equivalent characters when there are three or more.
Packit Service 02e2fd
Packit Service 02e2fd
Repeats of these items use the OP_TYPESTAR etc. set of opcodes, followed by
Packit Service 02e2fd
three code units: OP_PROP or OP_NOTPROP, and then the desired property type and
Packit Service 02e2fd
value.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Character classes
Packit Service 02e2fd
-----------------
Packit Service 02e2fd
Packit Service 02e2fd
If there is only one character in a class, OP_CHAR or OP_CHARI is used for a
Packit Service 02e2fd
positive class, and OP_NOT or OP_NOTI for a negative one (that is, for
Packit Service 02e2fd
something like [^a]), except when caselessly matching a character that has more
Packit Service 02e2fd
than two case-equivalent code points (which can happen only in UTF mode). In
Packit Service 02e2fd
this case a Unicode property item is used, as described above in "Matching
Packit Service 02e2fd
literal characters".
Packit Service 02e2fd
Packit Service 02e2fd
A set of repeating opcodes (called OP_NOTSTAR etc.) are used for repeated,
Packit Service 02e2fd
negated, single-character classes. The normal single-character opcodes
Packit Service 02e2fd
(OP_STAR, etc.) are used for repeated positive single-character classes.
Packit Service 02e2fd
Packit Service 02e2fd
When there is more than one character in a class, and all the code points are
Packit Service 02e2fd
less than 256, OP_CLASS is used for a positive class, and OP_NCLASS for a
Packit Service 02e2fd
negative one. In either case, the opcode is followed by a 32-byte (16-short,
Packit Service 02e2fd
8-word) bit map containing a 1 bit for every character that is acceptable. The
Packit Service 02e2fd
bits are counted from the least significant end of each unit. In caseless mode,
Packit Service 02e2fd
bits for both cases are set.
Packit Service 02e2fd
Packit Service 02e2fd
The reason for having both OP_CLASS and OP_NCLASS is so that, in UTF-8 and
Packit Service 02e2fd
16-bit and 32-bit modes, subject characters with values greater than 255 can be
Packit Service 02e2fd
handled correctly. For OP_CLASS they do not match, whereas for OP_NCLASS they
Packit Service 02e2fd
do.
Packit Service 02e2fd
Packit Service 02e2fd
For classes containing characters with values greater than 255 or that contain
Packit Service 02e2fd
\p or \P, OP_XCLASS is used. It optionally uses a bit map if any acceptable
Packit Service 02e2fd
code points are less than 256, followed by a list of pairs (for a range) and/or
Packit Service 02e2fd
single characters and/or properties. In caseless mode, all equivalent
Packit Service 02e2fd
characters are explicitly listed.
Packit Service 02e2fd
Packit Service 02e2fd
OP_XCLASS is followed by a LINK_SIZE value containing the total length of the
Packit Service 02e2fd
opcode and its data. This is followed by a code unit containing flag bits:
Packit Service 02e2fd
XCL_NOT indicates that this is a negative class, and XCL_MAP indicates that a
Packit Service 02e2fd
bit map is present. There follows the bit map, if XCL_MAP is set, and then a
Packit Service 02e2fd
sequence of items coded as follows:
Packit Service 02e2fd
Packit Service 02e2fd
  XCL_END      marks the end of the list
Packit Service 02e2fd
  XCL_SINGLE   one character follows
Packit Service 02e2fd
  XCL_RANGE    two characters follow
Packit Service 02e2fd
  XCL_PROP     a Unicode property (type, value) follows
Packit Service 02e2fd
  XCL_NOTPROP  a Unicode property (type, value) follows
Packit Service 02e2fd
Packit Service 02e2fd
If a range starts with a code point less than 256 and ends with one greater
Packit Service 02e2fd
than 255, it is split into two ranges, with characters less than 256 being
Packit Service 02e2fd
indicated in the bit map, and the rest with XCL_RANGE.
Packit Service 02e2fd
Packit Service 02e2fd
When XCL_NOT is set, the bit map, if present, contains bits for characters that
Packit Service 02e2fd
are allowed (exactly as for OP_NCLASS), but the list of items that follow it
Packit Service 02e2fd
specifies characters and properties that are not allowed.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Back references
Packit Service 02e2fd
---------------
Packit Service 02e2fd
Packit Service 02e2fd
OP_REF (caseful) or OP_REFI (caseless) is followed by a count containing the
Packit Service 02e2fd
reference number when the reference is to a unique capturing group (either by
Packit Service 02e2fd
number or by name). When named groups are used, there may be more than one
Packit Service 02e2fd
group with the same name. In this case, a reference to such a group by name
Packit Service 02e2fd
generates OP_DNREF or OP_DNREFI. These are followed by two counts: the index
Packit Service 02e2fd
(not the byte offset) in the group name table of the first entry for the
Packit Service 02e2fd
required name, followed by the number of groups with the same name. The
Packit Service 02e2fd
matching code can then search for the first one that is set.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Repeating character classes and back references
Packit Service 02e2fd
-----------------------------------------------
Packit Service 02e2fd
Packit Service 02e2fd
Single-character classes are handled specially (see above). This section
Packit Service 02e2fd
applies to other classes and also to back references. In both cases, the repeat
Packit Service 02e2fd
information follows the base item. The matching code looks at the following
Packit Service 02e2fd
opcode to see if it is one of these:
Packit Service 02e2fd
Packit Service 02e2fd
  OP_CRSTAR
Packit Service 02e2fd
  OP_CRMINSTAR
Packit Service 02e2fd
  OP_CRPOSSTAR
Packit Service 02e2fd
  OP_CRPLUS
Packit Service 02e2fd
  OP_CRMINPLUS
Packit Service 02e2fd
  OP_CRPOSPLUS
Packit Service 02e2fd
  OP_CRQUERY
Packit Service 02e2fd
  OP_CRMINQUERY
Packit Service 02e2fd
  OP_CRPOSQUERY
Packit Service 02e2fd
  OP_CRRANGE
Packit Service 02e2fd
  OP_CRMINRANGE
Packit Service 02e2fd
  OP_CRPOSRANGE
Packit Service 02e2fd
Packit Service 02e2fd
All but the last three are single-code-unit items, with no data. The range
Packit Service 02e2fd
opcodes are followed by the minimum and maximum repeat counts.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Brackets and alternation
Packit Service 02e2fd
------------------------
Packit Service 02e2fd
Packit Service 02e2fd
A pair of non-capturing round brackets is wrapped round each expression at
Packit Service 02e2fd
compile time, so alternation always happens in the context of brackets.
Packit Service 02e2fd
Packit Service 02e2fd
[Note for North Americans: "bracket" to some English speakers, including
Packit Service 02e2fd
myself, can be round, square, curly, or pointy. Hence this usage rather than
Packit Service 02e2fd
"parentheses".]
Packit Service 02e2fd
Packit Service 02e2fd
Non-capturing brackets use the opcode OP_BRA, capturing brackets use OP_CBRA. A
Packit Service 02e2fd
bracket opcode is followed by a LINK_SIZE value which gives the offset to the
Packit Service 02e2fd
next alternative OP_ALT or, if there aren't any branches, to the terminating
Packit Service 02e2fd
opcode. Each OP_ALT is followed by a LINK_SIZE value giving the offset to the
Packit Service 02e2fd
next one, or to the final opcode. For capturing brackets, the bracket number is
Packit Service 02e2fd
a count that immediately follows the offset.
Packit Service 02e2fd
Packit Service 02e2fd
There are several opcodes that mark the end of a subpattern group. OP_KET is
Packit Service 02e2fd
used for subpatterns that do not repeat indefinitely, OP_KETRMIN and
Packit Service 02e2fd
OP_KETRMAX are used for indefinite repetitions, minimally or maximally
Packit Service 02e2fd
respectively, and OP_KETRPOS for possessive repetitions (see below for more 
Packit Service 02e2fd
details). All four are followed by a LINK_SIZE value giving (as a positive
Packit Service 02e2fd
number) the offset back to the matching bracket opcode.
Packit Service 02e2fd
Packit Service 02e2fd
If a subpattern is quantified such that it is permitted to match zero times, it
Packit Service 02e2fd
is preceded by one of OP_BRAZERO, OP_BRAMINZERO, or OP_SKIPZERO. These are
Packit Service 02e2fd
single-unit opcodes that tell the matcher that skipping the following
Packit Service 02e2fd
subpattern entirely is a valid match. In the case of the first two, not
Packit Service 02e2fd
skipping the pattern is also valid (greedy and non-greedy). The third is used
Packit Service 02e2fd
when a pattern has the quantifier {0,0}. It cannot be entirely discarded,
Packit Service 02e2fd
because it may be called as a subroutine from elsewhere in the pattern.
Packit Service 02e2fd
Packit Service 02e2fd
A subpattern with an indefinite maximum repetition is replicated in the
Packit Service 02e2fd
compiled data its minimum number of times (or once with OP_BRAZERO if the
Packit Service 02e2fd
minimum is zero), with the final copy terminating with OP_KETRMIN or OP_KETRMAX
Packit Service 02e2fd
as appropriate.
Packit Service 02e2fd
Packit Service 02e2fd
A subpattern with a bounded maximum repetition is replicated in a nested
Packit Service 02e2fd
fashion up to the maximum number of times, with OP_BRAZERO or OP_BRAMINZERO
Packit Service 02e2fd
before each replication after the minimum, so that, for example, (abc){2,5} is
Packit Service 02e2fd
compiled as (abc)(abc)((abc)((abc)(abc)?)?)?, except that each bracketed group
Packit Service 02e2fd
has the same number.
Packit Service 02e2fd
Packit Service 02e2fd
When a repeated subpattern has an unbounded upper limit, it is checked to see
Packit Service 02e2fd
whether it could match an empty string. If this is the case, the opcode in the
Packit Service 02e2fd
final replication is changed to OP_SBRA or OP_SCBRA. This tells the matcher
Packit Service 02e2fd
that it needs to check for matching an empty string when it hits OP_KETRMIN or
Packit Service 02e2fd
OP_KETRMAX, and if so, to break the loop.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Possessive brackets
Packit Service 02e2fd
-------------------
Packit Service 02e2fd
Packit Service 02e2fd
When a repeated group (capturing or non-capturing) is marked as possessive by
Packit Service 02e2fd
the "+" notation, e.g. (abc)++, different opcodes are used. Their names all
Packit Service 02e2fd
have POS on the end, e.g. OP_BRAPOS instead of OP_BRA and OP_SCBRAPOS instead
Packit Service 02e2fd
of OP_SCBRA. The end of such a group is marked by OP_KETRPOS. If the minimum
Packit Service 02e2fd
repetition is zero, the group is preceded by OP_BRAPOSZERO.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Once-only (atomic) groups
Packit Service 02e2fd
-------------------------
Packit Service 02e2fd
Packit Service 02e2fd
These are just like other subpatterns, but they start with the opcode OP_ONCE.
Packit Service 02e2fd
The check for matching an empty string in an unbounded repeat is handled
Packit Service 02e2fd
entirely at runtime, so there is just this one opcode for atomic groups.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Assertions
Packit Service 02e2fd
----------
Packit Service 02e2fd
Packit Service 02e2fd
Forward assertions are also just like other subpatterns, but starting with one
Packit Service 02e2fd
of the opcodes OP_ASSERT or OP_ASSERT_NOT. Backward assertions use the opcodes
Packit Service 02e2fd
OP_ASSERTBACK and OP_ASSERTBACK_NOT, and the first opcode inside the assertion
Packit Service 02e2fd
is OP_REVERSE, followed by a count of the number of characters to move back the
Packit Service 02e2fd
pointer in the subject string. In ASCII or UTF-32 mode, the count is also the
Packit Service 02e2fd
number of code units, but in UTF-8/16 mode each character may occupy more than
Packit Service 02e2fd
one code unit. A separate count is present in each alternative of a lookbehind
Packit Service 02e2fd
assertion, allowing them to have different (but fixed) lengths.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Conditional subpatterns
Packit Service 02e2fd
-----------------------
Packit Service 02e2fd
Packit Service 02e2fd
These are like other subpatterns, but they start with the opcode OP_COND, or
Packit Service 02e2fd
OP_SCOND for one that might match an empty string in an unbounded repeat.
Packit Service 02e2fd
Packit Service 02e2fd
If the condition is a back reference, this is stored at the start of the
Packit Service 02e2fd
subpattern using the opcode OP_CREF followed by a count containing the
Packit Service 02e2fd
reference number, provided that the reference is to a unique capturing group.
Packit Service 02e2fd
If the reference was by name and there is more than one group with that name,
Packit Service 02e2fd
OP_DNCREF is used instead. It is followed by two counts: the index in the group
Packit Service 02e2fd
names table, and the number of groups with the same name. The allows the
Packit Service 02e2fd
matcher to check if any group with the given name is set.
Packit Service 02e2fd
Packit Service 02e2fd
If the condition is "in recursion" (coded as "(?(R)"), or "in recursion of
Packit Service 02e2fd
group x" (coded as "(?(Rx)"), the group number is stored at the start of the
Packit Service 02e2fd
subpattern using the opcode OP_RREF (with a value of RREF_ANY (0xffff) for "the
Packit Service 02e2fd
whole pattern") or OP_DNRREF (with data as for OP_DNCREF).
Packit Service 02e2fd
Packit Service 02e2fd
For a DEFINE condition, OP_FALSE is used (with no associated data). During
Packit Service 02e2fd
compilation, however, a DEFINE condition is coded as OP_DEFINE so that, when
Packit Service 02e2fd
the conditional group is complete, there can be a check to ensure that it
Packit Service 02e2fd
contains only one top-level branch. Once this has happened, the opcode is
Packit Service 02e2fd
changed to OP_FALSE, so the matcher never sees OP_DEFINE.
Packit Service 02e2fd
Packit Service 02e2fd
There is a special PCRE2-specific condition of the form (VERSION[>]=x.y), which
Packit Service 02e2fd
tests the PCRE2 version number. This compiles into one of the opcodes OP_TRUE
Packit Service 02e2fd
or OP_FALSE.
Packit Service 02e2fd
Packit Service 02e2fd
If a condition is not a back reference, recursion test, DEFINE, or VERSION, it
Packit Service 02e2fd
must start with a parenthesized assertion, whose opcode normally immediately
Packit Service 02e2fd
follows OP_COND or OP_SCOND. However, if automatic callouts are enabled, a
Packit Service 02e2fd
callout is inserted immediately before the assertion. It is also possible to
Packit Service 02e2fd
insert a manual callout at this point. Only assertion conditions may have
Packit Service 02e2fd
callouts preceding the condition.
Packit Service 02e2fd
Packit Service 02e2fd
A condition that is the negative assertion (?!) is optimized to OP_FAIL in all
Packit Service 02e2fd
parts of the pattern, so this is another opcode that may appear as a condition.
Packit Service 02e2fd
It is treated the same as OP_FALSE.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Recursion
Packit Service 02e2fd
---------
Packit Service 02e2fd
Packit Service 02e2fd
Recursion either matches the current pattern, or some subexpression. The opcode
Packit Service 02e2fd
OP_RECURSE is followed by a LINK_SIZE value that is the offset to the starting
Packit Service 02e2fd
bracket from the start of the whole pattern. OP_RECURSE is also used for
Packit Service 02e2fd
"subroutine" calls, even though they are not strictly a recursion. Up till
Packit Service 02e2fd
release 10.30 recursions were treated as atomic groups, making them
Packit Service 02e2fd
incompatible with Perl (but PCRE had them well before Perl did). From 10.30,
Packit Service 02e2fd
backtracking into recursions is supported.
Packit Service 02e2fd
Packit Service 02e2fd
Repeated recursions used to be wrapped inside OP_ONCE brackets, which not only
Packit Service 02e2fd
forced no backtracking, but also allowed repetition to be handled as for other
Packit Service 02e2fd
bracketed groups. From 10.30 onwards, repeated recursions are duplicated for
Packit Service 02e2fd
their minimum repetitions, and then wrapped in non-capturing brackets for the
Packit Service 02e2fd
remainder. For example, (?1){3} is treated as (?1)(?1)(?1), and (?1){2,4} is
Packit Service 02e2fd
treated as (?1)(?1)(?:(?1)){0,2}.
Packit Service 02e2fd
Packit Service 02e2fd
Packit Service 02e2fd
Callouts
Packit Service 02e2fd
--------
Packit Service 02e2fd
Packit Service 02e2fd
A callout may have either a numerical argument or a string argument. These use
Packit Service 02e2fd
OP_CALLOUT or OP_CALLOUT_STR, respectively. In each case these are followed by
Packit Service 02e2fd
two LINK_SIZE values giving the offset in the pattern string to the start of
Packit Service 02e2fd
the following item, and another count giving the length of this item. These
Packit Service 02e2fd
values make it possible for pcre2test to output useful tracing information
Packit Service 02e2fd
using callouts.
Packit Service 02e2fd
Packit Service 02e2fd
In the case of a numeric callout, after these two values there is a single code
Packit Service 02e2fd
unit containing the callout number, in the range 0-255, with 255 being used for
Packit Service 02e2fd
callouts that are automatically inserted as a result of the PCRE2_AUTO_CALLOUT
Packit Service 02e2fd
option. Thus, this opcode item is of fixed length:
Packit Service 02e2fd
Packit Service 02e2fd
  [OP_CALLOUT] [PATTERN_OFFSET] [PATTERN_LENGTH] [NUMBER]
Packit Service 02e2fd
Packit Service 02e2fd
For callouts with string arguments, OP_CALLOUT_STR has three more data items:
Packit Service 02e2fd
a LINK_SIZE value giving the complete length of the entire opcode item, a
Packit Service 02e2fd
LINK_SIZE item containing the offset within the pattern string to the start of
Packit Service 02e2fd
the string argument, and the string itself, preceded by its starting delimiter
Packit Service 02e2fd
and followed by a binary zero. When a callout function is called, a pointer to
Packit Service 02e2fd
the actual string is passed, but the delimiter can be accessed as string[-1] if
Packit Service 02e2fd
the application needs it. In the 8-bit library, the callout in /X(?C'abc')Y/ is
Packit Service 02e2fd
compiled as the following bytes (decimal numbers represent binary values):
Packit Service 02e2fd
Packit Service 02e2fd
  [OP_CALLOUT_STR]  [0] [10]  [0] [1]  [0] [14]  [0] [5] ['] [a] [b] [c] [0]
Packit Service 02e2fd
                    --------  -------  --------  -------
Packit Service 02e2fd
                       |         |        |         |
Packit Service 02e2fd
                       ------- LINK_SIZE items ------
Packit Service 02e2fd
Packit Service 02e2fd
Opcode table checking
Packit Service 02e2fd
---------------------
Packit Service 02e2fd
Packit Service 02e2fd
The last opcode that is defined in pcre2_internal.h is OP_TABLE_LENGTH. This is
Packit Service 02e2fd
not a real opcode, but is used to check at compile time that tables indexed by
Packit Service 02e2fd
opcode are the correct length, in order to catch updating errors.
Packit Service 02e2fd
Packit Service 02e2fd
Philip Hazel
Packit Service 02e2fd
20 July 2018