Commit | Line | Data |
e807ee50 |
1 | /* stolen_chunk_of_toke.c - from perl 5.8.8 toke.c |
2 | * |
3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
4 | * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public |
7 | * License or the Artistic License, as specified in the README file. |
8 | * |
9 | */ |
10 | |
11 | /* |
12 | * "It all comes from here, the stench and the peril." --Frodo |
13 | */ |
14 | |
15 | /* |
16 | * this is all blatantly stolen. I sincerely hopes it doesn't fuck anything |
17 | * up but if it does blame me (Matt S Trout), not the poor original authors |
18 | */ |
19 | |
71511651 |
20 | /* the following #defines are stolen from assorted headers, not toke.c (mst) */ |
e807ee50 |
21 | |
22 | #define DPTR2FPTR(t,p) ((t)PTR2nat(p)) /* data pointer to function pointer */ |
23 | #define FPTR2DPTR(t,p) ((t)PTR2nat(p)) /* function pointer to data pointer */ |
c630715a |
24 | #define PTR2nat(p) (PTRV)(p) /* pointer to integer of PTRSIZE */ |
e807ee50 |
25 | #define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t), |
14ab3472 |
26 | |
27 | /* conditionalise these two because as of 5.9.5 we already get them from |
28 | the headers (mst) */ |
29 | #ifndef Newx |
e807ee50 |
30 | #define Newx(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))) |
14ab3472 |
31 | #endif |
32 | #ifndef SvPVX_const |
0ba8c7aa |
33 | #define SvPVX_const(sv) ((const char*) (0 + SvPVX(sv))) |
14ab3472 |
34 | #endif |
35 | |
0ba8c7aa |
36 | #define SvPV_renew(sv,n) \ |
37 | STMT_START { SvLEN_set(sv, n); \ |
38 | SvPV_set((sv), (MEM_WRAP_CHECK_(n,char) \ |
39 | (char*)saferealloc((Malloc_t)SvPVX(sv), \ |
40 | (MEM_SIZE)((n))))); \ |
41 | } STMT_END |
e807ee50 |
42 | |
c630715a |
43 | /* On MacOS, respect nonbreaking spaces */ |
44 | #ifdef MACOS_TRADITIONAL |
45 | #define SPACE_OR_TAB(c) ((c)==' '||(c)=='\312'||(c)=='\t') |
46 | #else |
47 | #define SPACE_OR_TAB(c) ((c)==' '||(c)=='\t') |
48 | #endif |
e807ee50 |
49 | |
0ba8c7aa |
50 | #define CLINE (PL_copline = (CopLINE(PL_curcop) < PL_copline ? CopLINE(PL_curcop) : PL_copline)) |
51 | |
e807ee50 |
52 | #define LEX_NORMAL 10 /* normal code (ie not within "...") */ |
53 | #define LEX_INTERPNORMAL 9 /* code within a string, eg "$foo[$x+1]" */ |
54 | #define LEX_INTERPCASEMOD 8 /* expecting a \U, \Q or \E etc */ |
55 | #define LEX_INTERPPUSH 7 /* starting a new sublex parse level */ |
56 | #define LEX_INTERPSTART 6 /* expecting the start of a $var */ |
57 | |
58 | /* at end of code, eg "$x" followed by: */ |
59 | #define LEX_INTERPEND 5 /* ... eg not one of [, { or -> */ |
60 | #define LEX_INTERPENDMAYBE 4 /* ... eg one of [, { or -> */ |
61 | |
62 | #define LEX_INTERPCONCAT 3 /* expecting anything, eg at start of |
63 | string or after \E, $foo, etc */ |
64 | #define LEX_INTERPCONST 2 /* NOT USED */ |
65 | #define LEX_FORMLINE 1 /* expecting a format line */ |
66 | #define LEX_KNOWNEXT 0 /* next token known; just return it */ |
67 | |
14ab3472 |
68 | /* and these two are my own madness (mst) */ |
71511651 |
69 | |
70 | #if PERL_REVISION == 5 && PERL_VERSION == 8 && PERL_SUBVERSION >= 8 |
71 | #define PERL_5_8_8_PLUS |
72 | #endif |
73 | |
14ab3472 |
74 | #if PERL_REVISION == 5 && PERL_VERSION > 8 |
75 | #define PERL_5_9_PLUS |
76 | #endif |
77 | |
78 | #ifdef PERL_5_9_PLUS |
79 | /* 5.9+ moves a bunch of things to a PL_parser struct so we need to |
80 | declare the backcompat macros for things to still work (mst) */ |
81 | |
82 | /* XXX temporary backwards compatibility */ |
83 | #define PL_lex_brackets (PL_parser->lex_brackets) |
84 | #define PL_lex_brackstack (PL_parser->lex_brackstack) |
85 | #define PL_lex_casemods (PL_parser->lex_casemods) |
86 | #define PL_lex_casestack (PL_parser->lex_casestack) |
87 | #define PL_lex_defer (PL_parser->lex_defer) |
88 | #define PL_lex_dojoin (PL_parser->lex_dojoin) |
89 | #define PL_lex_expect (PL_parser->lex_expect) |
90 | #define PL_lex_formbrack (PL_parser->lex_formbrack) |
91 | #define PL_lex_inpat (PL_parser->lex_inpat) |
92 | #define PL_lex_inwhat (PL_parser->lex_inwhat) |
93 | #define PL_lex_op (PL_parser->lex_op) |
94 | #define PL_lex_repl (PL_parser->lex_repl) |
95 | #define PL_lex_starts (PL_parser->lex_starts) |
96 | #define PL_lex_stuff (PL_parser->lex_stuff) |
97 | #define PL_multi_start (PL_parser->multi_start) |
98 | #define PL_multi_open (PL_parser->multi_open) |
99 | #define PL_multi_close (PL_parser->multi_close) |
100 | #define PL_pending_ident (PL_parser->pending_ident) |
101 | #define PL_preambled (PL_parser->preambled) |
102 | #define PL_sublex_info (PL_parser->sublex_info) |
103 | #define PL_linestr (PL_parser->linestr) |
104 | #define PL_sublex_info (PL_parser->sublex_info) |
105 | #define PL_linestr (PL_parser->linestr) |
106 | #define PL_expect (PL_parser->expect) |
107 | #define PL_copline (PL_parser->copline) |
108 | #define PL_bufptr (PL_parser->bufptr) |
109 | #define PL_oldbufptr (PL_parser->oldbufptr) |
110 | #define PL_oldoldbufptr (PL_parser->oldoldbufptr) |
111 | #define PL_linestart (PL_parser->linestart) |
112 | #define PL_bufend (PL_parser->bufend) |
113 | #define PL_last_uni (PL_parser->last_uni) |
114 | #define PL_last_lop (PL_parser->last_lop) |
115 | #define PL_last_lop_op (PL_parser->last_lop_op) |
116 | #define PL_lex_state (PL_parser->lex_state) |
117 | #define PL_rsfp (PL_parser->rsfp) |
118 | #define PL_rsfp_filters (PL_parser->rsfp_filters) |
119 | #define PL_in_my (PL_parser->in_my) |
120 | #define PL_in_my_stash (PL_parser->in_my_stash) |
121 | #define PL_tokenbuf (PL_parser->tokenbuf) |
122 | #define PL_multi_end (PL_parser->multi_end) |
123 | #define PL_error_count (PL_parser->error_count) |
124 | /* these three are from the non-PERL_MAD path but I don't -think- I need |
125 | the PERL_MAD stuff since my code isn't really populating things (mst) */ |
126 | # define PL_nexttoke (PL_parser->nexttoke) |
127 | # define PL_nexttype (PL_parser->nexttype) |
128 | # define PL_nextval (PL_parser->nextval) |
129 | /* end of backcompat macros form 5.9 toke.c (mst) */ |
130 | /* we also need this because we define PERL_CORE so handy.h doesn't provide |
131 | it for us (mst) */ |
132 | #define NEWSV(x,len) newSV(len) |
133 | #endif |
134 | |
135 | /* when ccflags include -DDEBUGGING we need this for earlier 5.8 perls */ |
136 | #ifndef SvPV_nolen_const |
137 | #define SvPV_nolen_const SvPV_nolen |
138 | #endif |
139 | |
71511651 |
140 | /* and now we're back to the toke.c stuff again (mst) */ |
141 | |
e807ee50 |
142 | static const char ident_too_long[] = |
143 | "Identifier too long"; |
144 | static const char c_without_g[] = |
145 | "Use of /c modifier is meaningless without /g"; |
146 | static const char c_in_subst[] = |
147 | "Use of /c modifier is meaningless in s///"; |
148 | |
149 | #ifdef USE_UTF8_SCRIPTS |
150 | # define UTF (!IN_BYTES) |
151 | #else |
152 | # define UTF ((PL_linestr && DO_UTF8(PL_linestr)) || (PL_hints & HINT_UTF8)) |
153 | #endif |
154 | |
155 | /* Invoke the idxth filter function for the current rsfp. */ |
156 | /* maxlen 0 = read one text line */ |
157 | I32 |
158 | Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen) |
159 | { |
160 | filter_t funcp; |
161 | SV *datasv = NULL; |
162 | |
163 | if (!PL_rsfp_filters) |
164 | return -1; |
165 | if (idx > AvFILLp(PL_rsfp_filters)) { /* Any more filters? */ |
166 | /* Provide a default input filter to make life easy. */ |
167 | /* Note that we append to the line. This is handy. */ |
168 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
169 | "filter_read %d: from rsfp\n", idx)); |
170 | if (maxlen) { |
171 | /* Want a block */ |
172 | int len ; |
173 | const int old_len = SvCUR(buf_sv); |
174 | |
175 | /* ensure buf_sv is large enough */ |
176 | SvGROW(buf_sv, (STRLEN)(old_len + maxlen)) ; |
177 | if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len, maxlen)) <= 0){ |
178 | if (PerlIO_error(PL_rsfp)) |
179 | return -1; /* error */ |
180 | else |
181 | return 0 ; /* end of file */ |
182 | } |
183 | SvCUR_set(buf_sv, old_len + len) ; |
184 | } else { |
185 | /* Want a line */ |
186 | if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) { |
187 | if (PerlIO_error(PL_rsfp)) |
188 | return -1; /* error */ |
189 | else |
190 | return 0 ; /* end of file */ |
191 | } |
192 | } |
193 | return SvCUR(buf_sv); |
194 | } |
195 | /* Skip this filter slot if filter has been deleted */ |
196 | if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef) { |
197 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
198 | "filter_read %d: skipped (filter deleted)\n", |
199 | idx)); |
200 | return FILTER_READ(idx+1, buf_sv, maxlen); /* recurse */ |
201 | } |
202 | /* Get function pointer hidden within datasv */ |
203 | funcp = DPTR2FPTR(filter_t, IoANY(datasv)); |
204 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
205 | "filter_read %d: via function %p (%s)\n", |
206 | idx, datasv, SvPV_nolen_const(datasv))); |
207 | /* Call function. The function is expected to */ |
208 | /* call "FILTER_READ(idx+1, buf_sv)" first. */ |
209 | /* Return: <0:error, =0:eof, >0:not eof */ |
210 | return (*funcp)(aTHX_ idx, buf_sv, maxlen); |
211 | } |
212 | |
213 | STATIC char * |
214 | S_filter_gets(pTHX_ register SV *sv, register PerlIO *fp, STRLEN append) |
215 | { |
216 | #ifdef PERL_CR_FILTER |
217 | if (!PL_rsfp_filters) { |
218 | filter_add(S_cr_textfilter,NULL); |
219 | } |
220 | #endif |
221 | if (PL_rsfp_filters) { |
222 | if (!append) |
223 | SvCUR_set(sv, 0); /* start with empty line */ |
224 | if (FILTER_READ(0, sv, 0) > 0) |
225 | return ( SvPVX(sv) ) ; |
226 | else |
227 | return Nullch ; |
228 | } |
229 | else |
230 | return (sv_gets(sv, fp, append)); |
231 | } |
232 | |
233 | /* |
234 | * S_skipspace |
235 | * Called to gobble the appropriate amount and type of whitespace. |
236 | * Skips comments as well. |
237 | */ |
238 | |
239 | STATIC char * |
240 | S_skipspace(pTHX_ register char *s) |
241 | { |
242 | if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) { |
243 | while (s < PL_bufend && SPACE_OR_TAB(*s)) |
244 | s++; |
245 | return s; |
246 | } |
247 | for (;;) { |
248 | STRLEN prevlen; |
249 | SSize_t oldprevlen, oldoldprevlen; |
250 | SSize_t oldloplen = 0, oldunilen = 0; |
251 | while (s < PL_bufend && isSPACE(*s)) { |
252 | if (*s++ == '\n' && PL_in_eval && !PL_rsfp) |
253 | incline(s); |
254 | } |
255 | |
256 | /* comment */ |
257 | if (s < PL_bufend && *s == '#') { |
258 | while (s < PL_bufend && *s != '\n') |
259 | s++; |
260 | if (s < PL_bufend) { |
261 | s++; |
262 | if (PL_in_eval && !PL_rsfp) { |
263 | incline(s); |
264 | continue; |
265 | } |
266 | } |
267 | } |
268 | |
269 | /* only continue to recharge the buffer if we're at the end |
270 | * of the buffer, we're not reading from a source filter, and |
271 | * we're in normal lexing mode |
272 | */ |
273 | if (s < PL_bufend || !PL_rsfp || PL_sublex_info.sub_inwhat || |
274 | PL_lex_state == LEX_FORMLINE) |
275 | return s; |
276 | |
277 | /* try to recharge the buffer */ |
278 | if ((s = filter_gets(PL_linestr, PL_rsfp, |
279 | (prevlen = SvCUR(PL_linestr)))) == Nullch) |
280 | { |
281 | /* end of file. Add on the -p or -n magic */ |
282 | if (PL_minus_p) { |
283 | sv_setpv(PL_linestr, |
284 | ";}continue{print or die qq(-p destination: $!\\n);}"); |
285 | PL_minus_n = PL_minus_p = 0; |
286 | } |
287 | else if (PL_minus_n) { |
288 | sv_setpvn(PL_linestr, ";}", 2); |
289 | PL_minus_n = 0; |
290 | } |
291 | else |
292 | sv_setpvn(PL_linestr,";", 1); |
293 | |
294 | /* reset variables for next time we lex */ |
295 | PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = s = PL_linestart |
296 | = SvPVX(PL_linestr); |
297 | PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr); |
298 | PL_last_lop = PL_last_uni = Nullch; |
299 | |
300 | /* Close the filehandle. Could be from -P preprocessor, |
301 | * STDIN, or a regular file. If we were reading code from |
302 | * STDIN (because the commandline held no -e or filename) |
303 | * then we don't close it, we reset it so the code can |
304 | * read from STDIN too. |
305 | */ |
306 | |
307 | if (PL_preprocess && !PL_in_eval) |
308 | (void)PerlProc_pclose(PL_rsfp); |
309 | else if ((PerlIO*)PL_rsfp == PerlIO_stdin()) |
310 | PerlIO_clearerr(PL_rsfp); |
311 | else |
312 | (void)PerlIO_close(PL_rsfp); |
313 | PL_rsfp = Nullfp; |
314 | return s; |
315 | } |
316 | |
317 | /* not at end of file, so we only read another line */ |
318 | /* make corresponding updates to old pointers, for yyerror() */ |
319 | oldprevlen = PL_oldbufptr - PL_bufend; |
320 | oldoldprevlen = PL_oldoldbufptr - PL_bufend; |
321 | if (PL_last_uni) |
322 | oldunilen = PL_last_uni - PL_bufend; |
323 | if (PL_last_lop) |
324 | oldloplen = PL_last_lop - PL_bufend; |
325 | PL_linestart = PL_bufptr = s + prevlen; |
326 | PL_bufend = s + SvCUR(PL_linestr); |
327 | s = PL_bufptr; |
328 | PL_oldbufptr = s + oldprevlen; |
329 | PL_oldoldbufptr = s + oldoldprevlen; |
330 | if (PL_last_uni) |
331 | PL_last_uni = s + oldunilen; |
332 | if (PL_last_lop) |
333 | PL_last_lop = s + oldloplen; |
334 | incline(s); |
335 | |
336 | /* debugger active and we're not compiling the debugger code, |
337 | * so store the line into the debugger's array of lines |
338 | */ |
339 | if (PERLDB_LINE && PL_curstash != PL_debstash) { |
340 | SV * const sv = NEWSV(85,0); |
341 | |
342 | sv_upgrade(sv, SVt_PVMG); |
343 | sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr); |
344 | (void)SvIOK_on(sv); |
345 | SvIV_set(sv, 0); |
346 | av_store(CopFILEAV(PL_curcop),(I32)CopLINE(PL_curcop),sv); |
347 | } |
348 | } |
349 | } |
350 | |
351 | STATIC char * |
352 | S_scan_word(pTHX_ register char *s, char *dest, STRLEN destlen, int allow_package, STRLEN *slp) |
353 | { |
354 | register char *d = dest; |
355 | register char * const e = d + destlen - 3; /* two-character token, ending NUL */ |
356 | for (;;) { |
357 | if (d >= e) |
358 | Perl_croak(aTHX_ ident_too_long); |
359 | if (isALNUM(*s)) /* UTF handled below */ |
360 | *d++ = *s++; |
361 | else if (*s == '\'' && allow_package && isIDFIRST_lazy_if(s+1,UTF)) { |
362 | *d++ = ':'; |
363 | *d++ = ':'; |
364 | s++; |
365 | } |
366 | else if (*s == ':' && s[1] == ':' && allow_package && s[2] != '$') { |
367 | *d++ = *s++; |
368 | *d++ = *s++; |
369 | } |
370 | else if (UTF && UTF8_IS_START(*s) && isALNUM_utf8((U8*)s)) { |
371 | char *t = s + UTF8SKIP(s); |
372 | while (UTF8_IS_CONTINUED(*t) && is_utf8_mark((U8*)t)) |
373 | t += UTF8SKIP(t); |
374 | if (d + (t - s) > e) |
375 | Perl_croak(aTHX_ ident_too_long); |
376 | Copy(s, d, t - s, char); |
377 | d += t - s; |
378 | s = t; |
379 | } |
380 | else { |
381 | *d = '\0'; |
382 | *slp = d - dest; |
383 | return s; |
384 | } |
385 | } |
386 | } |
387 | |
388 | /* |
389 | * S_incline |
390 | * This subroutine has nothing to do with tilting, whether at windmills |
391 | * or pinball tables. Its name is short for "increment line". It |
392 | * increments the current line number in CopLINE(PL_curcop) and checks |
393 | * to see whether the line starts with a comment of the form |
394 | * # line 500 "foo.pm" |
395 | * If so, it sets the current line number and file to the values in the comment. |
396 | */ |
397 | |
14ab3472 |
398 | /* 5.9+ make the char *s in S_incline const and declare it in proto.h so we |
399 | need to do the same to avoid a prototype mismatch (mst) */ |
400 | #ifdef PERL_5_9_PLUS |
401 | #define S_INCLINE_CONST const |
402 | #else |
403 | #define S_INCLINE_CONST |
404 | #endif |
405 | |
e807ee50 |
406 | STATIC void |
14ab3472 |
407 | S_incline(pTHX_ S_INCLINE_CONST char *s) |
e807ee50 |
408 | { |
409 | char *t; |
14ab3472 |
410 | S_INCLINE_CONST char *n; |
411 | S_INCLINE_CONST char *e; |
e807ee50 |
412 | char ch; |
413 | |
414 | CopLINE_inc(PL_curcop); |
415 | if (*s++ != '#') |
416 | return; |
417 | while (SPACE_OR_TAB(*s)) s++; |
418 | if (strnEQ(s, "line", 4)) |
419 | s += 4; |
420 | else |
421 | return; |
422 | if (SPACE_OR_TAB(*s)) |
423 | s++; |
424 | else |
425 | return; |
426 | while (SPACE_OR_TAB(*s)) s++; |
427 | if (!isDIGIT(*s)) |
428 | return; |
429 | n = s; |
430 | while (isDIGIT(*s)) |
431 | s++; |
432 | while (SPACE_OR_TAB(*s)) |
433 | s++; |
434 | if (*s == '"' && (t = strchr(s+1, '"'))) { |
435 | s++; |
436 | e = t + 1; |
437 | } |
438 | else { |
14ab3472 |
439 | /* explicitly cast to char * in case S_INCLINE_CONST in force (mst) */ |
440 | for (t = (char *)s; !isSPACE(*t); t++) ; |
e807ee50 |
441 | e = t; |
442 | } |
443 | while (SPACE_OR_TAB(*e) || *e == '\r' || *e == '\f') |
444 | e++; |
445 | if (*e != '\n' && *e != '\0') |
446 | return; /* false alarm */ |
447 | |
448 | ch = *t; |
449 | *t = '\0'; |
450 | if (t - s > 0) { |
71511651 |
451 | /* this chunk was added to S_incline during 5.8.8. I don't know why but I don't |
452 | honestly care since I probably want to be bug-compatible anyway (mst) */ |
453 | |
454 | /* ... my kingdom for a perl parser in perl ... (mst) */ |
455 | |
456 | #ifdef PERL_5_8_8_PLUS |
e807ee50 |
457 | #ifndef USE_ITHREADS |
458 | const char *cf = CopFILE(PL_curcop); |
459 | if (cf && strlen(cf) > 7 && strnEQ(cf, "(eval ", 6)) { |
460 | /* must copy *{"::_<(eval N)[oldfilename:L]"} |
461 | * to *{"::_<newfilename"} */ |
462 | char smallbuf[256], smallbuf2[256]; |
463 | char *tmpbuf, *tmpbuf2; |
464 | GV **gvp, *gv2; |
465 | STRLEN tmplen = strlen(cf); |
466 | STRLEN tmplen2 = strlen(s); |
467 | if (tmplen + 3 < sizeof smallbuf) |
468 | tmpbuf = smallbuf; |
469 | else |
470 | Newx(tmpbuf, tmplen + 3, char); |
471 | if (tmplen2 + 3 < sizeof smallbuf2) |
472 | tmpbuf2 = smallbuf2; |
473 | else |
474 | Newx(tmpbuf2, tmplen2 + 3, char); |
475 | tmpbuf[0] = tmpbuf2[0] = '_'; |
476 | tmpbuf[1] = tmpbuf2[1] = '<'; |
477 | memcpy(tmpbuf + 2, cf, ++tmplen); |
478 | memcpy(tmpbuf2 + 2, s, ++tmplen2); |
479 | ++tmplen; ++tmplen2; |
480 | gvp = (GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, FALSE); |
481 | if (gvp) { |
482 | gv2 = *(GV**)hv_fetch(PL_defstash, tmpbuf2, tmplen2, TRUE); |
483 | if (!isGV(gv2)) |
484 | gv_init(gv2, PL_defstash, tmpbuf2, tmplen2, FALSE); |
485 | /* adjust ${"::_<newfilename"} to store the new file name */ |
486 | GvSV(gv2) = newSVpvn(tmpbuf2 + 2, tmplen2 - 2); |
487 | GvHV(gv2) = (HV*)SvREFCNT_inc(GvHV(*gvp)); |
488 | GvAV(gv2) = (AV*)SvREFCNT_inc(GvAV(*gvp)); |
489 | } |
490 | if (tmpbuf != smallbuf) Safefree(tmpbuf); |
491 | if (tmpbuf2 != smallbuf2) Safefree(tmpbuf2); |
492 | } |
493 | #endif |
71511651 |
494 | #endif |
495 | /* second endif closes out the "are we 5.8.(8+)" conditional */ |
e807ee50 |
496 | CopFILE_free(PL_curcop); |
497 | CopFILE_set(PL_curcop, s); |
498 | } |
499 | *t = ch; |
500 | CopLINE_set(PL_curcop, atoi(n)-1); |
501 | } |
0ba8c7aa |
502 | |
503 | /* scan_str |
504 | takes: start position in buffer |
505 | keep_quoted preserve \ on the embedded delimiter(s) |
506 | keep_delims preserve the delimiters around the string |
507 | returns: position to continue reading from buffer |
508 | side-effects: multi_start, multi_close, lex_repl or lex_stuff, and |
509 | updates the read buffer. |
510 | |
511 | This subroutine pulls a string out of the input. It is called for: |
512 | q single quotes q(literal text) |
513 | ' single quotes 'literal text' |
514 | qq double quotes qq(interpolate $here please) |
515 | " double quotes "interpolate $here please" |
516 | qx backticks qx(/bin/ls -l) |
517 | ` backticks `/bin/ls -l` |
518 | qw quote words @EXPORT_OK = qw( func() $spam ) |
519 | m// regexp match m/this/ |
520 | s/// regexp substitute s/this/that/ |
521 | tr/// string transliterate tr/this/that/ |
522 | y/// string transliterate y/this/that/ |
523 | ($*@) sub prototypes sub foo ($) |
524 | (stuff) sub attr parameters sub foo : attr(stuff) |
525 | <> readline or globs <FOO>, <>, <$fh>, or <*.c> |
526 | |
527 | In most of these cases (all but <>, patterns and transliterate) |
528 | yylex() calls scan_str(). m// makes yylex() call scan_pat() which |
529 | calls scan_str(). s/// makes yylex() call scan_subst() which calls |
530 | scan_str(). tr/// and y/// make yylex() call scan_trans() which |
531 | calls scan_str(). |
532 | |
533 | It skips whitespace before the string starts, and treats the first |
534 | character as the delimiter. If the delimiter is one of ([{< then |
535 | the corresponding "close" character )]}> is used as the closing |
536 | delimiter. It allows quoting of delimiters, and if the string has |
537 | balanced delimiters ([{<>}]) it allows nesting. |
538 | |
539 | On success, the SV with the resulting string is put into lex_stuff or, |
540 | if that is already non-NULL, into lex_repl. The second case occurs only |
541 | when parsing the RHS of the special constructs s/// and tr/// (y///). |
542 | For convenience, the terminating delimiter character is stuffed into |
543 | SvIVX of the SV. |
544 | */ |
545 | |
546 | STATIC char * |
547 | S_scan_str(pTHX_ char *start, int keep_quoted, int keep_delims) |
548 | { |
549 | SV *sv; /* scalar value: string */ |
550 | char *tmps; /* temp string, used for delimiter matching */ |
551 | register char *s = start; /* current position in the buffer */ |
552 | register char term; /* terminating character */ |
553 | register char *to; /* current position in the sv's data */ |
554 | I32 brackets = 1; /* bracket nesting level */ |
555 | bool has_utf8 = FALSE; /* is there any utf8 content? */ |
556 | I32 termcode; /* terminating char. code */ |
71511651 |
557 | /* 5.8.7+ uses UTF8_MAXBYTES but also its utf8.h defs _MAXLEN to it so |
558 | I'm reasonably hopeful this won't destroy anything (mst) */ |
559 | U8 termstr[UTF8_MAXLEN]; /* terminating string */ |
0ba8c7aa |
560 | STRLEN termlen; /* length of terminating string */ |
561 | char *last = NULL; /* last position for nesting bracket */ |
562 | |
563 | /* skip space before the delimiter */ |
564 | if (isSPACE(*s)) |
565 | s = skipspace(s); |
566 | |
567 | /* mark where we are, in case we need to report errors */ |
568 | CLINE; |
569 | |
570 | /* after skipping whitespace, the next character is the terminator */ |
571 | term = *s; |
572 | if (!UTF) { |
573 | termcode = termstr[0] = term; |
574 | termlen = 1; |
575 | } |
576 | else { |
577 | termcode = utf8_to_uvchr((U8*)s, &termlen); |
578 | Copy(s, termstr, termlen, U8); |
579 | if (!UTF8_IS_INVARIANT(term)) |
580 | has_utf8 = TRUE; |
581 | } |
582 | |
583 | /* mark where we are */ |
584 | PL_multi_start = CopLINE(PL_curcop); |
585 | PL_multi_open = term; |
586 | |
587 | /* find corresponding closing delimiter */ |
588 | if (term && (tmps = strchr("([{< )]}> )]}>",term))) |
589 | termcode = termstr[0] = term = tmps[5]; |
590 | |
591 | PL_multi_close = term; |
592 | |
593 | /* create a new SV to hold the contents. 87 is leak category, I'm |
594 | assuming. 79 is the SV's initial length. What a random number. */ |
595 | sv = NEWSV(87,79); |
596 | sv_upgrade(sv, SVt_PVIV); |
597 | SvIV_set(sv, termcode); |
598 | (void)SvPOK_only(sv); /* validate pointer */ |
599 | |
600 | /* move past delimiter and try to read a complete string */ |
601 | if (keep_delims) |
602 | sv_catpvn(sv, s, termlen); |
603 | s += termlen; |
604 | for (;;) { |
605 | if (PL_encoding && !UTF) { |
606 | bool cont = TRUE; |
607 | |
608 | while (cont) { |
609 | int offset = s - SvPVX_const(PL_linestr); |
610 | const bool found = sv_cat_decode(sv, PL_encoding, PL_linestr, |
611 | &offset, (char*)termstr, termlen); |
612 | const char *ns = SvPVX_const(PL_linestr) + offset; |
613 | char *svlast = SvEND(sv) - 1; |
614 | |
615 | for (; s < ns; s++) { |
616 | if (*s == '\n' && !PL_rsfp) |
617 | CopLINE_inc(PL_curcop); |
618 | } |
619 | if (!found) |
620 | goto read_more_line; |
621 | else { |
622 | /* handle quoted delimiters */ |
623 | if (SvCUR(sv) > 1 && *(svlast-1) == '\\') { |
624 | const char *t; |
625 | for (t = svlast-2; t >= SvPVX_const(sv) && *t == '\\';) |
626 | t--; |
627 | if ((svlast-1 - t) % 2) { |
628 | if (!keep_quoted) { |
629 | *(svlast-1) = term; |
630 | *svlast = '\0'; |
631 | SvCUR_set(sv, SvCUR(sv) - 1); |
632 | } |
633 | continue; |
634 | } |
635 | } |
636 | if (PL_multi_open == PL_multi_close) { |
637 | cont = FALSE; |
638 | } |
639 | else { |
640 | const char *t; |
641 | char *w; |
642 | if (!last) |
643 | last = SvPVX(sv); |
644 | for (t = w = last; t < svlast; w++, t++) { |
645 | /* At here, all closes are "was quoted" one, |
646 | so we don't check PL_multi_close. */ |
647 | if (*t == '\\') { |
648 | if (!keep_quoted && *(t+1) == PL_multi_open) |
649 | t++; |
650 | else |
651 | *w++ = *t++; |
652 | } |
653 | else if (*t == PL_multi_open) |
654 | brackets++; |
655 | |
656 | *w = *t; |
657 | } |
658 | if (w < t) { |
659 | *w++ = term; |
660 | *w = '\0'; |
661 | SvCUR_set(sv, w - SvPVX_const(sv)); |
662 | } |
663 | last = w; |
664 | if (--brackets <= 0) |
665 | cont = FALSE; |
666 | } |
667 | } |
668 | } |
669 | if (!keep_delims) { |
670 | SvCUR_set(sv, SvCUR(sv) - 1); |
671 | *SvEND(sv) = '\0'; |
672 | } |
673 | break; |
674 | } |
675 | |
676 | /* extend sv if need be */ |
677 | SvGROW(sv, SvCUR(sv) + (PL_bufend - s) + 1); |
678 | /* set 'to' to the next character in the sv's string */ |
679 | to = SvPVX(sv)+SvCUR(sv); |
680 | |
681 | /* if open delimiter is the close delimiter read unbridle */ |
682 | if (PL_multi_open == PL_multi_close) { |
683 | for (; s < PL_bufend; s++,to++) { |
684 | /* embedded newlines increment the current line number */ |
685 | if (*s == '\n' && !PL_rsfp) |
686 | CopLINE_inc(PL_curcop); |
687 | /* handle quoted delimiters */ |
688 | if (*s == '\\' && s+1 < PL_bufend && term != '\\') { |
689 | if (!keep_quoted && s[1] == term) |
690 | s++; |
691 | /* any other quotes are simply copied straight through */ |
692 | else |
693 | *to++ = *s++; |
694 | } |
695 | /* terminate when run out of buffer (the for() condition), or |
696 | have found the terminator */ |
697 | else if (*s == term) { |
698 | if (termlen == 1) |
699 | break; |
700 | if (s+termlen <= PL_bufend && memEQ(s, (char*)termstr, termlen)) |
701 | break; |
702 | } |
703 | else if (!has_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF) |
704 | has_utf8 = TRUE; |
705 | *to = *s; |
706 | } |
707 | } |
708 | |
709 | /* if the terminator isn't the same as the start character (e.g., |
710 | matched brackets), we have to allow more in the quoting, and |
711 | be prepared for nested brackets. |
712 | */ |
713 | else { |
714 | /* read until we run out of string, or we find the terminator */ |
715 | for (; s < PL_bufend; s++,to++) { |
716 | /* embedded newlines increment the line count */ |
717 | if (*s == '\n' && !PL_rsfp) |
718 | CopLINE_inc(PL_curcop); |
719 | /* backslashes can escape the open or closing characters */ |
720 | if (*s == '\\' && s+1 < PL_bufend) { |
721 | if (!keep_quoted && |
722 | ((s[1] == PL_multi_open) || (s[1] == PL_multi_close))) |
723 | s++; |
724 | else |
725 | *to++ = *s++; |
726 | } |
727 | /* allow nested opens and closes */ |
728 | else if (*s == PL_multi_close && --brackets <= 0) |
729 | break; |
730 | else if (*s == PL_multi_open) |
731 | brackets++; |
732 | else if (!has_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF) |
733 | has_utf8 = TRUE; |
734 | *to = *s; |
735 | } |
736 | } |
737 | /* terminate the copied string and update the sv's end-of-string */ |
738 | *to = '\0'; |
739 | SvCUR_set(sv, to - SvPVX_const(sv)); |
740 | |
741 | /* |
742 | * this next chunk reads more into the buffer if we're not done yet |
743 | */ |
744 | |
745 | if (s < PL_bufend) |
746 | break; /* handle case where we are done yet :-) */ |
747 | |
748 | #ifndef PERL_STRICT_CR |
749 | if (to - SvPVX_const(sv) >= 2) { |
750 | if ((to[-2] == '\r' && to[-1] == '\n') || |
751 | (to[-2] == '\n' && to[-1] == '\r')) |
752 | { |
753 | to[-2] = '\n'; |
754 | to--; |
755 | SvCUR_set(sv, to - SvPVX_const(sv)); |
756 | } |
757 | else if (to[-1] == '\r') |
758 | to[-1] = '\n'; |
759 | } |
760 | else if (to - SvPVX_const(sv) == 1 && to[-1] == '\r') |
761 | to[-1] = '\n'; |
762 | #endif |
763 | |
764 | read_more_line: |
765 | /* if we're out of file, or a read fails, bail and reset the current |
766 | line marker so we can report where the unterminated string began |
767 | */ |
768 | if (!PL_rsfp || |
769 | !(PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = filter_gets(PL_linestr, PL_rsfp, 0))) { |
770 | sv_free(sv); |
771 | CopLINE_set(PL_curcop, (line_t)PL_multi_start); |
772 | return Nullch; |
773 | } |
774 | /* we read a line, so increment our line counter */ |
775 | CopLINE_inc(PL_curcop); |
776 | |
777 | /* update debugger info */ |
778 | if (PERLDB_LINE && PL_curstash != PL_debstash) { |
779 | SV *sv = NEWSV(88,0); |
780 | |
781 | sv_upgrade(sv, SVt_PVMG); |
782 | sv_setsv(sv,PL_linestr); |
783 | (void)SvIOK_on(sv); |
784 | SvIV_set(sv, 0); |
785 | av_store(CopFILEAV(PL_curcop), (I32)CopLINE(PL_curcop), sv); |
786 | } |
787 | |
788 | /* having changed the buffer, we must update PL_bufend */ |
789 | PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr); |
790 | PL_last_lop = PL_last_uni = Nullch; |
791 | } |
792 | |
793 | /* at this point, we have successfully read the delimited string */ |
794 | |
795 | if (!PL_encoding || UTF) { |
796 | if (keep_delims) |
797 | sv_catpvn(sv, s, termlen); |
798 | s += termlen; |
799 | } |
800 | if (has_utf8 || PL_encoding) |
801 | SvUTF8_on(sv); |
802 | |
803 | PL_multi_end = CopLINE(PL_curcop); |
804 | |
805 | /* if we allocated too much space, give some back */ |
806 | if (SvCUR(sv) + 5 < SvLEN(sv)) { |
807 | SvLEN_set(sv, SvCUR(sv) + 1); |
71511651 |
808 | /* 5.8.8 uses SvPV_renew, no prior version actually has the damn thing (mst) */ |
809 | #ifdef PERL_5_8_8_PLUS |
0ba8c7aa |
810 | SvPV_renew(sv, SvLEN(sv)); |
71511651 |
811 | #else |
812 | Renew(SvPVX(sv), SvLEN(sv), char); |
813 | #endif |
0ba8c7aa |
814 | } |
815 | |
816 | /* decide whether this is the first or second quoted string we've read |
817 | for this op |
818 | */ |
819 | |
820 | if (PL_lex_stuff) |
821 | PL_lex_repl = sv; |
822 | else |
823 | PL_lex_stuff = sv; |
824 | return s; |
825 | } |
826 | |
827 | /* |
828 | * S_force_next |
829 | * When the lexer realizes it knows the next token (for instance, |
830 | * it is reordering tokens for the parser) then it can call S_force_next |
831 | * to know what token to return the next time the lexer is called. Caller |
832 | * will need to set PL_nextval[], and possibly PL_expect to ensure the lexer |
833 | * handles the token correctly. |
834 | */ |
835 | |
836 | STATIC void |
837 | S_force_next(pTHX_ I32 type) |
838 | { |
839 | PL_nexttype[PL_nexttoke] = type; |
840 | PL_nexttoke++; |
841 | if (PL_lex_state != LEX_KNOWNEXT) { |
842 | PL_lex_defer = PL_lex_state; |
843 | PL_lex_expect = PL_expect; |
844 | PL_lex_state = LEX_KNOWNEXT; |
845 | } |
846 | } |