1XS::Parse::Keyword(3) User Contributed Perl DocumentationXS::Parse::Keyword(3)
2
3
4

NAME

6       "XS::Parse::Keyword" - XS functions to assist in parsing keyword syntax
7

DESCRIPTION

9       This module provides some XS functions to assist in writing syntax
10       modules that provide new perl-visible syntax, primarily for authors of
11       keyword plugins using the "PL_keyword_plugin" hook mechanism. It is
12       unlikely to be of much use to anyone else; and highly unlikely to be
13       any use when writing perl code using these. Unless you are writing a
14       keyword plugin using XS, this module is not for you.
15
16       This module is also currently experimental, and the design is still
17       evolving and subject to change. Later versions may break ABI
18       compatibility, requiring changes or at least a rebuild of any module
19       that depends on it.
20

XS FUNCTIONS

22   boot_xs_parse_keyword
23          void boot_xs_parse_keyword(double ver);
24
25       Call this function from your "BOOT" section in order to initialise the
26       module and parsing hooks.
27
28       ver should either be 0 or a decimal number for the module version
29       requirement; e.g.
30
31          boot_xs_parse_keyword(0.14);
32
33   register_xs_parse_keyword
34          void register_xs_parse_keyword(const char *keyword,
35            const struct XSParseKeywordHooks *hooks, void *hookdata);
36
37       This function installs a set of parsing hooks to be associated with the
38       given keyword. Such a keyword will then be handled automatically by a
39       keyword parser installed by "XS::Parse::Keyword" itself.
40

PARSE HOOKS

42       The "XSParseKeywordHooks" structure provides the following hook stages,
43       which are invoked in the given order.
44
45   flags
46       The following flags are defined:
47
48       "XPK_FLAG_EXPR"
49           The parse or build function is expected to return
50           "KEYWORD_PLUGIN_EXPR".
51
52       "XPK_FLAG_STMT"
53           The parse or build function is expected to return
54           "KEYWORD_PLUGIN_STMT".
55
56           These two flags are largely for the benefit of giving static
57           information at registration time to assist static parsing or other
58           related tasks to know what kind of grammatical element this keyword
59           will produce.
60
61       "XPK_FLAG_AUTOSEMI"
62           The syntax forms a complete statement, which should be followed by
63           a statement separator semicolon (";"). This semicolon is optional
64           at the end of a block.
65
66           The semicolon, if present, will be consumed automatically.
67
68   The "permit" Stage
69          const char *permit_hintkey;
70          bool (*permit) (pTHX_ void *hookdata);
71
72       Called by the installed keyword parser hook which is used to handle
73       keywords registered by "register_xs_parse_keyword".
74
75       As a shortcut for the common case, the "permit_hintkey" may point to a
76       string to look up from the hints hash. If the given key name is not
77       found in the hints hash then the keyword is not permitted. If the key
78       is present then the "permit" function is invoked as normal.
79
80       If not rejected by a hint key that was not found in the hints hash, the
81       function part of the stage is called next and should inspect whether
82       the keyword is permitted at this time perhaps by inspecting other
83       lexical clues, and return true only if the keyword is permitted.
84
85       Both the string and the function are optional. Either or both may be
86       present.  If neither is present then the keyword is always permitted -
87       which is likely not what you wanted to do.
88
89   The "check" Stage
90          void (*check)(pTHX_ void *hookdata);
91
92       Invoked once the keyword has been permitted. If present, this hook
93       function can check the surrounding lexical context, state, or other
94       information and throw an exception if it is unhappy that the keyword
95       should apply in this position.
96
97   The "parse" Stage
98       This stage is invoked once the keyword has been checked, and actually
99       parses the incoming text into an optree. It is implemented by calling
100       the first of the following function pointers which is not NULL. The
101       invoked function may optionally build an optree to represent the parsed
102       syntax, and place it into the variable addressed by "out". If it does
103       not, then a simple "OP_NULL" will be constructed in its place.
104
105       "lex_read_space()" is called both before and after this stage is
106       invoked, so in many simple cases the hook function itself does not need
107       to bother with it.
108
109          int (*parse)(pTHX_ OP **out, void *hookdata);
110
111       If present, this should consume text from the parser buffer by invoking
112       "lex_*" or "parse_*" functions and eventually return a
113       "KEYWORD_PLUGIN_*" result value.
114
115       This is the most generic and powerful of the options, but requires the
116       most amount of implementation work.
117
118          int (*build)(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata);
119
120       If "parse" is not present, this is called instead after parsing a
121       sequence of arguments, of types given by the pieces field; which should
122       be a zero- terminated array of piece types.
123
124       This alternative is somewhat less generic and powerful than providing
125       "parse" yourself, but involves much less parsing work and is shorter
126       and easier to implement.
127
128          int (*build1)(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata);
129
130       If neither "parse" nor "build" are present, this is called as a simpler
131       variant of "build" when only a single argument is required. It takes
132       its type from the "piece1" field instead.
133

