| 6 |
restricted in what they could do by comparison with Perl. The interesting part |
restricted in what they could do by comparison with Perl. The interesting part |
| 7 |
about the algorithm was that the amount of space required to hold the compiled |
about the algorithm was that the amount of space required to hold the compiled |
| 8 |
form of an expression was known in advance. The code to apply an expression did |
form of an expression was known in advance. The code to apply an expression did |
| 9 |
not operate by backtracking, as the Henry Spencer and Perl code does, but |
not operate by backtracking, as the original Henry Spencer code and current |
| 10 |
instead checked all possibilities simultaneously by keeping a list of current |
Perl code does, but instead checked all possibilities simultaneously by keeping |
| 11 |
states and checking all of them as it advanced through the subject string. (In |
a list of current states and checking all of them as it advanced through the |
| 12 |
the terminology of Jeffrey Friedl's book, it was a "DFA algorithm".) When the |
subject string. (In the terminology of Jeffrey Friedl's book, it was a "DFA |
| 13 |
pattern was all used up, all remaining states were possible matches, and the |
algorithm".) When the pattern was all used up, all remaining states were |
| 14 |
one matching the longest subset of the subject string was chosen. This did not |
possible matches, and the one matching the longest subset of the subject string |
| 15 |
necessarily maximize the individual wild portions of the pattern, as is |
was chosen. This did not necessarily maximize the individual wild portions of |
| 16 |
expected in Unix and Perl-style regular expressions. |
the pattern, as is expected in Unix and Perl-style regular expressions. |
| 17 |
|
|
| 18 |
By contrast, the code originally written by Henry Spencer and subsequently |
By contrast, the code originally written by Henry Spencer and subsequently |
| 19 |
heavily modified for Perl actually compiles the expression twice: once in a |
heavily modified for Perl actually compiles the expression twice: once in a |
| 28 |
of store bounded by a multiple of the number of characters in the pattern, to |
of store bounded by a multiple of the number of characters in the pattern, to |
| 29 |
save on compiling time. However, because of the greater complexity in Perl |
save on compiling time. However, because of the greater complexity in Perl |
| 30 |
regular expressions, I couldn't do this. In any case, a first pass through the |
regular expressions, I couldn't do this. In any case, a first pass through the |
| 31 |
pattern is needed, in order to find internal flag settings like (?i) at top |
pattern is needed, for a number of reasons. PCRE works by running a very |
| 32 |
level. So PCRE works by running a very degenerate first pass to calculate a |
degenerate first pass to calculate a maximum store size, and then a second pass |
| 33 |
maximum store size, and then a second pass to do the real compile - which may |
to do the real compile - which may use a bit less than the predicted amount of |
| 34 |
use a bit less than the predicted amount of store. The idea is that this is |
store. The idea is that this is going to turn out faster because the first pass |
| 35 |
going to turn out faster because the first pass is degenerate and the second |
is degenerate and the second pass can just store stuff straight into the |
| 36 |
pass can just store stuff straight into the vector. It does make the compiling |
vector. It does make the compiling functions bigger, of course, but they have |
| 37 |
functions bigger, of course, but they have got quite big anyway to handle all |
got quite big anyway to handle all the Perl stuff. |
|
the Perl stuff. |
|
| 38 |
|
|
| 39 |
The compiled form of a pattern is a vector of bytes, containing items of |
The compiled form of a pattern is a vector of bytes, containing items of |
| 40 |
variable length. The first byte in an item is an opcode, and the length of the |
variable length. The first byte in an item is an opcode, and the length of the |
| 48 |
|
|
| 49 |
OP_END end of pattern |
OP_END end of pattern |
| 50 |
OP_ANY match any character |
OP_ANY match any character |
| 51 |
|
OP_ANYBYTE match any single byte, even in UTF-8 mode |
| 52 |
OP_SOD match start of data: \A |
OP_SOD match start of data: \A |
| 53 |
|
OP_SOM, start of match (subject + offset): \G |
| 54 |
OP_CIRC ^ (start of data, or after \n in multiline) |
OP_CIRC ^ (start of data, or after \n in multiline) |
| 55 |
OP_NOT_WORD_BOUNDARY \W |
OP_NOT_WORD_BOUNDARY \W |
| 56 |
OP_WORD_BOUNDARY \w |
OP_WORD_BOUNDARY \w |
| 63 |
OP_EODN match end of data or \n at end: \Z |
OP_EODN match end of data or \n at end: \Z |
| 64 |
OP_EOD match end of data: \z |
OP_EOD match end of data: \z |
| 65 |
OP_DOLL $ (end of data, or before \n in multiline) |
OP_DOLL $ (end of data, or before \n in multiline) |
|
OP_RECURSE match the pattern recursively |
|
| 66 |
|
|
| 67 |
|
|
| 68 |
Repeating single characters |
Repeating single characters |
| 120 |
Character classes |
Character classes |
| 121 |
----------------- |
----------------- |
| 122 |
|
|
| 123 |
OP_CLASS is used for a character class, provided there are at least two |
If there is only one character, OP_CHARS is used for a positive class, |
| 124 |
characters in the class. If there is only one character, OP_CHARS is used for a |
and OP_NOT for a negative one (that is, for something like [^a]). However, in |
| 125 |
positive class, and OP_NOT for a negative one (that is, for something like |
UTF-8 mode, this applies only to characters with values < 128, because OP_NOT |
| 126 |
[^a]). Another set of repeating opcodes (OP_NOTSTAR etc.) are used for a |
is confined to single bytes. |
| 127 |
repeated, negated, single-character class. The normal ones (OP_STAR etc.) are |
|
| 128 |
used for a repeated positive single-character class. |
Another set of repeating opcodes (OP_NOTSTAR etc.) are used for a repeated, |
| 129 |
|
negated, single-character class. The normal ones (OP_STAR etc.) are used for a |
| 130 |
OP_CLASS is followed by a 32-byte bit map containing a 1 bit for every |
repeated positive single-character class. |
| 131 |
character that is acceptable. The bits are counted from the least significant |
|
| 132 |
end of each byte. |
When there's more than one character in a class and all the characters are less |
| 133 |
|
than 256, OP_CLASS is used for a positive class, and OP_NCLASS for a negative |
| 134 |
|
one. In either case, the opcode is followed by a 32-byte bit map containing a 1 |
| 135 |
|
bit for every character that is acceptable. The bits are counted from the least |
| 136 |
|
significant end of each byte. |
| 137 |
|
|
| 138 |
|
The reason for having both OP_CLASS and OP_NCLASS is so that, in UTF-8 mode, |
| 139 |
|
subject characters with values greater than 256 can be handled correctly. For |
| 140 |
|
OP_CLASS they don't match, whereas for OP_NCLASS they do. |
| 141 |
|
|
| 142 |
|
For classes containing characters with values > 255, OP_XCLASS is used. It |
| 143 |
|
optionally uses a bit map (if any characters lie within it), followed by a list |
| 144 |
|
of pairs and single characters. There is a flag character than indicates |
| 145 |
|
whether it's a positive or a negative class. |
| 146 |
|
|
| 147 |
|
|
| 148 |
Back references |
Back references |
| 149 |
--------------- |
--------------- |
| 150 |
|
|
| 151 |
OP_REF is followed by a single byte containing the reference number. |
OP_REF is followed by two bytes containing the reference number. |
| 152 |
|
|
| 153 |
|
|
| 154 |
Repeating character classes and back references |
Repeating character classes and back references |
| 176 |
|
|
| 177 |
A pair of non-capturing (round) brackets is wrapped round each expression at |
A pair of non-capturing (round) brackets is wrapped round each expression at |
| 178 |
compile time, so alternation always happens in the context of brackets. |
compile time, so alternation always happens in the context of brackets. |
| 179 |
|
|
| 180 |
Non-capturing brackets use the opcode OP_BRA, while capturing brackets use |
Non-capturing brackets use the opcode OP_BRA, while capturing brackets use |
| 181 |
OP_BRA+1, OP_BRA+2, etc. [Note for North Americans: "bracket" to some English |
OP_BRA+1, OP_BRA+2, etc. [Note for North Americans: "bracket" to some English |
| 182 |
speakers, including myself, can be round, square, curly, or pointy. Hence this |
speakers, including myself, can be round, square, curly, or pointy. Hence this |
| 183 |
usage.] |
usage.] |
| 184 |
|
|
| 185 |
|
Originally PCRE was limited to 99 capturing brackets (so as not to use up all |
| 186 |
|
the opcodes). From release 3.5, there is no limit. What happens is that the |
| 187 |
|
first ones, up to EXTRACT_BASIC_MAX are handled with separate opcodes, as |
| 188 |
|
above. If there are more, the opcode is set to EXTRACT_BASIC_MAX+1, and the |
| 189 |
|
first operation in the bracket is OP_BRANUMBER, followed by a 2-byte bracket |
| 190 |
|
number. This opcode is ignored while matching, but is fished out when handling |
| 191 |
|
the bracket itself. (They could have all been done like this, but I was making |
| 192 |
|
minimal changes.) |
| 193 |
|
|
| 194 |
A bracket opcode is followed by two bytes which give the offset to the next |
A bracket opcode is followed by two bytes which give the offset to the next |
| 195 |
alternative OP_ALT or, if there aren't any branches, to the matching KET |
alternative OP_ALT or, if there aren't any branches, to the matching KET |
| 196 |
opcode. Each OP_ALT is followed by two bytes giving the offset to the next one, |
opcode. Each OP_ALT is followed by two bytes giving the offset to the next one, |
| 214 |
A subpattern with a bounded maximum repetition is replicated in a nested |
A subpattern with a bounded maximum repetition is replicated in a nested |
| 215 |
fashion up to the maximum number of times, with BRAZERO or BRAMINZERO before |
fashion up to the maximum number of times, with BRAZERO or BRAMINZERO before |
| 216 |
each replication after the minimum, so that, for example, (abc){2,5} is |
each replication after the minimum, so that, for example, (abc){2,5} is |
| 217 |
compiled as (abc)(abc)((abc)((abc)(abc)?)?)?. The 200-bracket limit does not |
compiled as (abc)(abc)((abc)((abc)(abc)?)?)?. The 99 and 200 bracket limits do |
| 218 |
apply to these internally generated brackets. |
not apply to these internally generated brackets. |
| 219 |
|
|
| 220 |
|
|
| 221 |
Assertions |
Assertions |
| 243 |
|
|
| 244 |
These are like other subpatterns, but they start with the opcode OP_COND. If |
These are like other subpatterns, but they start with the opcode OP_COND. If |
| 245 |
the condition is a back reference, this is stored at the start of the |
the condition is a back reference, this is stored at the start of the |
| 246 |
subpattern using the opcode OP_CREF followed by one byte containing the |
subpattern using the opcode OP_CREF followed by two bytes containing the |
| 247 |
reference number. Otherwise, a conditional subpattern will always start with |
reference number. If the condition is "in recursion" (coded as "(?(R)"), the |
| 248 |
one of the assertions. |
same scheme is used, with a "reference number" of 0xffff. Otherwise, a |
| 249 |
|
conditional subpattern always starts with one of the assertions. |
| 250 |
|
|
| 251 |
|
|
| 252 |
|
Recursion |
| 253 |
|
--------- |
| 254 |
|
|
| 255 |
|
Recursion either matches the current regex, or some subexpression. The opcode |
| 256 |
|
OP_RECURSE is followed by an value which is the offset to the starting bracket |
| 257 |
|
from the start of the whole pattern. |
| 258 |
|
|
| 259 |
|
|
| 260 |
|
Callout |
| 261 |
|
------- |
| 262 |
|
|
| 263 |
|
OP_CALLOUT is followed by one byte of data that holds a callout number in the |
| 264 |
|
range 0 to 255. |
| 265 |
|
|
| 266 |
|
|
| 267 |
Changing options |
Changing options |
| 268 |
---------------- |
---------------- |
| 269 |
|
|
| 270 |
If any of the /i, /m, or /s options are changed within a parenthesized group, |
If any of the /i, /m, or /s options are changed within a pattern, an OP_OPT |
| 271 |
an OP_OPT opcode is compiled, followed by one byte containing the new settings |
opcode is compiled, followed by one byte containing the new settings of these |
| 272 |
of these flags. If there are several alternatives in a group, there is an |
flags. If there are several alternatives, there is an occurrence of OP_OPT at |
| 273 |
occurrence of OP_OPT at the start of all those following the first options |
the start of all those following the first options change, to set appropriate |
| 274 |
change, to set appropriate options for the start of the alternative. |
options for the start of the alternative. Immediately after the end of the |
| 275 |
Immediately after the end of the group there is another such item to reset the |
group there is another such item to reset the flags to their previous values. A |
| 276 |
flags to their previous values. Other changes of flag within the pattern can be |
change of flag right at the very start of the pattern can be handled entirely |
| 277 |
handled entirely at compile time, and so do not cause anything to be put into |
at compile time, and so does not cause anything to be put into the compiled |
| 278 |
the compiled data. |
data. |
|
|
|
| 279 |
|
|
| 280 |
Philip Hazel |
Philip Hazel |
| 281 |
August 2000 |
August 2003 |