File Coverage

blib/lib/XS/Parse/Keyword/FromPerl.pm
Criterion Covered Total %
statement 23 25 92.0
branch n/a
condition n/a
subroutine 10 11 90.9
pod n/a
total 33 36 91.6


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2023 -- leonerd@leonerd.org.uk
5              
6             package XS::Parse::Keyword::FromPerl 0.07;
7              
8 9     9   1886765 use v5.26; # XS code needs op_class() and the OPclass_* constants
  9         105  
9 9     9   46 use warnings;
  9         20  
  9         486  
10              
11             require XSLoader;
12             XSLoader::load( __PACKAGE__, our $VERSION );
13              
14             =head1 NAME
15              
16             C - drive C directly from Perl
17              
18             =head1 DESCRIPTION
19              
20             This module provides a Perl-visible API wrapping (some of) the functionality
21             provided by L, allowing extension keywords to be added to
22             the Perl language by writing code in Perl itself.
23              
24             It provides a thin wrapping layer over the XS functions provided by XPK
25             itself. No real attempt is made here to provide further abstractions on top of
26             the API already provided by Perl and XPK, so users will have to be familiar
27             with the overall concepts there as well.
28              
29             This module is currently experimental, on top of the already-experimental
30             nature of C itself.
31              
32             =cut
33              
34 9     9   54 use Exporter 'import';
  9         23  
  9         1901  
