pcre2callout.3 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. .TH PCRE2CALLOUT 3 "03 February 2019" "PCRE2 10.33"
  2. .SH NAME
  3. PCRE2 - Perl-compatible regular expressions (revised API)
  4. .SH SYNOPSIS
  5. .rs
  6. .sp
  7. .B #include <pcre2.h>
  8. .PP
  9. .SM
  10. .nf
  11. .B int (*pcre2_callout)(pcre2_callout_block *, void *);
  12. .sp
  13. .B int pcre2_callout_enumerate(const pcre2_code *\fIcode\fP,
  14. .B " int (*\fIcallback\fP)(pcre2_callout_enumerate_block *, void *),"
  15. .B " void *\fIuser_data\fP);"
  16. .fi
  17. .
  18. .SH DESCRIPTION
  19. .rs
  20. .sp
  21. PCRE2 provides a feature called "callout", which is a means of temporarily
  22. passing control to the caller of PCRE2 in the middle of pattern matching. The
  23. caller of PCRE2 provides an external function by putting its entry point in
  24. a match context (see \fBpcre2_set_callout()\fP in the
  25. .\" HREF
  26. \fBpcre2api\fP
  27. .\"
  28. documentation).
  29. .P
  30. When using the \fBpcre2_substitute()\fP function, an additional callout feature
  31. is available. This does a callout after each change to the subject string and
  32. is described in the
  33. .\" HREF
  34. \fBpcre2api\fP
  35. .\"
  36. documentation; the rest of this document is concerned with callouts during
  37. pattern matching.
  38. .P
  39. Within a regular expression, (?C<arg>) indicates a point at which the external
  40. function is to be called. Different callout points can be identified by putting
  41. a number less than 256 after the letter C. The default value is zero.
  42. Alternatively, the argument may be a delimited string. The starting delimiter
  43. must be one of ` ' " ^ % # $ { and the ending delimiter is the same as the
  44. start, except for {, where the ending delimiter is }. If the ending delimiter
  45. is needed within the string, it must be doubled. For example, this pattern has
  46. two callout points:
  47. .sp
  48. (?C1)abc(?C"some ""arbitrary"" text")def
  49. .sp
  50. If the PCRE2_AUTO_CALLOUT option bit is set when a pattern is compiled, PCRE2
  51. automatically inserts callouts, all with number 255, before each item in the
  52. pattern except for immediately before or after an explicit callout. For
  53. example, if PCRE2_AUTO_CALLOUT is used with the pattern
  54. .sp
  55. A(?C3)B
  56. .sp
  57. it is processed as if it were
  58. .sp
  59. (?C255)A(?C3)B(?C255)
  60. .sp
  61. Here is a more complicated example:
  62. .sp
  63. A(\ed{2}|--)
  64. .sp
  65. With PCRE2_AUTO_CALLOUT, this pattern is processed as if it were
  66. .sp
  67. (?C255)A(?C255)((?C255)\ed{2}(?C255)|(?C255)-(?C255)-(?C255))(?C255)
  68. .sp
  69. Notice that there is a callout before and after each parenthesis and
  70. alternation bar. If the pattern contains a conditional group whose condition is
  71. an assertion, an automatic callout is inserted immediately before the
  72. condition. Such a callout may also be inserted explicitly, for example:
  73. .sp
  74. (?(?C9)(?=a)ab|de) (?(?C%text%)(?!=d)ab|de)
  75. .sp
  76. This applies only to assertion conditions (because they are themselves
  77. independent groups).
  78. .P
  79. Callouts can be useful for tracking the progress of pattern matching. The
  80. .\" HREF
  81. \fBpcre2test\fP
  82. .\"
  83. program has a pattern qualifier (/auto_callout) that sets automatic callouts.
  84. When any callouts are present, the output from \fBpcre2test\fP indicates how
  85. the pattern is being matched. This is useful information when you are trying to
  86. optimize the performance of a particular pattern.
  87. .
  88. .
  89. .SH "MISSING CALLOUTS"
  90. .rs
  91. .sp
  92. You should be aware that, because of optimizations in the way PCRE2 compiles
  93. and matches patterns, callouts sometimes do not happen exactly as you might
  94. expect.
  95. .
  96. .
  97. .SS "Auto-possessification"
  98. .rs
  99. .sp
  100. At compile time, PCRE2 "auto-possessifies" repeated items when it knows that
  101. what follows cannot be part of the repeat. For example, a+[bc] is compiled as
  102. if it were a++[bc]. The \fBpcre2test\fP output when this pattern is compiled
  103. with PCRE2_ANCHORED and PCRE2_AUTO_CALLOUT and then applied to the string
  104. "aaaa" is:
  105. .sp
  106. --->aaaa
  107. +0 ^ a+
  108. +2 ^ ^ [bc]
  109. No match
  110. .sp
  111. This indicates that when matching [bc] fails, there is no backtracking into a+
  112. (because it is being treated as a++) and therefore the callouts that would be
  113. taken for the backtracks do not occur. You can disable the auto-possessify
  114. feature by passing PCRE2_NO_AUTO_POSSESS to \fBpcre2_compile()\fP, or starting
  115. the pattern with (*NO_AUTO_POSSESS). In this case, the output changes to this:
  116. .sp
  117. --->aaaa
  118. +0 ^ a+
  119. +2 ^ ^ [bc]
  120. +2 ^ ^ [bc]
  121. +2 ^ ^ [bc]
  122. +2 ^^ [bc]
  123. No match
  124. .sp
  125. This time, when matching [bc] fails, the matcher backtracks into a+ and tries
  126. again, repeatedly, until a+ itself fails.
  127. .
  128. .
  129. .SS "Automatic .* anchoring"
  130. .rs
  131. .sp
  132. By default, an optimization is applied when .* is the first significant item in
  133. a pattern. If PCRE2_DOTALL is set, so that the dot can match any character, the
  134. pattern is automatically anchored. If PCRE2_DOTALL is not set, a match can
  135. start only after an internal newline or at the beginning of the subject, and
  136. \fBpcre2_compile()\fP remembers this. If a pattern has more than one top-level
  137. branch, automatic anchoring occurs if all branches are anchorable.
  138. .P
  139. This optimization is disabled, however, if .* is in an atomic group or if there
  140. is a backreference to the capture group in which it appears. It is also
  141. disabled if the pattern contains (*PRUNE) or (*SKIP). However, the presence of
  142. callouts does not affect it.
  143. .P
  144. For example, if the pattern .*\ed is compiled with PCRE2_AUTO_CALLOUT and
  145. applied to the string "aa", the \fBpcre2test\fP output is:
  146. .sp
  147. --->aa
  148. +0 ^ .*
  149. +2 ^ ^ \ed
  150. +2 ^^ \ed
  151. +2 ^ \ed
  152. No match
  153. .sp
  154. This shows that all match attempts start at the beginning of the subject. In
  155. other words, the pattern is anchored. You can disable this optimization by
  156. passing PCRE2_NO_DOTSTAR_ANCHOR to \fBpcre2_compile()\fP, or starting the
  157. pattern with (*NO_DOTSTAR_ANCHOR). In this case, the output changes to:
  158. .sp
  159. --->aa
  160. +0 ^ .*
  161. +2 ^ ^ \ed
  162. +2 ^^ \ed
  163. +2 ^ \ed
  164. +0 ^ .*
  165. +2 ^^ \ed
  166. +2 ^ \ed
  167. No match
  168. .sp
  169. This shows more match attempts, starting at the second subject character.
  170. Another optimization, described in the next section, means that there is no
  171. subsequent attempt to match with an empty subject.
  172. .
  173. .
  174. .SS "Other optimizations"
  175. .rs
  176. .sp
  177. Other optimizations that provide fast "no match" results also affect callouts.
  178. For example, if the pattern is
  179. .sp
  180. ab(?C4)cd
  181. .sp
  182. PCRE2 knows that any matching string must contain the letter "d". If the
  183. subject string is "abyz", the lack of "d" means that matching doesn't ever
  184. start, and the callout is never reached. However, with "abyd", though the
  185. result is still no match, the callout is obeyed.
  186. .P
  187. For most patterns PCRE2 also knows the minimum length of a matching string, and
  188. will immediately give a "no match" return without actually running a match if
  189. the subject is not long enough, or, for unanchored patterns, if it has been
  190. scanned far enough.
  191. .P
  192. You can disable these optimizations by passing the PCRE2_NO_START_OPTIMIZE
  193. option to \fBpcre2_compile()\fP, or by starting the pattern with
  194. (*NO_START_OPT). This slows down the matching process, but does ensure that
  195. callouts such as the example above are obeyed.
  196. .
  197. .
  198. .\" HTML <a name="calloutinterface"></a>
  199. .SH "THE CALLOUT INTERFACE"
  200. .rs
  201. .sp
  202. During matching, when PCRE2 reaches a callout point, if an external function is
  203. provided in the match context, it is called. This applies to both normal,
  204. DFA, and JIT matching. The first argument to the callout function is a pointer
  205. to a \fBpcre2_callout\fP block. The second argument is the void * callout data
  206. that was supplied when the callout was set up by calling
  207. \fBpcre2_set_callout()\fP (see the
  208. .\" HREF
  209. \fBpcre2api\fP
  210. .\"
  211. documentation). The callout block structure contains the following fields, not
  212. necessarily in this order:
  213. .sp
  214. uint32_t \fIversion\fP;
  215. uint32_t \fIcallout_number\fP;
  216. uint32_t \fIcapture_top\fP;
  217. uint32_t \fIcapture_last\fP;
  218. uint32_t \fIcallout_flags\fP;
  219. PCRE2_SIZE *\fIoffset_vector\fP;
  220. PCRE2_SPTR \fImark\fP;
  221. PCRE2_SPTR \fIsubject\fP;
  222. PCRE2_SIZE \fIsubject_length\fP;
  223. PCRE2_SIZE \fIstart_match\fP;
  224. PCRE2_SIZE \fIcurrent_position\fP;
  225. PCRE2_SIZE \fIpattern_position\fP;
  226. PCRE2_SIZE \fInext_item_length\fP;
  227. PCRE2_SIZE \fIcallout_string_offset\fP;
  228. PCRE2_SIZE \fIcallout_string_length\fP;
  229. PCRE2_SPTR \fIcallout_string\fP;
  230. .sp
  231. The \fIversion\fP field contains the version number of the block format. The
  232. current version is 2; the three callout string fields were added for version 1,
  233. and the \fIcallout_flags\fP field for version 2. If you are writing an
  234. application that might use an earlier release of PCRE2, you should check the
  235. version number before accessing any of these fields. The version number will
  236. increase in future if more fields are added, but the intention is never to
  237. remove any of the existing fields.
  238. .
  239. .
  240. .SS "Fields for numerical callouts"
  241. .rs
  242. .sp
  243. For a numerical callout, \fIcallout_string\fP is NULL, and \fIcallout_number\fP
  244. contains the number of the callout, in the range 0-255. This is the number
  245. that follows (?C for callouts that part of the pattern; it is 255 for
  246. automatically generated callouts.
  247. .
  248. .
  249. .SS "Fields for string callouts"
  250. .rs
  251. .sp
  252. For callouts with string arguments, \fIcallout_number\fP is always zero, and
  253. \fIcallout_string\fP points to the string that is contained within the compiled
  254. pattern. Its length is given by \fIcallout_string_length\fP. Duplicated ending
  255. delimiters that were present in the original pattern string have been turned
  256. into single characters, but there is no other processing of the callout string
  257. argument. An additional code unit containing binary zero is present after the
  258. string, but is not included in the length. The delimiter that was used to start
  259. the string is also stored within the pattern, immediately before the string
  260. itself. You can access this delimiter as \fIcallout_string\fP[-1] if you need
  261. it.
  262. .P
  263. The \fIcallout_string_offset\fP field is the code unit offset to the start of
  264. the callout argument string within the original pattern string. This is
  265. provided for the benefit of applications such as script languages that might
  266. need to report errors in the callout string within the pattern.
  267. .
  268. .
  269. .SS "Fields for all callouts"
  270. .rs
  271. .sp
  272. The remaining fields in the callout block are the same for both kinds of
  273. callout.
  274. .P
  275. The \fIoffset_vector\fP field is a pointer to a vector of capturing offsets
  276. (the "ovector"). You may read the elements in this vector, but you must not
  277. change any of them.
  278. .P
  279. For calls to \fBpcre2_match()\fP, the \fIoffset_vector\fP field is not (since
  280. release 10.30) a pointer to the actual ovector that was passed to the matching
  281. function in the match data block. Instead it points to an internal ovector of a
  282. size large enough to hold all possible captured substrings in the pattern. Note
  283. that whenever a recursion or subroutine call within a pattern completes, the
  284. capturing state is reset to what it was before.
  285. .P
  286. The \fIcapture_last\fP field contains the number of the most recently captured
  287. substring, and the \fIcapture_top\fP field contains one more than the number of
  288. the highest numbered captured substring so far. If no substrings have yet been
  289. captured, the value of \fIcapture_last\fP is 0 and the value of
  290. \fIcapture_top\fP is 1. The values of these fields do not always differ by one;
  291. for example, when the callout in the pattern ((a)(b))(?C2) is taken,
  292. \fIcapture_last\fP is 1 but \fIcapture_top\fP is 4.
  293. .P
  294. The contents of ovector[2] to ovector[<capture_top>*2-1] can be inspected in
  295. order to extract substrings that have been matched so far, in the same way as
  296. extracting substrings after a match has completed. The values in ovector[0] and
  297. ovector[1] are always PCRE2_UNSET because the match is by definition not
  298. complete. Substrings that have not been captured but whose numbers are less
  299. than \fIcapture_top\fP also have both of their ovector slots set to
  300. PCRE2_UNSET.
  301. .P
  302. For DFA matching, the \fIoffset_vector\fP field points to the ovector that was
  303. passed to the matching function in the match data block for callouts at the top
  304. level, but to an internal ovector during the processing of pattern recursions,
  305. lookarounds, and atomic groups. However, these ovectors hold no useful
  306. information because \fBpcre2_dfa_match()\fP does not support substring
  307. capturing. The value of \fIcapture_top\fP is always 1 and the value of
  308. \fIcapture_last\fP is always 0 for DFA matching.
  309. .P
  310. The \fIsubject\fP and \fIsubject_length\fP fields contain copies of the values
  311. that were passed to the matching function.
  312. .P
  313. The \fIstart_match\fP field normally contains the offset within the subject at
  314. which the current match attempt started. However, if the escape sequence \eK
  315. has been encountered, this value is changed to reflect the modified starting
  316. point. If the pattern is not anchored, the callout function may be called
  317. several times from the same point in the pattern for different starting points
  318. in the subject.
  319. .P
  320. The \fIcurrent_position\fP field contains the offset within the subject of the
  321. current match pointer.
  322. .P
  323. The \fIpattern_position\fP field contains the offset in the pattern string to
  324. the next item to be matched.
  325. .P
  326. The \fInext_item_length\fP field contains the length of the next item to be
  327. processed in the pattern string. When the callout is at the end of the pattern,
  328. the length is zero. When the callout precedes an opening parenthesis, the
  329. length includes meta characters that follow the parenthesis. For example, in a
  330. callout before an assertion such as (?=ab) the length is 3. For an an
  331. alternation bar or a closing parenthesis, the length is one, unless a closing
  332. parenthesis is followed by a quantifier, in which case its length is included.
  333. (This changed in release 10.23. In earlier releases, before an opening
  334. parenthesis the length was that of the entire group, and before an alternation
  335. bar or a closing parenthesis the length was zero.)
  336. .P
  337. The \fIpattern_position\fP and \fInext_item_length\fP fields are intended to
  338. help in distinguishing between different automatic callouts, which all have the
  339. same callout number. However, they are set for all callouts, and are used by
  340. \fBpcre2test\fP to show the next item to be matched when displaying callout
  341. information.
  342. .P
  343. In callouts from \fBpcre2_match()\fP the \fImark\fP field contains a pointer to
  344. the zero-terminated name of the most recently passed (*MARK), (*PRUNE), or
  345. (*THEN) item in the match, or NULL if no such items have been passed. Instances
  346. of (*PRUNE) or (*THEN) without a name do not obliterate a previous (*MARK). In
  347. callouts from the DFA matching function this field always contains NULL.
  348. .P
  349. The \fIcallout_flags\fP field is always zero in callouts from
  350. \fBpcre2_dfa_match()\fP or when JIT is being used. When \fBpcre2_match()\fP
  351. without JIT is used, the following bits may be set:
  352. .sp
  353. PCRE2_CALLOUT_STARTMATCH
  354. .sp
  355. This is set for the first callout after the start of matching for each new
  356. starting position in the subject.
  357. .sp
  358. PCRE2_CALLOUT_BACKTRACK
  359. .sp
  360. This is set if there has been a matching backtrack since the previous callout,
  361. or since the start of matching if this is the first callout from a
  362. \fBpcre2_match()\fP run.
  363. .P
  364. Both bits are set when a backtrack has caused a "bumpalong" to a new starting
  365. position in the subject. Output from \fBpcre2test\fP does not indicate the
  366. presence of these bits unless the \fBcallout_extra\fP modifier is set.
  367. .P
  368. The information in the \fBcallout_flags\fP field is provided so that
  369. applications can track and tell their users how matching with backtracking is
  370. done. This can be useful when trying to optimize patterns, or just to
  371. understand how PCRE2 works. There is no support in \fBpcre2_dfa_match()\fP
  372. because there is no backtracking in DFA matching, and there is no support in
  373. JIT because JIT is all about maximimizing matching performance. In both these
  374. cases the \fBcallout_flags\fP field is always zero.
  375. .
  376. .
  377. .SH "RETURN VALUES FROM CALLOUTS"
  378. .rs
  379. .sp
  380. The external callout function returns an integer to PCRE2. If the value is
  381. zero, matching proceeds as normal. If the value is greater than zero, matching
  382. fails at the current point, but the testing of other matching possibilities
  383. goes ahead, just as if a lookahead assertion had failed. If the value is less
  384. than zero, the match is abandoned, and the matching function returns the
  385. negative value.
  386. .P
  387. Negative values should normally be chosen from the set of PCRE2_ERROR_xxx
  388. values. In particular, PCRE2_ERROR_NOMATCH forces a standard "no match"
  389. failure. The error number PCRE2_ERROR_CALLOUT is reserved for use by callout
  390. functions; it will never be used by PCRE2 itself.
  391. .
  392. .
  393. .SH "CALLOUT ENUMERATION"
  394. .rs
  395. .sp
  396. .nf
  397. .B int pcre2_callout_enumerate(const pcre2_code *\fIcode\fP,
  398. .B " int (*\fIcallback\fP)(pcre2_callout_enumerate_block *, void *),"
  399. .B " void *\fIuser_data\fP);"
  400. .fi
  401. .sp
  402. A script language that supports the use of string arguments in callouts might
  403. like to scan all the callouts in a pattern before running the match. This can
  404. be done by calling \fBpcre2_callout_enumerate()\fP. The first argument is a
  405. pointer to a compiled pattern, the second points to a callback function, and
  406. the third is arbitrary user data. The callback function is called for every
  407. callout in the pattern in the order in which they appear. Its first argument is
  408. a pointer to a callout enumeration block, and its second argument is the
  409. \fIuser_data\fP value that was passed to \fBpcre2_callout_enumerate()\fP. The
  410. data block contains the following fields:
  411. .sp
  412. \fIversion\fP Block version number
  413. \fIpattern_position\fP Offset to next item in pattern
  414. \fInext_item_length\fP Length of next item in pattern
  415. \fIcallout_number\fP Number for numbered callouts
  416. \fIcallout_string_offset\fP Offset to string within pattern
  417. \fIcallout_string_length\fP Length of callout string
  418. \fIcallout_string\fP Points to callout string or is NULL
  419. .sp
  420. The version number is currently 0. It will increase if new fields are ever
  421. added to the block. The remaining fields are the same as their namesakes in the
  422. \fBpcre2_callout\fP block that is used for callouts during matching, as
  423. described
  424. .\" HTML <a href="#calloutinterface">
  425. .\" </a>
  426. above.
  427. .\"
  428. .P
  429. Note that the value of \fIpattern_position\fP is unique for each callout.
  430. However, if a callout occurs inside a group that is quantified with a non-zero
  431. minimum or a fixed maximum, the group is replicated inside the compiled
  432. pattern. For example, a pattern such as /(a){2}/ is compiled as if it were
  433. /(a)(a)/. This means that the callout will be enumerated more than once, but
  434. with the same value for \fIpattern_position\fP in each case.
  435. .P
  436. The callback function should normally return zero. If it returns a non-zero
  437. value, scanning the pattern stops, and that value is returned from
  438. \fBpcre2_callout_enumerate()\fP.
  439. .
  440. .
  441. .SH AUTHOR
  442. .rs
  443. .sp
  444. .nf
  445. Philip Hazel
  446. University Computing Service
  447. Cambridge, England.
  448. .fi
  449. .
  450. .
  451. .SH REVISION
  452. .rs
  453. .sp
  454. .nf
  455. Last updated: 03 February 2019
  456. Copyright (c) 1997-2019 University of Cambridge.
  457. .fi