t/pieces.xs

filter: Statementstmt 100% 121 / 121 Branchbran 100% 16 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 137 / 137 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« t/line.xs ↑ Summary t/probing.xs »
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 -- leonerd@leonerd.org.uk
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 16 static int build_expr(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
19 {
20 16 OP *expr = arg0->op;
21  
22 16 ▶ if(!expr) {
Branch: 1 branch
BranchTrueFalseTotal
if(!expr) { 2 14 16
23 2 *out = newOP(OP_STUB, 0);
24 2 return KEYWORD_PLUGIN_EXPR;
25 }
26  
27 /* wrap the result in "("...")" parens so we can unit-test how it parsed */
28 14 *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 14 return KEYWORD_PLUGIN_EXPR;
32 }
33  
34 1 static int build_prefixedblock(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
35 {
36 1 PADOFFSET padix = args[0]->padix;
37 1 OP *value = args[1]->op;
38 1 OP *body = args[2]->op;
39  
40 1 OP *padsvop = newOP(OP_PADSV, OPf_MOD|(OPpLVAL_INTRO<<8));
41 1 padsvop->op_targ = padix;
42  
43 1 *out = op_prepend_elem(OP_LINESEQ,
44 newBINOP(OP_SASSIGN, 0, value, padsvop),
45 body);
46  
47 1 return KEYWORD_PLUGIN_EXPR;
48 }
49  
50 2 static int build_anonsub(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
51 {
52 2 *out = newUNOP(OP_REFGEN, 0,
53 newSVOP(OP_ANONCODE, 0, (SV *)arg0->cv));
54 2 return KEYWORD_PLUGIN_EXPR;
55 }
56  
57 7 static int build_list(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
58 {
59 7 OP *list = arg0->op;
60  
61 7 ▶ if(!list) {
Branch: 1 branch
BranchTrueFalseTotal
if(!list) { 1 6 7
62 1 *out = newOP(OP_STUB, 0);
63 1 return KEYWORD_PLUGIN_EXPR;
64 }
65  
66 /* TODO: Consider always doing this? */
67 6 ▶ if(list->op_type != OP_LIST)
Branch: 1 branch
BranchTrueFalseTotal
if(list->op_type != OP_LIST) 4 2 6
68 4 list = newLISTOP(OP_LIST, 0, list, NULL);
69  
70 /* unshift $sep */
71 #if HAVE_PERL_VERSION(5, 22, 0)
72 6 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 6 *out = op_convert_list(OP_JOIN, 0, list);
82  
83 6 return KEYWORD_PLUGIN_EXPR;
84 }
85  
86 6 static int build_constsv(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
87 {
88 6 *out = newSVOP(OP_CONST, 0, arg0->sv);
89 6 return KEYWORD_PLUGIN_EXPR;
90 }
91  
92 4 static int build_constsv_or_undef(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
93 {
94 4 ▶ if(arg0->sv)
Branch: 1 branch
BranchTrueFalseTotal
if(arg0->sv) 2 2 4
95 2 *out = newSVOP(OP_CONST, 0, arg0->sv);
96 else
97 2 *out = newOP(OP_UNDEF, OPf_WANT_SCALAR);
98 4 return KEYWORD_PLUGIN_EXPR;
99 }
100  
101 2 static int build_constpadix(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
102 {
103 2 *out = newSVOP(OP_CONST, 0, newSVuv(arg0->padix));
104 2 return KEYWORD_PLUGIN_EXPR;
105 }
106  
107 8 static int build_literal(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
108 {
109 /* ignore arg0 */
110  
111 8 *out = newSVOP(OP_CONST, 0, SvREFCNT_inc((SV *)hookdata));
112  
113 8 return KEYWORD_PLUGIN_EXPR;
114 }
115  
116 4 static int build_attrs(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
117 {
118 4 int nattrs = args[0]->i;
119  
120 4 SV *retsv = newSV(0);
121 4 sv_setpvs(retsv, "");
122  
123 int argi;
124 11 ▶ for(argi = 0; argi < nattrs; argi++)
Branch: 1 branch
BranchTrueFalseTotal
for(argi = 0; argi < nattrs; argi++) 7 4 11
125 16 ▶ sv_catpvf(retsv, ":%s(%s)",
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvf(retsv, ":%s(%s)", 2 5 7
126 SvPV_nolen(args[argi+1]->attr.name),
127 SvPOK(args[argi+1]->attr.value) ? SvPV_nolen(args[argi+1]->attr.value) : "");
128  
129 4 *out = newSVOP(OP_CONST, 0, retsv);
130 4 return KEYWORD_PLUGIN_EXPR;
131 }
132  
133 3 static int build_infix_opname(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
134 {
135 3 const char *opname = PL_op_name[arg0->infix->opcode];
136 3 *out = newSVOP(OP_CONST, 0, newSVpvn(opname, strlen(opname)));
137 3 return KEYWORD_PLUGIN_EXPR;
138 }
139  
140 1 static int build_lexvar_intro(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata)
141 {
142 1 PADOFFSET padix = args[0]->padix;
143 1 OP *expr = args[1]->op;
144  
145 1 OP *varop = newOP(OP_PADSV, OPf_MOD|OPf_REF | (OPpLVAL_INTRO << 8));
146 1 varop->op_targ = padix;
147  
148 1 OP *assignop = newASSIGNOP(OPf_WANT_VOID, varop, 0, newSVOP(OP_CONST, 0, newSViv(1)));
149  
150 1 *out = newLISTOP(OP_LINESEQ, 0, assignop, expr);
151 1 return KEYWORD_PLUGIN_EXPR;
152 }
153  
154 3 static void setup_block_VAR(pTHX_ void *hookdata)
155 {
156 3 char *varname = hookdata;
157 3 PADOFFSET padix = pad_add_name_pvn(varname, strlen(varname), 0, NULL, NULL);
158 3 intro_my();
159  
160 3 sv_setpvs(PAD_SVl(padix), "Hello");
161 3 }
162  
163 4 static void callback_catpv_stages(pTHX_ const char *pv)
164 {
165 4 SV *sv = get_sv("main::STAGES", GV_ADD);
166 4 ▶ if(!SvPOK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if(!SvPOK(sv)) 1 3 4
167 1 sv_setpvs(sv, "");
168  
169 4 ▶ if(SvCUR(sv))
Branch: 1 branch
BranchTrueFalseTotal
if(SvCUR(sv)) 3 1 4
170 3 sv_catpvs(sv, ",");
171  
172 4 sv_catpv(sv, pv);
173 4 }
174  
175 1 static void callback_PREPARE(pTHX_ void *hookdata)
176 {
177 1 callback_catpv_stages(aTHX_ "PREPARE");
178 1 }
179  
180 1 static void callback_START(pTHX_ void *hookdata)
181 {
182 1 callback_catpv_stages(aTHX_ "START");
183 1 }
184  
185 1 static OP *callback_END(pTHX_ OP *o, void *hookdata)
186 {
187 1 callback_catpv_stages(aTHX_ "END");
188 1 return o;
189 }
190  
191 1 static OP *callback_WRAP(pTHX_ OP *o, void *hookdata)
192 {
193 1 callback_catpv_stages(aTHX_ "WRAP");
194 1 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 12 boot_xs_parse_keyword(0);
439  
440 12 register_xs_parse_keyword("pieceblock", &hooks_block, NULL);
441 12 register_xs_parse_keyword("pieceblock_scalar", &hooks_block_scalar, NULL);
442 12 register_xs_parse_keyword("pieceblock_list", &hooks_block_list, NULL);
443  
444 12 register_xs_parse_keyword("pieceprefixedblock", &hooks_prefixedblock, NULL);
445 12 register_xs_parse_keyword("pieceprefixedblock_VAR", &hooks_prefixedblock_VAR, "$VAR");
446  
447 12 register_xs_parse_keyword("pieceanonsub", &hooks_anonsub, NULL);
448  
449 12 register_xs_parse_keyword("piecestagedanonsub", &hooks_stagedanonsub, "$VAR");
450  
451 12 register_xs_parse_keyword("piecearithexpr", &hooks_arithexpr, NULL);
452 12 register_xs_parse_keyword("piecearithexpr_opt", &hooks_arithexpr_opt, NULL);
453 12 register_xs_parse_keyword("piecetermexpr", &hooks_termexpr, NULL);
454 12 register_xs_parse_keyword("piecetermexpr_opt", &hooks_termexpr_opt, NULL);
455 12 register_xs_parse_keyword("piecelistexpr", &hooks_listexpr, NULL);
456 12 register_xs_parse_keyword("piecelistexpr_opt", &hooks_listexpr_opt, NULL);
457  
458 12 register_xs_parse_keyword("pieceprefixedtermexpr_VAR", &hooks_prefixedtermexpr_VAR, "$VAR");
459  
460 12 register_xs_parse_keyword("pieceident", &hooks_ident, NULL);
461 12 register_xs_parse_keyword("pieceident_opt", &hooks_ident_opt, NULL);
462 12 register_xs_parse_keyword("piecepkg", &hooks_packagename, NULL);
463  
464 12 register_xs_parse_keyword("piecelexvarname", &hooks_lexvar_name, NULL);
465 12 register_xs_parse_keyword("piecelexvar", &hooks_lexvar, NULL);
466 12 register_xs_parse_keyword("piecelexvarmy", &hooks_lexvar_my, NULL);
467 12 register_xs_parse_keyword("piecelexvarmyintro", &hooks_lexvar_my_intro, NULL);
468  
469 12 register_xs_parse_keyword("pieceattrs", &hooks_attrs, NULL);
470  
471 12 register_xs_parse_keyword("piecevstring", &hooks_vstring, NULL);
472 12 register_xs_parse_keyword("piecevstring_opt", &hooks_vstring_opt, NULL);
473  
474 12 register_xs_parse_keyword("pieceinfix", &hooks_infix_relation, NULL);
475 12 register_xs_parse_keyword("pieceinfixeq", &hooks_infix_equality, NULL);
476  
477 12 register_xs_parse_keyword("piececolon", &hooks_colon, newSVpvs("colon"));
478  
479 12 register_xs_parse_keyword("piecestr", &hooks_str, newSVpvs("foo"));
480 12 register_xs_parse_keyword("piecekw", &hooks_kw, newSVpvs("bar"));
481  
482 12 register_xs_parse_keyword("pieceautosemi", &hooks_autosemi, newSVpvs("EOS"));
483  
484 12 register_xs_parse_keyword("piecewarning", &hooks_warning, &PL_sv_undef);
485 12 register_xs_parse_keyword("piecewarndep", &hooks_warning_deprecated, &PL_sv_undef);
« t/line.xs ↑ Summary t/probing.xs »