| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Devel::CallParser - custom parsing attached to subroutines | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | # to generate header prior to XS compilation | ||
| 8 | |||
| 9 | perl -MDevel::CallParser=callparser0_h \ | ||
| 10 | -e 'print callparser0_h' > callparser0.h | ||
| 11 | perl -MDevel::CallParser=callparser1_h \ | ||
| 12 | -e 'print callparser1_h' > callparser1.h | ||
| 13 | |||
| 14 | # in Perl part of module | ||
| 15 | |||
| 16 | use Devel::CallParser; | ||
| 17 | |||
| 18 | /* in XS */ | ||
| 19 | |||
| 20 | #include "callparser0.h" | ||
| 21 | |||
| 22 | cv_get_call_parser(cv, &psfun, &psobj); | ||
| 23 | static OP *my_psfun(pTHX_ GV *namegv, SV *psobj, U32 *flagsp); | ||
| 24 | cv_set_call_parser(cv, my_psfun, psobj); | ||
| 25 | |||
| 26 | #include "callparser1.h" | ||
| 27 | |||
| 28 | cv_get_call_parser(cv, &psfun, &psobj); | ||
| 29 | static OP *my_psfun(pTHX_ GV *namegv, SV *psobj, U32 *flagsp); | ||
| 30 | cv_set_call_parser(cv, my_psfun, psobj); | ||
| 31 | |||
| 32 | args = parse_args_parenthesised(&flags); | ||
| 33 | args = parse_args_nullary(&flags); | ||
| 34 | args = parse_args_unary(&flags); | ||
| 35 | args = parse_args_list(&flags); | ||
| 36 | args = parse_args_block_list(&flags); | ||
| 37 | args = parse_args_proto(namegv, protosv, &flags); | ||
| 38 | args = parse_args_proto_or_list(namegv, protosv, &flags); | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | This module provides a C API, for XS modules, concerned with custom | ||
| 43 | parsing. It is centred around the function C<cv_set_call_parser>, which | ||
| 44 | allows XS code to attach a magical annotation to a Perl subroutine, | ||
| 45 | resulting in resolvable calls to that subroutine having their arguments | ||
| 46 | parsed by arbitrary C code. (This is a more conveniently structured | ||
| 47 | facility than the core's C<PL_keyword_plugin> API.) This module makes | ||
| 48 | C<cv_set_call_parser> and several supporting functions available. | ||
| 49 | |||
| 50 | This module provides the implementation of the functions at runtime. | ||
| 51 | It also, at compile time, supplies the C header file and link | ||
| 52 | library which provide access to the functions. In normal use, | ||
| 53 | L</callparser0_h>/L</callparser1_h> and L</callparser_linkable> should | ||
| 54 | be called at build time (not authoring time) for the module that wishes | ||
| 55 | to use the C functions. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | package Devel::CallParser; | ||
| 60 | |||
| 61 | 11 | ▶ | { use 5.011002; } |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 62 | 11 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 63 | 11 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 64 | |||
| 65 | 11 | ▶ | use Devel::CallChecker 0.001 (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 66 | |||
| 67 | 11 | our $VERSION = "0.006"; | |
| 68 | |||
| 69 | 11 | ▶ | use parent "Exporter"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 70 | 11 | our @EXPORT_OK = qw(callparser0_h callparser1_h callparser_linkable); | |
| 71 | |||
| 72 | { | ||
| 73 | 11 | require DynaLoader; | |
| 74 | 11 | local our @ISA = qw(DynaLoader); | |
| 75 | 11 | ▶ | local *dl_load_flags = sub { 1 }; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 76 | 11 | __PACKAGE__->bootstrap($VERSION); | |
| 77 | } | ||
| 78 | |||
| 79 | =head1 CONSTANTS | ||
| 80 | |||
| 81 | =over | ||
| 82 | |||
| 83 | =item callparser0_h | ||
| 84 | |||
| 85 | Content of a C header file, intended to be named "C<callparser0.h>". | ||
| 86 | It is to be included in XS code, and C<perl.h> must be included first. | ||
| 87 | When the XS module is loaded at runtime, the C<Devel::CallParser> | ||
| 88 | module must be loaded first. This will result in a limited form of | ||
| 89 | the C functions C<cv_get_call_parser> and C<cv_set_call_parser> being | ||
| 90 | available to the XS code. | ||
| 91 | |||
| 92 | The C<cv_get_call_parser> and C<cv_set_call_parser> functions supplied | ||
| 93 | by this header are mostly as described below. However, for subroutines | ||
| 94 | that have default argument parsing behaviour, C<cv_get_call_parser> | ||
| 95 | will return null pointers for the parsing function and its SV argument, | ||
| 96 | rather than pointing to a real function that implements default parsing. | ||
| 97 | Correspondingly, C<cv_set_call_parser> will accept such a pair of | ||
| 98 | null pointers to restore default argument parsing for a subroutine. | ||
| 99 | The advantage of these modified semantics is that this much of the | ||
| 100 | functionality is available on Perl versions where it is not possible | ||
| 101 | to implement standard argument parsing as a distinct function. This is | ||
| 102 | the case on all Perl versions prior to 5.13.8. | ||
| 103 | |||
| 104 | This header is only available on Perl versions 5.11.2 and higher. | ||
| 105 | |||
| 106 | =item callparser1_h | ||
| 107 | |||
| 108 | Content of a C header file, intended to be named "C<callparser1.h>". | ||
| 109 | It is to be included in XS code, and C<perl.h> must be | ||
| 110 | included first. When the XS module is loaded at runtime, the | ||
| 111 | C<Devel::CallParser> module must be loaded first. This will result | ||
| 112 | in the C functions C<cv_get_call_parser>, C<cv_set_call_parser>, | ||
| 113 | C<parse_args_parenthesised>, C<parse_args_nullary>, C<parse_args_unary>, | ||
| 114 | C<parse_args_list>, C<parse_args_block_list>, C<parse_args_proto>, and | ||
| 115 | C<parse_args_proto_or_list>, as defined below, being available to the | ||
| 116 | XS code. | ||
| 117 | |||
| 118 | This header is only available on Perl versions 5.13.8 and higher. | ||
| 119 | |||
| 120 | =item callparser_linkable | ||
| 121 | |||
| 122 | List of names of files that must be used as additional objects when | ||
| 123 | linking an XS module that uses the C functions supplied by this module. | ||
| 124 | This list will be empty on many platforms. | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | 6 | ▶ | sub callparser_linkable() { |
|
Subroutine
callparser_linkable: called CC=1 SCAR=0.0
Pod
documented
| |||
| 129 | 6 | require DynaLoader::Functions; | |
| 130 | 6 | DynaLoader::Functions->VERSION(0.001); | |
| 131 | 6 | return DynaLoader::Functions::linkable_for_module(__PACKAGE__); | |
| 132 | } | ||
| 133 | |||
| 134 | =back | ||
| 135 | |||
| 136 | =head1 C FUNCTIONS | ||
| 137 | |||
| 138 | =over | ||
| 139 | |||
| 140 | =item cv_get_call_parser | ||
| 141 | |||
| 142 | Retrieves the function that will be used to parse the arguments for a | ||
| 143 | call to I<cv>. Specifically, the function is used for a subroutine call, | ||
| 144 | not marked with C<&>, where the callee can be identified at compile time | ||
| 145 | as I<cv>. | ||
| 146 | |||
| 147 | The C-level function pointer is returned in I<*psfun_p>, and an SV | ||
| 148 | argument for it is returned in I<*psobj_p>. The function is intended | ||
| 149 | to be called in this manner: | ||
| 150 | |||
| 151 | argsop = (*psfun_p)(aTHX_ namegv, (*psobj_p), &flags); | ||
| 152 | |||
| 153 | This call is to be made when the parser has just scanned and accepted | ||
| 154 | a bareword and determined that it begins the syntax of a call to I<cv>. | ||
| 155 | I<namegv> is a GV supplying the name that should be used by the parsing | ||
| 156 | function to refer to the callee if it needs to emit any diagnostics, | ||
| 157 | and I<flags> is a C<U32> that the parsing function can write to as an | ||
| 158 | additional output. It is permitted to apply the parsing function in | ||
| 159 | non-standard situations, such as to a call to a different subroutine. | ||
| 160 | |||
| 161 | The parsing function's main output is an op tree describing a list of | ||
| 162 | argument expressions. This may be null for an empty list. The argument | ||
| 163 | expressions will be combined with the expression that identified I<cv> and | ||
| 164 | used to build an C<entersub> op describing a complete subroutine call. | ||
| 165 | The parsing function may also set flag bits in I<flags> for special | ||
| 166 | effects. The bit C<CALLPARSER_PARENS> indicates that the argument | ||
| 167 | list was fully parenthesised, which makes a difference only in obscure | ||
| 168 | situations. The bit C<CALLPARSER_STATEMENT> indicates that what was | ||
| 169 | parsed was syntactically not an expression but a statement. | ||
| 170 | |||
| 171 | By default, the parsing function is | ||
| 172 | L<Perl_parse_args_proto_or_list|/parse_args_proto_or_list>, and the | ||
| 173 | SV parameter is I<cv> itself. This implements standard subroutine | ||
| 174 | argument parsing. It can be changed, for a particular subroutine, | ||
| 175 | by L</cv_set_call_parser>. | ||
| 176 | |||
| 177 | void cv_get_call_parser(CV *cv, Perl_call_parser *psfun_p, | ||
| 178 | SV **psobj_p) | ||
| 179 | |||
| 180 | =item cv_set_call_parser | ||
| 181 | |||
| 182 | Sets the function that will be used to parse the arguments for a call | ||
| 183 | to I<cv>. Specifically, the function is used for a subroutine call, | ||
| 184 | not marked with C<&>, where the callee can be identified at compile time | ||
| 185 | as I<cv>. | ||
| 186 | |||
| 187 | The C-level function pointer is supplied in I<psfun>, and an SV argument | ||
| 188 | for it is supplied in I<psobj>. The function is intended to be called | ||
| 189 | in this manner: | ||
| 190 | |||
| 191 | argsop = (*psfun_p)(aTHX_ namegv, (*psobj_p), &flags); | ||
| 192 | |||
| 193 | This call is to be made when the parser has just scanned and accepted | ||
| 194 | a bareword and determined that it begins the syntax of a call to I<cv>. | ||
| 195 | I<namegv> is a GV supplying the name that should be used by the parsing | ||
| 196 | function to refer to the callee if it needs to emit any diagnostics, | ||
| 197 | and I<flags> is a C<U32> that the parsing function can write to as an | ||
| 198 | additional output. It is permitted to apply the parsing function in | ||
| 199 | non-standard situations, such as to a call to a different subroutine. | ||
| 200 | |||
| 201 | The parsing function's main output is an op tree describing a list of | ||
| 202 | argument expressions. This may be null for an empty list. The argument | ||
| 203 | expressions will be combined with the expression that identified I<cv> and | ||
| 204 | used to build an C<entersub> op describing a complete subroutine call. | ||
| 205 | The parsing function may also set flag bits in I<flags> for special | ||
| 206 | effects. The bit C<CALLPARSER_PARENS> indicates that the argument | ||
| 207 | list was fully parenthesised, which makes a difference only in obscure | ||
| 208 | situations. The bit C<CALLPARSER_STATEMENT> indicates that what was | ||
| 209 | parsed was syntactically not an expression but a statement. | ||
| 210 | |||
| 211 | The current setting for a particular CV can be retrieved by | ||
| 212 | L</cv_get_call_parser>. | ||
| 213 | |||
| 214 | void cv_set_call_parser(CV *cv, Perl_call_parser psfun, | ||
| 215 | SV *psobj) | ||
| 216 | |||
| 217 | =item parse_args_parenthesised | ||
| 218 | |||
| 219 | Parse a parenthesised argument list for a subroutine call. The argument | ||
| 220 | list consists of an optional expression enclosed in parentheses. | ||
| 221 | This is the syntax that is used for any subroutine call where the first | ||
| 222 | thing following the subroutine name is an open parenthesis. It is used | ||
| 223 | regardless of the subroutine's prototype. | ||
| 224 | |||
| 225 | The op tree representing the argument list is returned. The bit | ||
| 226 | C<CALLPARSER_PARENS> is set in I<*flags_p>, to indicate that the argument | ||
| 227 | list was fully parenthesised. | ||
| 228 | |||
| 229 | OP *parse_args_parenthesised(U32 *flags_p) | ||
| 230 | |||
| 231 | =item parse_args_nullary | ||
| 232 | |||
| 233 | Parse an argument list for a call to a subroutine that is syntactically | ||
| 234 | a nullary function. The argument list is either parenthesised or | ||
| 235 | completely absent. This is the syntax that is used for a call to a | ||
| 236 | subroutine with a C<()> prototype. | ||
| 237 | |||
| 238 | The op tree representing the argument list is returned. The bit | ||
| 239 | C<CALLPARSER_PARENS> is set in I<*flags_p> if the argument list was | ||
| 240 | parenthesised. | ||
| 241 | |||
| 242 | OP *parse_args_nullary(U32 *flags_p) | ||
| 243 | |||
| 244 | =item parse_args_unary | ||
| 245 | |||
| 246 | Parse an argument list for a call to a subroutine that is syntactically | ||
| 247 | a unary function. The argument list is either parenthesised, absent, | ||
| 248 | or consists of an unparenthesised arithmetic expression. This is the | ||
| 249 | syntax that is used for a call to a subroutine with prototype C<($)>, | ||
| 250 | C<(;$)>, or certain similar prototypes. | ||
| 251 | |||
| 252 | The op tree representing the argument list is returned. The bit | ||
| 253 | C<CALLPARSER_PARENS> is set in I<*flags_p> if the argument list was | ||
| 254 | parenthesised. | ||
| 255 | |||
| 256 | OP *parse_args_unary(U32 *flags_p) | ||
| 257 | |||
| 258 | =item parse_args_list | ||
| 259 | |||
| 260 | Parse an argument list for a call to a subroutine that is syntactically | ||
| 261 | a list function. The argument list is either parenthesised, absent, or | ||
| 262 | consists of an unparenthesised list expression. This is the syntax that | ||
| 263 | is used for a call to a subroutine with any prototype that does not have | ||
| 264 | special handling (such as C<(@)> or C<($$)>) or with no prototype at all. | ||
| 265 | |||
| 266 | The op tree representing the argument list is returned. The bit | ||
| 267 | C<CALLPARSER_PARENS> is set in I<*flags_p> if the argument list was | ||
| 268 | parenthesised. | ||
| 269 | |||
| 270 | OP *parse_args_list(U32 *flags_p) | ||
| 271 | |||
| 272 | =item parse_args_block_list | ||
| 273 | |||
| 274 | Parse an argument list for a call to a subroutine that is syntactically | ||
| 275 | a block-and-list function. The argument list is either parenthesised, | ||
| 276 | absent, an unparenthesised list expression, or consists of a code block | ||
| 277 | followed by an optionl list expression. Where the first thing seen | ||
| 278 | is an open brace, it is always interpreted as a code block. This is | ||
| 279 | the syntax that is used for a call to a subroutine with any prototype | ||
| 280 | beginning with C<&>, such as C<(&@)> or C<(&$)>. | ||
| 281 | |||
| 282 | The op tree representing the argument list is returned. The bit | ||
| 283 | C<CALLPARSER_PARENS> is set in I<*flags_p> if the argument list was | ||
| 284 | parenthesised. | ||
| 285 | |||
| 286 | OP *parse_args_block_list(U32 *flags_p) | ||
| 287 | |||
| 288 | =item parse_args_proto | ||
| 289 | |||
| 290 | Parse a subroutine argument list based on a subroutine prototype. | ||
| 291 | The syntax used for the argument list will be that implemented by | ||
| 292 | L</parse_args_nullary>, L</parse_args_unary>, L</parse_args_list>, or | ||
| 293 | L</parse_args_block_list>, depending on the prototype. This is the | ||
| 294 | standard treatment used on a subroutine call, not marked with C<&>, | ||
| 295 | where the callee can be identified at compile time and has a prototype. | ||
| 296 | |||
| 297 | I<protosv> supplies the subroutine prototype to be applied to the call. | ||
| 298 | It may be a normal defined scalar, of which the string value will be used. | ||
| 299 | Alternatively, for convenience, it may be a subroutine object (a C<CV*> | ||
| 300 | that has been cast to C<SV*>) which has a prototype. | ||
| 301 | |||
| 302 | The I<namegv> parameter would be used to refer to the callee if required | ||
| 303 | in any error message, but currently no message does so. | ||
| 304 | |||
| 305 | The op tree representing the argument list is returned. The bit | ||
| 306 | C<CALLPARSER_PARENS> is set in I<*flags_p> if the argument list was | ||
| 307 | parenthesised. | ||
| 308 | |||
| 309 | OP *parse_args_proto(GV *namegv, SV *protosv, U32 *flags_p) | ||
| 310 | |||
| 311 | =item parse_args_proto_or_list | ||
| 312 | |||
| 313 | Parse a subroutine argument list either based on a subroutine prototype or | ||
| 314 | using default list-function syntax. The syntax used for the argument list | ||
| 315 | will be that implemented by L</parse_args_nullary>, L</parse_args_unary>, | ||
| 316 | L</parse_args_list>, or L</parse_args_block_list>, depending on the | ||
| 317 | prototype. This is the standard treatment used on a subroutine call, | ||
| 318 | not marked with C<&>, where the callee can be identified at compile time. | ||
| 319 | |||
| 320 | I<protosv> supplies the subroutine prototype to be applied to the call, or | ||
| 321 | indicates that there is no prototype. It may be a normal scalar, in which | ||
| 322 | case if it is defined then the string value will be used as a prototype, | ||
| 323 | and if it is undefined then there is no prototype. Alternatively, for | ||
| 324 | convenience, it may be a subroutine object (a C<CV*> that has been cast | ||
| 325 | to C<SV*>), of which the prototype will be used if it has one. | ||
| 326 | |||
| 327 | The I<namegv> parameter would be used to refer to the callee if required | ||
| 328 | in any error message, but currently no message does so. | ||
| 329 | |||
| 330 | The op tree representing the argument list is returned. The bit | ||
| 331 | C<CALLPARSER_PARENS> is set in I<*flags_p> if the argument list was | ||
| 332 | parenthesised. | ||
| 333 | |||
| 334 | OP *parse_args_proto_or_list(GV *namegv, SV *protosv, | ||
| 335 | U32 *flags_p) | ||
| 336 | |||
| 337 | =back | ||
| 338 | |||
| 339 | =head1 BUGS | ||
| 340 | |||
| 341 | Due to reliance on Perl core features to do anything interesting, only | ||
| 342 | a very limited form of custom parsing is possible prior to Perl 5.13.8, | ||
| 343 | and none at all prior to Perl 5.11.2. | ||
| 344 | |||
| 345 | The way this module determines which parsing code to use for a subroutine | ||
| 346 | conflicts with the expectations of some particularly tricky modules that | ||
| 347 | use nasty hacks to perform custom parsing without proper support from the | ||
| 348 | Perl core. In particular, this module is incompatible with versions of | ||
| 349 | L<Devel::Declare> prior to 0.006004 and versions of L<Data::Alias> prior | ||
| 350 | to 1.13. An arrangement has been reached that allows later versions of | ||
| 351 | those modules to coexist with this module. | ||
| 352 | |||
| 353 | Custom parsing code is only invoked if the subroutine to which it is | ||
| 354 | attached is invoked using an unqualified name. For example, the name | ||
| 355 | C<foo> works, but the name C<main::foo> will not, despite referring | ||
| 356 | to the same subroutine. This is an unavoidable limitation imposed by | ||
| 357 | the core's interim facility for custom parser plugins. This should | ||
| 358 | be resolved if the API provided by this module, or something similar, | ||
| 359 | migrates into the core in a future version of Perl. | ||
| 360 | |||
| 361 | =head1 SEE ALSO | ||
| 362 | |||
| 363 | L<Devel::CallChecker> | ||
| 364 | |||
| 365 | =head1 AUTHOR | ||
| 366 | |||
| 367 | Andrew Main (Zefram) <zefram@fysh.org> | ||
| 368 | |||
| 369 | =head1 COPYRIGHT | ||
| 370 | |||
| 371 | Copyright (C) 2011, 2013 Andrew Main (Zefram) <zefram@fysh.org> | ||
| 372 | |||
| 373 | =head1 LICENSE | ||
| 374 | |||
| 375 | This module is free software; you can redistribute it and/or modify it | ||
| 376 | under the same terms as Perl itself. | ||
| 377 | |||
| 378 | =cut | ||
| 379 | |||
| 380 | 11 | 1; | |