hax/lexer-additions.c.inc

filter: Statementstmt 49.3% 73 / 148 Branchbran 43.8% 57 / 130 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 46.7% 130 / 278 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« hax/newOP_CUSTOM.c.inc ↑ Summary hax/make_argcheck_ops.c.inc »
1 /* vi: set ft=c : */
2  
3 /* Perls before 5.18 lack isIDCONT_uni, but baring minor differences of weird
4 * Unicode characters, isALNUM_uni is close enough
5 */
6 #ifndef isIDCONT_uni
7 #define isIDCONT_uni(c) isALNUM_uni(c)
8 #endif
9  
10 #define sv_cat_c(sv, c) MY_sv_cat_c(aTHX_ sv, c)
11 18 static void MY_sv_cat_c(pTHX_ SV *sv, U32 c)
12 {
13 char ds[UTF8_MAXBYTES + 1], *d;
14 18 d = (char *)uvchr_to_utf8((U8 *)ds, c);
15 18 ▶ if (d - ds > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (d - ds > 1) { 0 18 18
16 0 sv_utf8_upgrade(sv);
17 }
18 18 sv_catpvn(sv, ds, d - ds);
19 18 }
20  
21 #define lex_consume(s) MY_lex_consume(aTHX_ s)
22 0 static int MY_lex_consume(pTHX_ char *s)
23 {
24 /* I want strprefix() */
25 size_t i;
26 0 ▶ for(i = 0; s[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; s[i]; i++) { 0 0 0
27 0 ▶ if(s[i] != PL_parser->bufptr[i])
Branch: 1 branch
BranchTrueFalseTotal
if(s[i] != PL_parser->bufptr[i]) 0 0 0
28 0 return 0;
29 }
30  
31 0 lex_read_to(PL_parser->bufptr + i);
32 0 return i;
33 }
34  
35 enum {
36 LEX_IDENT_PACKAGENAME = (1<<0),
37 };
38  
39 #define lex_scan_ident( ) MY_lex_scan_ident(aTHX_ 0)
40 #define lex_scan_packagename() MY_lex_scan_ident(aTHX_ LEX_IDENT_PACKAGENAME)
41 38 static SV *MY_lex_scan_ident(pTHX_ int flags)
42 {
43 I32 c;
44 38 bool at_start = TRUE;
45  
46 38 char *ident = PL_parser->bufptr;
47  
48 /* Don't get confused by things that look like attrs */
49 38 ▶ if((flags & LEX_IDENT_PACKAGENAME) && (ident[0] == ':' && ident[1] != ':'))
Branch: 3 branches
BranchTrueFalseTotal
if((flags & LEX_IDENT_PACKAGENAME) && (ident[0] == ':' && ident[1] != ':')) 4 34 38
if((flags & LEX_IDENT_PACKAGENAME) && (ident[0] == ':' && ident[1] != ':')) 0 4 4
if((flags & LEX_IDENT_PACKAGENAME) && (ident[0] == ':' && ident[1] != ':')) 0 0 0
50 0 return FALSE;
51  
52 157 ▶ while((c = lex_peek_unichar(0))) {
Branch: 1 branch
BranchTrueFalseTotal
while((c = lex_peek_unichar(0))) { 157 0 157
53 157 ▶ if(at_start ? isIDFIRST_uni(c) : isALNUM_uni(c))
Branch: 8 branches
BranchTrueFalseTotal
if(at_start ? isIDFIRST_uni(c) : isALNUM_uni(c)) 38 119 157
if(at_start ? isIDFIRST_uni(c) : isALNUM_uni(c)) 38 0 38
if(at_start ? isIDFIRST_uni(c) : isALNUM_uni(c)) 38 0 38
if(at_start ? isIDFIRST_uni(c) : isALNUM_uni(c)) 29 9 38
if(at_start ? isIDFIRST_uni(c) : isALNUM_uni(c)) 119 0 119
if(at_start ? isIDFIRST_uni(c) : isALNUM_uni(c)) 119 0 119
if(at_start ? isIDFIRST_uni(c) : isALNUM_uni(c)) 88 31 119
if(at_start ? isIDFIRST_uni(c) : isALNUM_uni(c)) 117 40 157
54 117 at_start = FALSE;
55 /* TODO: This sucks in the case of a false Foo:Bar match */
56 40 ▶ else if((flags & LEX_IDENT_PACKAGENAME) && (c == ':') && (PL_parser->bufptr[1] == ':')) {
Branch: 3 branches
BranchTrueFalseTotal
else if((flags & LEX_IDENT_PACKAGENAME) && (c == ':') && (PL_parser->bufptr[1] == ':')) { 6 34 40
else if((flags & LEX_IDENT_PACKAGENAME) && (c == ':') && (PL_parser->bufptr[1] == ':')) { 2 4 6
else if((flags & LEX_IDENT_PACKAGENAME) && (c == ':') && (PL_parser->bufptr[1] == ':')) { 2 0 2
57 2 lex_read_unichar(0);
58 2 ▶ if(lex_read_unichar(0) != ':')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_read_unichar(0) != ':') 0 2 2
59 /* Probably unreachable now due to condition above? */
60 0 croak("Expected colon to be followed by another in package name");
61 }
62 else
63 break;
64  
65 119 lex_read_unichar(0);
66 }
67  
68 38 STRLEN len = PL_parser->bufptr - ident;
69 38 ▶ if(!len)
Branch: 1 branch
BranchTrueFalseTotal
if(!len) 9 29 38
70 9 return NULL;
71  
72 29 SV *ret = newSVpvn(ident, len);
73 29 ▶ if(lex_bufutf8())
Branch: 1 branch
BranchTrueFalseTotal
if(lex_bufutf8()) 29 0 29
74 29 SvUTF8_on(ret);
75  
76 29 return ret;
77 }
78  
79 #define lex_scan_attrval_into(name, val) MY_lex_scan_attrval_into(aTHX_ name, val)
80 10 static bool MY_lex_scan_attrval_into(pTHX_ SV *name, SV *val)
81 {
82 /* TODO: really want lex_scan_ident_into() */
83 10 SV *n = lex_scan_ident();
84 10 ▶ if(!n)
Branch: 1 branch
BranchTrueFalseTotal
if(!n) 3 7 10
85 3 return FALSE;
86  
87 7 sv_setsv(name, n);
88 7 SvREFCNT_dec(n);
89  
90 7 ▶ if(name != val)
Branch: 1 branch
BranchTrueFalseTotal
if(name != val) 7 0 7
91 7 SvPOK_off(val);
92  
93 /* Do not read space here as space is not allowed between NAME(ARGS) */
94  
95 7 ▶ if(lex_peek_unichar(0) != '(')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) != '(') 5 2 7
96 5 return TRUE;
97  
98 2 lex_read_unichar(0);
99 2 ▶ if(name == val)
Branch: 1 branch
BranchTrueFalseTotal
if(name == val) 0 2 2
100 0 sv_cat_c(val, '(');
101 else
102 2 sv_setpvs(val, "");
103  
104 2 int count = 1;
105 2 I32 c = lex_peek_unichar(0);
106 6 ▶ while(count && c != -1) {
Branch: 2 branches
BranchTrueFalseTotal
while(count && c != -1) { 4 2 6
while(count && c != -1) { 4 0 4
107 4 ▶ if(c == '(')
Branch: 1 branch
BranchTrueFalseTotal
if(c == '(') 0 4 4
108 0 count++;
109 4 ▶ if(c == ')')
Branch: 1 branch
BranchTrueFalseTotal
if(c == ')') 2 2 4
110 2 count--;
111 4 ▶ if(c == '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if(c == '\\') { 0 4 4
112 /* The next char does not bump count even if it is ( or );
113 * the \\ is still captured
114 */
115 0 sv_cat_c(val, lex_read_unichar(0));
116 0 c = lex_peek_unichar(0);
117 0 ▶ if(c == -1)
Branch: 1 branch
BranchTrueFalseTotal
if(c == -1) 0 0 0
118 0 goto unterminated;
119 }
120  
121 /* Don't append final closing ')' on split name/val */
122 4 ▶ if(count || (name == val))
Branch: 2 branches
BranchTrueFalseTotal
if(count || (name == val)) 2 2 4
if(count || (name == val)) 0 2 2
123 2 sv_cat_c(val, c);
124 4 lex_read_unichar(0);
125  
126 4 c = lex_peek_unichar(0);
127 }
128  
129 2 ▶ if(c == -1)
Branch: 1 branch
BranchTrueFalseTotal
if(c == -1) 0 2 2
130 0 return FALSE;
131  
132 2 return TRUE;
133  
134 0 unterminated:
135 0 croak("Unterminated attribute parameter in attribute list");
136 }
137  
138 #define lex_scan_attr() MY_lex_scan_attr(aTHX)
139 0 static SV *MY_lex_scan_attr(pTHX)
140 {
141 0 SV *ret = newSV(0);
142 0 ▶ if(MY_lex_scan_attrval_into(aTHX_ ret, ret))
Branch: 1 branch
BranchTrueFalseTotal
if(MY_lex_scan_attrval_into(aTHX_ ret, ret)) 0 0 0
143 0 return ret;
144  
145 0 SvREFCNT_dec(ret);
146 0 return NULL;
147 }
148  
149 #define lex_scan_attrs(compcv) MY_lex_scan_attrs(aTHX_ compcv)
150 0 static OP *MY_lex_scan_attrs(pTHX_ CV *compcv)
151 {
152 /* Attributes are supplied to newATTRSUB() as an OP_LIST containing
153 * OP_CONSTs, one attribute in each as a plain SV. Note that we don't have
154 * to parse inside the contents of the parens; that is handled by the
155 * attribute handlers themselves
156 */
157 0 OP *attrs = NULL;
158 SV *attr;
159  
160 0 lex_read_space(0);
161 0 ▶ while((attr = lex_scan_attr())) {
Branch: 1 branch
BranchTrueFalseTotal
while((attr = lex_scan_attr())) { 0 0 0
162 0 lex_read_space(0);
163  
164 0 ▶ if(compcv && strEQ(SvPV_nolen(attr), "lvalue")) {
Branch: 2 branches
BranchTrueFalseTotal
if(compcv && strEQ(SvPV_nolen(attr), "lvalue")) { 0 0 0
if(compcv && strEQ(SvPV_nolen(attr), "lvalue")) { 0 0 0
165 0 CvLVALUE_on(compcv);
166 }
167  
168 0 ▶ if(!attrs)
Branch: 1 branch
BranchTrueFalseTotal
if(!attrs) 0 0 0
169 0 attrs = newLISTOP(OP_LIST, 0, NULL, NULL);
170  
171 0 attrs = op_append_elem(OP_LIST, attrs, newSVOP(OP_CONST, 0, attr));
172  
173 /* Accept additional colons to prefix additional attrs */
174 0 ▶ if(lex_peek_unichar(0) == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if(lex_peek_unichar(0) == ':') { 0 0 0
175 0 lex_read_unichar(0);
176 0 lex_read_space(0);
177 }
178 }
179  
180 0 return attrs;
181 }
182  
183 #define lex_scan_lexvar() MY_lex_scan_lexvar(aTHX)
184 7 static SV *MY_lex_scan_lexvar(pTHX)
185 {
186 7 int sigil = lex_peek_unichar(0);
187 7 ▶ switch(sigil) {
Branch: 1 branch
BranchTrueFalseTotal
switch(sigil) { 7 0 7
188 7 case '$':
189 case '@':
190 case '%':
191 7 lex_read_unichar(0);
192 7 break;
193  
194 0 default:
195 0 croak("Expected a lexical variable");
196 }
197  
198 7 SV *ret = lex_scan_ident();
199 7 ▶ if(!ret)
Branch: 1 branch
BranchTrueFalseTotal
if(!ret) 0 7 7
200 0 return NULL;
201  
202 /* prepend sigil - which we know to be a single byte */
203 7 ▶ SvGROW(ret, SvCUR(ret) + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(ret, SvCUR(ret) + 1); 7 0 7
SvGROW(ret, SvCUR(ret) + 1); 0 7 7
204 7 Move(SvPVX(ret), SvPVX(ret) + 1, SvCUR(ret), char);
205 7 SvPVX(ret)[0] = sigil;
206 7 SvCUR(ret)++;
207  
208 7 SvPVX(ret)[SvCUR(ret)] = 0;
209  
210 7 return ret;
211 }
212  
213 #define lex_scan_parenthesized() MY_lex_scan_parenthesized(aTHX)
214 0 static SV *MY_lex_scan_parenthesized(pTHX)
215 {
216 I32 c;
217 0 int parencount = 0;
218 0 SV *ret = newSVpvs("");
219 0 ▶ if(lex_bufutf8())
Branch: 1 branch
BranchTrueFalseTotal
if(lex_bufutf8()) 0 0 0
220 0 SvUTF8_on(ret);
221  
222 0 c = lex_peek_unichar(0);
223  
224 0 ▶ while(c != -1) {
Branch: 1 branch
BranchTrueFalseTotal
while(c != -1) { 0 0 0
225 0 sv_cat_c(ret, lex_read_unichar(0));
226  
227 0 switch(c) {
228 0 case '(': parencount++; break;
229 0 case ')': parencount--; break;
230 }
231 0 ▶ if(!parencount)
Branch: 1 branch
BranchTrueFalseTotal
if(!parencount) 0 0 0
232 0 break;
233  
234 0 c = lex_peek_unichar(0);
235 }
236  
237 0 ▶ if(SvCUR(ret))
Branch: 1 branch
BranchTrueFalseTotal
if(SvCUR(ret)) 0 0 0
238 0 return ret;
239  
240 0 SvREFCNT_dec(ret);
241 0 return NULL;
242 }
243  
244 #define lex_scan_version(flags) MY_lex_scan_version(aTHX_ flags)
245 5 static SV *MY_lex_scan_version(pTHX_ int flags)
246 {
247 I32 c;
248 5 SV *tmpsv = sv_2mortal(newSVpvs(""));
249  
250 /* scan_version() expects a version to end in linefeed, semicolon or
251 * openbrace; gets confused if other keywords are fine. We'll have to
252 * extract it first.
253 * https://rt.cpan.org/Ticket/Display.html?id=132903
254 */
255  
256 21 ▶ while((c = lex_peek_unichar(0))) {
Branch: 1 branch
BranchTrueFalseTotal
while((c = lex_peek_unichar(0))) { 21 0 21
257 /* Allow a single leading v before accepting only digits, dot, underscore */
258 21 ▶ if((!SvCUR(tmpsv) && (c == 'v')) || strchr("0123456789._", c))
Branch: 3 branches
BranchTrueFalseTotal
if((!SvCUR(tmpsv) && (c == 'v')) || strchr("0123456789._", c)) 5 16 21
if((!SvCUR(tmpsv) && (c == 'v')) || strchr("0123456789._", c)) 2 3 5
if((!SvCUR(tmpsv) && (c == 'v')) || strchr("0123456789._", c)) 13 5 18
259 16 sv_cat_c(tmpsv, lex_read_unichar(0));
260 else
261 break;
262 }
263  
264 5 ▶ if(!SvCUR(tmpsv) && (flags & PARSE_OPTIONAL))
Branch: 2 branches
BranchTrueFalseTotal
if(!SvCUR(tmpsv) && (flags & PARSE_OPTIONAL)) 2 3 5
if(!SvCUR(tmpsv) && (flags & PARSE_OPTIONAL)) 2 0 2
265 2 return NULL;
266  
267 3 SV *ret = newSV(0);
268 3 scan_version(SvPVX(tmpsv), ret, FALSE);
269  
270 3 return ret;
271 }
272  
273 #define parse_lexvar() MY_parse_lexvar(aTHX)
274 0 static PADOFFSET MY_parse_lexvar(pTHX)
275 {
276 /* TODO: Rewrite this in terms of using lex_scan_lexvar()
277 */
278 0 char *lexname = PL_parser->bufptr;
279  
280 0 ▶ if(lex_read_unichar(0) != '$')
Branch: 1 branch
BranchTrueFalseTotal
if(lex_read_unichar(0) != '$') 0 0 0
281 0 croak("Expected a lexical scalar at %s", lexname);
282  
283 0 ▶ if(!isIDFIRST_uni(lex_peek_unichar(0)))
Branch: 4 branches
BranchTrueFalseTotal
if(!isIDFIRST_uni(lex_peek_unichar(0))) 0 0 0
if(!isIDFIRST_uni(lex_peek_unichar(0))) 0 0 0
if(!isIDFIRST_uni(lex_peek_unichar(0))) 0 0 0
if(!isIDFIRST_uni(lex_peek_unichar(0))) 0 0 0
284 0 croak("Expected a lexical scalar at %s", lexname);
285 0 lex_read_unichar(0);
286 0 ▶ while(isIDCONT_uni(lex_peek_unichar(0)))
Branch: 4 branches
BranchTrueFalseTotal
while(isIDCONT_uni(lex_peek_unichar(0))) 0 0 0
while(isIDCONT_uni(lex_peek_unichar(0))) 0 0 0
while(isIDCONT_uni(lex_peek_unichar(0))) 0 0 0
while(isIDCONT_uni(lex_peek_unichar(0))) 0 0 0
287 0 lex_read_unichar(0);
288  
289 /* Forbid $_ */
290 0 ▶ if(PL_parser->bufptr - lexname == 2 && lexname[1] == '_')
Branch: 2 branches
BranchTrueFalseTotal
if(PL_parser->bufptr - lexname == 2 && lexname[1] == '_') 0 0 0
if(PL_parser->bufptr - lexname == 2 && lexname[1] == '_') 0 0 0
291 0 croak("Can't use global $_ in \"my\"");
292  
293 0 return pad_add_name_pvn(lexname, PL_parser->bufptr - lexname, 0, NULL, NULL);
294 }
295  
296 #define parse_scoped_block(flags) MY_parse_scoped_block(aTHX_ flags)
297 0 static OP *MY_parse_scoped_block(pTHX_ int flags)
298 {
299 OP *ret;
300 0 I32 save_ix = block_start(TRUE);
301 0 ret = parse_block(flags);
302 0 return block_end(save_ix, ret);
303 }
« hax/newOP_CUSTOM.c.inc ↑ Summary hax/make_argcheck_ops.c.inc »