src/keyword.c

filter: Statementstmt 87.1% 493 / 566 Branchbran 70.2% 229 / 326 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.9% 722 / 892 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« hax/force_list_keeping_pushmark.c.inc ↑ Summary t/infix.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-2023 -- leonerd@leonerd.org.uk
5 */
6  
7 #define PERL_NO_GET_CONTEXT
8  
9 #include "EXTERN.h"
10 #include "perl.h"
11 #include "XSUB.h"
12  
13 /* need core perl's keywords.h to get KEY_my */
14 #include "keywords.h"
15  
16 #include "XSParseKeyword.h"
17 #include "XSParseInfix.h"
18  
19 #include "keyword.h"
20 #include "infix.h"
21  
22 #include "perl-backcompat.c.inc"
23  
24 #ifndef wrap_keyword_plugin
25 # include "wrap_keyword_plugin.c.inc"
26 #endif
27  
28 #include "lexer-additions.c.inc"
29  
30 /* yycroak() is a long function and hard to emulate or copy-paste for our
31 * purposes; we'll reïmplement a smaller version of it
32 *
33 * ours will croak instead of warn
34 */
35  
36 #define LEX_IGNORE_UTF8_HINTS 0x00000002
37  
38 #define PL_linestr (PL_parser->linestr)
39  
40 #ifdef USE_UTF8_SCRIPTS
41 # define UTF cBOOL(!IN_BYTES)
42 #elif HAVE_PERL_VERSION(5, 16, 0)
43 # define UTF cBOOL((PL_linestr && DO_UTF8(PL_linestr)) || ( !(PL_parser->lex_flags & LEX_IGNORE_UTF8_HINTS) && (PL_hints & HINT_UTF8)))
44 #else
45 # define UTF cBOOL((PL_linestr && DO_UTF8(PL_linestr)) || (PL_hints & HINT_UTF8))
46 #endif
47  
48 #if HAVE_PERL_VERSION(5, 20, 0)
49 # define HAVE_UTF8f
50 #endif
51  
52 #define yycroak(s) S_yycroak(aTHX_ s)
53 4 static void S_yycroak(pTHX_ const char *s)
54 {
55 4 SV *message = sv_2mortal(newSVpvs_flags("", 0));
56  
57 4 char *context = PL_parser->oldbufptr;
58 4 STRLEN contlen = PL_parser->bufptr - PL_parser->oldbufptr;
59  
60 4 ▶ sv_catpvf(message, "%s at %s line %" IVdf,
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvf(message, "%s at %s line %" IVdf, 4 0 4
61 s, OutCopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
62  
63 4 ▶ if(context)
Branch: 1 branch
BranchTrueFalseTotal
if(context) 4 0 4
64 #ifdef HAVE_UTF8f
65 4 ▶ sv_catpvf(message, ", near \"%" UTF8f "\"", UTF8fARG(UTF, contlen, context));
Branch: 5 branches
BranchTrueFalseTotal
sv_catpvf(message, ", near \"%" UTF8f "\"", UTF8fARG(UTF, contlen, context)); 4 0 4
sv_catpvf(message, ", near \"%" UTF8f "\"", UTF8fARG(UTF, contlen, context)); 0 4 4
sv_catpvf(message, ", near \"%" UTF8f "\"", UTF8fARG(UTF, contlen, context)); 0 0 0
sv_catpvf(message, ", near \"%" UTF8f "\"", UTF8fARG(UTF, contlen, context)); 4 0 4
sv_catpvf(message, ", near \"%" UTF8f "\"", UTF8fARG(UTF, contlen, context)); 4 0 4
66 #else
67 sv_catpvf(message, ", near \"%" SVf "\"", SVfARG(newSVpvn_flags(context, contlen, SVs_TEMP | (UTF ? SVf_UTF8 : 0))));
68 #endif
69  
70 4 sv_catpvf(message, "\n");
71  
72 4 PL_parser->error_count++;
73 4 croak_sv(message);
74 }
75  
76 #define yycroakf(fmt, ...) yycroak(Perl_form(aTHX_ fmt, __VA_ARGS__))
77  
78 #define lex_expect_unichar(c) MY_lex_expect_unichar(aTHX_ c)
79 24 void MY_lex_expect_unichar(pTHX_ int c)
80 {
81 24 ▶ if(lex_peek_unichar(0) != c)
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != c) 0 24 24
82 /* TODO: A slightly different message if c == '\'' */
83 0 yycroakf("Expected '%c'", c);
84  
85 24 lex_read_unichar(0);
86 24 }
87  
88 #define CHECK_PARSEFAIL \
89 if(PL_parser->error_count) \
90 croak("parse failed--compilation aborted")
91  
92 /* TODO: Only ASCII */
93 #define lex_probe_str(s, b) MY_lex_probe_str(aTHX_ s, b)
94 44 STRLEN MY_lex_probe_str(pTHX_ const char *s, bool boundarycheck)
95 {
96 STRLEN i;
97 127 ▶ for(i = 0; s[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; s[i]; i++) { 103 24 127
98 103 ▶ if(s[i] != PL_parser->bufptr[i])
Branch: 1 branch
BranchTrueFalseTotal
if(s[i] != PL_parser->bufptr[i]) 20 83 103
99 20 return 0;
100 }
101  
102 24 ▶ if(boundarycheck && isALNUM(PL_parser->bufptr[i]))
Branch: 2 branches
BranchTrueFalseTotal
if(boundarycheck && isALNUM(PL_parser->bufptr[i])) 2 22 24
if(boundarycheck && isALNUM(PL_parser->bufptr[i])) 0 2 2
103 0 return 0;
104  
105 24 return i;
106 }
107  
108 #define lex_expect_str(s, b) MY_lex_expect_str(aTHX_ s, b)
109 8 void MY_lex_expect_str(pTHX_ const char *s, bool boundarycheck)
110 {
111 8 STRLEN len = lex_probe_str(s, boundarycheck);
112 8 ▶ if(!len)
Branch: 1 branch
BranchTrueFalseTotal
if(!len) 0 8 8
113 0 yycroakf("Expected \"%s\"", s);
114  
115 8 lex_read_to(PL_parser->bufptr + len);
116 8 }
117  
118 #define parse_autosemi() MY_parse_autosemi(aTHX)
119 4 void MY_parse_autosemi(pTHX)
120 {
121 4 int c = lex_peek_unichar(0);
122 4 ▶ if(c == ';')
Branch: 1 branch
BranchTrueFalseTotal
if(c == ';') 2 2 4
123 2 lex_read_unichar(0);
124 2 ▶ else if(!c || c == '}')
Branch: 2 branches
BranchTrueFalseTotal
else if(!c || c == '}') 2 0 2
else if(!c || c == '}') 0 2 2
125 ; /* all is good */
126 else
127 0 yycroak("Expected: ';' or end of block");
128 4 }
129  
130 struct Registration;
131 struct Registration {
132 struct Registration *next;
133 const char *kwname;
134 STRLEN kwlen;
135  
136 int apiver;
137 const struct XSParseKeywordHooks *hooks;
138 void *hookdata;
139  
140 STRLEN permit_hintkey_len;
141 };
142  
143 /* version 1's struct did not have the line on it */
144 typedef struct
145 {
146 union {
147 OP *op;
148 CV *cv;
149 SV *sv;
150 int i;
151 struct { SV *name; SV *value; } attr;
152 PADOFFSET padix;
153 };
154 } XSParseKeywordPiece_v1;
155  
156 static bool probe_piece(pTHX_ SV *argsv, size_t *argidx, const struct XSParseKeywordPieceType *piece, void *hookdata);
157 static void parse_piece(pTHX_ SV *argsv, size_t *argidx, const struct XSParseKeywordPieceType *piece, void *hookdata);
158 static void parse_pieces(pTHX_ SV *argsv, size_t *argidx, const struct XSParseKeywordPieceType *pieces, void *hookdata);
159  
160 81 static bool probe_piece(pTHX_ SV *argsv, size_t *argidx, const struct XSParseKeywordPieceType *piece, void *hookdata)
161 {
162 81 int argi = *argidx;
163  
164 81 ▶ if(argi >= (SvLEN(argsv) / sizeof(XSParseKeywordPiece)))
Branch: 1 branch
BranchTrueFalseTotal
if(argi >= (SvLEN(argsv) / sizeof(XSParseKeywordPiece))) 1 80 81
165 1 ▶ SvGROW(argsv, SvLEN(argsv) * 2);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(argsv, SvLEN(argsv) * 2); 1 0 1
SvGROW(argsv, SvLEN(argsv) * 2); 1 0 1
166  
167 #define THISARG ((XSParseKeywordPiece *)SvPVX(argsv))[argi]
168  
169 81 THISARG.line =
170 #if HAVE_PERL_VERSION(5, 20, 0)
171 /* on perl 5.20 onwards, CopLINE(PL_curcop) is only set at runtime; during
172 * parse the parser stores the line number directly */
173 81 ▶ (PL_parser->preambling != NOLINE) ? PL_parser->preambling :
Branch: 1 branch
BranchTrueFalseTotal
(PL_parser->preambling != NOLINE) ? PL_parser->preambling : 0 81 81
174 #endif
175 81 CopLINE(PL_curcop);
176  
177 81 bool is_special = !!(piece->type & XPK_TYPEFLAG_SPECIAL);
178  
179 81 U32 type = piece->type & 0xFFFF;
180  
181 81 switch(type) {
182 9 case XS_PARSE_KEYWORD_LITERALCHAR:
183 9 ▶ if(lex_peek_unichar(0) != piece->u.c)
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != piece->u.c) 5 4 9
184 5 return FALSE;
185  
186 4 lex_read_unichar(0);
187 4 lex_read_space(0);
188 4 return TRUE;
189  
190 36 case XS_PARSE_KEYWORD_LITERALSTR:
191 {
192 36 STRLEN len = lex_probe_str(piece->u.str, is_special);
193 36 ▶ if(!len)
Branch: 1 branch
BranchTrueFalseTotal
if(!len) 20 16 36
194 20 return FALSE;
195  
196 16 lex_read_to(PL_parser->bufptr + len);
197 16 lex_read_space(0);
198 16 return TRUE;
199 }
200  
201 0 case XS_PARSE_KEYWORD_FAILURE:
202 0 yycroak(piece->u.str);
203 0 NOT_REACHED;
204  
205 4 case XS_PARSE_KEYWORD_BLOCK:
206 4 ▶ if(lex_peek_unichar(0) != '{')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != '{') 2 2 4
207 2 return FALSE;
208  
209 2 parse_piece(aTHX_ argsv, argidx, piece, hookdata);
210 2 return TRUE;
211  
212 5 case XS_PARSE_KEYWORD_IDENT:
213 5 THISARG.sv = lex_scan_ident();
214 5 ▶ if(!THISARG.sv)
Branch: 1 branch
BranchTrueFalseTotal
if(!THISARG.sv) 2 3 5
215 2 return FALSE;
216 3 (*argidx)++;
217 3 return TRUE;
218  
219 2 case XS_PARSE_KEYWORD_PACKAGENAME:
220 2 THISARG.sv = lex_scan_packagename();
221 2 ▶ if(!THISARG.sv)
Branch: 1 branch
BranchTrueFalseTotal
if(!THISARG.sv) 1 1 2
222 1 return FALSE;
223 1 (*argidx)++;
224 1 return TRUE;
225  
226 2 case XS_PARSE_KEYWORD_VSTRING:
227 2 THISARG.sv = lex_scan_version(PARSE_OPTIONAL);
228 2 ▶ if(!THISARG.sv)
Branch: 1 branch
BranchTrueFalseTotal
if(!THISARG.sv) 1 1 2
229 1 return FALSE;
230  
231 1 (*argidx)++;
232 1 return TRUE;
233  
234 0 case XS_PARSE_KEYWORD_INFIX:
235 {
236 0 ▶ if(!XSParseInfix_parse(aTHX_ piece->u.c, &THISARG.infix))
Branch: 1 branch
BranchTrueFalseTotal
if(!XSParseInfix_parse(aTHX_ piece->u.c, &THISARG.infix)) 0 0 0
237 0 return FALSE;
238 0 (*argidx)++;
239 0 return TRUE;
240 }
241  
242 0 case XS_PARSE_KEYWORD_SETUP:
243 0 croak("ARGH probe_piece() should never see XS_PARSE_KEYWORD_SETUP!");
244  
245 0 case XS_PARSE_KEYWORD_SEQUENCE:
246 {
247 0 const struct XSParseKeywordPieceType *pieces = piece->u.pieces;
248  
249 0 ▶ if(!probe_piece(aTHX_ argsv, argidx, pieces++, hookdata))
Branch: 1 branch
BranchTrueFalseTotal
if(!probe_piece(aTHX_ argsv, argidx, pieces++, hookdata)) 0 0 0
250 0 return FALSE;
251  
252 0 lex_read_space(0);
253  
254 0 parse_pieces(aTHX_ argsv, argidx, pieces, hookdata);
255 0 return TRUE;
256 }
257  
258 7 case XS_PARSE_KEYWORD_CHOICE:
259 {
260 7 const struct XSParseKeywordPieceType *choices = piece->u.pieces;
261 7 THISARG.i = 0;
262 7 (*argidx)++; /* tentative */
263 19 ▶ while(choices->type) {
Branch: 1 branch
BranchTrueFalseTotal
while(choices->type) { 17 2 19
264 17 ▶ if(probe_piece(aTHX_ argsv, argidx, choices + 0, hookdata)) {
Branch: 1 branch
BranchTrueFalseTotal
if(probe_piece(aTHX_ argsv, argidx, choices + 0, hookdata)) { 5 12 17
265 5 return TRUE;
266 }
267 12 choices++;
268 12 THISARG.i++;
269 }
270 2 (*argidx)--;
271 2 return FALSE;
272 }
273  
274 5 case XS_PARSE_KEYWORD_TAGGEDCHOICE:
275 {
276 5 const struct XSParseKeywordPieceType *choices = piece->u.pieces;
277 5 (*argidx)++; /* tentative */
278 10 ▶ while(choices->type) {
Branch: 1 branch
BranchTrueFalseTotal
while(choices->type) { 9 1 10
279 9 ▶ if(probe_piece(aTHX_ argsv, argidx, choices + 0, hookdata)) {
Branch: 1 branch
BranchTrueFalseTotal
if(probe_piece(aTHX_ argsv, argidx, choices + 0, hookdata)) { 4 5 9
280 4 THISARG.i = choices[1].type;
281 4 return TRUE;
282 }
283 5 choices += 2;
284 }
285 1 (*argidx)--;
286 1 return FALSE;
287 }
288  
289 3 case XS_PARSE_KEYWORD_SEPARATEDLIST:
290 {
291 3 const struct XSParseKeywordPieceType *pieces = piece->u.pieces;
292 3 (*argidx)++; /* tentative */
293 3 ▶ if(!probe_piece(aTHX_ argsv, argidx, pieces + 1, hookdata)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!probe_piece(aTHX_ argsv, argidx, pieces + 1, hookdata)) { 1 2 3
294 1 (*argidx)--;
295 1 return FALSE;
296 }
297 /* we're now committed */
298 2 THISARG.i = 1;
299 2 lex_read_space(0);
300 2 ▶ if(pieces[2].type)
Branch: 1 branch
BranchTrueFalseTotal
if(pieces[2].type) 0 2 2
301 0 parse_pieces(aTHX_ argsv, argidx, pieces + 2, hookdata);
302  
303 2 lex_read_space(0);
304 2 ▶ if(!probe_piece(aTHX_ argsv, argidx, pieces + 0, hookdata))
Branch: 1 branch
BranchTrueFalseTotal
if(!probe_piece(aTHX_ argsv, argidx, pieces + 0, hookdata)) 1 1 2
305 1 return TRUE;
306  
307 while(1) {
308 1 parse_pieces(aTHX_ argsv, argidx, pieces + 1, hookdata);
309 1 THISARG.i++;
310  
311 1 lex_read_space(0);
312  
313 1 ▶ if(!probe_piece(aTHX_ argsv, argidx, pieces + 0, hookdata))
Branch: 1 branch
BranchTrueFalseTotal
if(!probe_piece(aTHX_ argsv, argidx, pieces + 0, hookdata)) 1 0 1
314 1 break;
315 }
316 1 return TRUE;
317 }
318  
319 2 case XS_PARSE_KEYWORD_PARENS:
320 2 ▶ if(piece->type & XPK_TYPEFLAG_MAYBEPARENS)
Branch: 1 branch
BranchTrueFalseTotal
if(piece->type & XPK_TYPEFLAG_MAYBEPARENS) 0 2 2
321 0 croak("TODO: probe_piece on type=PARENS+MAYBEPARENS");
322  
323 2 ▶ if(lex_peek_unichar(0) != '(')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != '(') 1 1 2
324 1 return FALSE;
325  
326 1 parse_piece(aTHX_ argsv, argidx, piece, hookdata);
327 1 return TRUE;
328  
329 2 case XS_PARSE_KEYWORD_BRACKETS:
330 2 ▶ if(lex_peek_unichar(0) != '[')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != '[') 1 1 2
331 1 return FALSE;
332  
333 1 parse_piece(aTHX_ argsv, argidx, piece, hookdata);
334 1 return TRUE;
335  
336 2 case XS_PARSE_KEYWORD_BRACES:
337 2 ▶ if(lex_peek_unichar(0) != '{')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != '{') 1 1 2
338 1 return FALSE;
339  
340 1 parse_piece(aTHX_ argsv, argidx, piece, hookdata);
341 1 return TRUE;
342  
343 2 case XS_PARSE_KEYWORD_CHEVRONS:
344 2 ▶ if(lex_peek_unichar(0) != '<')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != '<') 1 1 2
345 1 return FALSE;
346  
347 1 parse_piece(aTHX_ argsv, argidx, piece, hookdata);
348 1 return TRUE;
349 }
350  
351 0 croak("TODO: probe_piece on type=%d\n", type);
352 }
353  
354 3 static void parse_prefix_pieces(pTHX_ SV *argsv, size_t *argidx, const struct XSParseKeywordPieceType *pieces, void *hookdata)
355 {
356 9 ▶ while(pieces->type) {
Branch: 1 branch
BranchTrueFalseTotal
while(pieces->type) { 6 3 9
357 6 ▶ if(pieces->type == XS_PARSE_KEYWORD_SETUP)
Branch: 1 branch
BranchTrueFalseTotal
if(pieces->type == XS_PARSE_KEYWORD_SETUP) 2 4 6
358 2 (pieces->u.callback)(aTHX_ hookdata);
359 else {
360 4 parse_piece(aTHX_ argsv, argidx, pieces, hookdata);
361 4 lex_read_space(0);
362 }
363  
364 6 pieces++;
365 }
366  
367 3 intro_my(); /* in case any of the pieces was XPK_LEXVAR_MY */
368 3 }
369  
370 143 static void parse_piece(pTHX_ SV *argsv, size_t *argidx, const struct XSParseKeywordPieceType *piece, void *hookdata)
371 {
372 143 int argi = *argidx;
373  
374 #define CHECK_GROW_ARGSV \
375 do { \
376 if(argi >= (SvLEN(argsv) / sizeof(XSParseKeywordPiece))) \
377 SvGROW(argsv, SvLEN(argsv) * 2); \
378 } while(0)
379  
380 #define THISARG ((XSParseKeywordPiece *)SvPVX(argsv))[argi]
381  
382 143 ▶ CHECK_GROW_ARGSV;
Branch: 3 branches
BranchTrueFalseTotal
CHECK_GROW_ARGSV; 0 143 143
CHECK_GROW_ARGSV; 0 0 0
CHECK_GROW_ARGSV; 0 0 0
383  
384 143 THISARG.line =
385 #if HAVE_PERL_VERSION(5, 20, 0)
386 /* on perl 5.20 onwards, CopLINE(PL_curcop) is only set at runtime; during
387 * parse the parser stores the line number directly */
388 143 ▶ (PL_parser->preambling != NOLINE) ? PL_parser->preambling :
Branch: 1 branch
BranchTrueFalseTotal
(PL_parser->preambling != NOLINE) ? PL_parser->preambling : 0 143 143
389 #endif
390 143 CopLINE(PL_curcop);
391  
392 143 bool is_optional = !!(piece->type & XPK_TYPEFLAG_OPT);
393 143 bool is_special = !!(piece->type & XPK_TYPEFLAG_SPECIAL);
394 143 U8 want = 0;
395 143 switch(piece->type & (3 << 18)) {
396 0 case XPK_TYPEFLAG_G_VOID: want = G_VOID; break;
397 2 case XPK_TYPEFLAG_G_SCALAR: want = G_SCALAR; break;
398 1 case XPK_TYPEFLAG_G_LIST: want = G_LIST; break;
399 }
400 143 bool is_enterleave = !!(piece->type & XPK_TYPEFLAG_ENTERLEAVE);
401  
402 143 U32 optflag = is_optional ? PARSE_OPTIONAL : 0;
403  
404 143 U32 type = piece->type & 0xFFFF;
405  
406 143 switch(type) {
407 0 case 0:
408 0 return;
409  
410 3 case XS_PARSE_KEYWORD_LITERALCHAR:
411 3 lex_expect_unichar(piece->u.c);
412 3 return;
413  
414 8 case XS_PARSE_KEYWORD_LITERALSTR:
415 8 lex_expect_str(piece->u.str, is_special);
416 8 return;
417  
418 2 case XS_PARSE_KEYWORD_AUTOSEMI:
419 2 parse_autosemi();
420 2 return;
421  
422 3 case XS_PARSE_KEYWORD_WARNING:
423 {
424 3 int warnbit = piece->type >> 24;
425 3 ▶ if(warnbit && !ckWARN(warnbit))
Branch: 2 branches
BranchTrueFalseTotal
if(warnbit && !ckWARN(warnbit)) 2 1 3
if(warnbit && !ckWARN(warnbit)) 1 1 2
426 1 return;
427 2 warn("%s", piece->u.str);
428 2 return;
429 }
430  
431 0 case XS_PARSE_KEYWORD_FAILURE:
432 0 yycroak(piece->u.str);
433 0 NOT_REACHED;
434  
435 8 case XS_PARSE_KEYWORD_BLOCK:
436 {
437 8 ▶ if(is_enterleave)
Branch: 1 branch
BranchTrueFalseTotal
if(is_enterleave) 0 8 8
438 0 ENTER;
439  
440 8 I32 save_ix = block_start(1);
441  
442 8 ▶ if(piece->u.pieces) {
Branch: 1 branch
BranchTrueFalseTotal
if(piece->u.pieces) { 2 6 8
443 2 parse_prefix_pieces(aTHX_ argsv, argidx, piece->u.pieces, hookdata);
444  
445 2 ▶ if(*argidx > argi) {
Branch: 1 branch
BranchTrueFalseTotal
if(*argidx > argi) { 1 1 2
446 1 argi = *argidx;
447 1 ▶ CHECK_GROW_ARGSV;
Branch: 3 branches
BranchTrueFalseTotal
CHECK_GROW_ARGSV; 0 1 1
CHECK_GROW_ARGSV; 0 0 0
CHECK_GROW_ARGSV; 0 0 0
448 }
449 }
450  
451 /* TODO: Can we name the syntax keyword here to make a better message? */
452 8 ▶ if(lex_peek_unichar(0) != '{')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != '{') 0 8 8
453 0 yycroak("Expected a block");
454  
455 8 OP *body = parse_block(0);
456 8 ▶ CHECK_PARSEFAIL;
Branch: 1 branch
BranchTrueFalseTotal
CHECK_PARSEFAIL; 0 8 8
457  
458 8 THISARG.op = block_end(save_ix, body);
459  
460 8 ▶ if(is_special)
Branch: 1 branch
BranchTrueFalseTotal
if(is_special) 2 6 8
461 2 THISARG.op = op_scope(THISARG.op);
462  
463 8 ▶ if(want)
Branch: 1 branch
BranchTrueFalseTotal
if(want) 2 6 8
464 2 THISARG.op = op_contextualize(THISARG.op, want);
465  
466 8 (*argidx)++;
467  
468 8 ▶ if(is_enterleave)
Branch: 1 branch
BranchTrueFalseTotal
if(is_enterleave) 0 8 8
469 0 LEAVE;
470  
471 8 return;
472 }
473  
474 2 case XS_PARSE_KEYWORD_ANONSUB:
475 {
476 2 const struct XSParseKeywordPieceType *stages = piece->u.pieces;
477  
478 3 ▶ while(stages && stages->type == XS_PARSE_KEYWORD_ANONSUB_PREPARE) {
Branch: 2 branches
BranchTrueFalseTotal
while(stages && stages->type == XS_PARSE_KEYWORD_ANONSUB_PREPARE) { 2 1 3
while(stages && stages->type == XS_PARSE_KEYWORD_ANONSUB_PREPARE) { 1 1 2
479 1 (*stages->u.callback)(aTHX_ hookdata);
480 1 stages++;
481 }
482  
483 2 I32 floor_ix = start_subparse(FALSE, CVf_ANON);
484 2 SAVEFREESV(PL_compcv);
485  
486 2 I32 save_ix = block_start(0);
487  
488 4 ▶ while(stages && stages->type == XS_PARSE_KEYWORD_ANONSUB_START) {
Branch: 2 branches
BranchTrueFalseTotal
while(stages && stages->type == XS_PARSE_KEYWORD_ANONSUB_START) { 3 1 4
while(stages && stages->type == XS_PARSE_KEYWORD_ANONSUB_START) { 2 1 3
489 2 (*stages->u.callback)(aTHX_ hookdata);
490 2 stages++;
491 }
492  
493 2 OP *body = parse_block(0);
494 2 ▶ CHECK_PARSEFAIL;
Branch: 1 branch
BranchTrueFalseTotal
CHECK_PARSEFAIL; 0 2 2
495  
496 3 ▶ while(stages && stages->type == XS_PARSE_KEYWORD_ANONSUB_END) {
Branch: 2 branches
BranchTrueFalseTotal
while(stages && stages->type == XS_PARSE_KEYWORD_ANONSUB_END) { 2 1 3
while(stages && stages->type == XS_PARSE_KEYWORD_ANONSUB_END) { 1 1 2
497 1 body = (*stages->u.op_wrap_callback)(aTHX_ body, hookdata);
498 1 stages++;
499 }
500  
501 2 SvREFCNT_inc(PL_compcv);
502 2 body = block_end(save_ix, body);
503  
504 3 ▶ while(stages && stages->type == XS_PARSE_KEYWORD_ANONSUB_WRAP) {
Branch: 2 branches
BranchTrueFalseTotal
while(stages && stages->type == XS_PARSE_KEYWORD_ANONSUB_WRAP) { 2 1 3
while(stages && stages->type == XS_PARSE_KEYWORD_ANONSUB_WRAP) { 1 1 2
505 1 body = (*stages->u.op_wrap_callback)(aTHX_ body, hookdata);
506 1 stages++;
507 }
508  
509 2 THISARG.cv = newATTRSUB(floor_ix, NULL, NULL, NULL, body);
510 2 (*argidx)++;
511 2 return;
512 }
513  
514 31 case XS_PARSE_KEYWORD_ARITHEXPR:
515 case XS_PARSE_KEYWORD_TERMEXPR:
516 case XS_PARSE_KEYWORD_LISTEXPR:
517 {
518 31 ▶ if(is_enterleave)
Branch: 1 branch
BranchTrueFalseTotal
if(is_enterleave) 1 30 31
519 1 ENTER;
520  
521 31 ▶ if(piece->u.pieces) {
Branch: 1 branch
BranchTrueFalseTotal
if(piece->u.pieces) { 1 30 31
522 1 parse_prefix_pieces(aTHX_ argsv, argidx, piece->u.pieces, hookdata);
523  
524 1 ▶ if(*argidx > argi) {
Branch: 1 branch
BranchTrueFalseTotal
if(*argidx > argi) { 0 1 1
525 0 argi = *argidx;
526 0 ▶ CHECK_GROW_ARGSV;
Branch: 3 branches
BranchTrueFalseTotal
CHECK_GROW_ARGSV; 0 0 0
CHECK_GROW_ARGSV; 0 0 0
CHECK_GROW_ARGSV; 0 0 0
527 }
528 }
529  
530 /* TODO: This auto-parens behaviour ought to be tuneable, depend on how
531 * many args, open at i=0 and close at i=MAX, etc...
532 */
533 31 ▶ if(type != XS_PARSE_KEYWORD_LISTEXPR && lex_peek_unichar(0) == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if(type != XS_PARSE_KEYWORD_LISTEXPR && lex_peek_unichar(0) == '(') { 26 5 31
if(type != XS_PARSE_KEYWORD_LISTEXPR && lex_peek_unichar(0) == '(') { 3 23 26
534 /* consume a fullexpr and stop at the close paren */
535 3 lex_read_unichar(0);
536  
537 3 lex_read_space(0);
538  
539 3 ▶ if(lex_peek_unichar(0) == ')')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == ')') 1 2 3
540 1 THISARG.op = newOP(OP_STUB, 0);
541 else {
542 2 THISARG.op = parse_fullexpr(optflag);
543 2 ▶ CHECK_PARSEFAIL;
Branch: 1 branch
BranchTrueFalseTotal
CHECK_PARSEFAIL; 0 2 2
544  
545 2 lex_read_space(0);
546 }
547  
548 3 lex_expect_unichar(')');
549 }
550 else {
551 28 switch(type) {
552 5 case XS_PARSE_KEYWORD_ARITHEXPR:
553 5 THISARG.op = parse_arithexpr(optflag);
554 5 break;
555 18 case XS_PARSE_KEYWORD_TERMEXPR:
556 18 THISARG.op = parse_termexpr(optflag);
557 18 break;
558 5 case XS_PARSE_KEYWORD_LISTEXPR:
559 5 THISARG.op = parse_listexpr(optflag);
560 5 break;
561 }
562 28 ▶ CHECK_PARSEFAIL;
Branch: 1 branch
BranchTrueFalseTotal
CHECK_PARSEFAIL; 0 28 28
563 }
564  
565 31 ▶ if(want && THISARG.op)
Branch: 2 branches
BranchTrueFalseTotal
if(want && THISARG.op) 1 30 31
if(want && THISARG.op) 1 0 1
566 1 THISARG.op = op_contextualize(THISARG.op, want);
567  
568 31 (*argidx)++;
569  
570 31 ▶ if(is_enterleave)
Branch: 1 branch
BranchTrueFalseTotal
if(is_enterleave) 1 30 31
571 1 LEAVE;
572  
573 31 return;
574 }
575  
576 12 case XS_PARSE_KEYWORD_IDENT:
577 12 THISARG.sv = lex_scan_ident();
578 12 ▶ if(!THISARG.sv && !is_optional)
Branch: 2 branches
BranchTrueFalseTotal
if(!THISARG.sv && !is_optional) 2 10 12
if(!THISARG.sv && !is_optional) 1 1 2
579 1 yycroak("Expected an identifier");
580 11 (*argidx)++;
581 11 return;
582  
583 2 case XS_PARSE_KEYWORD_PACKAGENAME:
584 2 THISARG.sv = lex_scan_packagename();
585 2 ▶ if(!THISARG.sv && !is_optional)
Branch: 2 branches
BranchTrueFalseTotal
if(!THISARG.sv && !is_optional) 1 1 2
if(!THISARG.sv && !is_optional) 1 0 1
586 1 yycroak("Expected a package name");
587 1 (*argidx)++;
588 1 return;
589  
590 7 case XS_PARSE_KEYWORD_LEXVARNAME:
591 case XS_PARSE_KEYWORD_LEXVAR:
592 {
593 /* name vs. padix begin with similar structure */
594 7 SV *varname = lex_scan_lexvar();
595 7 ▶ if(!varname)
Branch: 1 branch
BranchTrueFalseTotal
if(!varname) 0 7 7
596 0 yycroak("Expected a lexical variable name");
597 7 switch(SvPVX(varname)[0]) {
598 5 case '$':
599 5 ▶ if(!(piece->u.c & XPK_LEXVAR_SCALAR))
Branch: 1 branch
BranchTrueFalseTotal
if(!(piece->u.c & XPK_LEXVAR_SCALAR)) 0 5 5
600 0 yycroak("Lexical scalars are not permitted");
601 5 break;
602 1 case '@':
603 1 ▶ if(!(piece->u.c & XPK_LEXVAR_ARRAY))
Branch: 1 branch
BranchTrueFalseTotal
if(!(piece->u.c & XPK_LEXVAR_ARRAY)) 0 1 1
604 0 yycroak("Lexical arrays are not permitted");
605 1 break;
606 1 case '%':
607 1 ▶ if(!(piece->u.c & XPK_LEXVAR_HASH))
Branch: 1 branch
BranchTrueFalseTotal
if(!(piece->u.c & XPK_LEXVAR_HASH)) 0 1 1
608 0 yycroak("Lexical hashes are not permitted");
609 1 break;
610 }
611 7 ▶ if(type == XS_PARSE_KEYWORD_LEXVARNAME) {
Branch: 1 branch
BranchTrueFalseTotal
if(type == XS_PARSE_KEYWORD_LEXVARNAME) { 3 4 7
612 3 THISARG.sv = varname;
613 3 (*argidx)++;
614 3 return;
615 }
616  
617 4 SAVEFREESV(varname);
618  
619 /* Forbid $_ / @_ / %_ */
620 4 ▶ if(SvCUR(varname) == 2 && SvPVX(varname)[1] == '_')
Branch: 2 branches
BranchTrueFalseTotal
if(SvCUR(varname) == 2 && SvPVX(varname)[1] == '_') 0 4 4
if(SvCUR(varname) == 2 && SvPVX(varname)[1] == '_') 0 0 0
621 0 yycroakf("Can't use global %s in \"my\"", SvPVX(varname));
622  
623 4 ▶ if(is_special)
Branch: 1 branch
BranchTrueFalseTotal
if(is_special) 3 1 4
624 3 THISARG.padix = pad_add_name_pvn(SvPVX(varname), SvCUR(varname), 0, NULL, NULL);
625 else
626 #if HAVE_PERL_VERSION(5, 16, 0)
627 1 THISARG.padix = pad_findmy_pvn(SvPVX(varname), SvCUR(varname), 0);
628 #else
629 THISARG.padix = pad_findmy(SvPVX(varname), SvCUR(varname), 0);
630 #endif
631  
632 4 (*argidx)++;
633 4 return;
634 }
635  
636 4 case XS_PARSE_KEYWORD_ATTRS:
637 {
638 4 THISARG.i = 0;
639 4 (*argidx)++;
640  
641 4 ▶ if(lex_peek_unichar(0) == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == ':') { 3 1 4
642 3 lex_read_unichar(0);
643 3 lex_read_space(0);
644  
645 3 SV *attrname = newSV(0), *attrval = newSV(0);
646 3 SAVEFREESV(attrname); SAVEFREESV(attrval);
647  
648 13 ▶ while(lex_scan_attrval_into(attrname, attrval)) {
Branch: 1 branch
BranchTrueFalseTotal
while(lex_scan_attrval_into(attrname, attrval)) { 7 3 10
649 7 lex_read_space(0);
650  
651 7 ▶ if(*argidx >= (SvLEN(argsv) / sizeof(XSParseKeywordPiece)))
Branch: 1 branch
BranchTrueFalseTotal
if(*argidx >= (SvLEN(argsv) / sizeof(XSParseKeywordPiece))) 0 7 7
652 0 ▶ SvGROW(argsv, SvLEN(argsv) * 2);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(argsv, SvLEN(argsv) * 2); 0 0 0
SvGROW(argsv, SvLEN(argsv) * 2); 0 0 0
653  
654 7 XSParseKeywordPiece *arg = &((XSParseKeywordPiece *)SvPVX(argsv))[*argidx];
655 7 arg->attr.name = newSVsv(attrname);
656 7 arg->attr.value = newSVsv(attrval);
657  
658 7 THISARG.i++;
659 7 (*argidx)++;
660  
661 /* Accept additional colons to prefix additional attrs, but do not require them */
662 7 ▶ if(lex_peek_unichar(0) == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == ':') { 2 5 7
663 2 lex_read_unichar(0);
664 2 lex_read_space(0);
665 }
666 }
667 }
668  
669 4 return;
670 }
671  
672 3 case XS_PARSE_KEYWORD_VSTRING:
673 3 THISARG.sv = lex_scan_version(optflag);
674 3 (*argidx)++;
675 3 return;
676  
677 1 case XS_PARSE_KEYWORD_INTRO_MY:
678 1 intro_my();
679 1 return;
680  
681 5 case XS_PARSE_KEYWORD_INFIX:
682 {
683 5 ▶ if(!XSParseInfix_parse(aTHX_ piece->u.c, &THISARG.infix))
Branch: 1 branch
BranchTrueFalseTotal
if(!XSParseInfix_parse(aTHX_ piece->u.c, &THISARG.infix)) 2 3 5
684 2 yycroak("Expected an infix operator");
685 3 (*argidx)++;
686 3 return;
687 }
688  
689 0 case XS_PARSE_KEYWORD_SETUP:
690 0 croak("ARGH parse_piece() should never see XS_PARSE_KEYWORD_SETUP!");
691  
692 32 case XS_PARSE_KEYWORD_SEQUENCE:
693 {
694 32 const struct XSParseKeywordPieceType *pieces = piece->u.pieces;
695  
696 32 ▶ if(is_optional) {
Branch: 1 branch
BranchTrueFalseTotal
if(is_optional) { 31 1 32
697 31 THISARG.i = 0;
698 31 (*argidx)++;
699 31 ▶ if(!probe_piece(aTHX_ argsv, argidx, pieces, hookdata))
Branch: 1 branch
BranchTrueFalseTotal
if(!probe_piece(aTHX_ argsv, argidx, pieces, hookdata)) 14 17 31
700 14 return;
701 17 THISARG.i++;
702 17 pieces++;
703 17 lex_read_space(0);
704 }
705  
706 18 parse_pieces(aTHX_ argsv, argidx, pieces, hookdata);
707 18 return;
708 }
709  
710 2 case XS_PARSE_KEYWORD_REPEATED:
711 2 THISARG.i = 0;
712 2 (*argidx)++;
713 8 ▶ while(probe_piece(aTHX_ argsv, argidx, piece->u.pieces + 0, hookdata)) {
Branch: 1 branch
BranchTrueFalseTotal
while(probe_piece(aTHX_ argsv, argidx, piece->u.pieces + 0, hookdata)) { 6 2 8
714 6 THISARG.i++;
715 6 lex_read_space(0);
716 6 parse_pieces(aTHX_ argsv, argidx, piece->u.pieces + 1, hookdata);
717 }
718 2 return;
719  
720 6 case XS_PARSE_KEYWORD_CHOICE:
721 case XS_PARSE_KEYWORD_TAGGEDCHOICE:
722 6 ▶ if(!probe_piece(aTHX_ argsv, argidx, piece, hookdata)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!probe_piece(aTHX_ argsv, argidx, piece, hookdata)) { 1 5 6
723 1 THISARG.i = -1;
724 1 (*argidx)++;
725 }
726 6 return;
727  
728 2 case XS_PARSE_KEYWORD_SEPARATEDLIST:
729 2 THISARG.i = 0;
730 2 (*argidx)++;
731 while(1) {
732 4 parse_pieces(aTHX_ argsv, argidx, piece->u.pieces + 1, hookdata);
733 4 THISARG.i++;
734  
735 4 lex_read_space(0);
736  
737 4 ▶ if(!probe_piece(aTHX_ argsv, argidx, piece->u.pieces + 0, hookdata))
Branch: 1 branch
BranchTrueFalseTotal
if(!probe_piece(aTHX_ argsv, argidx, piece->u.pieces + 0, hookdata)) 2 2 4
738 2 break;
739 2 lex_read_space(0);
740 }
741 2 return;
742  
743 4 case XS_PARSE_KEYWORD_PARENS:
744 {
745 4 bool has_paren = (lex_peek_unichar(0) == '(');
746  
747 4 ▶ if(is_optional) {
Branch: 1 branch
BranchTrueFalseTotal
if(is_optional) { 0 4 4
748 0 THISARG.i = 0;
749 0 (*argidx)++;
750 0 ▶ if(!has_paren) return;
Branch: 1 branch
BranchTrueFalseTotal
if(!has_paren) return; 0 0 0
751 0 THISARG.i++;
752 }
753  
754 4 ▶ if(has_paren) {
Branch: 1 branch
BranchTrueFalseTotal
if(has_paren) { 3 1 4
755 3 lex_expect_unichar('(');
756 3 lex_read_space(0);
757  
758 3 parse_pieces(aTHX_ argsv, argidx, piece->u.pieces, hookdata);
759  
760 3 lex_expect_unichar(')');
761 }
762 1 ▶ else if(piece->type & XPK_TYPEFLAG_MAYBEPARENS) {
Branch: 1 branch
BranchTrueFalseTotal
else if(piece->type & XPK_TYPEFLAG_MAYBEPARENS) { 1 0 1
763 /* We didn't find a '(' but that's OK; they're optional */
764 1 parse_pieces(aTHX_ argsv, argidx, piece->u.pieces, hookdata);
765 }
766 else
767 /* We know this should fail */
768 0 lex_expect_unichar('(');
769  
770 4 return;
771 }
772  
773 2 case XS_PARSE_KEYWORD_BRACKETS:
774 2 ▶ if(is_optional) {
Branch: 1 branch
BranchTrueFalseTotal
if(is_optional) { 0 2 2
775 0 THISARG.i = 0;
776 0 (*argidx)++;
777 0 ▶ if(lex_peek_unichar(0) != '[') return;
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != '[') return; 0 0 0
778 0 THISARG.i++;
779 }
780  
781 2 lex_expect_unichar('[');
782 2 lex_read_space(0);
783  
784 2 parse_pieces(aTHX_ argsv, argidx, piece->u.pieces, hookdata);
785  
786 2 lex_expect_unichar(']');
787  
788 2 return;
789  
790 2 case XS_PARSE_KEYWORD_BRACES:
791 2 ▶ if(is_optional) {
Branch: 1 branch
BranchTrueFalseTotal
if(is_optional) { 0 2 2
792 0 THISARG.i = 0;
793 0 (*argidx)++;
794 0 ▶ if(lex_peek_unichar(0) != '{') return;
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != '{') return; 0 0 0
795 0 THISARG.i++;
796 }
797  
798 2 lex_expect_unichar('{');
799 2 lex_read_space(0);
800  
801 2 parse_pieces(aTHX_ argsv, argidx, piece->u.pieces, hookdata);
802  
803 2 lex_expect_unichar('}');
804  
805 2 return;
806  
807 2 case XS_PARSE_KEYWORD_CHEVRONS:
808 2 ▶ if(is_optional) {
Branch: 1 branch
BranchTrueFalseTotal
if(is_optional) { 0 2 2
809 0 THISARG.i = 0;
810 0 (*argidx)++;
811 0 ▶ if(lex_peek_unichar(0) != '<') return;
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != '<') return; 0 0 0
812 0 THISARG.i++;
813 }
814  
815 2 lex_expect_unichar('<');
816 2 lex_read_space(0);
817  
818 2 parse_pieces(aTHX_ argsv, argidx, piece->u.pieces, hookdata);
819  
820 2 lex_expect_unichar('>');
821  
822 2 return;
823 }
824  
825 0 croak("TODO: parse_piece on type=%d\n", type);
826 }
827  
828 95 static void parse_pieces(pTHX_ SV *argsv, size_t *argidx, const struct XSParseKeywordPieceType *pieces, void *hookdata)
829 {
830 size_t idx;
831 172 ▶ for(idx = 0; pieces[idx].type; idx++) {
Branch: 1 branch
BranchTrueFalseTotal
for(idx = 0; pieces[idx].type; idx++) { 77 95 172
832 77 parse_piece(aTHX_ argsv, argidx, pieces + idx, hookdata);
833 77 lex_read_space(0);
834 }
835 95 }
836  
837 114 static int parse(pTHX_ OP **op, struct Registration *reg)
838 {
839 114 const struct XSParseKeywordHooks *hooks = reg->hooks;
840  
841 114 ▶ if(hooks->parse)
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->parse) 2 112 114
842 2 return (*hooks->parse)(aTHX_ op, reg->hookdata);
843  
844 /* parse in pieces */
845  
846 /* use the PV buffer of this SV as a growable array of args */
847 112 size_t maxargs = 4;
848 112 SV *argsv = newSV(maxargs * sizeof(XSParseKeywordPiece));
849 112 SAVEFREESV(argsv);
850  
851 112 bool is_blockscope = hooks->flags & XPK_FLAG_BLOCKSCOPE;
852  
853 int floor;
854 112 ▶ if(is_blockscope)
Branch: 1 branch
BranchTrueFalseTotal
if(is_blockscope) 1 111 112
855 1 floor = block_start(TRUE);
856  
857 112 size_t argidx = 0;
858 112 ▶ if(hooks->build)
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->build) 56 56 112
859 56 parse_pieces(aTHX_ argsv, &argidx, hooks->pieces, reg->hookdata);
860 else
861 56 parse_piece(aTHX_ argsv, &argidx, &hooks->piece1, reg->hookdata);
862  
863 108 ▶ if(hooks->flags & XPK_FLAG_AUTOSEMI) {
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->flags & XPK_FLAG_AUTOSEMI) { 2 106 108
864 2 lex_read_space(0);
865  
866 2 parse_autosemi();
867 }
868  
869 108 XSParseKeywordPiece *args = (XSParseKeywordPiece *)SvPVX(argsv);
870  
871 int ret;
872 108 ▶ if(hooks->build) {
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->build) { 56 52 108
873 /* build function takes an array of pointers to piece structs, so we can
874 * add new fields to the end of them without breaking back-compat. */
875 56 XSParseKeywordPiece **argptrs = NULL;
876 56 ▶ if(argidx) {
Branch: 1 branch
BranchTrueFalseTotal
if(argidx) { 56 0 56
877 56 SV *ptrssv = newSV(argidx * sizeof(XSParseKeywordPiece *));
878 56 SAVEFREESV(ptrssv);
879  
880 56 argptrs = (XSParseKeywordPiece **)SvPVX(ptrssv);
881 }
882  
883 int i;
884 141 ▶ for(i = 0; i < argidx; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < argidx; i++) 85 56 141
885 85 argptrs[i] = &args[i];
886  
887 56 ret = (*hooks->build)(aTHX_ op, argptrs, argidx, reg->hookdata);
888 }
889 52 ▶ else if(reg->apiver < 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if(reg->apiver < 2) { 0 52 52
890 /* version 1 ->build1 used to take a struct directly, not a pointer thereto */
891 0 int (*v1_build1)(pTHX_ OP **out, XSParseKeywordPiece_v1 arg0, void *hookdata) =
892 (int (*)(pTHX_ OP **, XSParseKeywordPiece_v1, void *))hooks->build1;
893 XSParseKeywordPiece_v1 arg0_v1;
894 0 Copy(args + 0, &arg0_v1, 1, XSParseKeywordPiece_v1);
895 0 ret = (*v1_build1)(aTHX_ op, arg0_v1, reg->hookdata);
896 }
897 else
898 52 ret = (*hooks->build1)(aTHX_ op, args + 0, reg->hookdata);
899  
900 108 ▶ if(is_blockscope)
Branch: 1 branch
BranchTrueFalseTotal
if(is_blockscope) 1 107 108
901 1 *op = op_scope(block_end(floor, *op));
902  
903 108 switch(hooks->flags & (XPK_FLAG_EXPR|XPK_FLAG_STMT)) {
904 2 case XPK_FLAG_EXPR:
905 2 ▶ if(ret && (ret != KEYWORD_PLUGIN_EXPR))
Branch: 2 branches
BranchTrueFalseTotal
if(ret && (ret != KEYWORD_PLUGIN_EXPR)) 2 0 2
if(ret && (ret != KEYWORD_PLUGIN_EXPR)) 0 2 2
906 0 yycroakf("Expected parse function for '%s' keyword to return KEYWORD_PLUGIN_EXPR but it did not",
907 reg->kwname);
908 2 break;
909  
910 2 case XPK_FLAG_STMT:
911 2 ▶ if(ret && (ret != KEYWORD_PLUGIN_STMT))
Branch: 2 branches
BranchTrueFalseTotal
if(ret && (ret != KEYWORD_PLUGIN_STMT)) 2 0 2
if(ret && (ret != KEYWORD_PLUGIN_STMT)) 0 2 2
912 0 yycroakf("Expected parse function for '%s' keyword to return KEYWORD_PLUGIN_STMT but it did not",
913 reg->kwname);
914 2 break;
915 }
916  
917 108 return ret;
918 }
919  
920 static struct Registration *registrations;
921  
922 427 static void reg(pTHX_ const char *kwname, int apiver, const struct XSParseKeywordHooks *hooks, void *hookdata)
923 {
924 427 ▶ if(!hooks->build1 && !hooks->build && !hooks->parse)
Branch: 3 branches
BranchTrueFalseTotal
if(!hooks->build1 && !hooks->build && !hooks->parse) 75 352 427
if(!hooks->build1 && !hooks->build && !hooks->parse) 2 73 75
if(!hooks->build1 && !hooks->build && !hooks->parse) 0 2 2
925 0 croak("struct XSParseKeywordHooks requires either a .build1, a .build, or .parse stage");
926  
927 struct Registration *reg;
928 427 Newx(reg, 1, struct Registration);
929  
930 427 reg->kwname = savepv(kwname);
931 427 reg->kwlen = strlen(kwname);
932  
933 427 reg->apiver = apiver;
934 427 reg->hooks = hooks;
935 427 reg->hookdata = hookdata;
936  
937 427 ▶ if(hooks->permit_hintkey)
Branch: 1 branch
BranchTrueFalseTotal
if(hooks->permit_hintkey) 427 0 427
938 427 reg->permit_hintkey_len = strlen(hooks->permit_hintkey);
939  
940 {
941 427 reg->next = registrations;
942 427 registrations = reg;
943 }
944 427 }
945  
946 0 void XSParseKeyword_register_v1(pTHX_ const char *kwname, const struct XSParseKeywordHooks *hooks, void *hookdata)
947 {
948 0 reg(aTHX_ kwname, 1, hooks, hookdata);
949 0 }
950  
951 427 void XSParseKeyword_register_v2(pTHX_ const char *kwname, const struct XSParseKeywordHooks *hooks, void *hookdata)
952 {
953 427 reg(aTHX_ kwname, 2, hooks, hookdata);
954 427 }
955  
956 static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
957  
958 116420 static int my_keyword_plugin(pTHX_ char *kw, STRLEN kwlen, OP **op)
959 {
960 116420 ▶ if(PL_parser && PL_parser->error_count)
Branch: 2 branches
BranchTrueFalseTotal
if(PL_parser && PL_parser->error_count) 116420 0 116420
if(PL_parser && PL_parser->error_count) 0 116420 116420
961 0 return (*next_keyword_plugin)(aTHX_ kw, kwlen, op);
962  
963 116420 char *orig_kw = kw;
964 116420 STRLEN orig_kwlen = kwlen;
965  
966 116420 HV *hints = GvHV(PL_hintgv);
967  
968 /* Can't ENTER/SAVE/LEAVE here because some keywords don't like it */
969 116420 U16 was_parser_in_my = PL_parser->in_my;
970 116420 PL_parser->in_my = 0;
971 116420 char *was_parser_bufptr = PL_parser->bufptr;
972  
973 #if HAVE_PERL_VERSION(5, 16, 0)
974 /* Huge hack to be able to handle 'my KW' by eating the 'my' then advance to
975 * the next keyword.
976 * We don't support this on Perl 5.14, because it causes absolutely bizarre
977 * failures of `utf8.pm` and `utf8_heavy.pl`.
978 */
979 116420 ▶ if(kwlen == 2 && strEQ(kw, "my")) {
Branch: 2 branches
BranchTrueFalseTotal
if(kwlen == 2 && strEQ(kw, "my")) { 38546 77874 116420
if(kwlen == 2 && strEQ(kw, "my")) { 18214 20332 38546
980 18214 lex_read_space(0);
981  
982 18214 I32 c = lex_peek_unichar(0);
983 18214 ▶ if(!isIDFIRST_uni(c))
Branch: 4 branches
BranchTrueFalseTotal
if(!isIDFIRST_uni(c)) 18214 0 18214
if(!isIDFIRST_uni(c)) 18214 0 18214
if(!isIDFIRST_uni(c)) 18213 1 18214
if(!isIDFIRST_uni(c)) 18213 1 18214
984 18213 goto next_keyword;
985  
986 1 kw = PL_parser->bufptr;
987  
988 1 lex_read_unichar(0);
989 7 ▶ while((c = lex_peek_unichar(0)) && isIDCONT_uni(c))
Branch: 6 branches
BranchTrueFalseTotal
while((c = lex_peek_unichar(0)) && isIDCONT_uni(c)) 7 0 7
while((c = lex_peek_unichar(0)) && isIDCONT_uni(c)) 7 0 7
while((c = lex_peek_unichar(0)) && isIDCONT_uni(c)) 7 0 7
while((c = lex_peek_unichar(0)) && isIDCONT_uni(c)) 6 1 7
while((c = lex_peek_unichar(0)) && isIDCONT_uni(c)) 6 1 7
while((c = lex_peek_unichar(0)) && isIDCONT_uni(c)) 0 0 0
990 6 lex_read_unichar(0);
991  
992 1 kwlen = PL_parser->bufptr - kw;
993  
994 1 PL_parser->in_my = KEY_my;
995 }
996 #endif
997  
998 struct Registration *reg;
999 1889620 ▶ for(reg = registrations; reg; reg = reg->next) {
Branch: 1 branch
BranchTrueFalseTotal
for(reg = registrations; reg; reg = reg->next) { 1791527 98093 1889620
1000 1791527 ▶ if(reg->kwlen != kwlen || !strnEQ(reg->kwname, kw, kwlen))
Branch: 2 branches
BranchTrueFalseTotal
if(reg->kwlen != kwlen || !strnEQ(reg->kwname, kw, kwlen)) 25849 1765678 1791527
if(reg->kwlen != kwlen || !strnEQ(reg->kwname, kw, kwlen)) 25733 116 25849
1001 1791411 continue;
1002  
1003 116 ▶ if(reg->hooks->permit_hintkey &&
Branch: 2 branches
BranchTrueFalseTotal
if(reg->hooks->permit_hintkey && 116 0 116
if(reg->hooks->permit_hintkey && 116 0 116
1004 116 ▶ (!hints || !hv_fetch(hints, reg->hooks->permit_hintkey, reg->permit_hintkey_len, 0)))
Branch: 1 branch
BranchTrueFalseTotal
(!hints || !hv_fetch(hints, reg->hooks->permit_hintkey, reg->permit_hintkey_len, 0))) 1 115 116
1005 1 continue;
1006  
1007 115 ▶ if(reg->hooks->permit &&
Branch: 1 branch
BranchTrueFalseTotal
if(reg->hooks->permit && 3 112 115
1008 3 ▶ !(*reg->hooks->permit)(aTHX_ reg->hookdata))
Branch: 1 branch
BranchTrueFalseTotal
!(*reg->hooks->permit)(aTHX_ reg->hookdata)) 1 2 3
1009 1 continue;
1010  
1011 114 ▶ if(PL_parser->in_my && !(reg->hooks->flags & XPK_FLAG_PERMIT_LEXICAL))
Branch: 2 branches
BranchTrueFalseTotal
if(PL_parser->in_my && !(reg->hooks->flags & XPK_FLAG_PERMIT_LEXICAL)) 1 113 114
if(PL_parser->in_my && !(reg->hooks->flags & XPK_FLAG_PERMIT_LEXICAL)) 0 1 1
1012 0 croak("'my %.*s' is not permitted as a lexical keyword", kwlen, kw);
1013  
1014 114 ▶ if(reg->hooks->check)
Branch: 1 branch
BranchTrueFalseTotal
if(reg->hooks->check) 2 112 114
1015 2 (*reg->hooks->check)(aTHX_ reg->hookdata);
1016  
1017 114 *op = NULL;
1018  
1019 114 lex_read_space(0);
1020  
1021 114 int ret = parse(aTHX_ op, reg);
1022  
1023 110 lex_read_space(0);
1024  
1025 110 ▶ if(ret && !*op)
Branch: 2 branches
BranchTrueFalseTotal
if(ret && !*op) 110 0 110
if(ret && !*op) 0 110 110
1026 0 *op = newOP(OP_NULL, 0);
1027  
1028 110 PL_parser->in_my = was_parser_in_my;
1029 110 return ret;
1030 }
1031  
1032 98093 next_keyword:
1033 116306 ▶ if(PL_parser->bufptr > was_parser_bufptr)
Branch: 1 branch
BranchTrueFalseTotal
if(PL_parser->bufptr > was_parser_bufptr) 18100 98206 116306
1034 18100 PL_parser->bufptr = was_parser_bufptr;
1035 116306 PL_parser->in_my = was_parser_in_my;
1036 116306 return (*next_keyword_plugin)(aTHX_ orig_kw, orig_kwlen, op);
1037 }
1038  
1039 24 void XSParseKeyword_boot(pTHX)
1040 {
1041 24 wrap_keyword_plugin(&my_keyword_plugin, &next_keyword_plugin);
1042 24 }
« hax/force_list_keeping_pushmark.c.inc ↑ Summary t/infix.xs »