35             push our @EXPORT_OK, qw(
36             register_xs_parse_keyword
37             );
38              
39             # Most of the optree stuff is imported from Optree::Generate
40             require Optree::Generate;
41             foreach my $sym (qw(
42             opcode
43             op_contextualize
44             op_scope
45             newOP
46             newASSIGNOP
47             newBINOP
48             newCONDOP
49             newFOROP
50             newGVOP
51             newLISTOP
52             newLOGOP
53             newPADxVOP
54             newSVOP
55             newUNOP
56             G_VOID G_SCALAR G_LIST
57             OPf_WANT OPf_WANT_VOID OPf_WANT_SCALAR OPf_WANT_LIST
58             OPf_KIDS
59             OPf_PARENS
60             OPf_REF
61             OPf_MOD
62             OPf_STACKED
63             OPf_SPECIAL
64             )) {
65             Optree::Generate->import( $sym );
66             push @EXPORT_OK, $sym;
67             }
68              
69             =head1 OPTREE FUNCTIONS
70              
71             The L module contains a selection of helper functions to
72             allow Perl code to get access to various parts of the C-level API that would
73             be useful when building optrees for keywords. They used to be part of this
74             module, and so are currently re-exported for convenience, but a later version
75             of this module may start emitting warnings when they are used this way, and
76             eventually that may stop being provided at all.
77              
78             Code needing these should import them directly:
79              
80             use Optree::Generate qw( opcode newUNOP ... );
81              
82             =head1 XPK FUNCTIONS
83              
84             =head2 register_xs_parse_keyword
85              
86             register_xs_parse_keyword "name" => %args;
87              
88             Registers a new extension keyword into the C registry,
89             defined using the given name and arguments.
90              
91             Takes the following named arguments:
92              
93             =over 4
94              
95             =item flags => INT
96              
97             Optional. If present, a bitmask of the following flag constants:
98              
99             =over 4
100              
101             =item XPK_FLAG_EXPR
102              
103             The build phase is expected to return C.
104              
105             =item XPK_FLAG_STMT
106              
107             The build phase is expected to return C.
108              
109             =item XPK_FLAG_AUTOSEMI
110              
111             The syntax forms a complete statement, which should be followed by C<;>.
112              
113             =back
114              
115             =item pieces => ARRAY
116              
117             Optional. If present, contains definitions for the syntax pieces to be parsed
118             for the syntax of this keyword. This must be composed of a list of calls to
119             the various C piece-generating functions; documented below.
120              
121             =item permit_hintkey => STRING
122              
123             Optional. A string value to use for the "permit_hintkey".
124              
125             =item permit => CODE
126              
127             Optional. Callback function for the "permit" phase of keyword parsing.
128              
129             $ok = $permit->( $hookdata );
130              
131             When invoked, it is passed a single arugment containing the (optional)
132             hookdata value, and its result should be a boolean scalar.
133              
134             At least one of C or C must be provided.
135              
136             =item check => CODE
137              
138             Optional. Callback function for the "check" phase of keyword parsing.
139              
140             $check->( $hookdata );
141              
142             When invoked, it is passsed a single argument containing the (optional)
143             bookdata value.
144              
145             =item build => CODE
146              
147             Callback function for the "build" phase of keyword parsing.
148              
149             $ret = $build->( \$out, \@args, $hookdata );
150              
151             When invoked, it is passed a SCALAR ref to store the output optree into, an
152             ARRAY reference containing the parsed arguments, and the (optional) hookdata
153             value.
154              
155             The C<@args> array will contain object references referring to the individual
156             arguments parsed by the parser pieces. See L.
157              
158             The callback function should be build an optree as a C fragment,
159             possibly by calling the various C functions defined above, and store
160             the eventual result into the scalar referred to by the first argument.
161              
162             The callback should return one of C or
163             C to indicate how its syntax should be interpreted by the
164             perl parser.
165              
166             =item hookdata => SCALAR
167              
168             Optional. If present, this scalar value is stored by the keyword definition
169             and passed into each of the phase callbacks when invoked. If not present then
170             C will be passed to the callbacks instead.
171              
172             =back
173              
174             =cut
175              
176             =head2 Piece Type Functions
177              
178             The following functions can be used to generate parsing pieces.
179              
180             =head3 XPK_BLOCK
181              
182             A block of code, returned in the I field.
183              
184             =head3 XPK_ANONSUB
185              
186             An anonymous subroutine, returned in the I field.
187              
188             =head3 XPK_ARITHEXPR
189              
190             An arithemetic expression, returned in the I field.
191              
192             =head3 XPK_TERMEXPR
193              
194             A term expression, returned in the I field.
195              
196             =head3 XPK_LISTEXPR
197              
198             A list expression, returned in the I field.
199              
200             =head3 XPK_IDENT, XPK_IDENT_OPT
201              
202             An identifier, returned as a string in the I field.
203              
204             The C<_OPT> variant is optional.
205              
206             =head3 XPK_PACKAGENAME, XPK_PACKAGENAME_OPT
207              
208             A package name, returned as a string in the I field.
209              
210             The C<_OPT> variant is optional.
211              
212             =head3 XPK_LEXVARNAME
213              
214             XPK_LEXVARNAME($kind)
215              
216             A lexical variable name, returned as a string in the I field.
217              
218             The C<$kind> must be a bitmask of C, C,
219             C; or C for convenience to set all three.
220              
221             =head3 XPK_VSTRING, XPK_VSTRING_OPT
222              
223             A version string, returned as a L object instance in the I field.
224              
225             The C<_OPT> variant is optional.
226              
227             =head3 XPK_LEXVAR
228              
229             XPK_LEXVAR($kind)
230              
231             A lexical variable that already exists in the pad, returned as a pad offset in
232             the I field.
233              
234             C<$kind> is specified as in C.
235              
236             =head3 XPK_LEXVAR_MY
237              
238             XPK_LEXVAR_MY($kind)
239              
240             A lexical variable, parsed as if it appeared in a C expression. It will be
241             added to the pad and returned as a pad offset in the I field.
242              
243             =head3 XPK_COMMA
244              
245             =head3 XPK_COLON
246              
247             =head3 XPK_EQUALS
248              
249             A literal comma, colon or equals sign. These do not appear in the arguments
250             list.
251              
252             =head3 XPK_LITERAL
253              
254             XPK_LISTEXPR($string)
255              
256             A literal string match. No value is returned.
257              
258             This should be avoided if at all possible, in favour of the character matches
259             above, or C.
260              
261             =head3 XPK_KEYWORD
262              
263             XPK_KEYWORD($string)
264              
265             A literal string match, which requires that the following text does not begin
266             with an identifier character (thus avoiding prefix-match problems). No value
267             is returned.
268              
269             =head3 XPK_INTRO_MY
270              
271             Calls the perl C function immediately. No input is consumed and no
272             output value is generated.
273              
274             =head3 XPK_SEQUENCE
275              
276             XPK_SEQUENCE(@pieces)
277              
278             A sub-sequence. Normally this is not necessary, as most of the
279             structure-forming functions already take a sequence of pieces. It is mostly
280             useful as as an option to the C function.
281              
282             Nothing extra is returned, beyond the values from the individual pieces.
283              
284             =head3 XPK_OPTIONAL
285              
286             XPK_OPTIONAL(@pieces)
287              
288             An optional sequence of pieces that may or may not be present. Returns an
289             integer value in the I field of 0 if the sequence was not found, or 1
290             followed by its values if the sequence was found.
291              
292             =head3 XPK_REPEATED
293              
294             XPK_REPEATED(@pieces)
295              
296             A repeating sequence of pieces. Returns an integer value in the I field
297             indicating how many times the sequence repeated, followed by all the values
298             returned by each.
299              
300             =head3 XPK_CHOICE
301              
302             XPK_CHOICE(@pieces)
303              
304             The pieces of this function are not interpreted as a sequence, but instead
305             as a list of possible choices. Returns an integer value in the I field
306             indicating which choice was found, followed by all the values returned by
307             that sequence.
308              
309             The first possible choice is numbered C<0>. If no choice matched, it returns
310             C<-1>. To cause an error instead, use L.
311              
312             =head3 XPK_FAILURE
313              
314             XPK_FAILURE($message)
315              
316             Attempting to parse this piece type will immediately cause a compile-time
317             failure with the given message. This can be used as the final option in
318             C to ensure a valid match.
319              
320             =head3 XPK_PARENS
321              
322             XPK_PARENS(@pieces)
323              
324             Expects to find a sequence of pieces, all surrounded by parentheses (round
325             brackets, C<( ... )>).
326              
327             Nothing extra is returned, beyond the values from the individual contained
328             pieces.
329              
330             =head3 XPK_ARGS
331              
332             XPK_ARGS(@pieces)
333              
334             A container similar to C except that the parentheses themselves
335             are optional, similar to perl's parsing of calls to known functions.
336              
337             =head3 XPK_BRACKETS
338              
339             XPK_BRACKETS(@pieces)
340              
341             Expects to find a sequence of pieces, all surrounded by square brackets
342             (C<[ ... ]>).
343              
344             Nothing extra is returned, beyond the values from the individual contained
345             pieces.
346              
347             =head3 XPK_BRACES
348              
349             XPK_BRACES(@pieces)
350              
351             Expects to find a sequence of pieces, all surrounded by braces (C<{ ... }>).
352              
353             Nothing extra is returned, beyond the values from the individual contained
354             pieces.
355              
356             =head3 XPK_CHEVRONS
357              
358             XPK_CHEVRONS(@pieces)
359              
360             Expects to find a sequence of pieces, all surrounded by chevrons (angle
361             brackets, C<< < ... > >>).
362              
363             Nothing extra is returned, beyond the values from the individual contained
364             pieces.
365              
366             =cut
367              
368             # Simple pieces
369             foreach (qw(
370             BLOCK ANONSUB ARITHEXPR TERMEXPR LISTEXPR IDENT IDENT_OPT
371             PACKAGENAME PACKAGENAME_OPT VSTRING VSTRING_OPT COMMA COLON EQUALS
372             )) {
373             my $name = "XPK_$_";
374             push @EXPORT_OK, $name;
375              
376 9     9   67 no strict 'refs';
  9         26  
  9         1063  
377 12     12   9412 *$name = sub { bless [$name], "XS::Parse::Keyword::FromPerl::_Piece" };
378             }
379             # Single-SV parametric pieces
380             foreach (qw(
381             LEXVARNAME LEXVAR LEXVAR_MY LITERAL KEYWORD FAILURE
382             )) {
383             my $name = "XPK_$_";
384             push @EXPORT_OK, $name;
385              
386 9     9   64 no strict 'refs';
  9         20  
  9         947  
387 3     3   886 *$name = sub { bless [$name, $_[0]], "XS::Parse::Keyword::FromPerl::_Piece" };
388             }
389             # Structural multiple-value pieces
390             foreach (qw(
391             SEQUENCE OPTIONAL REPEATED CHOICE
392             PARENS ARGS BRACKETS BRACES CHEVRONS
393             )) {
394             my $name = "XPK_$_";
395             push @EXPORT_OK, $name;
396              
397 9     9   64 no strict 'refs';
  9         18  
  9         1246  
398 3     3   118 *$name = sub { bless [$name, [@_]], "XS::Parse::Keyword::FromPerl::_Piece" };
399             }
400              
401             # Back-compat wrappers for the old names
402             foreach (qw( PAREN ARG BRACKET BRACE CHEVRON )) {
403             my $macroname = "XPK_${_}S";
404             my $funcname = "XPK_${_}SCOPE";
405             push @EXPORT_OK, $funcname;
406              
407 9     9   103 no strict 'refs';
  9         20  
  9         1127  
408             *$funcname = sub {
409 0     0     warnings::warnif deprecated => "$funcname is now deprecated; use $macroname instead";
410 0           bless [$macroname, [@_]], "XS::Parse::Keyword::FromPerl::_Piece";
411             };
412             }
413              
414             =head2 Parser Arguments
415              
416             Each of the values given in the C<@args> array for the "build" phase callback
417             are given as object references having the following methods
418              
419             =head3 op
420              
421             $op = $arg->op;
422              
423             Returns an optree.
424              
425             =head3 cv
426              
427             $cv = $arg->cv;
428              
429             Returns a CV wrapped in a CODE reference.
430              
431             =head3 sv
432              
433             $sv = $arg->sv;
434              
435             Returns the SV itself, or C if the optional SV was absent.
436              
437             =head3 has_sv
438              
439             $ok = $arg->has_sv;
440              
441             Returns true if the optional SV was present (even if it was C), or
442             false if it was absent.
443              
444             =head3 i
445              
446             $i = $arg->i;
447              
448             Returns an integer.
449              
450             =head3 padix
451              
452             $padix = $arg->padix;
453              
454             Returns a pad offset index as an integer.
455              
456             =head3 line
457              
458             $line = $arg->line;
459              
460             Returns the line number of the source text on which the piece was started.
461              
462             =cut
463              
464             =head1 AUTHOR
465              
466             Paul Evans
467              
468             =cut
469              
470             0x55AA;