Debian Codegraph @

t/pieces.xs

raw
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, 2021-2022 -- [email protected]
5 */
6
7 #include "EXTERN.h"
8 #include "perl.h"
9 #include "XSUB.h"
10
11 #include "XSParseKeyword.h"
12 #include "XSParseInfix.h"
13
14 #include "perl-backcompat.c.inc"
15
16 static const char hintkey[] = "t::pieces/permit";
17
18 static int build_expr(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
19 {
20 OP *expr = arg0->op;
21
22 if(!expr) {
23 *out = newOP(OP_STUB, 0);
24 return KEYWORD_PLUGIN_EXPR;
25 }
26
27 /* wrap the result in "("...")" parens so we can unit-test how it parsed */
28 *out = newBINOP(OP_CONCAT, 0,
29 newBINOP(OP_CONCAT, 0, newSVOP(OP_CONST, 0, newSVpvs("(")), op_scope(expr)),
30 newSVOP(OP_CONST, 0, newSVpvs(")")));
31 return KEYWORD_PLUGIN_EXPR;
32 }
33
34 static int build_prefixedblock(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
35 {
36 PADOFFSET padix = args[0]->padix;
37 OP *value = args[1]->op;
38 OP *body = args[2]->op;
39
40 OP *padsvop = newOP(OP_PADSV, OPf_MOD|(OPpLVAL_INTRO<<8));
41 padsvop->op_targ = padix;
42
43 *out = op_prepend_elem(OP_LINESEQ,
44 newBINOP(OP_SASSIGN, 0, value, padsvop),
45 body);
46
47 return KEYWORD_PLUGIN_EXPR;
48 }
49
50 static int build_anonsub(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
51 {
52 *out = newUNOP(OP_REFGEN, 0,
53 newSVOP(OP_ANONCODE, 0, (SV *)arg0->cv));
54 return KEYWORD_PLUGIN_EXPR;
55 }
56
57 static int build_list(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
58 {
59 OP *list = arg0->op;
60
61 if(!list) {
62 *out = newOP(OP_STUB, 0);
63 return KEYWORD_PLUGIN_EXPR;
64 }
65
66 /* TODO: Consider always doing this? */
67 if(list->op_type != OP_LIST)
68 list = newLISTOP(OP_LIST, 0, list, NULL);
69
70 /* unshift $sep */
71 #if HAVE_PERL_VERSION(5, 22, 0)
72 op_sibling_splice(list, cUNOPx(list)->op_first, 0, newSVOP(OP_CONST, 0, newSVpvs(",")));
73 #else
74 {
75 OP *o = newSVOP(OP_CONST, 0, newSVpvs(","));
76 o->op_sibling = cUNOPx(list)->op_first->op_sibling;
77 cUNOPx(list)->op_first->op_sibling = o;
78 }
79 #endif
80
81 *out = op_convert_list(OP_JOIN, 0, list);
82
83 return KEYWORD_PLUGIN_EXPR;
84 }
85
86 static int build_constsv(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
87 {
88 *out = newSVOP(OP_CONST, 0, arg0->sv);
89 return KEYWORD_PLUGIN_EXPR;
90 }
91
92 static int build_constsv_or_undef(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
93 {
94 if(arg0->sv)
95 *out = newSVOP(OP_CONST, 0, arg0->sv);
96 else
97 *out = newOP(OP_UNDEF, OPf_WANT_SCALAR);
98 return KEYWORD_PLUGIN_EXPR;
99 }
100
101 static int build_constpadix(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
102 {
103 *out = newSVOP(OP_CONST, 0, newSVuv(arg0->padix));
104 return KEYWORD_PLUGIN_EXPR;
105 }
106
107 static int build_literal(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
108 {
109 /* ignore arg0 */
110
111 *out = newSVOP(OP_CONST, 0, SvREFCNT_inc((SV *)hookdata));
112
113 return KEYWORD_PLUGIN_EXPR;
114 }
115
116 static int build_attrs(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
117 {
118 int nattrs = args[0]->i;
119
120 SV *retsv = newSV(0);
121 sv_setpvs(retsv, "");
122
123 int argi;
124 for(argi = 0; argi < nattrs; argi++)
125 sv_catpvf(retsv, ":%s(%s)",
126 SvPV_nolen(args[argi+1]->attr.name),
127 SvPOK(args[argi+1]->attr.value) ? SvPV_nolen(args[argi+1]->attr.value) : "");
128
129 *out = newSVOP(OP_CONST, 0, retsv);
130 return KEYWORD_PLUGIN_EXPR;
131 }
132
133 static int build_infix_opname(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
134 {
135 const char *opname = PL_op_name[arg0->infix->opcode];
136 *out = newSVOP(OP_CONST, 0, newSVpvn(opname, strlen(opname)));
137 return KEYWORD_PLUGIN_EXPR;
138 }
139
140 static int build_lexvar_intro(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
141 {
142 PADOFFSET padix = args[0]->padix;
143 OP *expr = args[1]->op;
144
145 OP *varop = newOP(OP_PADSV, OPf_MOD|OPf_REF | (OPpLVAL_INTRO << 8));
146 varop->op_targ = padix;
147
148 OP *assignop = newASSIGNOP(OPf_WANT_VOID, varop, 0, newSVOP(OP_CONST, 0, newSViv(1)));
149
150 *out = newLISTOP(OP_LINESEQ, 0, assignop, expr);
151 return KEYWORD_PLUGIN_EXPR;
152 }
153
154 static void setup_block_VAR(pTHX_ void *hookdata)
155 {
156 char *varname = hookdata;
157 PADOFFSET padix = pad_add_name_pvn(varname, strlen(varname), 0, NULL, NULL);
158 intro_my();
159
160 sv_setpvs(PAD_SVl(padix), "Hello");
161 }
162
163 static void callback_catpv_stages(pTHX_ const char *pv)
164 {
165 SV *sv = get_sv("main::STAGES", GV_ADD);
166 if(!SvPOK(sv))
167 sv_setpvs(sv, "");
168
169 if(SvCUR(sv))
170 sv_catpvs(sv, ",");
171
172 sv_catpv(sv, pv);
173 }
174
175 static void callback_PREPARE(pTHX_ void *hookdata)
176 {
177 callback_catpv_stages(aTHX_ "PREPARE");
178 }
179
180 static void callback_START(pTHX_ void *hookdata)
181 {
182 callback_catpv_stages(aTHX_ "START");
183 }
184
185 static OP *callback_END(pTHX_ OP *o, void *hookdata)
186 {
187 callback_catpv_stages(aTHX_ "END");
188 return o;
189 }
190
191 static OP *callback_WRAP(pTHX_ OP *o, void *hookdata)
192 {
193 callback_catpv_stages(aTHX_ "WRAP");
194 return o;
195 }
196
197 static const struct XSParseKeywordHooks hooks_block = {
198 .permit_hintkey = hintkey,
199
200 .piece1 = XPK_BLOCK,
201 .build1 = &build_expr,
202 };
203 static const struct XSParseKeywordHooks hooks_block_scalar = {
204 .permit_hintkey = hintkey,
205
206 .piece1 = XPK_BLOCK_SCALARCTX,
207 .build1 = &build_list,
208 };
209 static const struct XSParseKeywordHooks hooks_block_list = {
210 .permit_hintkey = hintkey,
211
212 .piece1 = XPK_BLOCK_LISTCTX,
213 .build1 = &build_list,
214 };
215
216 static const struct XSParseKeywordHooks hooks_prefixedblock = {
217 .permit_hintkey = hintkey,
218
219 .pieces = (const struct XSParseKeywordPieceType []){
220 XPK_PREFIXED_BLOCK( XPK_LEXVAR_MY(XPK_LEXVAR_SCALAR), XPK_EQUALS, XPK_TERMEXPR_SCALARCTX, XPK_COMMA ),
221 {0}
222 },
223 .build = &build_prefixedblock,
224 };
225
226 static const struct XSParseKeywordHooks hooks_prefixedblock_VAR = {
227 .permit_hintkey = hintkey,
228
229 .piece1 = XPK_PREFIXED_BLOCK( XPK_SETUP(&setup_block_VAR) ),
230 .build1 = &build_expr,
231 };
232
233 static const struct XSParseKeywordHooks hooks_anonsub = {
234 .permit_hintkey = hintkey,
235
236 .piece1 = XPK_ANONSUB,
237 .build1 = &build_anonsub,
238 };
239
240 static const struct XSParseKeywordHooks hooks_stagedanonsub = {
241 .permit_hintkey = hintkey,
242
243 .piece1 = XPK_STAGED_ANONSUB(
244 XPK_ANONSUB_PREPARE(&callback_PREPARE),
245 XPK_ANONSUB_START(&callback_START),
246 XPK_ANONSUB_START(&setup_block_VAR),
247 XPK_ANONSUB_END(&callback_END),
248 XPK_ANONSUB_WRAP(&callback_WRAP)
249 ),
250 .build1 = &build_anonsub,
251 };
252
253 static const struct XSParseKeywordHooks hooks_arithexpr = {
254 .permit_hintkey = hintkey,
255
256 .piece1 = XPK_ARITHEXPR,
257 .build1 = &build_expr,
258 };
259 static const struct XSParseKeywordHooks hooks_arithexpr_opt = {
260 .permit_hintkey = hintkey,
261
262 .piece1 = XPK_ARITHEXPR_OPT,
263 .build1 = &build_expr,
264 };
265
266 static const struct XSParseKeywordHooks hooks_termexpr = {
267 .permit_hintkey = hintkey,
268
269 .piece1 = XPK_TERMEXPR,
270 .build1 = &build_expr,
271 };
272 static const struct XSParseKeywordHooks hooks_termexpr_opt = {
273 .permit_hintkey = hintkey,
274
275 .piece1 = XPK_TERMEXPR_OPT,
276 .build1 = &build_expr,
277 };
278
279 static const struct XSParseKeywordHooks hooks_prefixedtermexpr_VAR = {
280 .permit_hintkey = hintkey,
281
282 .piece1 = XPK_PREFIXED_TERMEXPR_ENTERLEAVE( XPK_SETUP(&setup_block_VAR) ),
283 .build1 = &build_expr,
284 };
285
286 static const struct XSParseKeywordHooks hooks_listexpr = {
287 .permit_hintkey = hintkey,
288
289 .piece1 = XPK_LISTEXPR,
290 .build1 = &build_list,
291 };
292 static const struct XSParseKeywordHooks hooks_listexpr_opt = {
293 .permit_hintkey = hintkey,
294
295 .piece1 = XPK_LISTEXPR_OPT,
296 .build1 = &build_list,
297 };
298
299 static const struct XSParseKeywordHooks hooks_ident = {
300 .permit_hintkey = hintkey,
301
302 .piece1 = XPK_IDENT,
303 .build1 = &build_constsv,
304 };
305
306 static const struct XSParseKeywordHooks hooks_ident_opt = {
307 .permit_hintkey = hintkey,
308
309 .piece1 = XPK_IDENT_OPT,
310 .build1 = &build_constsv_or_undef,
311 };
312
313 static const struct XSParseKeywordHooks hooks_packagename = {
314 .permit_hintkey = hintkey,
315
316 .piece1 = XPK_PACKAGENAME,
317 .build1 = &build_constsv,
318 };
319
320 static const struct XSParseKeywordHooks hooks_lexvar_name = {
321 .permit_hintkey = hintkey,
322
323 .piece1 = XPK_LEXVARNAME(XPK_LEXVAR_ANY),
324 .build1 = &build_constsv,
325 };
326
327 static const struct XSParseKeywordHooks hooks_lexvar = {
328 .permit_hintkey = hintkey,
329
330 .piece1 = XPK_LEXVAR(XPK_LEXVAR_ANY),
331 .build1 = &build_constpadix,
332 };
333
334 static const struct XSParseKeywordHooks hooks_lexvar_my = {
335 .permit_hintkey = hintkey,
336
337 .piece1 = XPK_LEXVAR_MY(XPK_LEXVAR_ANY),
338 .build1 = &build_constpadix,
339 };
340
341 static const struct XSParseKeywordHooks hooks_lexvar_my_intro = {
342 .flags = XPK_FLAG_BLOCKSCOPE,
343 .permit_hintkey = hintkey,
344
345 .pieces = (const struct XSParseKeywordPieceType []){
346 XPK_LEXVAR_MY(XPK_LEXVAR_ANY),
347 XPK_KEYWORD("in"),
348 XPK_INTRO_MY,
349 XPK_TERMEXPR,
350 0
351 },
352 .build = &build_lexvar_intro,
353 };
354
355 static const struct XSParseKeywordHooks hooks_attrs = {
356 .permit_hintkey = hintkey,
357
358 .pieces = (const struct XSParseKeywordPieceType []){
359 XPK_ATTRIBUTES,
360 {0},
361 },
362 .build = &build_attrs,
363 };
364
365 static const struct XSParseKeywordHooks hooks_vstring = {
366 .permit_hintkey = hintkey,
367
368 .piece1 = XPK_VSTRING,
369 .build1 = &build_constsv,
370 };
371
372 static const struct XSParseKeywordHooks hooks_vstring_opt = {
373 .permit_hintkey = hintkey,
374
375 .piece1 = XPK_VSTRING_OPT,
376 .build1 = &build_constsv_or_undef,
377 };
378
379 static const struct XSParseKeywordHooks hooks_infix_relation = {
380 .permit_hintkey = hintkey,
381
382 .piece1 = XPK_INFIX_RELATION,
383 .build1 = &build_infix_opname,
384 };
385
386 static const struct XSParseKeywordHooks hooks_infix_equality = {
387 .permit_hintkey = hintkey,
388
389 .piece1 = XPK_INFIX_EQUALITY,
390 .build1 = &build_infix_opname,
391 };
392
393 static const struct XSParseKeywordHooks hooks_colon = {
394 .permit_hintkey = hintkey,
395
396 .piece1 = XPK_COLON,
397 .build1 = &build_literal,
398 };
399
400 static const struct XSParseKeywordHooks hooks_str = {
401 .permit_hintkey = hintkey,
402
403 .piece1 = XPK_LITERAL("foo"),
404 .build1 = &build_literal,
405 };
406
407 static const struct XSParseKeywordHooks hooks_kw = {
408 .permit_hintkey = hintkey,
409
410 .piece1 = XPK_KEYWORD("bar"),
411 .build1 = &build_literal,
412 };
413
414 static const struct XSParseKeywordHooks hooks_autosemi = {
415 .permit_hintkey = hintkey,
416
417 .piece1 = XPK_AUTOSEMI,
418 .build1 = &build_literal,
419 };
420
421 static const struct XSParseKeywordHooks hooks_warning = {
422 .permit_hintkey = hintkey,
423
424 .piece1 = XPK_WARNING("A warning here\n"),
425 .build1 = &build_literal,
426 };
427
428 static const struct XSParseKeywordHooks hooks_warning_deprecated = {
429 .permit_hintkey = hintkey,
430
431 .piece1 = XPK_WARNING_DEPRECATED("A deprecated warning here\n"),
432 .build1 = &build_literal,
433 };
434
435 MODULE = t::pieces PACKAGE = t::pieces
436
437 BOOT:
438 boot_xs_parse_keyword(0);
439
440 register_xs_parse_keyword("pieceblock", &hooks_block, NULL);
441 register_xs_parse_keyword("pieceblock_scalar", &hooks_block_scalar, NULL);
442 register_xs_parse_keyword("pieceblock_list", &hooks_block_list, NULL);
443
444 register_xs_parse_keyword("pieceprefixedblock", &hooks_prefixedblock, NULL);
445 register_xs_parse_keyword("pieceprefixedblock_VAR", &hooks_prefixedblock_VAR, "$VAR");
446
447 register_xs_parse_keyword("pieceanonsub", &hooks_anonsub, NULL);
448
449 register_xs_parse_keyword("piecestagedanonsub", &hooks_stagedanonsub, "$VAR");
450
451 register_xs_parse_keyword("piecearithexpr", &hooks_arithexpr, NULL);
452 register_xs_parse_keyword("piecearithexpr_opt", &hooks_arithexpr_opt, NULL);
453 register_xs_parse_keyword("piecetermexpr", &hooks_termexpr, NULL);
454 register_xs_parse_keyword("piecetermexpr_opt", &hooks_termexpr_opt, NULL);
455 register_xs_parse_keyword("piecelistexpr", &hooks_listexpr, NULL);
456 register_xs_parse_keyword("piecelistexpr_opt", &hooks_listexpr_opt, NULL);
457
458 register_xs_parse_keyword("pieceprefixedtermexpr_VAR", &hooks_prefixedtermexpr_VAR, "$VAR");
459
460 register_xs_parse_keyword("pieceident", &hooks_ident, NULL);
461 register_xs_parse_keyword("pieceident_opt", &hooks_ident_opt, NULL);
462 register_xs_parse_keyword("piecepkg", &hooks_packagename, NULL);
463
464 register_xs_parse_keyword("piecelexvarname", &hooks_lexvar_name, NULL);
465 register_xs_parse_keyword("piecelexvar", &hooks_lexvar, NULL);
466 register_xs_parse_keyword("piecelexvarmy", &hooks_lexvar_my, NULL);
467 register_xs_parse_keyword("piecelexvarmyintro", &hooks_lexvar_my_intro, NULL);
468
469 register_xs_parse_keyword("pieceattrs", &hooks_attrs, NULL);
470
471 register_xs_parse_keyword("piecevstring", &hooks_vstring, NULL);
472 register_xs_parse_keyword("piecevstring_opt", &hooks_vstring_opt, NULL);
473
474 register_xs_parse_keyword("pieceinfix", &hooks_infix_relation, NULL);
475 register_xs_parse_keyword("pieceinfixeq", &hooks_infix_equality, NULL);
476
477 register_xs_parse_keyword("piececolon", &hooks_colon, newSVpvs("colon"));
478
479 register_xs_parse_keyword("piecestr", &hooks_str, newSVpvs("foo"));
480 register_xs_parse_keyword("piecekw", &hooks_kw, newSVpvs("bar"));
481
482 register_xs_parse_keyword("pieceautosemi", &hooks_autosemi, newSVpvs("EOS"));
483
484 register_xs_parse_keyword("piecewarning", &hooks_warning, &PL_sv_undef);
485 register_xs_parse_keyword("piecewarndep", &hooks_warning_deprecated, &PL_sv_undef);