PIECES AND PIECE TYPES

135       When using the "build" or "build1" alternatives for the "parse" phase,
136       the actual syntax is parsed automatically by this module, according to
137       the specification given by the pieces or piece1 field. The result of
138       that parsing step is placed into the args or arg0 parameter to the
139       invoked function, using a "struct" type consisting of the following
140       fields:
141
142          typedef struct
143             union {
144                OP *op;
145                CV *cv;
146                SV *sv;
147                int i;
148                struct {
149                   SV *name;
150                   SV *value;
151                } attr;
152                PADOFFSET padix;
153                struct XSParseInfixInfo *infix;
154             };
155             int line;
156          } XSParseKeywordPiece;
157
158       Which field of the anonymous union is set depends on the type of the
159       piece.  The line field contains the line number of the source file
160       where parsing of that piece began.
161
162       Some piece types are "atomic", whose definition is self-contained.
163       Others are structural, defined in terms of inner pieces. Together these
164       form an entire tree-shaped definition of the syntax that the keyword
165       expects to find.
166
167       Atomic types generally provide exactly one argument into the list of
168       args (with the exception of literal matches, which do not provide
169       anything).  Structural types may provide an initial argument
170       themselves, followed by a list of the values of each sub-piece they
171       contained inside them. Thus, while the data structure defining the
172       syntax shape is a tree, the argument values it parses into is passed as
173       a flat array to the "build" function.
174
175       Some structural types need to be able to determine whether or not
176       syntax relating some optional part of them is present in the incoming
177       source text. In this case, the pieces relating to those optional parts
178       must support "probing".  This ability is also noted below.
179
180       The type of each piece should be one of the following macro values.
181
182   XPK_BLOCK
183       atomic, can probe, emits op.
184
185          XPK_BLOCK
186
187       A brace-delimited block of code is expected, passed as an optree in the
188       op field. This will be parsed as a block within the current function
189       scope.
190
191       This can be probed by checking for the presence of an open-brace ("{")
192       character.
193
194       Be careful defining grammars with this because an open-brace is also a
195       valid character to start a term expression, for example. Given a choice
196       between "XPK_BLOCK" and "XPK_TERMEXPR", either of them could try to
197       consume such code as
198
199          { 123, 456 }
200
201   XPK_BLOCK_VOIDCTX, XPK_BLOCK_SCALARCTX, XPK_BLOCK_LISTCTX
202       Variants of "XPK_BLOCK" which wrap a void, scalar or list-context scope
203       around the block.
204
205   XPK_PREFIXED_BLOCK
206       structural, emits op.
207
208          XPK_PREFIXED_BLOCK(pieces ...)
209
210       Some pieces are expected, followed by a brace-delimited block of code,
211       which is passed as an optree in the op field. The prefix pieces are
212       parsed first, and their results are passed before the block itself.
213
214       The entire sequence, including the prefix items, is contained within a
215       pair of "block_start()" / "block_end()" calls. This permits the prefix
216       pieces to introduce new items into the lexical scope of the block - for
217       example by the use of "XPK_LEXVAR_MY".
218
219       A call to "intro_my()" is automatically made at the end of the prefix
220       pieces, before the block itself is parsed, ensuring any new lexical
221       variables are now visible.
222
223       In addition, the following extra piece types are recognised here:
224
225       XPK_SETUP
226              void setup(pTHX_ void *hookdata);
227
228              XPK_SETUP(&setup)
229
230           atomic, emits nothing.
231
232           This piece type runs a function given by pointer. Typically this
233           function may be used to introduce new lexical state into the
234           parser, or in some other way have some side-effect on the parsing
235           context of the block to be parsed.
236
237   XPK_PREFIXED_BLOCK_ENTERLEAVE
238       A variant of "XPK_PREFIXED_BLOCK" which additionally wraps the entire
239       parsing operation, including the "block_start()", "block_end()" and any
240       calls to "XPK_SETUP" functions, within a "ENTER"/"LEAVE" pair.
241
242       This should not make a difference to the standard parser pieces
243       provided here, but may be useful behaviour for the code in the setup
244       function, especially if it wishes to modify parser state and use the
245       savestack to ensure it is restored again when parsing has finished.
246
247   XPK_ANONSUB
248       atomic, emits op.
249
250       A brace-delimited block of code is expected, and assembled into the
251       body of a new anonymous subroutine. This will be passed as a protosub
252       CV in the cv field.
253
254   XPK_TERMEXPR
255       atomic, emits op.
256
257          XPK_TERMEXPR
258
259       A term expression is expected, parsed using "parse_termexpr()", and
260       passed as an optree in the op field.
261
262   XPK_TERMEXPR_VOIDCTX, XPK_TERMEXPR_SCALARCTX
263       Variants of "XPK_TERMEXPR" which puts the expression in void or scalar
264       context.
265
266   XPK_LISTEXPR
267       atomic, emits op.
268
269          XPK_LISTEXPR
270
271       A list expression is expected, parsed using "parse_listexpr()", and
272       passed as an optree in the op field.
273
274   XPK_LISTEXPR_LISTCTX
275       Variant of "XPK_LISTEXPR" which puts the expression in list context.
276
277   XPK_IDENT, XPK_IDENT_OPT
278       atomic, can probe, emits sv.
279
280       A bareword identifier name is expected, and passed as an SV containing
281       a PV in the sv field. An identifier is not permitted to contain a
282       double colon ("::").
283
284       The "_OPT"-suffixed version is optional; if no identifier is found then
285       sv is set to "NULL".
286
287   XPK_PACKAGENAME, XPK_PACKAGENAME_OPT
288       atomic, can probe, emits sv.
289
290       A bareword package name is expected, and passed as an SV containing a
291       PV in the sv field. A package name is similar to an identifier, except
292       it permits double colons in the middle.
293
294       The "_OPT"-suffixed version is optional; if no package name is found
295       then sv is set to "NULL".
296
297   XPK_LEXVARNAME
298       atomic, emits sv.
299
300          XPK_LEXVARNAME(kind)
301
302       A lexical variable name is expected, and passed as an SV containing a
303       PV in the sv field. The "kind" argument specifies what kinds of
304       variable are permitted, and should be a bitmask of one or more bits
305       from "XPK_LEXVAR_SCALAR", "XPK_LEXVAR_ARRAY" and "XPK_LEXVAR_HASH". A
306       convenient shortcut "XPK_LEXVAR_ANY" permits all three.
307
308   XPK_ATTRIBUTES
309       atomic, emits i followed by more args.
310
311       A list of ":"-prefixed attributes is expected, in the same format as
312       sub or variable attributes. An optional leading ":" indicates the
313       presence of attributes, then one or more of them are parsed. Attributes
314       may be optionally separated by additional ":"s, but this is not
315       required.
316
317       Each attribute is expected to be an identifier name, followed by an
318       optional value wrapped in parentheses. Whitespace is NOT permitted
319       between the name and value, as per standard Perl parsing rules.
320
321          :attrname
322          :attrname(value)
323
324       The i field indicates how many attributes were found. That number of
325       additional arguments are then passed, each containing two SVs in the
326       attr.name and attr.value fields. This number may be zero.
327
328       It is not an error for there to be no attributes present, or for the
329       optional colon to be missing. In this case i will be set to zero.
330
331   XPK_VSTRING, XPK_VSTRING_OPT
332       atomic, can probe, emits sv.
333
334       A version string is expected, of the form "v1.234" including the
335       leading "v" character. It is passed as a version SV object in the sv
336       field.
337
338       The "_OPT"-suffixed version is optional; if no version string is found
339       then sv is set to "NULL".
340
341   XPK_LEXVAR_MY
342       atomic, emits padix.
343
344          XPK_LEXVAR_MY(kind)
345
346       A lexical variable name is expected, added to the current pad as if
347       specified in a "my" expression, and passed as the pad index in the
348       padix field.
349
350       The "kind" argument specifies what kinds of variable are permitted, as
351       per "XPK_LEXVARNAME".
352
353   XPK_COMMA, XPK_COLON, XPK_EQUALS
354       atomic, can probe, emits nothing.
355
356       A literal character (",", ":" or "=") is expected. No argument value is
357       passed.
358
359   XPK_INFIX_*
360       atomic, can probe, emits infix.
361
362       An infix operator as recognised by XS::Parse::Infix. The returned
363       pointer points to a structure allocated by "XS::Parse::Infix"
364       describing the operator.
365
366       Various versions of the macro are provided, each using a different
367       selection filter to choose certain available infix operators:
368
369          XPK_INFIX_RELATION         # any relational operator
370          XPK_INFIX_EQUALITY         # an equality operator like `==` or `eq`
371          XPK_INFIX_MATCH_NOSMART    # any sort of "match"-like operator, except smartmatch
372          XPK_INFIX_MATCH_SMART      # XPK_INFIX_MATCH_NOSMART plus smartmatch
373
374   XPK_LITERAL
375       atomic, can probe, emits nothing.
376
377          XPK_LITERAL("literal")
378
379       A literal string match is expected. No argument value is passed.
380
381       This form should generally be avoided if at all possible, because it is
382       very easy to abuse to make syntaxes which confuse humans and code tools
383       alike.  Generally it is best reserved just for the first component of a
384       "XPK_OPTIONAL" or "XPK_REPEATED" sequence, to provide a "secondary
385       keyword" that such a repeated item can look out for.
386
387       This was previously called "XPK_STRING", and is provided as a synonym
388       for back-compatibility but new code should use this new name instead.
389
390   XPK_SEQUENCE
391       structural, might support probe, emits nothing.
392
393          XPK_SEQUENCE(pieces ...)
394
395       A structural type which contains a number of pieces. This is normally
396       equivalent to simply placing the pieces in sequence inside their own
397       container, but it is useful inside "XPK_CHOICE" or "XPK_TAGGEDCHOICE".
398
399       An "XPK_SEQUENCE" supports probe if its first contained piece does;
400       i.e.  is transparent to probing.
401
402   XPK_OPTIONAL
403       structural, emits i.
404
405          XPK_OPTIONAL(pieces ...)
406
407       A structural type which may expects to find its contained pieces, or is
408       happy not to. This will pass an argument whose i field contains either
409       1 or 0, depending whether the contents were found. The first piece type
410       within must support probe.
411
412   XPK_REPEATED
413       structural, emits i.
414
415          XPK_REPEATED(pieces ...)
416
417       A structural type which expects to find zero or more repeats of its
418       contained pieces. This will pass an argument whose i field contains the
419       count of the number of repeats it found. The first piece type within
420       must support probe.
421
422   XPK_CHOICE
423       structural, can probe, emits i.
424
425          XPK_CHOICE(options ...)
426
427       A structural type which expects to find one of a number of alternative
428       options. An ordered list of types is provided, all of which must
429       support probe. This will pass an argument whose i field gives the index
430       of the first choice that was accepted. The first option takes the value
431       0.
432
433       As each of the options is interpreted as an alternative, not a
434       sequence, you should use "XPK_SEQUENCE" if a sequence of multiple items
435       should be considered as a single alternative.
436
437       It is not an error if no choice matches. At that point, the i field
438       will be set to -1.
439
440       If you require a failure message in this case, set the final choice to
441       be of type "XPK_FAILURE". This will cause an error message to be
442       printed instead.
443
444          XPK_FAILURE("message string")
445
446   XPK_TAGGEDCHOICE
447       structural, can probe, emits i.
448
449          XPK_TAGGEDCHOICE(choice, tag, ...)
450
451       A structural type similar to "XPK_CHOICE", except that each choice type
452       is followed by an element of type "XPK_TAG" which gives an integer. It
453       is that integer value, rather than the positional index of the choice
454       within the list, which is passed in the i field.
455
456          XPK_TAG(value)
457
458       As each of the options is interpreted as an alternative, not a
459       sequence, you should use "XPK_SEQUENCE" if a sequence of multiple items
460       should be considered as a single alternative.
461
462   XPK_COMMALIST
463       structural, might support probe, emits i.
464
465          XPK_COMMALIST(pieces ...)
466
467       A structural type which expects to find one or more repeats of its
468       contained pieces, separated by literal comma (",") characters. This is
469       somewhat similar to "XPK_REPEATED", except that it needs at least one
470       copy, needs commas between its items, but does not require that the
471       first contained piece support probe (the comma itself is sufficient to
472       indicate a repeat).
473
474       An "XPK_COMMALIST" supports probe if its first contained piece does;
475       i.e.  is transparent to probing.
476
477   XPK_PARENSCOPE
478       structural, can probe, emits nothing.
479
480          XPK_PARENSCOPE(pieces ...)
481
482       A structural type which expects to find a sequence of pieces, all
483       contained in parentheses as "( ... )". This will pass no extra
484       arguments.
485
486   XPK_BRACKETSCOPE
487       structural, can probe, emits nothing.
488
489          XPK_BRACKETSCOPE(pieces ...)
490
491       A structural type which expects to find a sequence of pieces, all
492       contained in square brackets as "[ ... ]". This will pass no extra
493       arguments.
494
495   XPK_BRACESCOPE
496       structural, can probe, emits nothing.
497
498          XPK_BRACESCOPE(pieces ...)
499
500       A structural type which expects to find a sequence of pieces, all
501       contained in braces as "{ ... }". This will pass no extra arguments.
502
503       Note that this is not necessary to use with "XPK_BLOCK" or
504       "XPK_ANONSUB"; those will already consume a set of braces. This is
505       intended for special constrained syntax that should not just accept an
506       arbitrary block.
507
508   XPK_CHEVRONSCOPE
509       structural, can probe, emits nothing.
510
511          XPK_CHEVRONSCOPE(pieces ...)
512
513       A structural type which expects to find a sequence of pieces, all
514       contained in angle brackets as "< ... >". This will pass no extra
515       arguments.
516
517       Remember that expressions like "a > b" are valid term expressions, so
518       the contents of this scope shouldn't allow arbitrary expressions or the
519       closing bracket will be ambiguous.
520
521   XPK_PARENSCOPE_OPT, XPK_BRACKETSCOPE_OPT, XPK_BRACESCOPE_OPT,
522       XPK_CHEVRONSCOPE_OPT
523       structural, can probe, emits i.
524
525          XPK_PARENSCOPE_OPT(pieces ...)
526          XPK_BRACKETSCOPE_OPT(pieces ...)
527          XPK_BRACESCOPE_OPT(pieces ...)
528          XPK_CHEVERONSCOPE_OPT(pieces ...)
529
530       Each of the four "XPK_...SCOPE" macros above has an optional variant,
531       whose name is suffixed by "_OPT". These pass an argument whose i field
532       is either true or false, indicating whether the scope was found,
533       followed by the values from the scope itself.
534
535       This is a convenient shortcut to nesting the scope within a
536       "XPK_OPTIONAL" macro.
537

AUTHOR

539       Paul Evans <leonerd@leonerd.org.uk>
540
541
542
543perl v5.34.0                      2021-10-12             XS::Parse::Keyword(3)
Impressum