//proc/self/root/usr/share/perl5/pod/=head1 NAME
perlreapi - perl regular expression plugin interface
=head1 DESCRIPTION
As of Perl 5.9.5 there is a new interface for plugging and using other
regular expression engines than the default one.
Each engine is supposed to provide access to a constant structure of the
following format:
typedef struct regexp_engine {
REGEXP* (*comp) (pTHX_ const SV * const pattern, const U32 flags);
I32 (*exec) (pTHX_ REGEXP * const rx, char* stringarg, char* strend,
char* strbeg, I32 minend, SV* screamer,
void* data, U32 flags);
char* (*intuit) (pTHX_ REGEXP * const rx, SV *sv, char *strpos,
char *strend, U32 flags,
struct re_scream_pos_data_s *data);
SV* (*checkstr) (pTHX_ REGEXP * const rx);
void (*free) (pTHX_ REGEXP * const rx);
void (*numbered_buff_FETCH) (pTHX_ REGEXP * const rx, const I32 paren,
SV * const sv);
void (*numbered_buff_STORE) (pTHX_ REGEXP * const rx, const I32 paren,
SV const * const value);
I32 (*numbered_buff_LENGTH) (pTHX_ REGEXP * const rx, const SV * const sv,
const I32 paren);
SV* (*named_buff) (pTHX_ REGEXP * const rx, SV * const key,
SV * const value, U32 flags);
SV* (*named_buff_iter) (pTHX_ REGEXP * const rx, const SV * const lastkey,
const U32 flags);
SV* (*qr_package)(pTHX_ REGEXP * const rx);
#ifdef USE_ITHREADS
void* (*dupe) (pTHX_ REGEXP * const rx, CLONE_PARAMS *param);
#endif
When a regexp is compiled, its C field is then set to point at
the appropriate structure, so that when it needs to be used Perl can find
the right routines to do so.
In order to install a new regexp handler, C<$^H{regcomp}> is set
to an integer which (when casted appropriately) resolves to one of these
structures. When compiling, the C method is executed, and the
resulting regexp structure's engine field is expected to point back at
the same structure.
The pTHX_ symbol in the definition is a macro used by perl under threading
to provide an extra argument to the routine holding a pointer back to
the interpreter that is executing the regexp. So under threading all
routines get an extra argument.
=head1 Callbacks
=head2 comp
REGEXP* comp(pTHX_ const SV * const pattern, const U32 flags);
Compile the pattern stored in C using the given C and
return a pointer to a prepared C structure that can perform
the match. See L below for an explanation of
the individual fields in the REGEXP struct.
The C parameter is the scalar that was used as the
pattern. previous versions of perl would pass two C indicating
the start and end of the stringified pattern, the following snippet can
be used to get the old parameters:
STRLEN plen;
char* exp = SvPV(pattern, plen);
char* xend = exp + plen;
Since any scalar can be passed as a pattern it's possible to implement
an engine that does something with an array (C<< "ook" =~ [ qw/ eek
hlagh / ] >>) or with the non-stringified form of a compiled regular
expression (C<< "ook" =~ qr/eek/ >>). perl's own engine will always
stringify everything using the snippet above but that doesn't mean
other engines have to.
The C parameter is a bitfield which indicates which of the
C flags the regex was compiled with. It also contains
additional info such as whether C routine.
The REGEXP structure contains all the data that perl needs to be aware of
to properly work with the regular expression. It includes data about
optimisations that perl can use to determine if the regex engine should
really be used, and various other control info that is needed to properly
execute patterns in various contexts such as is the pattern anchored in
some way, or what flags were used during the compile, or whether the
program contains special constructs that perl needs to be aware of.
In addition it contains two fields that are intended for the private
use of the regex engine that compiled the pattern. These are the
C and C members. C is a void pointer to
an arbitrary structure whose use and management is the responsibility
of the compiling engine. perl will never modify either of these
values.
typedef struct regexp {
/* what engine created this regexp? */
const struct regexp_engine* engine;
/* what re is this a lightweight copy of? */
struct regexp* mother_re;
/* Information about the match that the perl core uses to manage things */
U32 extflags; /* Flags used both externally and internally */
I32 minlen; /* mininum possible length of string to match */
I32 minlenret; /* mininum possible length of $& */
U32 gofs; /* chars left of pos that we search from */
/* substring data about strings that must appear
in the final match, used for optimisations */
struct reg_substr_data *substrs;
U32 nparens; /* number of capture groups */
/* private engine specific data */
U32 intflags; /* Engine Specific Internal flags */
void *pprivate; /* Data private to the regex engine which
created this object. */
/* Data about the last/current match. These are modified during matching*/
U32 lastparen; /* last open paren matched */
U32 lastcloseparen; /* last close paren matched */
regexp_paren_pair *swap; /* Swap copy of *offs */
regexp_paren_pair *offs; /* Array of offsets for (@-) and (@+) */
char *subbeg; /* saved or original string so \digit works forever. */
SV_SAVED_COPY /* If non-NULL, SV which is COW from original */
I32 sublen; /* Length of string pointed by subbeg */
/* Information about the match that isn't often used */
I32 prelen; /* length of precomp */
const char *precomp; /* pre-compilation regular expression */
char *wrapped; /* wrapped version of the pattern */
I32 wraplen; /* length of wrapped */
I32 seen_evals; /* number of eval groups in the pattern - for security checks */
HV *paren_names; /* Optional hash of paren names */
/* Refcount of this regexp */
I32 refcnt; /* Refcount of this regexp */
} regexp;
The fields are discussed in more detail below:
=head2 C
This field points at a regexp_engine structure which contains pointers
to the subroutines that are to be used for performing a match. It
is the compiling routine's responsibility to populate this field before
returning the regexp object.
Internally this is set to C unless a custom engine is specified in
C<$^H{regcomp}>, perl's own set of callbacks can be accessed in the struct
pointed to by C.
=head2 C
TODO, see L
=head2 C
This will be used by perl to see what flags the regexp was compiled
with, this will normally be set to the value of the flags parameter by
the L callback. See the L documentation for
valid flags.
=head2 C C
The minimum string length required for the pattern to match. This is used to
prune the search space by not bothering to match any closer to the end of a
string than would allow a match. For instance there is no point in even
starting the regex engine if the minlen is 10 but the string is only 5
characters long. There is no way that the pattern can match.
C is the minimum length of the string that would be found
in $& after a match.
The difference between C and C can be seen in the
following pattern:
/ns(?=\d)/
where the C would be 3 but C would only be 2 as the \d is
required to match but is not actually included in the matched content. This
distinction is particularly important as the substitution logic uses the
C to tell whether it can do in-place substitution which can result in
considerable speedup.
=head2 C
Left offset from pos() to start match at.
=head2 C
Substring data about strings that must appear in the final match. This
is currently only used internally by perl's engine for but might be
used in the future for all engines for optimisations.
=head2 C, C, and C
These fields are used to keep track of how many paren groups could be matched
in the pattern, which was the last open paren to be entered, and which was
the last close paren to be entered.
=head2 C
The engine's private copy of the flags the pattern was compiled with. Usually
this is the same as C unless the engine chose to modify one of them.
=head2 C
A void* pointing to an engine-defined data structure. The perl engine uses the
C structure (see L) but a custom
engine should use something else.
=head2 C
Unused. Left in for compatibility with perl 5.10.0.
=head2 C
A C structure which defines offsets into the string being
matched which correspond to the C<$&> and C<$1>, C<$2> etc. captures, the
C struct is defined as follows:
typedef struct regexp_paren_pair {
I32 start;
I32 end;
} regexp_paren_pair;
If C<< ->offs[num].start >> or C<< ->offs[num].end >> is C<-1> then that
capture group did not match. C<< ->offs[0].start/end >> represents C<$&> (or
C<${^MATCH> under C/p>) and C<< ->offs[paren].end >> matches C<$$paren> where
C<$paren >= 1>.
=head2 C C
Used for optimisations. C holds a copy of the pattern that
was compiled and C its length. When a new pattern is to be
compiled (such as inside a loop) the internal C operator
checks whether the last compiled C's C and C
are equivalent to the new one, and if so uses the old pattern instead
of compiling a new one.
The relevant snippet from C:
if (!re || !re->precomp || re->prelen != (I32)len ||
memNE(re->precomp, t, len))
/* Compile a new pattern */
=head2 C
This is a hash used internally to track named capture groups and their
offsets. The keys are the names of the buffers the values are dualvars,
with the IV slot holding the number of buffers with the given name and the
pv being an embedded array of I32. The values may also be contained
independently in the data array in cases where named backreferences are
used.
=head2 C
Holds information on the longest string that must occur at a fixed
offset from the start of the pattern, and the longest string that must
occur at a floating offset from the start of the pattern. Used to do
Fast-Boyer-Moore searches on the string to find out if its worth using
the regex engine at all, and if so where in the string to search.
=head2 C C C
Used during execution phase for managing search and replace patterns.
=head2 C C
Stores the string C stringifies to. The perl engine for example
stores C<(?^:eek)> in the case of C.
When using a custom engine that doesn't support the C<(?:)> construct
for inline modifiers, it's probably best to have C stringify to
the supplied pattern, note that this will create undesired patterns in
cases such as:
my $x = qr/a|b/; # "a|b"
my $y = qr/c/i; # "c"
my $z = qr/$x$y/; # "a|bc"
There's no solution for this problem other than making the custom
engine understand a construct like C<(?:)>.
=head2 C
This stores the number of eval groups in the pattern. This is used for security
purposes when embedding compiled regexes into larger patterns with C.
=head2 C
The number of times the structure is referenced. When this falls to 0 the
regexp is automatically freed by a call to pregfree. This should be set to 1 in
each engine's L routine.
=head1 HISTORY
Originally part of L.
=head1 AUTHORS
Originally written by Yves Orton, expanded by Evar ArnfjErE
Bjarmason.
=head1 LICENSE
Copyright 2006 Yves Orton and 2007 Evar ArnfjErE Bjarmason.
This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut